#property link          "KK"
#property version       "2.00"

#property indicator_chart_window
#property indicator_buffers 12
#property indicator_plots   2

// Plot 0 - Sárga (Slow)
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrYellow, clrYellow
#property indicator_width1  2
#property indicator_label1  "Slow Supertrend"

// Plot 1 - Cián (Fast)
#property indicator_type2   DRAW_COLOR_LINE
#property indicator_color2  clrCyan, clrCyan
#property indicator_width2  2
#property indicator_label2  "Fast Supertrend"

// Plot 0 buffers: data=0, colorindex=1
double Trend1[], TrendColor1[];
// Plot 1 buffers: data=2, colorindex=3
double Trend2[], TrendColor2[];

// Calculation buffers
double TrendDirection1[];
double up1[], dn1[], trend1[];
double TrendDirection2[];
double up2[], dn2[], trend2[];

int ATR_handle1, ATR_handle2;

input string IndicatorName   = "DUAL_SPRTRND";
input double ATRMultiplier   = 2.0;    // Slow ATR multiplier (Yellow)
input int    ATRPeriod       = 100;    // Slow ATR period (Yellow)
input double ATRMultiplier2i = 1.0;    // Fast ATR multiplier (Cyan)
input int    ATRPeriod2i     = 100;     // Fast ATR period (Cyan)
input int    ATRMaxBars      = 2000;
input int    Shift           = 0;

//+------------------------------------------------------------------+
int OnInit()
{
    IndicatorSetString(INDICATOR_SHORTNAME, IndicatorName);

    // Plot 0 - Sárga: data buffer=0, color buffer=1
    SetIndexBuffer(0, Trend1,      INDICATOR_DATA);
    SetIndexBuffer(1, TrendColor1, INDICATOR_COLOR_INDEX);

    // Plot 1 - Cián: data buffer=2, color buffer=3
    SetIndexBuffer(2, Trend2,      INDICATOR_DATA);
    SetIndexBuffer(3, TrendColor2, INDICATOR_COLOR_INDEX);

    // Calculation buffers
    SetIndexBuffer(4,  TrendDirection1, INDICATOR_CALCULATIONS);
    SetIndexBuffer(5,  up1,             INDICATOR_CALCULATIONS);
    SetIndexBuffer(6,  dn1,             INDICATOR_CALCULATIONS);
    SetIndexBuffer(7,  trend1,          INDICATOR_CALCULATIONS);
    SetIndexBuffer(8,  TrendDirection2, INDICATOR_CALCULATIONS);
    SetIndexBuffer(9,  up2,             INDICATOR_CALCULATIONS);
    SetIndexBuffer(10, dn2,             INDICATOR_CALCULATIONS);
    SetIndexBuffer(11, trend2,          INDICATOR_CALCULATIONS);

    PlotIndexSetInteger(0, PLOT_SHIFT, Shift);
    PlotIndexSetInteger(1, PLOT_SHIFT, Shift);

    PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
    PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);

    ArraySetAsSeries(Trend1,          true);
    ArraySetAsSeries(TrendColor1,     true);
    ArraySetAsSeries(Trend2,          true);
    ArraySetAsSeries(TrendColor2,     true);
    ArraySetAsSeries(TrendDirection1, true);
    ArraySetAsSeries(up1,             true);
    ArraySetAsSeries(dn1,             true);
    ArraySetAsSeries(trend1,          true);
    ArraySetAsSeries(TrendDirection2, true);
    ArraySetAsSeries(up2,             true);
    ArraySetAsSeries(dn2,             true);
    ArraySetAsSeries(trend2,          true);

    ATR_handle1 = iATR(Symbol(), Period(), ATRPeriod);
    ATR_handle2 = iATR(Symbol(), Period(), ATRPeriod2i);

    if (ATR_handle1 == INVALID_HANDLE || ATR_handle2 == INVALID_HANDLE)
    {
        Print("Failed to create ATR handles");
        return INIT_FAILED;
    }

    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    if (ATR_handle1 != INVALID_HANDLE) IndicatorRelease(ATR_handle1);
    if (ATR_handle2 != INVALID_HANDLE) IndicatorRelease(ATR_handle2);
}

//+------------------------------------------------------------------+
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(High,  true);
    ArraySetAsSeries(Low,   true);
    ArraySetAsSeries(Close, true);

    int counted_bars = 0;
    if (prev_calculated > 0) counted_bars = prev_calculated - 1;
    if (counted_bars < 0) return -1;
    if (counted_bars > 0) counted_bars--;

    int limit = rates_total - counted_bars;
    if (limit > ATRMaxBars)
    {
        limit = ATRMaxBars;
        if (rates_total < ATRMaxBars + 2 + ATRPeriod) limit = rates_total - 2 - ATRPeriod;
        if (limit <= 0) { Print("Need more historical data."); return 0; }
    }
    if (limit > rates_total - 2 - ATRPeriod) limit = rates_total - 2 - ATRPeriod;

    double ATR_buf1[], ATR_buf2[];
    ArraySetAsSeries(ATR_buf1, true);
    ArraySetAsSeries(ATR_buf2, true);
    CopyBuffer(ATR_handle1, 0, 0, limit + 1, ATR_buf1);
    CopyBuffer(ATR_handle2, 0, 0, limit + 1, ATR_buf2);

    for (int i = limit; i >= 0; i--)
    {
        bool flag, flagh;

        // === VONAL 1 - Sárga ===
        Trend1[i] = EMPTY_VALUE;

        double atr1    = ATR_buf1[i];
        double median1 = (High[i] + Low[i]) / 2;
        up1[i]    = median1 + ATRMultiplier * atr1;
        dn1[i]    = median1 - ATRMultiplier * atr1;
        trend1[i] = 1;

        if      (Close[i] > up1[i+1])  trend1[i] = 1;
        else if (Close[i] < dn1[i+1])  trend1[i] = -1;
        else if (trend1[i+1] ==  1)    trend1[i] = 1;
        else if (trend1[i+1] == -1)    trend1[i] = -1;

        flag  = (trend1[i] < 0 && trend1[i+1] > 0);
        flagh = (trend1[i] > 0 && trend1[i+1] < 0);

        if (trend1[i] > 0 && dn1[i] < dn1[i+1]) dn1[i] = dn1[i+1];
        if (trend1[i] < 0 && up1[i] > up1[i+1]) up1[i] = up1[i+1];
        if (flag)  up1[i] = median1 + ATRMultiplier * atr1;
        if (flagh) dn1[i] = median1 - ATRMultiplier * atr1;

        if      (trend1[i] ==  1) { Trend1[i] = dn1[i]; TrendColor1[i] = 0; }
        else if (trend1[i] == -1) { Trend1[i] = up1[i]; TrendColor1[i] = 1; }
        TrendDirection1[i] = TrendColor1[i];

        // === VONAL 2 - Cián ===
        bool flag2, flagh2;
        Trend2[i] = EMPTY_VALUE;

        double atr2    = ATR_buf2[i];
        double median2 = (High[i] + Low[i]) / 2;
        up2[i]    = median2 + ATRMultiplier2i * atr2;
        dn2[i]    = median2 - ATRMultiplier2i * atr2;
        trend2[i] = 1;

        if      (Close[i] > up2[i+1])  trend2[i] = 1;
        else if (Close[i] < dn2[i+1])  trend2[i] = -1;
        else if (trend2[i+1] ==  1)    trend2[i] = 1;
        else if (trend2[i+1] == -1)    trend2[i] = -1;

        flag2  = (trend2[i] < 0 && trend2[i+1] > 0);
        flagh2 = (trend2[i] > 0 && trend2[i+1] < 0);

        if (trend2[i] > 0 && dn2[i] < dn2[i+1]) dn2[i] = dn2[i+1];
        if (trend2[i] < 0 && up2[i] > up2[i+1]) up2[i] = up2[i+1];
        if (flag2)  up2[i] = median2 + ATRMultiplier2i * atr2;
        if (flagh2) dn2[i] = median2 - ATRMultiplier2i * atr2;

        if      (trend2[i] ==  1) { Trend2[i] = dn2[i]; TrendColor2[i] = 0; }
        else if (trend2[i] == -1) { Trend2[i] = up2[i]; TrendColor2[i] = 1; }
        TrendDirection2[i] = TrendColor2[i];
    }

    return rates_total;
}
//+------------------------------------------------------------------+