in oncalculate high[] not return a right number

 

Hi, why in my script ,the high[] not  return a  good high ?  

//+------------------------------------------------------------------+
//|                                                   supertrend.mq5 |
//+------------------------------------------------------------------+
#property version   "1.02"
#property indicator_chart_window
#property indicator_plots 2
#property indicator_buffers 3
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_color1 clrNONE, clrNONE
#property indicator_type2 DRAW_NONE

//--- Input Parameters ---
input int    ATRPeriod       = 7;              // Period for ATR calculation
input double Multiplier      = 3.0;            // ATR multiplier for band calculation
input color SuperTrendColorUp   = clrPaleGreen;   // Colore linea uptrend
input color SuperTrendColorDown = clrLightPink;     // Colore linea downtrend
input ENUM_APPLIED_PRICE SourcePrice = PRICE_MEDIAN; // Price source for calculations
input bool   TakeWicksIntoAccount = true;       // Include wicks in calculations
input bool   ShowSuperTrendLine = true;         // Visualizza la linea SuperTrend
input color DotColorLong  = clrLime;  // Colore del pallino all’inizio del trend long
input color DotColorShort = clrAqua;    // Colore del pallino all’inizio del trend short
input double DotOffsetPoints = 10.0;  // Offset verticale dei pallini in punti


//--- Indicator Handles ---
int    atrHandle;                               // Handle for ATR indicator

//--- Indicator Buffers ---
double SuperTrendBuffer[];                      // Main SuperTrend line values
double SuperTrendColorBuffer[];                 // Color index buffer (0 = Green, 1 = Red)
double SuperTrendDirectionBuffer[];             // Direction buffer (1 = Up, -1 = Down)
 int dotCTrlUp = 0;
 int dotCTrlDown = 0;

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    atrHandle = iATR(NULL, 0, ATRPeriod);
    if(atrHandle == INVALID_HANDLE)
    {
        Print("Error creating ATR indicator. Error code: ", GetLastError());
        return INIT_FAILED;
    }

    SetIndexBuffer(0, SuperTrendBuffer, INDICATOR_DATA);
    SetIndexBuffer(1, SuperTrendColorBuffer, INDICATOR_COLOR_INDEX);
    SetIndexBuffer(2, SuperTrendDirectionBuffer, INDICATOR_DATA);

    PlotIndexSetString(0, PLOT_LABEL, "SuperTrend");
    PlotIndexSetString(2, PLOT_LABEL, "SuperTrend direction");
PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, SuperTrendColorUp);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, SuperTrendColorDown);

    ArraySetAsSeries(SuperTrendBuffer, false);
    ArraySetAsSeries(SuperTrendDirectionBuffer, false);
    ArraySetAsSeries(SuperTrendColorBuffer, false);

    if(!ShowSuperTrendLine)
    {
        PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_NONE);  // Nasconde la linea
    }

    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Deinizializzazione                                               |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    IndicatorRelease(atrHandle);

    // Rimuove i pallini in caso di ricaricamento
    ObjectsDeleteAll(0, "TrendDot_");
}

//+------------------------------------------------------------------+
//| Calcolo principale                                               |
//+------------------------------------------------------------------+
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[]
)
{
    ArraySetAsSeries(time, false);
    ArraySetAsSeries(open, false);
    ArraySetAsSeries(high, false);
    ArraySetAsSeries(low, false);
    ArraySetAsSeries(close, false);

    double atrBuffer[];
    ArraySetAsSeries(atrBuffer, false);

    double srcPrice, highPrice, lowPrice, atr;
    double longStop, longStopPrev, shortStop, shortStopPrev;
    int supertrend_dir = 1;

    for(int i = prev_calculated; i < rates_total; i++)
    {
        // Source price
        switch(SourcePrice)
        {
            case PRICE_CLOSE: srcPrice = close[i]; break;
            case PRICE_OPEN: srcPrice = open[i]; break;
            case PRICE_HIGH: srcPrice = high[i]; break;
            case PRICE_LOW: srcPrice = low[i]; break;
            case PRICE_MEDIAN: srcPrice = (high[i] + low[i]) / 2.0; break;
            case PRICE_TYPICAL: srcPrice = (high[i] + low[i] + close[i]) / 3.0; break;
            default: srcPrice = (high[i] + low[i] + 2 * close[i]) / 4.0; break;
        }

        highPrice = TakeWicksIntoAccount ? high[i] : close[i];
        lowPrice = TakeWicksIntoAccount ? low[i] : close[i];

        if(CopyBuffer(atrHandle, 0, rates_total - i - 1, 1, atrBuffer) == -1)
        {
            Print("Error copying ATR buffer. Error code: ", GetLastError());
        }
        atr = atrBuffer[0];

        longStop = srcPrice - Multiplier * atr;
        longStopPrev = i > 0 ? SuperTrendBuffer[i - 1] : longStop;
        if(longStop > 0)
        {
            if(open[i] == close[i] && open[i] == low[i] && open[i] == high[i])
                longStop = longStopPrev;
            else
                longStop = (lowPrice > longStopPrev ? MathMax(longStop, longStopPrev) : longStop);
        }
        else longStop = longStopPrev;

        shortStop = srcPrice + Multiplier * atr;
        shortStopPrev = i > 0 ? SuperTrendBuffer[i - 1] : shortStop;
        if(shortStop > 0)
        {
            if(open[i] == close[i] && open[i] == low[i] && open[i] == high[i])
                shortStop = shortStopPrev;
            else
                shortStop = (highPrice < shortStopPrev ? MathMin(shortStop, shortStopPrev) : shortStop);
        }
        else shortStop = shortStopPrev;

        if(i > 0)
        {
            supertrend_dir = (int)SuperTrendDirectionBuffer[i - 1];
            if(supertrend_dir == -1 && highPrice > shortStopPrev)
                supertrend_dir = 1;
            else if(supertrend_dir == 1 && lowPrice < longStopPrev)
                supertrend_dir = -1;
        }


        if(supertrend_dir == 1)
        {
            SuperTrendBuffer[i] = longStop;
            SuperTrendDirectionBuffer[i] = 1;
            SuperTrendColorBuffer[i] = 0;
            // ↪ Disegna pallino SOLO se è un nuovo trend long
           // if (dotCTrlUp == 0)
            // {
                 string name = "TrendDot_" + IntegerToString(i);
                 double pointOffset = DotOffsetPoints * _Point;
                 double y = high[i] + pointOffset;
                 ObjectCreate(0, name, OBJ_ARROW, 0, time[i], y);
                 ObjectSetInteger(0, name, OBJPROP_ARROWCODE, 159);
                 ObjectSetInteger(0, name, OBJPROP_COLOR, DotColorLong);
                 ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);
                 dotCTrlUp=1;
                 dotCTrlDown=0;
             //}
        }
        else
        {
            SuperTrendBuffer[i] = shortStop;
            SuperTrendDirectionBuffer[i] = -1;
            SuperTrendColorBuffer[i] = 1;
             // ↪ Disegna pallino SOLO se è un nuovo trend short
             if (dotCTrlDown == 0)
             {
                 string name = "TrendDot_" + IntegerToString(i);
                 double pointOffset = DotOffsetPoints * _Point;
                 double y = low[i] - pointOffset;
                 ObjectCreate(0, name, OBJ_ARROW, 0, time[i], y);
                 ObjectSetInteger(0, name, OBJPROP_ARROWCODE, 159);
                 ObjectSetInteger(0, name, OBJPROP_COLOR, DotColorShort);
                 ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);
                 dotCTrlUp=0;
                 dotCTrlDown=1;
             }
        }



    }

    return(rates_total - 1);
}