#property copyright "Indicator for Tradeable Trends and Reversals on XAUUSD"
#property link ""
#property version "1.00"
#property indicator_chart_window // Draw on the main chart window
#property indicator_buffers 4    // We use 4 buffers for plotting

// Indicator buffers for plotting
double UpTrendBuffer[];
double DownTrendBuffer[];
double UpReversalBuffer[];
double DownReversalBuffer[];

// Input parameters for the indicator
extern int MainMAPeriod = 100; // Period for the main Moving Average
extern ENUM_MA_METHOD MainMAMethod = MODE_SMA; // Method for the main MA
extern ENUM_APPLIED_PRICE MainMAPrice = PRICE_CLOSE; // Price type for the main MA

// Corrected declaration for FastMATimeframe using MQL4 compatible type
extern int FastMATimeframe_int = 5; // Use an integer for the timeframe. 5 corresponds to M5.
extern int FastMAPeriod = 10; // Period for the fast MA
extern ENUM_MA_METHOD FastMAMethod = MODE_SMA; // Method for the fast MA
extern ENUM_APPLIED_PRICE FastMAPrice = PRICE_CLOSE; // Price type for the fast MA

// Drawing properties for chart
#property indicator_label1 "UpTrend"
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color1 clrLightGreen // Pastel green for Up-trends

#property indicator_label2 "DownTrend"
#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#property indicator_color2 clrCoral // Pastel red for Down-trends

#property indicator_label3 "UpReversal"
#property indicator_type3 DRAW_ARROW
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
#property indicator_color3 clrSpringGreen // Pastel green for up-reversal arrows

#property indicator_label4 "DownReversal"
#property indicator_type4 DRAW_ARROW
#property indicator_style4 STYLE_SOLID
#property indicator_width4 2
#property indicator_color4 clrOrangeRed // Pastel red for down-reversal arrows


// Indicator initialization
int OnInit()
{
    // Set buffers for drawing and assign them to indices
    SetIndexBuffer(0, UpTrendBuffer);
    SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, clrLightGreen);
    SetIndexLabel(0, "UpTrend");

    SetIndexBuffer(1, DownTrendBuffer);
    SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2, clrCoral);
    SetIndexLabel(1, "DownTrend");

    SetIndexBuffer(2, UpReversalBuffer);
    SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, 2, clrSpringGreen);
    SetIndexArrow(2, 233); // Set arrow symbol for up-reversal
    SetIndexLabel(2, "UpReversal");

    SetIndexBuffer(3, DownReversalBuffer);
    SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, 2, clrOrangeRed);
    SetIndexArrow(3, 234); // Set arrow symbol for down-reversal
    SetIndexLabel(3, "DownReversal");

    // Clear all buffers
    ArrayInitialize(UpTrendBuffer, EMPTY_VALUE);
    ArrayInitialize(DownTrendBuffer, EMPTY_VALUE);
    ArrayInitialize(UpReversalBuffer, EMPTY_VALUE);
    ArrayInitialize(DownReversalBuffer, EMPTY_VALUE);
    
    return(INIT_SUCCEEDED);
}

// Main calculation function with correct MQL4 signature
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[])
{
    int i;
    int limit;

    // Set calculation limit
    if (prev_calculated > rates_total || prev_calculated <= 0)
    {
        limit = rates_total - 1;
    }
    else
    {
        limit = rates_total - prev_calculated;
    }

    // Main logic loop
    for (i = limit - 1; i >= 0; i--)
    {
        // Check for sufficient bars before accessing previous data
        if (i + 1 >= rates_total) continue;

        // Get Main and Fast MA values, using the integer variable for timeframe
        double MainMAValue = iMA(NULL, 0, MainMAPeriod, 0, MainMAMethod, MainMAPrice, i);
        double FastMAValue = iMA(NULL, FastMATimeframe_int, FastMAPeriod, 0, FastMAMethod, FastMAPrice, i);
        double FastMAValue_prev = iMA(NULL, FastMATimeframe_int, FastMAPeriod, 0, FastMAMethod, FastMAPrice, i + 1);

        // Reset buffers for the current bar
        UpTrendBuffer[i] = EMPTY_VALUE;
        DownTrendBuffer[i] = EMPTY_VALUE;
        UpReversalBuffer[i] = EMPTY_VALUE;
        DownReversalBuffer[i] = EMPTY_VALUE;

        // Up-trend logic
        if (close[i] > MainMAValue && FastMAValue > FastMAValue_prev)
        {
            UpTrendBuffer[i] = close[i];
            // Check for potential reversal from a down-trend
            if (i + 1 < rates_total && DownTrendBuffer[i + 1] != EMPTY_VALUE)
            {
                UpReversalBuffer[i] = low[i] - 10 * Point; // Place arrow below the candle
            }
        }
        // Down-trend logic
        else if (close[i] < MainMAValue && FastMAValue < FastMAValue_prev)
        {
            DownTrendBuffer[i] = close[i];
            // Check for potential reversal from an up-trend
            if (i + 1 < rates_total && UpTrendBuffer[i + 1] != EMPTY_VALUE)
            {
                DownReversalBuffer[i] = high[i] + 10 * Point; // Place arrow above the candle
            }
        }
    }

    return(rates_total);
}
