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.
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.
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.
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

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.