SQX 126 - Generated MQ4 does not work if SL or TP is smaller that Bid/Ask spread

Hi Mark,



Generated a strategy (attached MQ4 and SQX) which uses a fixed SL and TP as the only exit type.  This runs using GBP/USD on H1.  Tickdata shifted to GMT+2 with US DST to align with New York.

int ProfitTarget = 15;
int StopLoss = 10;



Backtest in SQX is fine, however using Dukascopy data using TDS to backtest the strategy, it fails at the point the real spread is more than the stop loss.


2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: - SQ LOG 2016.12.26 00:04 Warning! Cannot delete order and Sl/PT was not set! Error: 4108
2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: OrderDelete error 4108
2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: market order #159 cannot be deleted
2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: - SQ LOG 2016.12.26 00:04 Cannot set SL / PT for this order, deleting it!
2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: - SQ LOG 2016.12.26 00:04 Cannot modify SL, it cannot be closer than minimum allowed: 1.04565 > 1.04416
2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: - SQ LOG 2016.12.26 00:04 Modifying order with ticket: 159, SL: 1.04565 and PT: 1.04815
2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: open #159 buy 12.00 GBPUSD at 1.04665 ok
2020.02.16 15:21:21.587 2016.12.26 00:04:01  Strategy 0.134502 GBPUSD,H1: - SQ LOG 2016.12.26 00:04 Opening order type BUY with price 1.04665. Current market prices: 1.04665 / 1.04416


Note at this point, the strategy fails in MetaTrader4 because we end up with a trade having no SL to TP, and therefore the trade simply remains open and stuck until the backtest completes!


I would suggest that the MQL4 should set the SL and TP size to the required calculated price, but if this fails due to the SL or TP price being inside the spread, then either set the SL to the closest price possible or close the Market Order immediately.


The code at the moment is trying to delete a market order, which is of course impossible.


As it is, the backtest of the strategy is completely different due to this one trade failing to set a SL and TP because every subsequent order is rejected because this order with no SL/TP is open.


2020.02.16 15:21:34.806 2017.04.26 00:00:00  Strategy 0.134502 GBPUSD,H1: - SQ LOG 2017.04.26 00:00 Order with these parameters already exists and duplicate trades are not allowed. Canceling order...


Thanks,


Mike



Attachments
Strategy 0134502.sqx
(99.46 KiB)
Strategy 0134502.mq4
(204.45 KiB)
  • Votes +4
  • Project StrategyQuant X
  • Type Bug
  • Status Fixed
  • Priority Normal

History

m
#1

mikeyc

16.02.2020 17:33

Task created

AG
#2

Tukirito

16.02.2020 19:24
Voted for this task.
TB
#3

Tomas Brynda

25.03.2020 10:57
Hi Mike,

I am surprised that the spread is so high. The common spread on GBPUSD I am used to is just a few pips, in your case it appears to be over 20 pips.

A spread that high will have a highly negative impact on the results. What broker do you use? 


As a fix to this issue I would set a fixed spread in SQ's Builder and Retester to match your broker's spread and develop strategies using these settings.

I you want to backtest your attached strategy, you can set MinimumSL and MinimumPT in the EA parameters to be higher than the symbol's spread.

But the backtesting results will be much different than in SQ.


Best regards,

Tomas


g
#4

geektrader

06.04.2020 15:12
Voted for this task.
g
#5

geektrader

06.04.2020 15:13
It is still something that needs to be fixed if such a scenario happens (for example overnight), so that the trade will not be stuck forever and the system is basically "disabled" and dead because of that.
m
#6

mikeyc

18.04.2020 19:56
Voted for this task.
m
#7

mikeyc

18.04.2020 19:59
Yes this must be fixed.  Often even brokers that have normally very small bid/ask spread experience much larger spread around midnight and during high impact economic new events.


The result is that under real trading conditions the MT4 EA generated by SQX becomes stuck and useless.  The is what you see in the MT4 log.


2020.04.16 07:35:40.536 2016.12.26 00:04:01  Strategy 0.134502 EURUSD,H1: - SQ LOG 2016.12.26 00:04 Warning! Cannot delete order and Sl/PT was not set! Error: 4108

2020.04.16 07:35:40.536 2016.12.26 00:04:01  Strategy 0.134502 EURUSD,H1: OrderDelete error 4108

2020.04.16 07:35:40.536 2016.12.26 00:04:01  Strategy 0.134502 EURUSD,H1: market order #159 cannot be deleted

2020.04.16 07:35:40.536 2016.12.26 00:04:01  Strategy 0.134502 EURUSD,H1: - SQ LOG 2016.12.26 00:04 Cannot set SL / PT for this order, deleting it!

2020.04.16 07:35:40.536 2016.12.26 00:04:01  Strategy 0.134502 EURUSD,H1: - SQ LOG 2016.12.26 00:04 Cannot modify SL, it cannot be closer than minimum allowed: 1.04566 > 1.04416

2020.04.16 07:35:40.536 2016.12.26 00:04:01  Strategy 0.134502 EURUSD,H1: - SQ LOG 2016.12.26 00:04 Modifying order with ticket: 159, SL: 1.04566 and PT: 1.04816

2020.04.16 07:35:40.536 2016.12.26 00:04:01  Strategy 0.134502 EURUSD,H1: open #159 buy 4.40 EURUSD at 1.04666 ok


After that point the EA stops trading and becomes useless with one hanging trade that has no SL or TP set.


2020.04.16 07:35:57.801 2017.03.20 00:00:19  Strategy 0.134502 EURUSD,H1: - SQ LOG 2017.03.20 00:00 Order with these parameters already exists and duplicate trades are not allowed. Canceling order...

2020.04.16 07:35:57.801 2017.03.20 00:00:19  Strategy 0.134502 EURUSD,H1: - SQ LOG 2017.03.20 00:00 Opening order type BUY with price 1.07357. Current market prices: 1.07357 / 1.07318


This repeats forever due to the stuck trade.


Please fix this logic in the MQ4 code!

DB
#8

Enyx

18.04.2020 20:36
Voted for this task.
DB
#9

Enyx

18.04.2020 20:51

SQX simulation engine has similar mechanism. Such orders are marked as "Deleted" close type. For attached strategy set high spread 200 :) and all orders will be marked as such.


The problem is that SQX engine will close the order during simulation while in live trading you got a different behavior.


I believe it's much bigger issue with error handling since SQX engine assumes successful execution while in reality execution might fail.

TB
#10

Tomas Brynda

15.05.2020 10:50

Status changed from New to Fixed

Fixed. The problematic orders will close correctly in the next release

Votes: +4

Drop files to upload

or

choose files

Max size: 5MB

Not allowed: exe, msi, application, reg, php, js, htaccess, htpasswd, gitignore

...
Wait please