code for a stop loss exit on the last x bars

 

Can anyone give me the MQL code or a function to tell if I was stopped out of a trade within the last 3 bars? In an expert advisor I want to check this part of my criteria for resumption of a trend.

Thanks in advance!

 
fxstr58 wrote >>

Can anyone give me the MQL code or a function to tell if I was stopped out of a trade within the last 3 bars? In an expert advisor I want to check this part of my criteria for resumption of a trend.

Thanks in advance!

I am rephrasing this question, hoping to get an answer. How can I code to check whether the last buy (or sell) order that I issued stopped out? I am guessing that it is very simple.

Thanks.

 
fxstr58:

I am rephrasing this question, hoping to get an answer. How can I code to check whether the last buy (or sell) order that I issued stopped out? I am guessing that it is very simple.

Thanks.

Here you are:

int hstTotal=OrdersHistoryTotal()-1;
   if(OrderSelect(hstTotal,SELECT_BY_POS,MODE_HISTORY)==true)
     {
       if (OrderComment()=="[sl]") 
       {
         // ....
       }
       if (OrderComment()=="[tp]") 
       {
         // ...
       }
     }
Cheers
 
ggekko:

Here you are:

if (OrderComment()=="[sl]")

With all the brokers I've seen, the [sl] or [tp] is added to the comment string. Therefore, the comment only equals [sl] or [tp] if it was originally blank.


The discussion in the recent topic 'How to determine the last trade closed by stoploss or not?' recommends doing StringFind(OrderComment(), "[sl]") != 1


No-one seems to be certain exactly how reliable this method is. The alternatives discussed elsewhere include looking at the close price of an order versus the s/l price, and making some sort of allowance for slippage. This obviously becomes very simple if you're running a system where trades are always allowed to run until they get taken out at either s/l or t/p, and are never closed by the system.

 
jjc:

With all the brokers I've seen, the [sl] or [tp] is added to the comment string. Therefore, the comment only equals [sl] or [tp] if it was originally blank.


The discussion in the recent topic 'How to determine the last trade closed by stoploss or not?' recommends doing StringFind(OrderComment(), "[sl]") != 1


No-one seems to be certain exactly how reliable this method is. The alternatives discussed elsewhere include looking at the close price of an order versus the s/l price, and making some sort of allowance for slippage. This obviously becomes very simple if you're running a system where trades are always allowed to run until they get taken out at either s/l or t/p, and are never closed by the system.


Yes. Modified code:

   int hstTotal=OrdersHistoryTotal()-1;
   if(OrderSelect(hstTotal,SELECT_BY_POS,MODE_HISTORY)==true)
     {
       if (StringFind(OrderComment(),"[sl]",0)!=-1) 
       {
         // stop loss
       }
       if (StringFind(OrderComment(),"[tp]",0)!=-1) 
       {
         // take profit
       }
     }
Reason: