Problems with new Trade on Stop Loss Event

 

Dear Community,

I try to write a function, that opens a new opposite Order when the last Order by the EA was closed by Stop Loss. The function should open only one new position. But my function opens endless number of new positions. I do not know why it happens? I tried to solve the problem with the static variable "StaticTicketNumber", but this does not help. I will be very grateful for any help.

Thank you very much!


void checkForRecovery(){
   int MagicNumber;
   static int StaticTicketNumber;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--){
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      MagicNumber = OrderMagicNumber();
      int check = OrderTicket(); 
      Print("StaticNumber = " + StaticTicketNumber);
      Print("check = " + check); 
      if(StaticTicketNumber != check && OrderProfit()<0){
         StaticTicketNumber = OrderTicket();
         if(MagicNumber == MagicNumberNormal && OrderType() == OP_BUY){
            OrderSend(Symbol(),OP_SELL,Lots_Recovery_1,Bid,Slippage,Bid+StopLoss_Recovery_1*Point,Bid-d_TakeProfit_Recovery_1,CommentEA_2,MagicNumberRecovery_1,0,Red);
         }else if(MagicNumber == MagicNumberNormal && OrderType() == OP_SELL){
            OrderSend(Symbol(),OP_BUY,Lots_Recovery_1,Ask,Slippage,Ask-StopLoss_Recovery_1*Point,Ask+d_TakeProfit_Recovery_1,CommentEA_2,MagicNumberRecovery_1,0,Blue);
         }else if(MagicNumber == MagicNumberRecovery_1 && OrderType() == OP_BUY){
            OrderSend(Symbol(),OP_SELL,Lots_Recovery_2,Bid,Slippage,Bid+StopLoss_Recovery_2*Point,Bid-d_TakeProfit_Recovery_2,CommentEA_3,MagicNumberRecovery_2,0,Red);   
         }else if(MagicNumber == MagicNumberRecovery_1 && OrderType() == OP_SELL){
            OrderSend(Symbol(),OP_BUY,Lots_Recovery_2,Ask,Slippage,Ask-StopLoss_Recovery_2*Point,Ask+d_TakeProfit_Recovery_2,CommentEA_3,MagicNumberRecovery_2,0,Blue);   
         }        
      }
      
      Print("StaticTicketNumber Ende: " + StaticTicketNumber);   
   }
}
 

You must check to see if the OrderSelect worked.

You are not checking the magic number before doing stuff.

You are not checking the symbol before doing stuff.

The OrderHistory is not in a convenient order and there is no evidence of you looking for the last order to close.

You are not checking the OrderSends to see if they worked.

 

I changed the position, of the "StaticTicketNumber" variable and add you comments, but this does not help. What do you mean with: I do not check the MagicNumber?

void checkForRecovery(){
   int MagicNumber;
   static int StaticTicketNumber;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--){
      
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         MagicNumber = OrderMagicNumber();
         int check = OrderTicket();
         string MyOrderSymbol = OrderSymbol(); 
         Print("StaticNumber = " + StaticTicketNumber);
         Print("check = " + check); 
         if(StaticTicketNumber != check && OrderProfit()<0 && MyOrderSymbol==Symbol()){
         
            if(MagicNumber == MagicNumberNormal && OrderType() == OP_BUY){
               StaticTicketNumber = OrderTicket();
               OrderSend(Symbol(),OP_SELL,Lots_Recovery_1,Bid,Slippage,Bid+StopLoss_Recovery_1*Point,Bid-d_TakeProfit_Recovery_1,CommentEA_2,MagicNumberRecovery_1,0,Red);
            }else if(MagicNumber == MagicNumberNormal && OrderType() == OP_SELL){
               StaticTicketNumber = OrderTicket();
               OrderSend(Symbol(),OP_BUY,Lots_Recovery_1,Ask,Slippage,Ask-StopLoss_Recovery_1*Point,Ask+d_TakeProfit_Recovery_1,CommentEA_2,MagicNumberRecovery_1,0,Blue);
            }else if(MagicNumber == MagicNumberRecovery_1 && OrderType() == OP_BUY){
               StaticTicketNumber = OrderTicket();
               OrderSend(Symbol(),OP_SELL,Lots_Recovery_2,Bid,Slippage,Bid+StopLoss_Recovery_2*Point,Bid-d_TakeProfit_Recovery_2,CommentEA_3,MagicNumberRecovery_2,0,Red);   
            }else if(MagicNumber == MagicNumberRecovery_1 && OrderType() == OP_SELL){
               StaticTicketNumber = OrderTicket();
               OrderSend(Symbol(),OP_BUY,Lots_Recovery_2,Ask,Slippage,Ask-StopLoss_Recovery_2*Point,Ask+d_TakeProfit_Recovery_2,CommentEA_3,MagicNumberRecovery_2,0,Blue);   
            }        
         }
      
         Print("StaticTicketNumber Ende: " + StaticTicketNumber);   
      }
   }
}

My Problem is not, that the EA does not opens the positions, it opens too many. It should open only one position, when the last position by the EA was closed by Stop Loss. I do not know why the following expression is true, after one new position was opened, by this function checkForRecovery()?

if(StaticTicketNumber != check && OrderProfit()<0 && MyOrderSymbol==Symbol()){
 

My Problem is not, that the EA does not opens the positions, it opens too many. It should open only one position, when the last position by the EA was closed by Stop Loss. I do not know why the following expression is true, after one new position was opened, by this function checkForRecovery()?

You need to check for Market-Orders as well. Instead of just checking the Historical-Orders. If a Market-Order Exist then you don't want to place anymore orders. If you're placing more than 1-Order-At-A-Time then let me know and we'll go from there.

 
My EA opens more than one position at the same time. So it could be, that I a have two ore more Orders with the same Stop Loss. The function "checkForRecovery()" should opens for each of them a new opposite order, if they were closed by Stop Loss. Thank you very much for you help!
 
user_123:
My EA opens more than one position at the same time. So it could be, that I a have two ore more Orders with the same Stop Loss. The function "checkForRecovery()" should opens for each of them a new opposite order, if they were closed by Stop Loss. Thank you very much for you help!

Something like below. Just wrote and not tested.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    int iMagic=7; string iSymbol=Symbol();
    checkForRecovery(iMagic,iSymbol);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void checkForRecovery(int Magic, string Symb){
    static int Last_LossTime;
    for(int i=0; i<OrdersHistoryTotal(); i++){
        if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
        && OrderMagicNumber()==Magic
        && OrderSymbol()==Symb
        && OrderProfit()<0
        && OrderType()<2
        && OrderCloseTime()>=Last_LossTime){
            if(OrderSend(...)>-1){
                Last_LossTime=OrderCloseTime();
}   }   }   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Thank you very much! It works, like I want.
Reason: