how to calculate number of bars elapsed since 2 successive loosing trends - page 2

 
  1. nirVaan:
    double lastProfit1,lastProfit2=0.0;
    if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)==true){lastProfit1=OrderProfit();}
    if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)==true){lastProfit2=OrderProfit();}
    This only tests the first and second order ever opened. You want the LAST two orders for THIS EA. Also if (bool == true) is redundant. true == true means true. if (bool) is sufficient

  2. RaptorUK:
    I think this is how I would do it . . . .
    same as #1
  3. nirVaan:
    one specify a function or a algorithm to calculate number of bars elapsed since 2 successive loosing trends
    I assume you mean find the last two consecutive loosing trades.
    int barsSinceDoubleLoss(){
        int loosingTrade=0;
        for(int pos=OrdersHistoryTotal()-1; pos >= 0; pos--) if (
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()         <= OP_SELL){// Avoid cr/bal https://www.mql5.com/en/forum/126192
            if (OrderProfit() > 0)  loosingTrade = 0;
            else{
                loosingTrade++;
                                             // Remember the latest close time
                /**/ if (loosingTrade == 1)  datetime lastLoss = OrderCloseTime();
                                             // Found 2 successive loosing trades.
                else if (loosingTrade == 2)  return( iBarShift(NULL,0, lastLoss) );
            }
        }
        return( Bars );
    }
 

Thanks for all the info i have updated the original code i think i got it right this time :P.if not your advice is highly appreciated

int CurrentTicket;
int lastLossTktNum=0;

int start()
  {
//----
// only one order should be open at a given Time
OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)
if(OrderTicket()==CurrentTicket){ //order is completed there are no open orders 

  double lastProfit1,lastProfit2=0.0;
   //assuming only one EA is executed at a time and all orders are executed by the given EA
   if(OrderSelect(OrdersHistoryTotal()-2,SELECT_BY_POS,MODE_HISTORY)) lastProfit2=OrderProfit();
   
   if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)) lastProfit1=OrderProfit();
   
   if((lastProfit1<0) && (lastProfit2<0) && OrderTicket()!=lastLossTktNum )
      {
      Sleep(5 * Period() * 60 * 1000);    // wait for 5 bars * Bar length in minutes * 60 sec * 1000 (converts to milliseconds))
      lastLossTktNum=OrderTicket();       // uses Ticket Number from order position 0
      }

      evalLongSetupCondition();

 }else{//currently a order is open monitor that order
 
      monitorCurrOrder();
 
 }

//----
   return(0);
}
Reason: