Counting trades for risk control

 

Hello Guys,


I made a bool function to count my positive and negative trades daily, if there is more then 3 it returns true.


In backtesting it works because all positionated volume closes at once.

In real trades, the volume is closed in parts, so the function counts more then 1 positive trade and returns true.


I couldn't find a code that counts "POSITIONS OUT" only "DEALS OUT". Someone solved this problem?

bool RiskControl()
{
   int         trades=0; 
   int         trades_pos=0;
   int         trades_neg=0;
   
   MqlDateTime data;
   TimeCurrent(data);
   string x = string(data.year) + "." + string(data.mon) + "." + string(data.day) + " 00:00:01";
   
   HistorySelect(StringToTime(x),TimeCurrent()); 
      int total=HistoryDealsTotal(); 

   for(int i=0;i<total;i++) 
   { 
      ulong ticket=HistoryDealGetTicket(i);
      string symbol=HistoryDealGetString(ticket,DEAL_SYMBOL); 
      
      if ( (HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT) && (symbol==Symbol()) )
      { 
         double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         
         trades+=1;
         if (profit>0)  trades_pos+=1; 
         else           trades_neg+=1; 
      } 
   } 
   if (trades_pos>3)    return(true);    
   if (trades_neg>3)    return(true);   
   
   return(false);
}
 
santosphm:

Hello Guys,

I made a bool function to count my positive and negative trades daily, if there is more then 3 it returns true.

In backtesting it works because all positionated volume closes at once.

In real trades, the volume is closed in parts, so the function counts more then 1 positive trade and returns true.

I couldn't find a code that counts "POSITIONS OUT" only "DEALS OUT". Someone solved this problem?

I haven't tried before, but would it help to look through orders instead of deals?

or if you prefer to stick to deals, how about checking DEAL_ORDER to make sure only unique ones are counted?

 
Seng Joo Thio:

how about checking DEAL_ORDER to make sure only unique ones are counted?

Didn't work, so I used the DEAL_POSITION_ID to count only the first ORDER OUT, then it worked!


DEAL_POSITION_ID is the same during the entire lifetime of the position. 

DEAL_ORDER changes during the position.


thanks for the tip :)

Reason: