Disble trade if 3 consecutive losses

 

I'm having dificulty writing the logic and OR implementation... Hoping you guru's can help out.

I have a external variable that set the # of consecutive losses for a pair can have before trading is disabled. It can be traded again by a reset flag as external parameter

extern int        eLossThreshold       = 3;
extern bool       resetTrading         =false;
init start()...
if ( LossCounts() || resetTrading){ 
  if ( EnableTrading ){
   if ( openTrades < maxOpenOrders && IsTimeOK() ){
   .... code for opening poistion


bool LossCounts(){
int count=0;
for (int j = OrdersHistoryTotal()-1; j >= 0; j--) // loop to check closed Orders . . .
   {
    if( ! OrderSelect(j, SELECT_BY_POS, MODE_HISTORY) ) {
     Print("OrderSelect, history position: ", j, " failed.");
     continue;
    }
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
     if( OrderTakeProfit() < 0 ){
      count++;
     } else { 
      count=0;
     if ( count == eLossThreshold )

      return (false);
      break;
    }
   }
  return(true);
}
 
The problem with your logic is that you are looking through the entire order history but you want only the last few trades. You need to read the history, sort into date order, then pick the last few, working backwards in time from the most recently closed. The OrderHistory should be considered as being in a random order.
 

Some idea not example https://www.mql5.com/en/forum/138894

Another idea : since this actually a money management you should consider to "disable trading when you loss some $$$ or some % of your account, or the remain of your account is $$$", not disable trading when previous 3 or 6 trades were loss consecutively.

 
dabbler:
The problem with your logic is that you are looking through the entire order history but you want only the last few trades. You need to read the history, sort into date order, then pick the last few, working backwards in time from the most recently closed. The OrderHistory should be considered as being in a random order.
Exactly, random order, and you only want to look at the magic number, current pair.
bool isDisabled;
datetime disableOCT;
int init(){ 
    isDisabled = false; disableOCT = TimeCurrent();
    :
}
int start(){
    if (isDisabled){ comment("disabled"); return(0); }
    int  nLoss = 0, tickets[], nTickets = GetHistoryOrderByCloseTime(tickets);
    for(int iTicket = 0; iTicket < nTickets; iTicket++) if (
        OrderSelect(tickets[iTicket], SELECT_BY_TICKET)
    ){
        if (OrderCloseTime() < disableOCT) break;
        double  profit  = OrderProfit() + OrderSwap() + OrderCommission();
        if (profit > 0) break;
        nLoss++;
    }
    isDisabled = nLoss >= 3;
    :
}
Could EA Really Live By Order_History Alone? - MQL4 forum
Reason: