moving average of equity curve

 

Hi,

Hello,

I'm trying to introduce an equity control within code of ea to improve it. 

For example imagine I have an ea called EA1 ; this one will trade in min Lots with its own MagicNumber

EA2 will trade with regular Lots, different MagicNumber, same conditions as EA1 plus one: only allow trade if Past Trades Equity of EA1 is above the moving average of its last n Equity points. 


I tried with following code in my ea but for some reason I get different results in Backtests than when I do the manual Equity Control.


If anyone sees the mistake that would be nice.


Regards,

for (i = OrdersHistoryTotal()-1; i>=0 ;  i--)            // Count number of deals traded by EA1 
 {
 if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) )   
  {  
  if (OrderMagicNumber() == EA1MagicNumber ) hist ++ ;
  }
 } 
if (hist >= maperiod) )                                  // only when at least (maperiod) closed deals frome EA1 so we can compute Equity Curve Moving Average
 {
 for (n = 0 ; n <= maperiod-1 ; n++)                     //  Computing Equity sum
  {
  for (m = OrdersHistoryTotal()-n-1;m>=0; m--)           // Computing each Equity
   { 
   if (OrderSelect(m,SELECT_BY_POS,MODE_HISTORY))
    {
    if (OrderMagicNumber() == EA1MagicNumber && (OrderType() == OP_BUY || OrderType() == OP_SELL ) )  equity[n] = equity[n] + OrderProfit();
    }
   }
  equityMA = equityMA + equity[pp] ;              // adding Equity Point
  }
 equityMA = equityMA / maperiod ;                        // moving average of EA1 Equity
 
 if (equity[0] >= equityMA) AllowEA2totrade =true ;      // Current Equity should be above its Moving Average
 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  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

  3. You are also assuming everything in history is an closed order.
 
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  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

  3. You are also assuming everything in history is an closed order.

Thanks for your help.

1. I did edit my post. Hope it's better now.

2. Good to know and thanks for the tip. In my case I looked and on the case that I manually tested, OrdersHistory seemed aligned with dates.

3. Yes, thats what I want.


Still looking into it so far nothing conclusive

 
marsupilami:

Hi,

Hello,

I'm trying to introduce an equity control within code of ea to improve it. 

For example imagine I have an ea called EA1 ; this one will trade in min Lots with its own MagicNumber

EA2 will trade with regular Lots, different MagicNumber, same conditions as EA1 plus one: only allow trade if Past Trades Equity of EA1 is above the moving average of its last n Equity points. 


I tried with following code in my ea but for some reason I get different results in Backtests than when I do the manual Equity Control.


If anyone sees the mistake that would be nice.


Regards,

Hi,

I haven’t looked at your code extensively, but noticed that in the first for loop, it starts at the number of trades and then uses i++.   Should this be i- - (minus minus).

Regards

Mark
 
Mark Wilson:
Hi,

I haven’t looked at your code extensively, but noticed that in the first for loop, it starts at the number of trades and then uses i++.   Should this be i- - (minus minus).

Regards

Mark

You're right thanks! Just amended it. No the problem though as it was already corrected in my ea

Reason: