why this indicator not plot nothing ?

 

Hi guys  anyone can explain me why this indicator not plot  nothing ? 

//+------------------------------------------------------------------+
//|                                                     CorrelationIndi.mq5 |
//|              Indicatore per calcolare la correlazione tra due strumenti               |
//+------------------------------------------------------------------+
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1

// Proprietà del plot
#property indicator_label1  "Correlation"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlack
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

// Buffer per i dati dell'indicatore
double CorrelationBuffer[];

// Parametri di input
input string Symbol1 = "ETHUSDT";   // Simbolo del primo strumento
input string Symbol2 = "BTCUSDT";   // Simbolo del secondo strumento
input int Periods = 10;            // Numero di periodi per il calcolo della correlazione

//+------------------------------------------------------------------+
//| Funzione di inizializzazione dell'indicatore                      |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Imposta il buffer dell'indicatore
   SetIndexBuffer(0, CorrelationBuffer, INDICATOR_DATA);
   ArraySetAsSeries(CorrelationBuffer, true);
   IndicatorSetInteger(INDICATOR_DIGITS, 3);

   // Imposta il valore vuoto per il plot
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   
   // Controlla se i simboli sono validi
   if(Symbol1 == "" || Symbol2 == "")
     {
      Print("Errore: I simboli non sono specificati correttamente.");
      return(INIT_FAILED);
     }

   // Controlla se i simboli esistono nella piattaforma
   if(!SymbolSelect(Symbol1, true) || !SymbolSelect(Symbol2, true))
     {
      Print("Errore: Uno dei simboli non è disponibile nella piattaforma.");
      return(INIT_FAILED);
     }

   Print("Inizializzazione completata con successo.");
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Funzione principale dell'indicatore                              |
//+------------------------------------------------------------------+
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[])
  {
   // Controlla se ci sono abbastanza dati
   if(rates_total < Periods)
   {
      Print("Non ci sono abbastanza dati per calcolare la correlazione.");
      return(0);
   }

   int start = prev_calculated;
   if(prev_calculated == 0)
      start = Periods;

   // Loop attraverso le candele per calcolare la correlazione
   for(int i = start; i < rates_total; i++)
     {
      // Preleva i dati dei prezzi di chiusura per entrambi i simboli
      double prices1[], prices2[];
      int copied1 = CopyClose(Symbol1, 0, i - Periods + 1, Periods, prices1);
      int copied2 = CopyClose(Symbol2, 0, i - Periods + 1, Periods, prices2);

      // Verifica se sono stati copiati abbastanza dati
      if(copied1 != Periods || copied2 != Periods)
        {
         CorrelationBuffer[i] = EMPTY_VALUE; // Imposta a EMPTY_VALUE se non ci sono abbastanza dati
         Print("Non ci sono abbastanza dati copiati per la barra ", i);
         continue;
        }

      // Calcola la correlazione di Pearson
      double correlation = PearsonCorrelation(prices1, prices2, Periods);

      // Salva il valore nel buffer
      CorrelationBuffer[i] = correlation;
      Print("Correlazione alla barra ", i, ": ", correlation);
     }

   return(rates_total);
  }

//+------------------------------------------------------------------+
//| Funzione per calcolare la correlazione di Pearson                |
//+------------------------------------------------------------------+
double PearsonCorrelation(double &x[], double &y[], int size)
  {
   double sumX = 0, sumY = 0, sumXY = 0;
   double sumX2 = 0, sumY2 = 0;

   for(int i = 0; i < size; i++)
     {
      sumX += x[i];
      sumY += y[i];
      sumXY += x[i] * y[i];
      sumX2 += x[i] * x[i];
      sumY2 += y[i] * y[i];
     }

   double numerator = size * sumXY - sumX * sumY;
   double denominator = MathSqrt((size * sumX2 - sumX * sumX) * (size * sumY2 - sumY * sumY));

   if(denominator == 0)
      return 0;

   return numerator / denominator;
  }
 

Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

You have been informed of this before, so please post in the correct section for your queries.

 
Fernando Carreiro #:

Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

You have been informed of this before, so please post in the correct section for your queries.

sorry some time do thing with out  think sorry

 
   for(int i = start; i < rates_total; i++)
     {
⋮
      CorrelationBuffer[i] = correlation;

 Your loop is non-series, but your buffer defaults to as-series.

In MT5, you must set the direction.

To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] and other arrays (copyXXXX functions), 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
 
not understund i must use  ArrayGetAsSeries()  instead of ArraySetAsSeries()  ?
Documentation on MQL5: Array Functions / ArrayGetAsSeries
Documentation on MQL5: Array Functions / ArrayGetAsSeries
  • www.mql5.com
It checks direction of an array index. Parameters array [in]  Checked array. Return Value Returns true , if the specified array has the...
 
faustf #:
not understund i must use  ArrayGetAsSeries()  instead of ArraySetAsSeries()  ?

ArraySetAsSeries()

Documentation on MQL5: Array Functions / ArraySetAsSeries
Documentation on MQL5: Array Functions / ArraySetAsSeries
  • www.mql5.com
The function sets the AS_SERIES flag to a selected object of a dynamic array , and elements will be indexed like in timeseries . Parameters array[]...