Strategy Tester - Back Test On MA Crossover - Weird Result

 


Hi all,

I wrote an EA on MA crossover strategy but got weird result when I did a back test on it.

As you can see from the images above, there is a buy order executed at 11:05 but the two MA lines did not cross. There shouldn't be any trade.

I am on Exponential MA method, 5 minutes chart, next buy order after 1 bar.

Any thought on this please? 

Any help would be greatly appreciated.

 
alexanderlim:


Hi all,

I wrote an EA on MA crossover strategy but got weird result when I did a back test on it.

As you can see from the images above, there is a buy order executed at 11:05 but the two MA lines did not cross. There shouldn't be any trade.

I am on Exponential MA method, 5 minutes chart, next buy order after 1 bar.

Any thought on this please? 

Any help would be greatly appreciated.


may be - it depends on your stragery ... MT4? Because MT4 does not have backtesting with real ticks for example (you can 'convert' your strategy to MT5 to backtest it with the real ticks with MT5.
MA crossing on close bar or open bar?
I am not a coder but if you upload some part of the code so the users may correct it (it is mistake in coding for example).
 
alexanderlim:


Hi all,

I wrote an EA on MA crossover strategy but got weird result when I did a back test on it.

As you can see from the images above, there is a buy order executed at 11:05 but the two MA lines did not cross. There shouldn't be any trade.

I am on Exponential MA method, 5 minutes chart, next buy order after 1 bar.

Any thought on this please? 

Any help would be greatly appreciated.

Are you sure the MAs settings on your chart and in your EA match ?

If yes, show your code, maybe you made an error.

 
alexanderlim: As you can see from the images above, there is a buy order executed at 11:05 but the two MA lines did not cross. There shouldn't be any trade.
  1. You can get many crosses when looking at bar zero.
  2. Post your code, there aren't any mind readers here.
 

Thank you all for responding.

Here are my codes extract:

extern int MA_Shorter_Period = 5;
extern int MA_Longer_Period = 20;
extern ENUM_MA_METHOD MA_Method=MODE_EMA;
int LotDigits; //initialized in OnInit
int MagicNumber = 1134890;
int NextOpenTradeAfterBars = 1; //next open trade after time
datetime LastTradeTime = 0;
int MinTradeDurationBars = 1; //minimum trade duration
extern double TradeSize = 0.1;
int MaxSlippage = 3; //adjusted in OnInit
bool crossed[4]; //initialized to true, used in function Cross
bool Audible_Alerts = true;
bool Push_Notifications = true;
int MaxOpenTrades = 1;
int MaxLongTrades = 1000;
int MaxShortTrades = 1000;
int MaxPendingOrders = 1000;
bool Hedging = true;
int OrderRetry = 5; //# of retries if sending order returns error
int OrderWait = 5; //# of seconds to wait if sending order returns error
double myPoint; //initialized in OnInit

:
:

void OnTick()
  {
   int ticket = -1;
   double price;   
   
   
   //Close Long Positions, instant signal is tested first
   if(Cross(1, iMA(NULL, PERIOD_CURRENT, MA_Shorter_Period, 0, MA_Method, PRICE_CLOSE, 0) < iMA(NULL, PERIOD_CURRENT, MA_Longer_Period, 0, MA_Method, PRICE_CLOSE, 0)) //Moving Average crosses below Moving Average
   )
     {   
      if(IsTradeAllowed())
         myOrderClose(OP_BUY, 100, "");
      else //not autotrading => only send alert
         myAlert("order", "");
     }
   
   //Close Short Positions, instant signal is tested first
   if(Cross(0, iMA(NULL, PERIOD_CURRENT, MA_Shorter_Period, 0, MA_Method, PRICE_CLOSE, 0) > iMA(NULL, PERIOD_CURRENT, MA_Longer_Period, 0, MA_Method, PRICE_CLOSE, 0)) //Moving Average crosses above Moving Average
   )
     {   
      if(IsTradeAllowed())
         myOrderClose(OP_SELL, 100, "");
      else //not autotrading => only send alert
         myAlert("order", "");
     }
   
   //Open Buy Order, instant signal is tested first
   if(Cross(2, iMA(NULL, PERIOD_CURRENT, MA_Shorter_Period, 0, MA_Method, PRICE_CLOSE, 0) > iMA(NULL, PERIOD_CURRENT, MA_Longer_Period, 0, MA_Method, PRICE_CLOSE, 0)) //Moving Average crosses above Moving Average
   )
     {
      RefreshRates();
      price = Ask;
      if(TimeCurrent() - LastTradeTime < NextOpenTradeAfterBars * PeriodSeconds()) return; //next open trade after time   
      if(IsTradeAllowed())
        {
         ticket = myOrderSend(OP_BUY, price, TradeSize, "");
         if(ticket <= 0) return;
        }
      else //not autotrading => only send alert
         myAlert("order", "");
      LastTradeTime = TimeCurrent();
     }
   
   //Open Sell Order, instant signal is tested first
   if(Cross(3, iMA(NULL, PERIOD_CURRENT, MA_Shorter_Period, 0, MA_Method, PRICE_CLOSE, 0) < iMA(NULL, PERIOD_CURRENT, MA_Longer_Period, 0, MA_Method, PRICE_CLOSE, 0)) //Moving Average crosses below Moving Average
   )
     {
      RefreshRates();
      price = Bid;
      if(TimeCurrent() - LastTradeTime < NextOpenTradeAfterBars * PeriodSeconds()) return; //next open trade after time   
      if(IsTradeAllowed())
        {
         ticket = myOrderSend(OP_SELL, price, TradeSize, "");
         if(ticket <= 0) return;
        }
      else //not autotrading => only send alert
         myAlert("order", "");
      LastTradeTime = TimeCurrent();
     }
  }

Thanks.

 
It is because you use current bar (0) and close price for crossovers, which can be redrawn.
Reason: