Cumulative Profit, reset when target reached

 

Hi all,

 

I wrote the code below to support my EA. Essentially, I’m trying to add the cumulative profit until the target is reached, and then I reset the counter to zero. When the Cumulative profit is negative, I would then increase my lot size. So this would be similar to a Martingale strategy. So for example:

Profit/Loss Trade 1 = $19               Rolling Profit = $19

                Since my cumulative profit is >0, we then reset the counter to 0.

Profit/Loss Trade 2 =-$5                 Rolling Profit = -$5

Profit/Loss Trade 3 = -$2               Rolling Profit = -$5 - $2 = -$7

Profit/Loss Trade 4 = +$25            Rolling Profit = -$7 + $25 = $18

                Since my cumulative profit is >0, we then reset the counter to 0.

Profit/Loss Trade 5 = -$5               Rolling Profit = -$5

Profit/Loss Trade 6 = +$45            Rolling Profit = -$5 + $45 = $40

                Since my cumulative profit is >0, we then reset the counter to 0.

 

The second part of the code calculates how many trades it took to get a positive Net Profit. So for the example above:

Trade 1                 1 trade

Trade 2-4             3 trades

Trade 5-6             2 trades

 

My code works well about 80% of the time. Every once in a while it resets to zero too soon, often after the first positive trade , not the net positive which is what I would like to do.

Also, sometimes if resets to 0, but for the second trade it use the calculations from the previous Rolling Profit, instead of adding to the latest set. Can someone please help me to improve my code.

double Profit_Rolling()     // Count consecutive trade profit - if profit target was not reached, then add to number                                                        
{  double BaseProfit = 0;         
   double RollingProfit = 0;
   int OrdersHistTotal=OrdersHistoryTotal();
   for(int i=0;i<OrdersHistTotal;i++)
   {  if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE) && (OrderSymbol() == Symbol())) 
      BaseProfit = RollingProfit;
      {  BaseProfit += OrderProfit() + OrderSwap() + OrderCommission();  
         if(BaseProfit<0)         RollingProfit = BaseProfit; 
         else if(BaseProfit>0)    RollingProfit = 0;   
     }} return(RollingProfit); 
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------          

int Trades()     // Count consecutive trades/losses to reach positive net profit                                                         
{  int Losses=0;              int OrdersHistTotal=OrdersHistoryTotal();
  double Profit = Profit_Rolling(); 
   for(int i=0;i<OrdersHistTotal;i++)
   {  if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE) && (OrderSymbol() == Symbol())) 
      { if(Profit<0)  Losses++; 
        else Losses=0; 
      }} return(Losses);
} //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------          
 
Apologies … I understand what you want to do, but not quite understand you code… first of all… you keep you AccountHistory to “All History” right? It means that at every tick you are counting over and over your entire History right? 

Your ProfitRolling() function is looping your entire history for that symbol and is returning total trades profit…
 But with second function I don’t understand what are you trying to achieve … if your profit returned by ProfitRolling() is negative, your are counting all trades to losses right? If this profit is negative, then your count++ will keep adding … so you are counting your entire history to losses otherwise if profit is>0 , losses = 0; what I am trying to say is that you are counting all trades over and over at every tick 
 

Thanks Daniel, firstly yes, you have understood correctly. And i see what you mean. I made some basic errors for both parts. 

Let me work on an updated code and resend. 

Thanks. 

 
siyasauka #:

Thanks Daniel, firstly yes, you have understood correctly. And i see what you mean. I made some basic errors for both parts. 

Let me work on an updated code and resend. 

Thanks. 

Ok… first I would use a Magic Number … so I can only work with the trades coming from this EA. Also… you know that with Martingale you only need to loose one time… 😁
 
Yes a magic number.

But you probably want the MN to be unique for each "profit block" - when your counter is reset to zero......so maybe base it around the date of the first order in that profit block.

Then, when reviewing the orders from history and trades you can get a list of lot sizes etc confined to that MN....

Also, it means you are relying on data from mt4 terminal and not arrays within your code...