Number of Consecutive Sell Losses and Buy Losses

To add comments, please log in or register
Simon Mungwira
163
Simon Mungwira  

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 ;  
   
}
William Roeder
20207
William Roeder  
  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
Simon Mungwira
163
Simon Mungwira  
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 ;  
   
}
nicholi shen
2392
nicholi shen  
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);
To add comments, please log in or register