I want to simultaneously use TEMA and MACD indicator to open/close position. But my EA always got "array out of range" error in my MACD code part.
now, the problem is existing in
MACD[i] is "array out of range". Why this happened?
int i=iBars(Symbol(),PERIOD_CURRENT); // this cannot be the index of a bar int i=iBars(Symbol(),PERIOD_CURRENT)-1; // this can be!
-
Of course, it does, MACD, signal, fastTEMAVal and slow are arrays with zero size; you never put values into them.
-
Arrays must be manually sized. They have no direction. You must move elements if you want a stack/as-series (set non-series, enlarge the array, set as-series).
-
Buffers are automatically size, are as-series, and elements are moved for you, new elements are set to EMPTY_VALUE (or your designated. They can also draw on the chart automatically.
-
In MT4, buffers and MT4 predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.
To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
Event Handling Functions - Functions - Language Basics - MQL4 Reference -
In MT5, you must set the direction.
To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5
-
-
Of course, it does, MACD, signal, fastTEMAVal and slow are arrays with zero size; you never put values into them.
-
Arrays must be manually sized. They have no direction. You must move elements if you want a stack/as-series (set non-series, enlarge the array, set as-series).
-
Buffers are automatically size, are as-series, and elements are moved for you, new elements are set to EMPTY_VALUE (or your designated. They can also draw on the chart automatically.
-
In MT4, buffers and MT4 predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.
-
In MT5, you must set the direction.
-
Really thanks for your detailed reply.
I have now fixed the problem. Thank you very much!
- 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 want to simultaneously use TEMA and MACD indicator to open/close position. But my EA always got "array out of range" error in my MACD code part.
now, the problem is existing in
MACD[i] is "array out of range". Why this happened?