EA works in demo testing but fails in backtest

 

This EA works and places orders in demo (forward) testing but fails in backtest. I thought that backtest is simply a simulation of demo. Somehow they run differently.
CopyBuffer fails all the time in backtest while in demo testing there are no errors and orders are placed fine. 
Technical analysis is not the best in this EA, since whatever outcome is only Buy should be placed. But that is beyond the point. The point is that it runs fine in demo, but fails in backtest.

Is this a bug in MT5?

 

#include <Trade\Trade.mqh>

// exported variables
input double Lots3 = 0.1;
input int Stoploss3 = 20;
input int Takeprofit3 = 30;


// local variables
double PipValue=1bool Terminated = false;
string LF = "\n";  
int NDigits = 4;  
int ObjCount = 0//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    //---
    NDigits = Digits();
    if (NDigits == 3 || NDigits == 5) PipValue = 10;
    
    if (AccountInfoInteger(ACCOUNT_TRADE_EXPERT) == false)
    {
        Print("Check terminal options because EA trade option is set to not allowed.");
        Comment("Check terminal options because EA trade option is set to not allowed.");
    }
    
    if (true) ObjectsDeleteAll(0);      // clear the chart
    
    
    Comment("");    // clear the chart
    //---
    return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    //---
    if (true) ObjectsDeleteAll(0);
    
    
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    //---
    if (Terminated == true)
    {
        Comment("EA Terminated.");
    }
    
    OnEveryTick1();
}

//+------------------------------------------------------------------+
//| Get Low for specified bar index                                  |
//+------------------------------------------------------------------+
double Low(int index)
{
    double arr[];
    double low = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyLow(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) low = arr[index];
    return (low);
}
//+------------------------------------------------------------------+
//| Get the High for specified bar index                             |
//+------------------------------------------------------------------+
double High(int index)
{
    double arr[];
    double high = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyHigh(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) high=arr[index];
    return(high);
}
//+------------------------------------------------------------------+
//| Get Close for specified bar index                                |
//+------------------------------------------------------------------+
double Close(int index)
{
    double arr[];
    double close = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyClose(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) close = arr[index];
    return (close);
}
//+------------------------------------------------------------------+
//| Get Open for specified bar index                                 |
//+------------------------------------------------------------------+
double Open(int index)
{
    double arr[];
    double open = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyClose(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) open = arr[index];
    return (open);
}
//+------------------------------------------------------------------+
//| Get current bid value                                            |
//+------------------------------------------------------------------+
double Bid()
{
    return (SymbolInfoDouble(Symbol(), SYMBOL_BID));
}

//+------------------------------------------------------------------+
//| Get current ask value                                            |
//+------------------------------------------------------------------+
double Ask()
{
    return (SymbolInfoDouble(Symbol(), SYMBOL_ASK));
}

//+------------------------------------------------------------------+
//| Is there an error                                                |
//+------------------------------------------------------------------+
bool IsError(MqlTradeResult& result, string function)
{
    if (result.retcode != 0 && result.retcode != TRADE_RETCODE_DONE && result.retcode != TRADE_RETCODE_PLACED)
    {
        Print("Function: ", function, " Error: ", result.retcode, " ", result.comment);
        return (true);
    }
    else
    Print("> Executed: [", function, "]");
    return (false);
}

bool IsError(CTrade& trade, string function)
{
    if (trade.ResultRetcode() != 0 && trade.ResultRetcode() != TRADE_RETCODE_DONE && trade.ResultRetcode() != TRADE_RETCODE_PLACED)
    {
        Print("Function: ", function, " Error: ", trade.ResultRetcode(), " ", trade.ResultRetcodeDescription());
        return (true);
    }
    else
    Print("> Executed: [", function, "]");
    return (false);
}

//+------------------------------------------------------------------+
//| Get indicator value back                                         |
//+------------------------------------------------------------------+
double GetIndicator(int handle, int buffer_num, int index)
{
    //--- dynamic array for the indicator values
    double arr[];
    //--- obtain the indicator value in the last two bars
    int ret = CopyBuffer(handle, buffer_num, 0, index+1, arr);
    if (ret == index+1 && arr[index] != EMPTY_VALUE)
    {
        IndicatorRelease(handle);
        return (arr[index]);
    }
    Print("CopyBuffer failed ret = ", ret);
    IndicatorRelease(handle);
    return 0;
}

//+------------------------------------------------------------------+
//| Building blocks                                                  |
//+------------------------------------------------------------------+
void OnEveryTick1()
{
    
    if (NDigits == 3 || NDigits == 5) PipValue = 10;
    
    TechnicalAnalysis2();
    TechnicalAnalysis6();
    
}

void TechnicalAnalysis2()
{
    
    if (GetIndicator(iMA(NULL, NULL,20,2,MODE_SMA,PRICE_CLOSE),0,0) > GetIndicator(iMA(NULL, NULL,9,2,MODE_SMA,PRICE_CLOSE),0,0))
    {
        BuyOrder3();
        
    }
}

void BuyOrder3()
{
    
    //--- prepare a request
    MqlTradeRequest request;
    request.action = TRADE_ACTION_DEAL;          // setting a deal order
    request.magic = 1;                   // ORDER_MAGIC
    request.symbol = Symbol();                   // symbol
    request.volume= Lots3;                      // volume in lots
    request.price = Ask();
    request.sl = Ask() - Stoploss3*PipValue*Point();      // Stop Loss specified
    request.tp = Ask() + Takeprofit3*PipValue*Point();    // Take Profit specified
    request.deviation= 4;             // deviation in points
    request.type = ORDER_TYPE_BUY;
    request.comment = "Order";
    
    MqlTradeResult result;
    OrderSend(request,result);
    // check the result
    if (!IsError(result, __FUNCTION__))
    {
            
    }
}

void TechnicalAnalysis6()
{
    
    if (GetIndicator(iMA(NULL, NULL,20,2,MODE_SMA,PRICE_CLOSE),0,0) < GetIndicator(iMA(NULL, NULL,9,2,MODE_SMA,PRICE_CLOSE),0,0))
    {
        BuyOrder3();
        
    }
}




Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 
tempii:

This EA works and places orders in demo (forward) testing but fails in backtest. I thought that backtest is simply a simulation of demo. Somehow they run differently.
CopyBuffer fails all the time in backtest while in demo testing there are no errors and orders are placed fine. 
Technical analysis is not the best in this EA, since whatever outcome is only Buy should be placed. But that is beyond the point. The point is that it runs fine in demo, but fails in backtest.

Is this a bug in MT5?

 

Hi, I met the same problem as you did, do you find and solutions? 
 

Please could someone tell how to  make ea visible on the chat, i have installed it in my platform but couldn't work

 
LOVING:

Please could someone tell how to  make ea visible on the chat, i have installed it in my platform but couldn't work

Forum on trading, automated trading systems and testing trading strategies

Modified Metatrader

Sergey Golubev, 2016.08.03 08:46

I mean - if all MT5 EAs does not work in your MT5 so you can check the following: autotrading for the EA, in common tab of the EA properties.

Allow Auto Trading — this option allows or prohibits trading using Expert Advisors and scripts. If it is disabled, scripts and Expert Advisors can work, but are not able to trade. This limitation can be useful for testing the analytical capabilities of an Expert Advisor in the real-time mode (not to be confused with testing on history data).

The option enables/disables automated trading for the entire platform. If you disable it, no Expert Advisor will be allowed to trade, even if you enable automated trading individually in the Expert Advisor settings. If you enable it, the Expert Advisors will be allowed to trade, unless automated trading is individually disabled in the Expert Advisor parameters

If everything is fine with the setting but EAs does not work (any EA - how many did you try?) so ask your broker (it may be the broker's limitation).

Forum on trading, automated trading systems and testing trading strategies

Modified Metatrader

Sergey Golubev, 2016.08.03 09:38

Where to check autotrading:


and


If you attached EA to the chart and you see "smiling face" so it is fine with autotading (in most of the cases):


If EA's face is not smiling and not happy so you can check everything once again:


It was for MT4 but MT5 is same with this case.



 
MFocus3:
Hi, I met the same problem as you did, do you find and solutions? 

The last time this user posted was in 2010.

So that's about 6 years ago.

The chance of him responding to your question is small.

If you want us to look at it please post the code.

Reason: