//+------------------------------------------------------------------+
//|                                                   Supertrend.mq4 |
//|                   Copyright © 2005, Jason Robinson (jnrtrading). |
//|                                      http://www.jnrtrading.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)."
#property link      "http://www.jnrtrading.co.uk"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 White
#property indicator_width1 2
#property indicator_width2 2

double TrendUp[];
double TrendDown[];
double Ema[];
extern int period=50;
extern int period2=2;
extern int Mode=3;
extern int PRICE=PRICE_TYPICAL;
extern bool Histo=false;
extern bool CloseOn=false;
extern string SET____________DISPLAY________ ; 

;
extern int  TextSize  = 15;
extern int  SetNumber = 5;
extern int  SetNumber2 = 2;
extern int  SetNumber3 = 0;
extern int  pos = 10;

//extern int  corner = 1;
extern color Colora = Gray;
extern color Colorb = Red;
extern color Colorc = White;

int st = 0;
extern string windowName = "Supertrend1";

//extern int SlowerEMA = 6;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   //SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(0, TrendUp);
   //SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexBuffer(1, TrendDown);
   SetIndexBuffer(2, Ema);
   if(Histo)
   SetIndexStyle(2, DRAW_HISTOGRAM,0,2);
   /*SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 159);
   SetIndexBuffer(0, TrendUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 159);
   SetIndexBuffer(1, TrendDown);*/
   
   /*for(int i = 0; i < Bars; i++) {
      TrendUp[i] = NULL;
      TrendDown[i] = NULL;
   }*/
   
    IndicatorShortName(windowName);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   /*for(int i = 0; i < Bars; i++) {
      TrendUp[i] = NULL;
      TrendDown[i] = NULL;
   }*/
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   int limit, i, counter;
   double Range, AvgRange, cciTrendNow, cciTrendPrevious;

   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i = limit; i >= 0; i--) {
      cciTrendNow = iCCI(NULL, 0, period, PRICE_TYPICAL, i);
      cciTrendPrevious = iCCI(NULL, 0, period, PRICE_TYPICAL, i+1);
       Ema[i] = iMA(NULL, 0, period2,0,Mode, PRICE, i);
     if(  CloseOn)
       Ema[i] = Close[i];
       
      //st = st * 100;
      
      counter = i;
      Range = 0;
      AvgRange = 0;
      for (counter = i; counter >= i-9; counter--) {
         AvgRange = AvgRange + MathAbs(High[counter]-Low[counter]);
      }
      Range = AvgRange/10;
      if (cciTrendNow >= st && cciTrendPrevious < st) {
         TrendUp[i+1] = TrendDown[i+1];
      }
      
      if (cciTrendNow <= st && cciTrendPrevious > st) {
         TrendDown[i+1] = TrendUp[i+1];
      }
      
      if (cciTrendNow >= st) {
         TrendUp[i] = Low[i] - iATR(NULL, 0, 5, i);         
         if (TrendUp[i] < TrendUp[i+1]) {
            TrendUp[i] = TrendUp[i+1];
         }
      }
      else if (cciTrendNow <= st) {
         TrendDown[i] = High[i] + iATR(NULL, 0, 5, i);
         if (TrendDown[i] > TrendDown[i+1]) {
            TrendDown[i] = TrendDown[i+1];
         }
      }
      
 
      
       OBJ_LABEL_("Supertrend1",  "Trend:  "+DoubleToStr(Ema[0] ,SetNumber),10,0,Colora,TextSize);//10-10-10 
       if(cciTrendNow >= st)
       OBJ_LABEL_("Supertrend1",  "TrendUp:  "+DoubleToStr(Ema[0],SetNumber),10,0,Lime,TextSize);//10-10-10 
       if(cciTrendNow <= st)
       OBJ_LABEL_("Supertrend1",  "TrendDn:  "+DoubleToStr(Ema[0],SetNumber),10,0,Red,TextSize);//10-10-10 
       
        
       OBJ_LABEL_("Supertrend1CCI", "CCI:  "+DoubleToStr(cciTrendNow,SetNumber2),10,26,Colora,TextSize);//10-10-10
       if(cciTrendNow>0)
       OBJ_LABEL_("Supertrend1CCI", "CCIup:  "+DoubleToStr(cciTrendNow,SetNumber2),10,26,Lime,TextSize);//10-10-10
       if(cciTrendNow<0)
       OBJ_LABEL_("Supertrend1CCI", "CCIdn:  "+DoubleToStr(cciTrendNow,SetNumber2),10,26,Red,TextSize);//10-10-10
       
          OBJ_LABEL_("SupertrendRangeTrend", "RangeUp: "+DoubleToStr(Close[0]/Point- TrendUp[0]/Point,SetNumber3),10,50,Gray,TextSize);//10-10-10

       if(cciTrendNow>0 &&Close[0]/Point- TrendUp[0]/Point>0 )
       OBJ_LABEL_("SupertrendRangeTrend", "RangeUp: "+DoubleToStr(Close[0]/Point- TrendUp[0]/Point,SetNumber3),10,50,Lime,TextSize);//10-10-10
       
       if(cciTrendNow<0 &&Close[0]/Point- TrendDown[0]/Point<0 )
       OBJ_LABEL_("SupertrendRangeTrend", "RangeDn: "+DoubleToStr(Close[0]/Point- TrendDown[0]/Point,SetNumber3),10,50,Red,TextSize);//10-10-10
      
   }
//----
   return(0);
  }
  
     void OBJ_LABEL_(string str="",string TEXT="",int X=0, int Y=0,color COLOR=Blue,int FONT_SIZE=10)
{
   int Win_ind = WindowFind(windowName);
   if (Win_ind != -1)
   {
      if(ObjectFind(str) == -1)
      {
         ObjectCreate(str,OBJ_LABEL,Win_ind,0,0);
      }
      ObjectSetText(str,TEXT,FONT_SIZE,"",COLOR);
      ObjectSet(str,OBJPROP_CORNER, 1);  
      ObjectSet(str,OBJPROP_XDISTANCE,pos);
      ObjectSet(str,OBJPROP_YDISTANCE,Y);
      
      
   
    }
 }  

  
  
  
  
//+------------------------------------------------------------------+


