input int atr_period=14; // period of calculation double iATRBuffer[]; int handle; //+------------------------------------------------------------------+ int OnInit() { handle=iATR(_Symbol,PERIOD_CURRENT,atr_period); //--- if the handle is not created if(handle==INVALID_HANDLE) { Print("Error creating \"ATR\" indicator"); ExpertRemove(); } ArraySetAsSeries(iATRBuffer,true); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnTick(void) { if(CopyBuffer(handle,0,0,10,iATRBuffer)!=10) return; //--- work for(int i=0; i<10; i++) Print("ATR ",i,": ",iATRBuffer[i]); }
Thank you for the answer.
What is the problem with my code?
I got this in the MT5: "cannot load indicator 'Average True Range' [4002]".
4002 is "Wrong parameter in the inner call of the client terminal function". I can't see what is wrong.
int hATR; double valATR[]; double atr; int atrPeriod=14; int OnInit() { chartSymbol=_Symbol; hATR=iATR(chartSymbol,PERIOD_CURRENT,atrPeriod); if(hATR==INVALID_HANDLE) return(INIT_FAILED); return(INIT_SUCCEEDED); } int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { if(CopyBuffer(hATR,0,0,1,valATR)>0) atr=valATR[0]; return(rates_total); }
And it is desirable to clear the array.
ZeroMemory(valATR); if(CopyBuffer(hATR,0,0,1,valATR)>0) atr=valATR[0];
Pay attention to ArraySetAsSeries. The order of the elements of indicator arrays differs in MQL4/5.
And it is desirable to clear the array.
I don't see a reason why it's desirable. If the function succeeds you have the right data, if it fails you don't have to use the data.
I don't see a reason why it's desirable. If the function succeeds you have the right data, if it fails you don't have to use the data.
Why store unnecessary data? Clogged work. If they can be cleaned immediately.
Thank you for the answer.
What is the problem with my code?
I got this in the MT5: "cannot load indicator 'Average True Range' [4002]".
4002 is "Wrong parameter in the inner call of the client terminal function". I can't see what is wrong.
Unless you have "chartSymbol" defined somewhere else, then you can not compile that code
Declare the chartSymbol as string and it will work with no problems at all
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, I am new to MT5 and confused.
In MT4 it was easy to get an indicator value, for example:
The documentation is kinda difficult for me.
How can I get the "atr" value in MT5? How does the above code look like in MT5?
Thanks