Number of Consecutive Sell Losses and Buy Losses

 

Hi All

I am trying to get the consecutive number of losses from within my EA. The number does count up but when i get a win ,either on buy or sell the counter does not reset. Is there something wrong with my code?

void BuyCountLosses()
{
   int LossBuy =0; 
   
   for( cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--)
   {
       sel =OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
      if(OrderSymbol() == Symbol())
      {
         if ( OrderType() == OP_BUY){
         if (OrderProfit() < 0) 
           { 
             LossBuy++;
              }
              };
           
          if ( OrderType() == OP_BUY){
         if (OrderProfit() > 0) 
           { 
             LossBuy=0;
              }
              };
               

        
        
        
      }
   }
   
   TotalLossBuy  = LossBuy;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void SellCountLosses()
{
   int  LossSell = 0; 
   
   for( cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--)
   {
       sel =OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
      if(OrderSymbol() == Symbol())
      {
         if ( OrderType() == OP_SELL){
         if  (OrderProfit() < 0)
           { 
             LossSell++;
              }
            }
         
           if ( OrderType() == OP_SELL){
         if  (OrderProfit() > 0)
           { 
             LossSell=0;
              }
            }     

        
        
       
      }
   }
 TotalLossSell = LossSell ;  
   
}
 
  1. Set the count to zero and start looping. If it's a loss, increment the count. If it's a win, exit the loop, you have your count already set.
  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum
 
whroeder1:
  1. Set the count to zero and start looping. If it's a loss, increment the count. If it's a win, exit the loop, you have your count already set.
  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum

Works perfectly now. Thanks 


void BuyCountLosses()
{
   int LossBuy =0; 
   
   for( cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--)
   {
       sel =OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
      if(OrderSymbol() == Symbol())
      {
         if ( OrderType() == OP_BUY){
         if (OrderProfit() < 0) 
           { 
             LossBuy++;
              }
              };
           
          if ( OrderType() == OP_BUY){
         if (OrderProfit() > 0) 
           { 
              break;
              }
              };
               

        
        
        
      }
   }
   
   TotalLossBuy  = LossBuy;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void SellCountLosses()
{
   int  LossSell = 0; 
   
   for( cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--)
   {
       sel =OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
      if(OrderSymbol() == Symbol())
      {
         if ( OrderType() == OP_SELL){
         if  (OrderProfit() < 0)
           { 
             LossSell++;
              }
            }
         
           if ( OrderType() == OP_SELL){
         if  (OrderProfit() > 0)
           { 
              break;
              }
            }     

        
        
       
      }
   }
 TotalLossSell = LossSell ;  
   
}
 
Simon Mungwira:

Works perfectly now. Thanks 


You missed a step... you need to first sort the order history pool by close-time (see whroeder's #2), and then loop over it to count losses. That part isn't so easily done, but here is an example of how to do it. 

#include <Arrays\List.mqh>
class Deal : public CObject
{
public:
   int      ticket;
   datetime close_time;
   double   profit;
   Deal(int t,datetime ct,double p):ticket(t),close_time(ct),profit(p){}
   virtual int Compare(const CObject *node,const int mode=0)const override
   {
      const Deal *other=node;
      if(this.close_time > other.close_time) return -1; //sort descending
      if(this.close_time < other.close_time) return 1;
      return 0;
   }
};

int consecutive_losses(int order_type)
{
   CList deal_list;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==_Symbol && OrderType()==order_type)
         deal_list.Add(new Deal(OrderTicket(), OrderCloseTime(), OrderProfit()+OrderCommission()+OrderSwap()));
   
   deal_list.Sort(0);
   int count = 0;
   for(Deal *deal=deal_list.GetFirstNode(); deal!=NULL; deal=deal.Next())
      if(deal.profit < 0.0)
         count++;
      else
         break;
   return count;
}

...and then you would call it like... 

buy_losses = consecutive_losses(OP_BUY);
sell_losses = consecutive_losses(OP_SELL);
 
nicholish en:

You missed a step... you need to first sort the order history pool by close-time (see whroeder's #2), and then loop over it to count losses. That part isn't so easily done, but here is an example of how to do it. 

...and then you would call it like... 


Can you give me an indicator which show win and loss counter?

 

Hi, Please I need your help...  I am trying to get the consecutive number of losses from within my EA. This code most of the times does the counting well, but this code doesn't work when the first trade of the EA is in loss, it only works well when the first trade is positive. I don't know how to solve it. I appreciate your help please.

Files:
 
double LossOrdersCount(){
   double LossOrders=0; 
    for (int i = OrdersHistoryTotal()-1; i >=0; i--) 
    if(
        OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
    &&  OrderSymbol()      == Symbol()
    &&  OrderMagicNumber() == magic
      )  
      {
      if (OrderProfit() <  0) LossOrders++;
      if (OrderProfit() >= 0) return(LossOrders);
      } 
    }

double LossOrdersSum(){
   double LossOrdersSM=0; 
    for (int i = OrdersHistoryTotal()-1; i >=0; i--) 
    if(
        OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
    &&  OrderSymbol()      == Symbol()
    &&  OrderMagicNumber() == magic
      )   
      {
      if (OrderProfit() <  0) LossOrdersSM = LossOrdersSM + OrderProfit() + OrderCommission() + OrderSwap();
      if (OrderProfit() >= 0) return(LossOrdersSM);
      } 
    }
 
jarc1027 #: but this code doesn't work when the first trade of the EA is in loss,
  1. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

  2. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Reason: