Simple recovery loss in 10 trades

 

Hi,

I'm trying to make a simple EA which should be able to recover any losses in 10 trades. But my code won't works and I can't see where the mistake is. Are there anybody who could give me a hint to where the mistake is ?

int start()
  {
    int total = OrdersTotal();
   
   double recovery_amount;
   double per_trade;
   int recovery_trades;
 
   int i;
   for(i=OrdersHistoryTotal();i>0;i--)
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
     {  
      if(OrderProfit()<0)
      {
      recovery_amount = OrderProfit();
      recovery_trades = 10;
      per_trade = recovery_amount / 10;
      break;
      }
      if(OrderProfit() > 0)
         recovery_trades = recovery_trades-1;
         
      break;
   
     }

   double one_pip = 0.0001;
   double current_price = Close[0];
   double pip_value = (one_pip / current_price);
   double recovery = MathRound(((((per_trade) / pip_value/4)/1000)*1000)/100000);

    if(TimeHour(TimeCurrent())==0 && recovery_trades > 0)
     {
        int ticket = OrderSend(Symbol(),OP_SELLSTOP,-1*recovery,NormalizeDouble(buy_average,4),3,buy_average+0.0200,buy_average-0.0040,"");
     }
    
     if(TimeHour(TimeCurrent())==0 && (recovery_trades == 0 || recovery_trades < 0))
      {
         ticket = OrderSend(Symbol(),OP_SELLSTOP,0.01,NormalizeDouble(buy_average,4),3,buy_average+0.0200,buy_average-0.0040,"");
      }

}
 
rexden1:

Hi,

I'm trying to make a simple EA which should be able to recover any losses in 10 trades. But my code won't works and I can't see where the mistake is. Are there anybody who could give me a hint to where the mistake is ?

The Orders in the History pool are in an array, so their positions are 0, 1, 2, 3, 4 . . . . OrdersHistoryTotal() - 1 you loop starts at OrdersHistoryTotal() which is outside of the array.


You need to check the return values from your trading functions and then report any errors and associated variables so you can debug any errors that occur . . . read this and implement it: What are Function return values ? How do I use them ?

 

Thank you very much it solved my problem, but now I got another one. My counter "recovery_trades" should count down, but it just count down one number and reset. So I don't get the 10 trades but just one ??!?

I can't see why it should reset, is it because of the variables in top ??

 

You do not have a loop following your for

for(i=OrdersHistoryTotal();i>0;i--)
                                     //where is the {  ?
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
       
 
GumRai:

You do not have a loop following your for

It's not needed . . .
 
rexden1:

Thank you very much it solved my problem, but now I got another one. My counter "recovery_trades" should count down, but it just count down one number and reset. So I don't get the 10 trades but just one ??!?

I can't see why it should reset, is it because of the variables in top ??

What do you think break does ?

      if(OrderProfit() > 0)
         recovery_trades = recovery_trades-1;
         
      break;
 
RaptorUK:
It's not needed . . .


Thankyou,

I didn't realise that

 
RaptorUK:

What do you think break does ?


It terminates the for - block. Now when I then don't have the " break", it counts down as it should, but when the orderprofit < 0, then the counter is set to 10, and stays there. even though the next trade profit is positive.

why is that ? If I remove the break in:

if(OrderProfit()<0)
      {
      recovery_amount = OrderProfit();
      recovery_trades = 10;
      per_trade = recovery_amount / 10;
      break;

counts down as:

-1, -2-,3.....-22 then a negative proftit... -12, -12, 12, ......

it doesn't seems logical for me

 
rexden1:


It terminates the for - block. Now when I then don't have the " break", it counts down as it should, but when the orderprofit < 0, then the counter is set to 10, and stays there. even though the next trade profit is positive.

why is that ? If I remove the break in:

counts down as:

-1, -2-,3.....-22 then a negative proftit... -12, -12, 12, ......

it doesn't seems logical for me

You have no comments in your code saying what you are trying to do, you haven't said what you are trying to do with this code in your posts so if we assume your code is wrong it's very difficult to see why and where it is wrong.

Is this what you wanted ?

int start()
  {
    int total = OrdersTotal();
   
   double recovery_amount;
   double per_trade;
   int recovery_trades = 10;
 
   int i;
   for(i=OrdersHistoryTotal();i>0;i--)
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
     {  
      if(OrderProfit()<0)
      {
      recovery_amount = OrderProfit();
      // recovery_trades = 10;
      per_trade = recovery_amount / recovery_trades;
      // break;
      }
      if(OrderProfit() > 0)
         recovery_trades = recovery_trades-1;
         
      // break;
   
     }
 

Sorry I know I should be better to comment my code.

But what I'm trying to do is:

If a loss' occurs, let say -100. then I would like for the next ten trades to recover that loss.

I think I should have a counter - "recovery_trades" which keep keep watching of how many trades there will be left.

recovery_trades initial value must therefore be zero.

Then a loss occurs, and the recovery_trades is set to 10.

for the next ten trades the counter should countdown by -10 wich means that the cycle of ten trades is finished.


that's what i am trying to do.

 
try calculating per_trade AFTER you find the recovery_amount AND number successful trades since the last loss.
   recovery_trades = 10; // Assume last trade was the loss
   per_trade       =  0; // Assume there was NO losers in history
   for(i=OrdersHistoryTotal();i>0;i--)
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
     {  
      if(OrderProfit()<0)
      {
      recovery_amount = OrderProfit();
      per_trade = recovery_amount / recovery_trades;
      break;
      }
      // Not negitive, must be positive. Unnecessary if. if(OrderProfit() > 0)
         recovery_trades = recovery_trades-1;
         
      // keep going, until a loss. break;
   
     }
Reason: