Not logged in
Viewing 15 posts - 1 through 15 (of 19 total)

Forums>StrategyQuant>Application Support>EXIT RULE in MT4 problem!!

  • #114590 |
    Participant
    22 Posts
    Hello,
    I am contacting you because I have a big problem that arises in many circumstances in generating strategies and improvement strategies
    The problem that reflected many times, is the following:
    when I check the “Exit Rule (price + + operators indicators …)”, strategyquant in specific conditions, using a very good solution as this example that I give you below:
    these are the conditions of exit strategyquant
    and this is what happens instead of metatrader4
    both short position in this case have the output conditions before the entry because the volume is greater than the avarage 50 (red line) and MetaTrader opens and closes at the same time.
    This, however, ‘does not happen in strategyquant, as is assumed that the output condition must be initiated only at the close of the candle of entry
    It would be like saying that the condition of exit is at the close of the candle of entry and this, in many circumstances it is very good. but metatrader not happen and opens and closes at the same time without waiting for the finalization of the candle, as happens in strategyquant, giving me good results
    how do I make it clear to metatrader that must wait for the closing of the candle of entry and then activate the exit rules as strategyquant?
    thank you very much
    PS:
    THE LOGICAL THAT I USE IS NOT GOOD FOR TRADING..BUT THE QUESTION IS THE SAME
    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?

    #134663
    Customer
    557 Posts

    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.

    #134664
    Participant
    22 Posts

    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?

    #134665
    Customer
    557 Posts

    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?

    #134666
    Participant
    22 Posts
    yes yes I am 100% sure … orders are opened and closed at once
    the report on mt4 is clear about the opening and closing ..
     
    I repeat: strategyquant activates the exit rule immediately after the close of the candle where you opened the trade
    instead mt4 this does not happen, the exit rule is active at the same time opening.
     
    how do I control to wait for the closing of the candle?
    I have to manually add an amendment to the strategies? You can explain how I fill the this change in order for this to happen?
     
    I tried to close (1) open (1) but does not go, then I tried with cloase (2) open (1) and any order opens it correctly ..: /
    #134667
    Moderator
    1559 Posts

    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 support@strategyquant.com so I can verify it? The solution here can be a fix for SQ or editing your strategy code to wait for bar close.

    #134673
    Customer
    432 Posts

    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.

    #135652
    Customer
    557 Posts

     

     
    I repeat: strategyquant activates the exit rule immediately after the close of the candle where you opened the trade
    instead 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…..

    #135653
    Customer
    557 Posts

    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);
    }
    
    #135655
    Participant
    22 Posts

     

    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);
    }
    

     

    I am very happy that finally someone has realized that the problem was not in the backtest mt4 but it is a problem of STRATEGYQUANT ..
    Now, if I understand correctly, when the strategy is over, to solve the problem, I have to write in the code of the strategy, the strings that you just reported .. right?
    But at what point of the lines of strategy code should I insert this scripts line?
    did you explain better where i put them?
     
    you are a genius
     
    thank you so much
    #135656
    Customer
    432 Posts

    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.

    #135658
    Customer
    557 Posts

    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.

    #135660
    Customer
    432 Posts

    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.

    #135663
    Customer
    557 Posts

    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.

    #135664
    Participant
    22 Posts

    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

Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic.