Unable to close MT4 positions from a previous Zorro session.

Posted By: Grant

Unable to close MT4 positions from a previous Zorro session. - 04/05/22 14:52

I'm currently testing the MT4 bride and during a single Zorro session (i.e. no re-starts) everything runs fine after some minor adjustments to my script.
However, after re-starting both Zorro and MT4 during the weekend, Zorro wasn't able to close trades during market hours from the previous Zorro session.
I'm sure that multiple attempts were made, since I included a printf() function before exitLong() & exitShort().


A couple of points:

- I do receive a 'Error 013: Invalid expression ()' during the beginning of my script (only in TRADEMODE). I've read the manual about this error, but I can't pinpoint the exact cause.
- I also receive messages like '[EUR/USD::L00531] - not found (2123772 lots)' during the beginning. There's no error code included, so I'm not sure what's causing it, since there are no active positions in MT4 with this lot size.
- There's no 075 error message and there are no order-related error messages in the MT4 logs.
- No NFA flag is set.
- I don't run a hedge strategy, but I do run a multi-asset strategy where every asset is assigned to a single strategy.

The manual (MT4 bride) states: "Note that you can not run multiple strategies in parallel on no-hedging or FIFO compliant accounts.", but from what I understand this isn't the case in my setup (or IS it?).
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/06/22 01:58

There's also another issue. When I close Zorro during market hours to provoke / test this situation, all trades are automatically closed. How can this be prevented?

[Linked Image]

Edit: never mind, I saw there was a dialog box and I pressed too fast.
Posted By: Petra

Re: Unable to close MT4 positions from a previous Zorro session. - 04/10/22 14:43

Invalid expression is a hard bug, a script crash due to a wrong expression.

Not found means the trade ending with 00531 was not found in your mt4 trade list and could therefore not be continued. This happens typically when you had closed it manually or when you continue a session with a different account.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/11/22 10:53

Thank you for your explanation, Petra.

Now I see these 'not found' messages are explained in the manual, so I will make some notes!

As for this 013 error message, it isn't causing a crash in my script itself. As stated, it only happens in TRADEMODE during the beginning of a session, right after a data pre-fetch procedure in my script, that runs fine. There's no message in my .log file, but only in Zorro's output window:

[Linked Image]

Maybe this in an internal bug in the MT4 plug-in? I'm using version 4.24.

I will test this as well in a Windows environment. If I can reproduce this error with a sample script, then I can provide it to support when needed.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/12/22 02:39

I notice that this 013 error doesn't occur in every session. Under Linux/Wine it often does, but not always. So far it doesn't under Windows 10, but that's only one single session so far. I will run more Windows sessions to find out more.

However, the main problem still remains. My script knows the positions, but it's unable to close open positions from a previous session.
Posted By: Petra

Re: Unable to close MT4 positions from a previous Zorro session. - 04/13/22 12:16

I would fix the 013 bug first because such a bug can have all sorts of bad consequences. It's in your script. If it only happens sometimes then it's dependent on data content, prices or the like.

You can also in your screenshot see a date from 1899, so obvously something is wrong.

https://zorro-project.com/manual/en/trouble.htm

If you cannot find it, we have a script fixing service.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/13/22 16:34

Good point, I missed that wrong date and this might cause this 013 bug.

I'm currently testing my script under Windows and the log shows a normal date range for the LookBack period in TRADEMODE without this 013 error, so the actual cause might be a wrong date handling issue by Wine. It's frustrating, because I prefer Linux over Windows as a future VPS environment.

Quote
Lookback 2000 bars, 2022-04-08..2022-04-12

[990: Fri 22-04-08 16:30] (1.08738)
Start of pre-fetch at Bar #990...
End of pre-fetch.
[991: Fri 22-04-08 16:31] (1.08739)


I don't want to interrupt the live demo test during the week, but I will restart both Zorro and MT4 this weekend to see if it's able to close paper trades next week.
Posted By: jcl

Re: Unable to close MT4 positions from a previous Zorro session. - 04/13/22 21:09

The reason is probably not Wine. There are still some subtle differences in some Windows date/time functions, but they should have normally no effect in the script.

A 1899 date corresponds to the number 0. So one of your functions under Linux returned 0 instead of a valid date. Maybe it's a missing timestamp in preloaded or downloaded history.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/15/22 14:02

Luckily, you're correct JCL.

This 013 error only occurs on specific dates and only in TRADEMODE with StartDate = NOW. From what I've seen, it only happens during the end of a weekend or right after it. Maybe setting a BarMode can solve this issue? I can't test this right now because there's currenly no 013 error / date 0 issue.

I don't believe it's the cause of my main problem. Even without this error I'm not able to close trades from a previous Zorro/MT4 session.

My trading script does return messages like these during the beginning and there is a recent *.trd file in my Data folder, but active trades remain open after exitLong() / exitShort() attempts.

Quote
[EUR/USD::L01211] - resumed (66 lots)
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/17/22 21:58

I've made a sample script which uses a duplicated *_d.trd version from my trade script to view all active trades.
Just like my trading script, this sample script is unable to close the trades from my previous MT4 session.
There are no error messages at all in the MT4 & Zorro log files. In fact there is no sign from any trading attempts.

output:
Quote

TestCloseAllTrades
Login 0 MT4/5..
!ZorroMT4.dll V4.26
!IG Group Limited connected at UTC 04-17 21:44
Load EUR/USD prices.. 292 h
Load USD/JPY prices.. 292 h
Load GBP/USD prices.. 292 h
Load AUD/USD prices.. 292 h
Load USD/CAD prices.. 292 h
V 2.444 on Sun 22-04-17 23:42:35 ('My Name')

Trade: TestCloseAllTrades 2022-04-17
[USD/CAD::S59229] - resumed (38 lots)
[USD/JPY::L61556] - resumed (1 lots)
[EUR/USD::S67148] - resumed (1 lots)
Assets History\AssetsIG.csv
Lookback 80 bars, 2022-04-12..2022-04-17

[Tue 22-04-12 13:00] 5492 -38.26 0/3 (1.08954)
Asset: EUR/USD | Short open/pending trades: 1
Asset: USD/JPY | Long open/pending trades: 1
Asset: USD/CAD | Short open/pending trades: 1
Logout.. ok


code:
Code
int i;

function run()
	{
	assetList("History\\AssetsIG.csv");
	
	if(is(INITRUN))
		{
		asset("EUR/USD");
		asset("USD/JPY");
		asset("GBP/USD");
		asset("AUD/USD");
		asset("USD/CAD");
		}

	if(Bar > 1)
		{
		while(asset(loop("EUR/USD", "USD/JPY", "GBP/USD", "AUD/USD", "USD/CAD")))	
			{
			i = 0;
			for(current_trades)
				{
				i++;
				if(TradeIsLong)
					{
					printf("\nAsset: %s | Long open/pending trades: %i", Asset, i);	
					exitLong();//Nothing happens
					}
				else
					{
					printf("\nAsset: %s | Short open/pending trades: %i", Asset, i);	
					exitShort();//Nothing happens
					}
				}
			}
		quit();
		}
	}
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/17/22 22:11

As for that date issue (error code 013), I was 'hoping' to reproduce this, since it happened exactly 1 week ago during the market opening, but no 'luck' wink
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 10:59

Maybe the problem lies inside MT4 and/or the MT4 plugin?

When I watch a graph to where the Zorro EA is attached to, a trade which was opened during the previous MT4 session has no arrow sign (short entry in this case). Only today's short exit/long entry has one.

[Linked Image]

The good thing however is, is that Zorro is able to close a position from a formal session. With my sample script (see page 1) this wasn't possible.




Posted By: Zheka

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 11:48

Its a chart of EURUSD - so only EURUSD trades will be marked on it.

Recent usdjpy trade (SHORT) will appear on the usdjpy chart.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 12:58

Correct, because it's a multi asset strategy and it's advised to attach the Zorro EA only once to a chart.
Posted By: Zheka

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 13:42

Does MT4 journal show a short EURUSD trade (which has no arrow sign)?
How does it appear in Zorro log?
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 14:02

Yes, the MT4 'account history' tab shows the trade details from this EURUSD short trade.
Quote
Ticket Open Time Type Size Item Price S / L T / P Close Time Price Commission Taxes Swap Profit
99067148 2022.04.15 20:54:12 sell 0.01 eurusd 1.08063 0.00000 0.00000 2022.04.18 07:55:14 1.07985 0.00 0.00 0.00 0.78


Because I started a new Zorro session this week, only the closing of the trade is in my log, which is the same price as in MT4's 'account history'.
Quote
[EUR/USD::S67148] Cover 1@1.07985: +0.72 at 06:53:00


My *_d.trd file however contains (well, 'contained') the entry details from this trade.
Here's Zorro's output during the start of this week's session:

Quote
[EUR/USD::S67148] - resumed (1 lots)
Posted By: Zheka

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 16:28

If the trade is booked both by Zorro (resumed from *.trd and is in trades.csv) and MT4, and only the arrow on the chart is missing - then, well, consider this to be an "occasional glitch" in MT4.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 18:07

I've reported this because it could add context (or maybe even explain) my main issue.
In case it's only a visual flaw, than I don't mind at all.

I still find it odd that a couple of early exit attempts in this week's new session failed without any error messages.
Posted By: Zheka

Re: Unable to close MT4 positions from a previous Zorro session. - 04/18/22 23:50

MT4's Journal would contain some description of the error/reason why the order has not been executed.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/19/22 00:08

That would make sense to me as well, but that wasn't the case.

I just ran my test script again (please see 'TestCloseAllTrades' on page #1) with the same Zorro output (other than some new positions) as a result.
There was no recent activity in my MT4 log at all.

So basically, the exitLong() & exitShort() commands are being completely ignored and I wonder why.

Posted By: Zheka

Re: Unable to close MT4 positions from a previous Zorro session. - 04/19/22 07:24

Re-read the manual on enter/exit commands: trading is disabled during the lookback period, and the first bar after the lookback is StartBar, not 1.

So, remove Bar>1 ( or substitute it with ( if (!is(LOOKBACK)) or Bar>StartBar) and remove quit().
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/20/22 13:43

OK, thanks a lot for this, very insightful.

I thought that the LOOKBACK period only had an impact in TESTMODE with a specific StartDate.
So, in TRADEMODE, this basically means that it's best to only re-start Zorro during the weekend when it's really needed, because live trading is always idle for the length of the LOOKBACK period, even when StartDate is 'NOW'.

Is this correct?
Posted By: Zheka

Re: Unable to close MT4 positions from a previous Zorro session. - 04/20/22 14:41

In Live, Lookback history is back-filled - from existing data and/or freshly loaded from the broker. So trades are allowed from the first complete bar onward. Not sure how this works with StartDate=NOW.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/20/22 14:50

Originally Posted by Zheka
Not sure how this works with StartDate=NOW.


Based on what I read in the manual, it seems best not to define StartDate at all in TRADEMODE.

Quote
StartDate = NOW sets the start date to the current day, and executes the run function in [Trade] mode immediately after the lookback period, regardless of the current time. Useful for strategies that do not run permanently, but are only started for modifying a portfolio.
- StartDate = 0 (default) starts the simulation with NumYears before the current year.
Posted By: Zheka

Re: Unable to close MT4 positions from a previous Zorro session. - 04/20/22 16:33

StartDate is 'ignored' in Live,unless set to NOW.
Posted By: Grant

Re: Unable to close MT4 positions from a previous Zorro session. - 04/20/22 17:42

I see, so that NOW setting in TRADEMODE did more harm than good.
I will log some details to be sure that things run correct right from the start.

I appreciate your help Zheka.
© 2024 lite-C Forums