Why oncalculate return wrong value for close[0] ?

 

Hi  guys  , i try to return a value of Close[0] in a indicator i am in xauusd  and hi have quoatation near 3292.70 but when i print close[0] return 1320.31 0_o    why ?

my code anyone know  why happen this ?

//+------------------------------------------------------------------+
//| Simple EMA with Alert and Debug Print                           |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "EMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_width1  2

input int PeriodEMA = 50;                   // EMA Period
input ENUM_APPLIED_PRICE PriceType = PRICE_CLOSE;
input int AlertDistancePoints = 5;          // distanza in punti per alert
input bool EnableAlert = true;

double EMABuffer[];

static datetime lastAlertTime = 0;

//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, EMABuffer);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, clrBlue);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
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[])
  {
   // DEBUG PRINT all’inizio
   PrintFormat("DEBUG OnCalculate: Symbol=%s, Period=%d, rates_total=%d, prev_calculated=%d", Symbol(), Period(), rates_total, prev_calculated);
PrintFormat("DEBUG: close[0] = %.5f, EMABuffer[0] = %.5f", close[0], EMABuffer[0]);

   if(rates_total < PeriodEMA)
      return(0);

   int start = prev_calculated > 0 ? prev_calculated - 1 : PeriodEMA;

   // Calcolo EMA manuale (per chiarezza)
   double k = 2.0 / (PeriodEMA + 1);
   EMABuffer[start-1] = close[start-1];

   for(int i = start; i < rates_total; i++)
     {
      EMABuffer[i] = close[i] * k + EMABuffer[i-1] * (1 - k);
     }

   // Alert: se prezzo si avvicina alla EMA entro AlertDistancePoints
   if(EnableAlert && rates_total > 1)
     {
      double distance = MathAbs(close[rates_total -1] - EMABuffer[rates_total -1]);

      // distanza in punti (moltiplica per _Point)
      double threshold = AlertDistancePoints * _Point;

      bool near = (distance <= threshold);

      if(near && lastAlertTime != time[rates_total -1])
        {
         lastAlertTime = time[rates_total -1];
         Alert("Price near EMA on ", Symbol(), " at ", TimeToString(time[rates_total -1], TIME_DATE | TIME_SECONDS));
        }
     }

   return(rates_total);
  }
 

Hello , close[0] is the first most ancient bar in the chart

close[rates_total-1] is the most recent

This website uses cookies. Learn more about our Cookies Policy.