I'm going to make an Array from 2 existing dynamic Array.
But I get the following error:
array out of range in 'test.mq5'
Please help me to correct the codes:
I see that you're attempting to calculate the difference between two indicators for a given number of candles. However, I can see a couple of issues in your code that might be causing the "array out of range" error.
-
You've defined double buffer1[]; and double buffer2[]; arrays, but you're not populating them with data from your indicators.
-
You're attempting to calculate series3[i] = buffer1[i] - buffer2[i]; using the buffer1 and buffer2 arrays, but these arrays are empty and uninitialized.
Here is the correct way to do it, this should compile without errors.
enum ENUM_INDICATORS {RSI, CCI}; input ENUM_INDICATORS indi = RSI; string indicator = EnumToString(indi); input int nu_candles = 100; // Number of candles int handle_indicator1; // Variable for storing the handle of the SYMBOL1 indicator int handle_indicator2; // Variable for storing the handle of the SYMBOL2 indicator // Define the symbols for the indicators string symbol_1 = "EURUSD"; // Replace with the symbol you're using string symbol_2 = "GBPUSD"; // Replace with the symbol you're using //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Create handle of the indicator handle_indicator1 = iCustom(symbol_1, _Period, "Examples\\" + indicator); handle_indicator2 = iCustom(symbol_2, _Period, "Examples\\" + indicator); // Check if the handles are valid if (handle_indicator1 == INVALID_HANDLE || handle_indicator2 == INVALID_HANDLE) { Print("Failed to create indicator handles."); return INIT_FAILED; } return INIT_SUCCEEDED; } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Perform cleanup if needed } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { double buffer1[]; double buffer2[]; double series3[]; ArraySetAsSeries(buffer1, true); ArraySetAsSeries(buffer2, true); ArraySetAsSeries(series3, true); int start_pos = 0; int count = nu_candles; // Fetch indicator data for buffer1 and buffer2 if (!CopyBuffer(handle_indicator1, 0, start_pos, count, buffer1)) return; if (!CopyBuffer(handle_indicator2, 0, start_pos, count, buffer2)) return; for (int i = nu_candles - 1; i > 0; i--) { series3[i] = buffer1[i] - buffer2[i]; } // Now you can use the values in the series3 array as needed }
Hope this helps.....
I see that you're attempting to calculate the difference between two indicators for a given number of candles. However, I can see a couple of issues in your code that might be causing the "array out of range" error.
-
You've defined double buffer1[]; and double buffer2[]; arrays, but you're not populating them with data from your indicators.
-
...
- 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'm going to make an Array from 2 existing dynamic Array.
But I get the following error:
array out of range in 'test.mq5'
Please help me to correct the codes: