//+------------------------------------------------------------------+
//| Advanced Color-Coded Trend Indicator                             |
//| Displays trend strength with slope-based color changes           |
//|                                          Author Alpha24          |
//+------------------------------------------------------------------+
#property link "Alpha24"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 clrYellow // Main Line
#property indicator_color2 clrTeal   // Uptrend
#property indicator_color3 clrCrimson    // Downtrend
#property indicator_width1 3
#property indicator_width2 4
#property indicator_width3 4
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_type3 DRAW_HISTOGRAM

// Indicator Buffers
double MainBuffer[];
double UpBuffer[];
double DownBuffer[];

// Input Parameters
input int EMA1_Period = 5; //9;
input int EMA2_Period = 8; //12;
input int EMA3_Period = 13; //21;
input int EMA4_Period = 21; //50;
input int EMA5_Period = 50; //100;

// Moving Average Calculation Function
double SmoothedEMA(int period, int applied_price, int shift) {
    return iMA(NULL, 0, period, 0, MODE_EMA, applied_price, shift);
}

// Indicator Initialization
int OnInit() {
    IndicatorBuffers(3);
    SetIndexBuffer(0, MainBuffer);
    SetIndexBuffer(1, UpBuffer);
    SetIndexBuffer(2, DownBuffer);
    return INIT_SUCCEEDED;
}

// Main Calculation Function
int start() {
    int rates_total = Bars;
    for (int i = 1; i < rates_total - 1; i++) {
        double ema1 = SmoothedEMA(EMA1_Period, PRICE_CLOSE, i);
        double ema2 = SmoothedEMA(EMA2_Period, MODE_EMA, i);
        double ema3 = SmoothedEMA(EMA3_Period, MODE_EMA, i);
        double ema4 = SmoothedEMA(EMA4_Period, MODE_EMA, i);
        double ema5 = SmoothedEMA(EMA5_Period, MODE_EMA, i);
        
        double histogramValue = (ema1 - ema2) + (ema2 - ema3) + (ema3 - ema4) + (ema4 - ema5);
        MainBuffer[i] = histogramValue;
        
        // Slope-based Color Change
        if (histogramValue > MainBuffer[i + 1]) {
            UpBuffer[i] = histogramValue;
            DownBuffer[i] = 0;
        } else {
            UpBuffer[i] = 0;
            DownBuffer[i] = histogramValue;
        }
    }
    return 0;
}
