//+------------------------------------------------------------------+
//| TC_SuperTrend.mq4                                                |
//+------------------------------------------------------------------+
#property copyright   "Tradi Coders"
#property link        "https://tradicoders.com"
#property version     "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3

#include <TC_Common_MQL4.mqh>
#include <TC_Inputs.mqh>
#include <TC_SuperTrendCore.mqh>

#define TC_PRODUCT_NAME  "TC SuperTrend"
#define TC_PRODUCT_VER   "1.00"

double ExtST[];
double ExtBuy[];
double ExtSell[];

input int    InpATRPeriod   = TC_ATR_PERIOD_DEFAULT;
input double InpMultiplier  = 3.0;
input bool   InpShowLine    = true;
input bool   InpShowArrows  = true;
input bool   InpAlertOnFlip = true;
input int    InpArrowGap    = 8;

//+------------------------------------------------------------------+
int OnInit()
  {
   TC_ApplyTheme(InpTC_Theme);
   IndicatorShortName(TC_BrandComment(TC_PRODUCT_NAME));
   SetIndexBuffer(0,ExtST); SetIndexBuffer(1,ExtBuy); SetIndexBuffer(2,ExtSell);
   SetIndexStyle(0,InpShowLine?DRAW_LINE:DRAW_NONE,STYLE_SOLID,2,TC_CLR_BRAND_PRIMARY);
   SetIndexStyle(1,InpShowArrows?DRAW_ARROW:DRAW_NONE,0,1,TC_SignalBuy);
   SetIndexStyle(2,InpShowArrows?DRAW_ARROW:DRAW_NONE,0,1,TC_SignalSell);
   SetIndexArrow(1,233); SetIndexArrow(2,234);
   SetIndexEmptyValue(1,EMPTY_VALUE); SetIndexEmptyValue(2,EMPTY_VALUE);
   SetIndexDrawBegin(0,InpATRPeriod+1);
   TC_BrandInputsApply(TC_PRODUCT_NAME,TC_PRODUCT_VER);
   return INIT_SUCCEEDED;
  }

//+------------------------------------------------------------------+
void OnDeinit(const int r) { TC_BrandDeinit(); }
void OnChartEvent(const int id,const long &l,const double &d,const string &s)
  { TC_BrandOnChartEvent(id,l,d,s); }

//+------------------------------------------------------------------+
int start()
  {
   int total=Bars;
   if(total<InpATRPeriod+3) return 0;

   static double h[],l[],c[],a[];
   ArrayResize(h,total); ArrayResize(l,total); ArrayResize(c,total); ArrayResize(a,total);
   for(int s=0;s<total;s++)
     {
      int i=total-1-s;
      h[i]=High[s]; l[i]=Low[s]; c[i]=Close[s];
      a[i]=iATR(NULL,0,InpATRPeriod,s);
     }

   int begin=InpATRPeriod+1;
   double st=0; int dir=TC_ST_UP;
   double gap=InpArrowGap*Point;
   int lastFlip=0;

   for(int i=begin;i<total;i++)
     {
      int sh=total-1-i;
      ExtBuy[sh]=EMPTY_VALUE; ExtSell[sh]=EMPTY_VALUE;
      if(i==begin) { st=0.5*(h[i]+l[i])-InpMultiplier*a[i]; dir=TC_ST_UP; ExtST[sh]=st; continue; }
      int shPrev=total-1-(i-1);
      int prevDir=dir;
      double prevST=ExtST[shPrev];
      TC_SuperTrend_Step(h[i],l[i],c[i],a[i],InpMultiplier,prevST,prevDir,st,dir);
      ExtST[sh]=st;
      if(dir!=prevDir)
        {
         if(dir==TC_ST_UP) ExtBuy[sh]=l[i]-gap;
         else ExtSell[sh]=h[i]+gap;
         if(i==total-2) lastFlip=dir;
        }
     }

   if(InpAlertOnFlip && lastFlip!=0)
     {
      if(TC_AlertOncePerBar("ST_"+Symbol()+IntegerToString(Period()),Time[1]))
         TC_SendAlert(TC_PRODUCT_NAME,(lastFlip==TC_ST_UP?"SuperTrend BUY":"SuperTrend SELL")+" "+Symbol());
     }
   return total;
  }
//+------------------------------------------------------------------+
