MQL5 indicator buffer random values
During my coding of an indicator random values would end up in the index buffer if one would switch time frames.
It took me a few hours to figure out that if I didn't explicitly set an empty value this would cause this to happen - so the solution is to always set a value during your OnCalculate() loop both for your empty bars and not.
/*
OnCalculate
===========
Called only in custom indicators when it's necessary to calculate the indicator values by the Calculate event.
*/
int OnCalculate(const int iRatesTotal,
const int iPreviousCalculated
,const datetime &time[]
,const double &aOpen[]
,const double &aHigh[]
,const double &aLow[]
,const double &aClose[]
,const long &tick_volume[]
,const long &volume[]
,const int &spread[])
{
Print(">>> OnCalculate(): `iRatesTotal=" + iRatesTotal + "`, `iPreviousCalculated=" + iPreviousCalculated + "`");
//int iStart = (iPreviousCalculated == 0 ? 3 : iPreviousCalculated);
for(int i=(iPreviousCalculated == 0 ? 3 : iPreviousCalculated-1); i < iRatesTotal;i++)
{
// must explicitly assign our EMPTY_VALUE
aBufferBuyEP[i]=NULL;
aBufferBuySL[i]=NULL;
aBufferBuyTP[i]=NULL;
...
}
Print("<<< OnCalculate(): `iRatesTotal=" + iRatesTotal + "`");
return(iRatesTotal);
}
Follow me on twitter @danielsokolows.

Comments
Post a Comment