OnCalculate won't be called during backtest EA?

 

I am using MT4 strategy tester. I write an EA and an indicator. I will call the indicator within my EA by using 

double returnVal = iCustom(Symbol(), PERIOD_M5, "IndicatorName", IndicatorParam01, IndicatorParam02, channel, index);

This works in live trading but no in tester.

In the strategy tester, I selected Expert Advisor for my backtest. I printed log and found that function OnInit() will be called but OnCalculated(const int rates_total, ......) will not be called. This is a problem since my EA needed information provided by that indicator.

I would like to ask that am I make a wrong setting or this is no supported in MT4.

If that is no supported, what is the common implementation style in MT4 ?

Thank you very much.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 

OnCalculate is the "OnTick" function for indicators.

EA need to have OnTick, not OnCalculate.

 
Fabio Cavalloni:

OnCalculate is the "OnTick" function for indicators.

EA need to have OnTick, not OnCalculate.

I called: iCustom() wihtin OnTick() in my EA. I guess iCustom() will activate the indicator so that OnCalculated() will be called during the backtest process, but this is not the case in MT4.

I printed log and see that OnInit() within my indicator was called but not the OnCalculated(), so I would like to ask what is the common code style in MT4 if I want to use a custom indicator within an EA.

 
What you did it's correct,
But you are assigning iCustom return value to an int value, it must be a double one.

 
sulfred:

I am using MT4 strategy tester. I write an EA and an indicator. I will call the indicator within my EA by using 

This works in live trading but no in tester.

In the strategy tester, I selected Expert Advisor for my backtest. I printed log and found that function OnInit() will be called but OnCalculated(const int rates_total, ......) will not be called. This is a problem since my EA needed information provided by that indicator.

I would like to ask that am I make a wrong setting or this is no supported in MT4.

If that is no supported, what is the common implementation style in MT4 ?

Thank you very much.

iCustom returns a double not an int.

Of course OnCalculate() is called when an indicator is used from an EA with iCustom. If that's not the case either you check wrongly or there is a problem with your iCustom call or indicator.

 
Alain Verleyen:

iCustom returns a double not an int.

Of course OnCalculate() is called when an indicator is used from an EA with iCustom. If that's not the case either you check wrongly or there is a problem with your iCustom call or indicator.

Thank you for your reply.

I try one more thing.

int OnInit()
{
    ...
    ResetLastError();
    double retValue = iCustom(Symbol(), PERIOD_M5, "OsMA", 12, 26, 9, 0, 0);
    int errorCode = GetLastError();
    PrintFormat("[%s:%d] [ERROR] lasterror: %d retValue: %f", __FUNCTION__, __LINE__, errorCode, retValue);
    ...
    return(INIT_SUCCEEDED);
}

When I put this EA to the chart, I get

lasterror: 0 retValue: -3.647698

When I put this EA to backtest, I get

lasterror: 4073 retValue: 0.000000

I found that 4073 is ERR_NO_HISTORY_DATA, from https://docs.mql4.com/constants/errorswarnings/errorcodes

Since that indicator is shipped with MT4, so I assume that has no problem.

But the problem is that OnTick() is called during backtest, I added:

void OnTick()
{
    PrintFormat("[%s:%d] close: %f", __FUNCTION__, __LINE__, Close[0]);
}

And I can see the data is streaming to the EA.


So, I would like to ask how to provide data to indicator when I am backtesting the EA? Since the indicator is working fine if I backtest Indicator instead of EA.

 
sulfred:

Thank you for your reply.

I try one more thing.

When I put this EA to the chart, I get

When I put this EA to backtest, I get

I found that 4073 is ERR_NO_HISTORY_DATA, from https://docs.mql4.com/constants/errorswarnings/errorcodes

Since that indicator is shipped with MT4, so I assume that has no problem.

But the problem is that OnTick() is called during backtest, I added:

And I can see the data is streaming to the EA.


So, I would like to ask how to provide data to indicator when I am backtesting the EA? Since the indicator is working fine if I backtest Indicator instead of EA.


Finally, I see the solution from https://forexsb.com/forum/topic/6625/error-in-mt4-backtesting/

I should tick Use date selection

Reason: