Create an Array from 2 existing dynamic Array

 
Hello respected experts

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:

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 
//+------------------------------------------------------------------+
//| 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);
//--- if the handle is not created
   if(handle_indicator1==INVALID_HANDLE)
     {
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
   if(handle_indicator2==INVALID_HANDLE)
     {
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double buffer1[] ;
   ArraySetAsSeries(buffer1,true) ;
   double buffer2[] ;
   ArraySetAsSeries(buffer2,true) ;
   double series3[] ;
   ArraySetAsSeries(series3,true) ;

   int start_pos=0,count=nu_candles;
   if(!iGetArray(handle_indicator1,0,start_pos,count,indicator1))
      return;
   if(!iGetArray(handle_indicator2,0,start_pos,count,indicator2))
      return;

   for(int i=nu_candles-1; i>0 ; i--) 
     {
     series3[i]=buffer1[i]-buffer2[i];
     }
   }
//***********************************************************
 
habibie60:
Hello respected experts

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. 

  1. You've defined double buffer1[]; and double buffer2[]; arrays, but you're not populating them with data from your indicators.

  2. 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.....

 
Nardus Van Staden #:

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. 

  1. You've defined double buffer1[]; and double buffer2[]; arrays, but you're not populating them with data from your indicators.

  2. ...


Thank you very much Mr. Nardus
You are right about number 1, it was a typo on my part, but after correcting it, the error still exists
I completely replaced the codes with your code but still the problem!!
Files:
test.mq5  5 kb