Download MetaTrader 5

Metatrader being the devil with backtesting?

To add comments, please log in or register
Louis Stoltz
637
Louis Stoltz  

I found that metatrader and backtesting is looking like a devil saying yes and laughing at me.

I ran many expert advisors including the moving average that comes default with metatrader and when you run it over a few days with tick data or openprices I dont see the profit on report having the same number if you keep running it over and over on same days. The moving average expert just gave me around $900 then said $1000 and then the cents keep changing. So I dont believe metatrader is telling me the truth here. How can the numbers change if you are using mathematical equations and it keeps spitting out lies. How does it spit out lies?


If you run this on one day and you repeat backtesting I dont ever see the same number. What is the point of this? Im using 90% modelling quality. You can also use open price backtesting and keep firing that start button and you see it changing. I think its a waste of time for me to backtest with this nonsense, its like a little devil saying yesss! What other programs are out there to get correct reports everytime

//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20050610

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.02;
extern double DecreaseFactor     = 3;
extern double MovingPeriod       = 12;
extern double MovingShift        = 6;


double profit;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---- sell conditions
   if(Open[1]>ma && Close[1]<ma)  
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//---- buy conditions
   if(Open[1]<ma && Close[1]>ma)  
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma) {profit += OrderProfit(); OrderClose(OrderTicket(),OrderLots(),Bid,3,White);}
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma) {profit += OrderProfit();OrderClose(OrderTicket(),OrderLots(),Ask,3,White);}
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
  
  Comment(profit);
  
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
9641
Keith Watford  
As has been repeated many times on this forum, you are probably using current spread which can give different results on every backtest, depending on what the current spread is.
Simon Gniadkowski
Moderator
18003
Simon Gniadkowski  
bonechair:

I found that metatrader and backtesting is looking like a devil saying yes and laughing at me.

I ran many expert advisors including the moving average that comes default with metatrader and when you run it over a few days with tick data or openprices I dont see the profit on report having the same number if you keep running it over and over on same days. The moving average expert just gave me around $900 then said $1000 and then the cents keep changing. So I dont believe metatrader is telling me the truth here. How can the numbers change if you are using mathematical equations and it keeps spitting out lies. How does it spit out lies?


If you run this on one day and you repeat backtesting I dont ever see the same number. What is the point of this? Im using 90% modelling quality. You can also use open price backtesting and keep firing that start button and you see it changing. I think its a waste of time for me to backtest with this nonsense, its like a little devil saying yesss! What other programs are out there to get correct reports everytime

Read the latter posts in this thread: https://www.mql5.com/en/forum/148577/page4
Louis Stoltz
637
Louis Stoltz  

Thanks for the answer but they must record spread in history to. Its giving me losses in one backtest and then profit in others, its nuts.

You can set the spread before running backtest I know but can you set that spread inside an expert advisor to the average spread of that pair calculated.

Simon Gniadkowski
Moderator
18003
Simon Gniadkowski  
bonechair:

Thanks for the answer but they must record spread in history to. Its giving me losses in one backtest and then profit in others, its nuts.

You can set the spread before running backtest I know but can you set that spread inside an expert advisor to the average spread of that pair calculated.

You obviously didn't read the thread . . please do.

Spread is NOT saved in the history, only Bid prices. There are other parameters that can influence the results from a Backtest . . . the main one is Spread but not the only one. Read the thread . . . then repeat your test with a fixed Spread while disconnected from your Broker.
Jon Grah
367
Jon Grah  

tick data backtesting is the way to go for the highest accuracy. But there are very few ways to do it. TickStory and Birt's Tick Data Suite with dukascopy tick data (or whatever source you prefer; dukascopy is popular because they make it easy to download and the quality is acceptable to simulate ECN spread and market conditions)

Mt5 doesn't even allow any 3rd party data, and that platform is supposed to be the more advanced one of the 2. Anyway, be glad that you can still use mt4 to simulate the strategies with tick data.

Simon Gniadkowski
Moderator
18003
Simon Gniadkowski  
4evermaat:


Mt5 doesn't even allow any 3rd party data, and that platform is supposed to be the more advanced one of the 2. Anyway, be glad that you can still use mt4 to simulate the strategies with tick data.

Have you tried this with the beta ?
Louis Stoltz
637
Louis Stoltz  
I did read that link thanks RaptorUK, I did understand it thanks.
To add comments, please log in or register