EXIT RULE in MT4 problem!!
18 replies
Gaudio Fx
8 years ago #114590
i can change this exit rule with one more logical same this:
but if this condition is true before order, in metatrader the close is at same point to open ..but in strategyquant the close is after the candle close!! .. i want the same condition in MT4..IT’S POSSIBLE?
mikeyc
8 years ago #134663
That exit rule doesn’t make logical sense to me.
Why would an above average volume signify closing a short position and a below average volume signify closing a long position or vice versa? This looks like another example of strange logic in choosing symmetrical rules.
The volume being higher or lower than average does not have a directional bias.
Gaudio Fx
8 years ago #134664
it is not logical I know .. but it was just an example .. is the concept that I think is important, or whether the ‘exit rule is active before the opening of trade, WHY in metatrader4 is active at the same time opening while on strategyquant is active only in closing candle ????? .. THIS IS tHE QUESTION… not for logical exemple exit…
i can change this exit rule with one more logical same this:
but if this condition is true before order, in metatrader the close is at same point to open ..but in strategyquant the close is after the candle close!! .. i want the same condition in MT4..IT’S POSSIBLE?
mikeyc
8 years ago #134665
Looks like they are pending orders (limit or stop orders). Are you sure they are being closed and not deleted?
What does the results tab show in MT4 rather than the chart?
Gaudio Fx
8 years ago #134666
tomas262
8 years ago #134667
Gaudio, it seems to me that entry & exit conditions sometimes can “collide” and depending on backtesting precision you then get different behavior like immediate exit after entry. Can you send me the strategy (*.STR file) to [email protected] so I can verify it? The solution here can be a fix for SQ or editing your strategy code to wait for bar close.
geektrader
8 years ago #134673
Yea, that´s a precision problem. If you backtest such a strategy with real tick-data, it acts the same in SQ as well and will fail. So make sure to always verify strategies with real tick data for the last years at least to avoid such things. Personally I´d also never use “volume” for any trading logic since volume is totally different between each broker. Same for “hour”, except for the case that you know that the historical data you are using is absolutely in sync server-time-wise with your current broker.
mikeyc
8 years ago #135652
I repeat: strategyquant activates the exit rule immediately after the close of the candle where you opened the tradeinstead mt4 this does not happen, the exit rule is active at the same time opening.
I have just found this issue with SQ vs MT4 too.
In SQ, the position is opened at the bar open, and the position closed at the end of the bar/open of the next bar. In MT4 (using every tick precision), the order is opened and closed immediately on bar open.
So in MT4 you see no profit, in SQ very good profitability. The results are completely different.
This happens in strategies that have an exit rule that is true at the same time as the entry rule is true, meaning open a trade and close a trade this bar. It appears SQ takes this to mean, open the trade at bar open and close at the end of the bar, and MT4 logic does both at bar open.
What is worrying if this applies to many strategies with entry and exit rules…..
mikeyc
8 years ago #135653
Initial testing shows you can “fix” the MT4 strategies to work as they do in SQ by adding one line of code to the following function:
void closePositionAtMarket() { RefreshRates(); double priceCP; if(OrderType() == OP_BUY) { priceCP = Bid; } else { priceCP = Ask; } if(OrderOpenTime() >= Time[0]) return; // New line of Code! Exit if the order was just opened rettmp = OrderClose(OrderTicket(), OrderLots(), priceCP, MaxSlippage); }
Gaudio Fx
8 years ago #135655
Initial testing shows you can “fix” the MT4 strategies to work as they do in SQ by adding one line of code to the following function:
void closePositionAtMarket() { RefreshRates(); double priceCP; if(OrderType() == OP_BUY) { priceCP = Bid; } else { priceCP = Ask; } if(OrderOpenTime() >= Time[0]) return; // New line of Code! Exit if the order was just opened rettmp = OrderClose(OrderTicket(), OrderLots(), priceCP, MaxSlippage); }
geektrader
8 years ago #135656
Yes, but actually if you think about it, SQ is doing it wrong in the backtest, not MT4. If a rule for open and close applies at the same time, the order should be opened and closed within 2 ticks – tick 1 open, tick 2 close it. Holding it for one bar is actually less valid then closing it right away like MT4 does. So actually SQ backtesting code should be adjusted to close the order right away too and to actually invalidate the strategy this way too, because that´s all what it is, you can´t have a open and close rule applying at the same moment. It would be valid IF the strategy would ALSO have the option to hold the trade at least 1 bar, then all right, your above MT4 code would be right then. But if the open and close rule are valid at the same time, what SQ does is exactly what is logically right and how such a strategy would actually also work if it´s manually traded. E.g. the order rules would say: open a trade if Previous close > current open and close it if Previous close > current open. What you´d have to do in that case to follow the rules is to open the order and close it right away again – surely makes no sense, but that´s just what the strategies do as well that have such rules. Would it say, open a trade if Previous close > current open, hold at least 1 bar and close it if Previous close > current open, then all right, this makes sense.
mikeyc
8 years ago #135658
Search the mq4 file for closePositionAtMarket()
Add the line
if(OrderOpenTime() >= Time[0]) return; // New line of Code! Exit if the order was just opened
Where I have shown it.
geektrader
8 years ago #135660
Yea, I totally understand what you mean, but that equals to a extra rule of “hold trade at least 1 bar before exiting”, which is not a rule in SQ for that strategy as you say and hence is wrong LOGICALLY. Surely your fix does what you want it to do, I understand that, but logically it´s not correct still. If you have the same rules for open and close applying at the same time, to translate the logic from SQ correctly to MT4, the order MUST be open and closed within 2 ticks if exposed to the real market. Sure, IF you add the extra rule to “hold at least 1 bar”, then your solution is correct, but since that rule is not in the original strategy in SQ, it´s not correct to do it like that in MT4. Just saying, it´s a logic issue and SQ acts correct here, it´s not a bug really but exactly how such rules (without the extra “hold trade at least 1 bar” rule) would act in live trading.
mikeyc
8 years ago #135663
I’m just proposing a solution to make MT4 behave like SQ does. No one has to do this, but if they want the live trading to match SQ, this is what it takes.
Gaudio Fx
8 years ago #135664
Search the mq4 file for closePositionAtMarket()
Add the line
if(OrderOpenTime() >= Time[0]) return; // New line of Code! Exit if the order was just opened
Where I have shown it.
okok..
if i want this in logical “or” and not “if”, did change “if” with “or”?
eg: or(OrderOpenTime() >= Time[0]) return; // New line of Code! Exit if the order was just opened
geektrader
8 years ago #135665
I’m just proposing a solution to make MT4 behave like SQ does. No one has to do this, but if they want the live trading to match SQ, this is what it takes.
Yup, with tick-data backtesting though, SQ opens and closes the order within 2 ticks as well, just like MT4 does.