int OnInit(){ MqlRates rates[]; int totalbars=Bars(_Symbol,_Period); ArraySetAsSeries(rates, true); CopyRates(_Symbol,_Period,0,totalbars,rates); ArraySetAsSeries(rsi,true); int rsiHandle=iRSI(_Symbol,_Period,21,PRICE_CLOSE); CopyBuffer(rsiHandle,0,0,totalbars,rsi);
- don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
How to call indicators in MQL5 - MQL5 Articles 12 March 2010 -
Check your return codes
This is the MQL4 section, but your code appears to be MQL5.
Please confirm what it is intended to be.
This is the MQL4 section, but your code appears to be MQL5.
Please confirm what it is intended to be.
Sorry about that. I thought I was in the right place. I must've missed a step while finding the MQL5 forum
- don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
How to call indicators in MQL5 - MQL5 Articles 12 March 2010 -
Check your return codes
Thank you for your quick and very clear explanation.
I have made the necessary changes as you have recommended as below.
int OnInit() { ArraySetAsSeries(rsi,true); int rsiHandle=iRSI(_Symbol,_Period,21,PRICE_CLOSE); if (rsiHandle<0) Alert ("RSI indicator not initialized"); ArraySetAsSeries(cci,true); int cciHandle=iCustom(_Symbol,_Period,"Examples\\CCI"); if (cciHandle<0) Alert ("CCI indicator not initialized"); //--- return(INIT_SUCCEEDED); } void OnTick() { //--- MqlRates rates[]; int totalbars=Bars(_Symbol,_Period); ArraySetAsSeries(rates, true); int ratescopied=CopyRates(_Symbol,_Period,0,totalbars,rates); if (ratescopied<0) Alert ("Rates not copied"); int rsicopied=CopyBuffer(rsiHandle,0,0,totalbars,rsi); if (rsicopied<0) Alert ("Rsi not copied"); int ccicopied=CopyBuffer(cciHandle,0,0,totalbars,cci); if (ccicopied<0) Alert ("CCI not copied");
I am still getting the same Critical Error but after checking the CopyBuffer return codes, it turns out the problem is the indicators are not copying the values. What could I be doing wrong and what could be the solution? Thanks in advance.
- www.mql5.com
It looks like the problem wasn't that the indicators weren't copying values. I have solved that but still get the same OnTick Critical Error.
Use ArrayResize(), to give a count size your cci and rsi buffers with value of totalbars size.
- ArraySize
- No need, OP already knows the size in the returned values (e.g. rsicopied.)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I keep getting the runtime OnTick Critical error when I try to test my ea. Here is part of the code;
I understand that it is caused by the array being out of range but I have tried everything I can think of but I can't get anywhere. Any help will be highly appreciated. Thanks.