//+------------------------------------------------------------------+
//|                                                  APB Candles.mq5 |
//|                          Transmuted with love by Cagliostro 2024 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2024, Cagliostro"
#property link      "https://www.forex-station.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 1

#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_color1 clrRed, clrGreen
#property indicator_width1 2
#property indicator_label1 "APB Candles"

//--- indicator buffers
double ExtOpenBuffer[];
double ExtHighBuffer[];
double ExtLowBuffer[];
double ExtCloseBuffer[];
double ColorBuffer[];

//+------------------------------------------------------------------+
//| Indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
    //--- indicator buffer mapping
    SetIndexBuffer(0, ExtOpenBuffer, INDICATOR_DATA);
    SetIndexBuffer(1, ExtHighBuffer, INDICATOR_DATA);
    SetIndexBuffer(2, ExtLowBuffer, INDICATOR_DATA);
    SetIndexBuffer(3, ExtCloseBuffer, INDICATOR_DATA);
    SetIndexBuffer(4, ColorBuffer, INDICATOR_COLOR_INDEX);
    
    //--- set buffer styles
    PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_CANDLES);
    
    //--- set buffer colors
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, clrRed);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, clrGreen);

    //--- name for DataWindow
    PlotIndexSetString(0, PLOT_LABEL, "APB Candles");

    return (INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Indicator iteration function                              |
//+------------------------------------------------------------------+
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[]) {
    
    double APBOpen, APBHigh, APBLow, APBClose;

    int start = prev_calculated > 0 ? prev_calculated - 1 : 0;

    for (int i = start; i < rates_total; i++) {
        APBClose = NormalizeDouble((open[i] + high[i] + low[i] + close[i]) / 4.0, _Digits);
        APBClose = (APBClose + close[i]) / 2.0;
        APBOpen = i == 0 ? open[i] : (ExtOpenBuffer[i - 1] + ExtCloseBuffer[i - 1]) / 2.0;
        APBHigh = MathMax(high[i], MathMax(APBOpen, APBClose));
        APBLow = MathMin(low[i], MathMin(APBOpen, APBClose));

        ExtOpenBuffer[i] = APBOpen;
        ExtHighBuffer[i] = APBHigh;
        ExtLowBuffer[i] = APBLow;
        ExtCloseBuffer[i] = APBClose;

        if (APBOpen < APBClose) {
            ColorBuffer[i] = 1;  // Green
        } else {
            ColorBuffer[i] = 0;  // Red
        }
    }

    return (rates_total);
}
//+------------------------------------------------------------------+
