//+------------------------------------------------------------------+
//|                                                     HAT.mq5      |
//|                        Copyright 2024, Your Name                 |
//|                                         https://www.mql5.com     |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red

input ENUM_TIMEFRAMES timeframe = PERIOD_CURRENT;
input double T3Factor = 0.3;
input int T3Length = 7;
input double percentSqueeze = 0.2;

double haOpen[], haClose[], haHigh[], haLow[];
double t3Open[], t3Close[], t3High[], t3Low[];

double percentHL[];
double crossPlot[];
double plotColor[];

int OnInit() {
   SetIndexBuffer(0, crossPlot);
   SetIndexBuffer(1, plotColor);
   
   IndicatorShortName("Heikin Ashi Trend Indicator (HAT)");
   
   return INIT_SUCCEEDED;
}

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 begin = T3Length * 3;

    for(int i = begin; i < rates_total; i++) {
        haOpen[i] = (open[i - 1] + close[i - 1]) / 2;
        haClose[i] = (open[i] + high[i] + low[i] + close[i]) / 4;
        haHigh[i] = MathMax(high[i], MathMax(haOpen[i], haClose[i]));
        haLow[i] = MathMin(low[i], MathMin(haOpen[i], haClose[i]));
        
        t3Open[i] = T3(haOpen[i], T3Length, T3Factor);
        t3Close[i] = T3(haClose[i], T3Length, T3Factor);
        t3High[i] = T3(haHigh[i], T3Length, T3Factor);
        t3Low[i] = T3(haLow[i], T3Length, T3Factor);
        
        percentHL[i] = ((t3Open[i] - t3Close[i]) / (t3Open[i] + t3Close[i]) / 2) * 100;

        bool high_squeeze = MathAbs(percentHL[i]) < percentSqueeze && percentHL[i] > 0;
        bool low_squeeze = MathAbs(percentHL[i]) < percentSqueeze && percentHL[i] < 0;
        
        plotColor[i] = high_squeeze ? clrMaroon : low_squeeze ? clrTeal : t3Open[i] < t3Close[i] ? clrLime : clrRed;
        crossPlot[i] = t3Open[i] < t3Close[i] ? t3Low[i] : t3High[i];
        
        // Alert logic
        if(t3Open[i] < t3Close[i]) {
            // Buy signal
            if (OpenOrderSignal()) {
                // Alert: Buy
            }
        } else {
            // Sell signal
            if (CloseOrderSignal()) {
                // Alert: Sell
            }
        }
    }
    
    return rates_total;
}

double T3(double price, int length, double factor) {
    double result;
    double ema1 = iMA(NULL, 0, length, 0, MODE_EMA, price, 0);
    double ema2 = iMA(NULL, 0, length, 0, MODE_EMA, ema1, 0);
    double ema3 = iMA(NULL, 0, length, 0, MODE_EMA, ema2, 0);
    
    result = ema1 * (1 + factor) - ema2 * factor;
    return result;
}

bool OpenOrderSignal() {
    // Implement your custom open order signal logic here
    return true;
}

bool CloseOrderSignal() {
    // Implement your custom close order signal logic here
    return true;
}
