//+------------------------------------------------------------------+
//|                            BreakoutKumoPullbackEntry_EDIT_AI.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property strict
#property indicator_chart_window
#property indicator_buffers 2

// Input Parameters
extern int ArrowSize = 3;
extern color BuyColor = clrLime;
extern color SellColor = clrRed;

// Ichimoku Parameters
extern int TenkanSen = 9;
extern int KijunSen = 26;
extern int SenkouSpanB = 52;

// MACD Parameters
extern int MACDFast = 12;
extern int MACDSlow = 26;
extern int MACDSignal = 9;

// Stochastic Parameters
extern int KPeriod = 5;
extern int DPeriod = 3;
extern int Slowing = 3;

// Indicator Buffers
double BuyBuffer[];
double SellBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                           |
//+------------------------------------------------------------------+
int OnInit()
{
    SetIndexBuffer(0, BuyBuffer);
    SetIndexBuffer(1, SellBuffer);
    
    SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, ArrowSize, BuyColor);
    SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, ArrowSize, SellColor);
    
    SetIndexArrow(0, 233); // Up arrow
    SetIndexArrow(1, 234); // Down arrow
    
    SetIndexLabel(0, "Buy Signal");
    SetIndexLabel(1, "Sell Signal");
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Check if price is above Kumo                                       |
//+------------------------------------------------------------------+
bool IsPriceAboveKumo(int shift)
{
    double senkou_a = iIchimoku(NULL, 0, TenkanSen, KijunSen, SenkouSpanB, MODE_SENKOUSPANA, shift);
    double senkou_b = iIchimoku(NULL, 0, TenkanSen, KijunSen, SenkouSpanB, MODE_SENKOUSPANB, shift);
    double price = Close[shift];
    
    return (price > MathMax(senkou_a, senkou_b));
}

//+------------------------------------------------------------------+
//| Check if price is below Kumo                                       |
//+------------------------------------------------------------------+
bool IsPriceBelowKumo(int shift)
{
    double senkou_a = iIchimoku(NULL, 0, TenkanSen, KijunSen, SenkouSpanB, MODE_SENKOUSPANA, shift);
    double senkou_b = iIchimoku(NULL, 0, TenkanSen, KijunSen, SenkouSpanB, MODE_SENKOUSPANB, shift);
    double price = Close[shift];
    
    return (price < MathMin(senkou_a, senkou_b));
}

//+------------------------------------------------------------------+
//| Custom 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[])
{
    int limit = rates_total - prev_calculated;
    if(prev_calculated > 0) limit++;
    
    for(int i = limit - 1; i >= 0; i--)
    {
        // Initialize buffers
        BuyBuffer[i] = EMPTY_VALUE;
        SellBuffer[i] = EMPTY_VALUE;
        
        if(i >= rates_total-3) continue; // Skip the first few bars
        
        // Get indicator values
        double macd_main = iMACD(NULL, 0, MACDFast, MACDSlow, MACDSignal, PRICE_CLOSE, MODE_MAIN, i);
        double stoch_main = iStochastic(NULL, 0, KPeriod, DPeriod, Slowing, MODE_SMA, 0, MODE_MAIN, i);
        double stoch_prev = iStochastic(NULL, 0, KPeriod, DPeriod, Slowing, MODE_SMA, 0, MODE_MAIN, i+1);
        
        // Buy Signal Conditions
        bool buyCondition = IsPriceAboveKumo(i) && // Price above Kumo
                           macd_main > 0 &&        // MACD above 0
                           stoch_prev < 20 &&      // Previous Stoch below 20
                           stoch_main >= 20;       // Current Stoch touching/crossing 20
        
        // Sell Signal Conditions
        bool sellCondition = IsPriceBelowKumo(i) && // Price below Kumo
                            macd_main < 0 &&        // MACD below 0
                            stoch_prev > 80 &&      // Previous Stoch above 80
                            stoch_main <= 80;       // Current Stoch touching/crossing 80
        
        // Generate signals
        if(buyCondition)
        {
            BuyBuffer[i] = Low[i] - (10 * Point); // Place arrow below the candle
        }
        
        if(sellCondition)
        {
            SellBuffer[i] = High[i] + (10 * Point); // Place arrow above the candle
        }
    }
    
    return(rates_total);
}