//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_label1  "Super trend"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrLimeGreen,clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

//
//
//

input int              atrPeriod     = 14;        // ATR period
input double           atrMultiplier = 2.0;      // ATR multiplier

double val[],valc[];
struct sGlobalStruct
{
   double  atr;
   double  cPrc;
   double  mPrc;
};
sGlobalStruct glo;

//------------------------------------------------------------------
//
//------------------------------------------------------------------

int OnInit()
{
   SetIndexBuffer(0,val,INDICATOR_DATA); 
   SetIndexBuffer(1,valc,INDICATOR_COLOR_INDEX); 
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[])
{
   if (Bars(_Symbol,_Period)<rates_total) return(-1);
   
   //
   //
   //
   
   struct sWorkStruct
   {
      double up;
      double dn;
      double dir;
   };   
   static sWorkStruct wrk[];
   static int         wrkSize = -1;
                  if (wrkSize<=rates_total) wrkSize = ArrayResize(wrk,rates_total+500,2000);

   
   //
   //
   //
   
   for (int i=(int)fmax(prev_calculated-1,0); i<rates_total && !IsStopped(); i++)
   {
      glo.atr = 0; 
      for (int k=0;k<atrPeriod && (i-k-1)>=0; k++) 
         glo.atr += fmax(high[i-k],close[i-k-1])-fmin(low[i-k],close[i-k-1]);
         glo.atr /= atrPeriod;
      
         //
         //
         //
      
         glo.cPrc  = close[i];
         glo.mPrc  = (high[i]+low[i])*0.50;
         wrk[i].up = glo.mPrc+atrMultiplier*glo.atr;
         wrk[i].dn = glo.mPrc-atrMultiplier*glo.atr;
         
         //
         //
         //
         
         wrk[i].dir = (i>0) ? (glo.cPrc>wrk[i-1].up) ? 1 : (glo.cPrc<wrk[i-1].dn) ? -1 : wrk[i-1].dir : 0;

         if (wrk[i].dir== 1 && i>0) { wrk[i].dn = fmax(wrk[i].dn,wrk[i-1].dn); val[i] = wrk[i].dn; }
         if (wrk[i].dir==-1 && i>0) { wrk[i].up = fmin(wrk[i].up,wrk[i-1].up); val[i] = wrk[i].up; }
         
         valc[i] = (i>0) ? (wrk[i].dir== 1) ? 0 : (wrk[i].dir==-1) ? 1 : valc[i-1] : 0;
   }
return(rates_total);
}
