//+------------------------------------------------------------------+
//|                                                      ST.mq5      |
//|                                       BY ALI KAZIM & JAWAD       |
//+------------------------------------------------------------------+
#property copyright "2026"
#property version   "14.00"
#property strict

#include <Trade\Trade.mqh>

input int    InpAtrPeriod      = 10;      // ATR Period
input double InpMultiplier     = 3.0;     // ATR Multiplier
input bool   InpShowSignals    = true;    // Show Chart Overlay & Signals
input double InpLotSize        = 0.01;    // Lot Size
input ulong  InpMagic          = 20260101;// Magic Number
input string InpComment        = "ST_EA"; // Order Comment

CTrade Trade;

int hSuperTrend = INVALID_HANDLE;
int LastTrend   = 0;

//+------------------------------------------------------------------+
//| Reads the confirmed trend directly from the SuperTrend indicator |
//| via iCustom — buffer 0 is the Up line, buffer 1 is the Down line |
//+------------------------------------------------------------------+
bool GetSupertrendFromIndicator(int &trendOut)
  {
   if(BarsCalculated(hSuperTrend) < 2)
      return false;

   double bufUp[], bufDn[];
   ArraySetAsSeries(bufUp, true);
   ArraySetAsSeries(bufDn, true);

   if(CopyBuffer(hSuperTrend, 0, 0, 2, bufUp) < 2)
      return false;
   if(CopyBuffer(hSuperTrend, 1, 0, 2, bufDn) < 2)
      return false;

//--- Use the last closed bar (index 1), the confirmed value
   if(bufUp[1] != EMPTY_VALUE)
     {
      trendOut = 1;
      return true;
     }
   if(bufDn[1] != EMPTY_VALUE)
     {
      trendOut = -1;
      return true;
     }
   return false;
  }

//+------------------------------------------------------------------+
//| Opens a buy at market, no SL/TP — exit happens only on the next  |
//| opposite signal                                                  |
//+------------------------------------------------------------------+
void OpenBuy()
  {
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   Trade.Buy(InpLotSize, _Symbol, ask, 0, 0, InpComment);
  }

//+------------------------------------------------------------------+
//| Opens a sell at market, no SL/TP — exit happens only on the next |
//| opposite signal                                                  |
//+------------------------------------------------------------------+
void OpenSell()
  {
   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   Trade.Sell(InpLotSize, _Symbol, bid, 0, 0, InpComment);
  }

//+------------------------------------------------------------------+
//| Closes any open position from this EA on this symbol             |
//+------------------------------------------------------------------+
void CloseAll()
  {
   for(int i = PositionsTotal()-1; i >= 0; i--)
     {
      ulong t = PositionGetTicket(i);
      if(!t)
         continue;
      if(PositionGetString(POSITION_SYMBOL) != _Symbol || PositionGetInteger(POSITION_MAGIC) != (long)InpMagic)
         continue;
      Trade.PositionClose(t);
     }
  }

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   Trade.SetExpertMagicNumber(InpMagic);
   Trade.SetDeviationInPoints(50);

   hSuperTrend = iCustom(_Symbol, _Period, "Super Trend Indicator", InpAtrPeriod, InpMultiplier, InpShowSignals);
   if(hSuperTrend == INVALID_HANDLE)
     {
      Print("Failed to load SuperTrend indicator via iCustom");
      return INIT_FAILED;
     }

   if(InpShowSignals)
      ChartIndicatorAdd(0, 0, hSuperTrend);

   LastTrend = 0;
   return INIT_SUCCEEDED;
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(hSuperTrend != INVALID_HANDLE)
      IndicatorRelease(hSuperTrend);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int trend = 0;
   if(!GetSupertrendFromIndicator(trend))
      return;

//--- On the very first confirmed trend the EA sees, just record it as a
//--- baseline — do not open a trade yet, since there was no actual
//--- SuperTrend reversal at this point, only whatever trend already
//--- happened to be in progress when the EA was attached
   if(LastTrend == 0)
     {
      LastTrend = trend;
      return;
     }

//--- Every subsequent confirmed signal change — close and reverse
   if(trend != LastTrend)
     {
      CloseAll();
      if(trend == 1)
         OpenBuy();
      else
         OpenSell();
      LastTrend = trend;
     }
  }
//+------------------------------------------------------------------+