Download MetaTrader 5

Strategy Tester in MQL5 gives very Different Results than in MQL4.

To add comments, please log in or register
Cornelis van der Westhuizen
Cornelis van der Westhuizen  

Hi everyone,

I'm a newbie when it comes to the MQL5 version. I took the exact script I used in MqL4 and adjuste it to Mql5 for learning purposes. To my surprise, the tester result was horrible in Mql5, while the tester result in Mql5 was outstanding. Am I doing something wrong?? Please help and thank you.


//|                                                 Real Trender.mq5 |
//|                                                            Test  |
//|                                     |
#property copyright "Test"
#property link      "Test"
#property version   "1.00"

double SignalP;
double SignalN;
double Ratio;
double PVIT;
double PVIY;
double NVIT;
double NVIY;
string EAName="Real Trender";
int TakeProfit=220;     // 22 pips
int Stoploss=2500;      // 250 pips
int MagicNumber=12345;
int ticket;
int NumOfTrades=0;
int Lots=1;
int MaxTrades=3;
double RiskAdjuster=0;
double MaximumRisk = 0.10;

MqlTradeRequest trReq;
MqlTradeResult trRez;

//| Calculate optimal lot size                                       |
double LotsOptimized(void)
//--- select lot size
   double lot=NormalizeDouble(AccountInfoDouble(ACCOUNT_FREEMARGIN)*MaximumRisk/1000.0,1);
//--- calculate number of losses orders without a break
      //--- select history for access
      int    orders=HistoryDealsTotal();  // total history deals
      int    losses=0;                    // number of losses orders without a break

      for(int i=orders-1;i>=0;i--)
         ulong ticket=HistoryDealGetTicket(i);
            Print("HistoryDealGetTicket failed, no trade history");
         //--- check symbol
         if(HistoryDealGetString(ticket,DEAL_SYMBOL)!=_Symbol) continue;
         //--- check profit
         double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         if(profit>0.0) break;
         if(profit<0.0) losses++;
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/RiskAdjuster,1);
//--- normalize and check limits
   double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

   double minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
   if(lot<minvol) lot=minvol;

   double maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
   if(lot>maxvol) lot=maxvol;
//--- return trading volume

//| expert initialization function                                   |
int OnInit()

   //Set default values for all new order requests
      trReq.symbol=Symbol();                 // Trade symbol
      trReq.volume=LotsOptimized();          // Requested optimized volume for a deal in lots
      trReq.deviation=3;                     // Maximal possible deviation from the requested price;                   // Take Profit;                     // StopLoss
      trReq.type_filling=ORDER_FILLING_AON;  // Order execution type
      trReq.type_time=ORDER_TIME_GTC;        // Order execution time
      trReq.comment="Real Trender";
   //Support for acount with 5 decimals
     // }
//| expert deinitialization function                                 |
void OnDeinit(const int reason)

//| expert start function                                            |
void OnTick()

   MqlTick tick; //variable for tick info
      Print("Failed to get Symbol info!");

   //Create Variables for indicators
   double CurrentVolume, PreviousVolume1, PreviousVolume2;
   long myVolume[];
   CurrentVolume = myVolume[0]; // current bar Volume.
   PreviousVolume1 = myVolume[1]; // equals bar Volume 1 bar back from current bar.
   PreviousVolume2 = myVolume[2]; // equals bar Volume 2 bars back from current bar.

   double CurrentClose, PreviousClose1, PreviousClose2;
   double myClose[];
   CopyClose(_Symbol,PERIOD_M15, 0, 3, myClose); 
   CurrentClose = myClose[0]; // current bar close.
   PreviousClose1 = myClose[1]; // equals bar close 1 bar back from current bar.
   PreviousClose2 = myClose[2]; // equals bar close 2 bars back from current bar.
   if (CurrentClose>PreviousClose1)
   SignalP= PVIY+PVIT;
   if (CurrentClose<PreviousClose1)
   Ratio = NormalizeDouble(((100*SignalP)/(SignalP+SignalN)),2);
   Comment("Ratio Percentage  : ",NormalizeDouble(Ratio,2)," % ",
   "\nSignalP ",NormalizeDouble(SignalP,2),
   "\nSignalN ",NormalizeDouble(SignalN,2));
   if ((Ratio < 25.00)&& (OrdersTotal()<MaxTrades))
         trReq.price=tick.ask;                   // SymbolInfoDouble(NULL,SYMBOL_ASK);*Stoploss;      // Stop Loss level of the order*TakeProfit;    // Take Profit level of the order
         trReq.type=ORDER_TYPE_BUY;              // Order type

   if ((Ratio > 75.00)&& (OrdersTotal()<MaxTrades))
   {;*Stoploss;      // Stop Loss level of the order*TakeProfit;    // Take Profit level of the order
         trReq.type=ORDER_TYPE_SELL;             // Order type



Cornelis van der Westhuizen
Cornelis van der Westhuizen  

I made a typo! Should read Mql5 test result = horrible & Mql4 test result = great.

To add comments, please log in or register