//+------------------------------------------------------------------+
//| mladen                                      SuperTrend Clean.mq4 |
//+------------------------------------------------------------------+
//| SuperTrend_Clean_mtf_TRO_MODIFIED_VERSION                        |
//| MODIFIED BY AVERY T. HORTON, JR. AKA THERUMPLEDONE@GMAIL.COM     |
//| I am NOT the ORIGINAL author 
//  and I am not claiming authorship of this indicator. 
//  All I did was modify it. I hope you find my modifications useful.|
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red


//+--------- TRO MODIFICATION ---------------------------------------+ 

extern bool   Sound.Alert    = false ;

extern bool   Show.PriceBox  = true ;
extern int    myBoxWidth  = 3;

extern string timeFrame = "Current time frame";
extern string TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-CurrentTF";

//

double Trend[];
double TrendDown[];
double UpDown[];
int    TimeFrame;

//+--------- TRO MODIFICATION ---------------------------------------+ 
string symbol, tChartPeriod,  tShortName ;  
int    digits, period  ; 

bool Trigger ;
color tColor = Yellow ;
int OldBars = -1 ;


//+------------------------------------------------------------------+

int init()
{

//+--------- TRO MODIFICATION ---------------------------------------+  
   period       = Period() ;     
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;   

   tShortName = "tbb"+ symbol + tChartPeriod  ;
    
   IndicatorBuffers(3);
   SetIndexBuffer(0, Trend);
   SetIndexBuffer(1, TrendDown);
   SetIndexBuffer(2, UpDown);
   SetIndexLabel(0,"SuperTrend");
   SetIndexLabel(1,NULL);
   
 
   TimeFrame = stringToTimeFrame(timeFrame);
   IndicatorShortName("SuperTrend "+TimeFrameToString(TimeFrame));  
}

//+------------------------------------------------------------------+
int deinit()
{
   ObjectDelete(tShortName+"01"); 
   return(0);
}
//+------------------------------------------------------------------+

int start()
{
//+--------- TRO MODIFICATION ---------------------------------------+   
   if( Bars != OldBars ) { Trigger = True ; }

   int      counted_bars = IndicatorCounted();
   int      limit,i;
   double   cciTrend;


   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
      limit = Bars-counted_bars;

   //
   //    MTF mode operating
   //

   if (TimeFrame != Period())
      {
         limit = MathMax(limit,TimeFrame/Period());
         datetime TimeArray[];
            ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,TimeFrame);
         
         
            for(i=0,int y=0; i<limit; i++) {
               if(Time[i]<TimeArray[y]) y++;
                     Trend[i]     = iCustom(NULL,TimeFrame,"SuperTrend_Clean_mtf",0,y);
                     TrendDown[i] = iCustom(NULL,TimeFrame,"SuperTrend_Clean_mtf",1,y);
            }
         return(0);         
      }
      
         
   for(i = limit; i >= 0; i--) {
         cciTrend = iCCI(NULL, 0, 50, PRICE_TYPICAL, i);
         
         
         if(UpDown[i+1]==1) TrendDown[i+1] = EMPTY_VALUE;
                            TrendDown[i]   = EMPTY_VALUE; 
         if (cciTrend > 0){ Trend[i]       = MathMax(Low[i]  - iATR(NULL, 0, 5, i),Trend[i+1]); UpDown[i] =  1; }
         else             { Trend[i]       = MathMin(High[i] + iATR(NULL, 0, 5, i),Trend[i+1]); UpDown[i] = -1;
                            TrendDown[i]   = Trend[i];
                            TrendDown[i+1] = Trend[i+1];
         }
   }
      

//+--------- TRO MODIFICATION ---------------------------------------+  
      
      if ( Trigger &&  Sound.Alert ) 
      {
        if( Close[0] == Trend[0] ) 
        { Trigger = False ; 
          Alert(symbol,"  ", tChartPeriod, " Price at SuperTrend Line "+ DoubleToStr(Trend[0] ,digits));        
          if( UpDown[0] == -1 ) { tColor = indicator_color2 ; } else { tColor = indicator_color1 ; }
        }        
        
      }

   if(Show.PriceBox) { DoBox() ; }
    
   OldBars = Bars ;   

//+--------- TRO MODIFICATION ---------------------------------------+                  
   return(0);
}

//+--------- TRO MODIFICATION ---------------------------------------+  
void DoBox()    
{


       if (ObjectFind(tShortName+"01") != 0)
      {
          ObjectCreate(tShortName+"01",OBJ_ARROW,0,Time[0],Trend[0]);
          ObjectSet(tShortName+"01",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"01",OBJPROP_COLOR,tColor);  
          ObjectSet(tShortName+"01",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"01",0,Time[0],Trend[0]);
         ObjectSet(tShortName+"01",OBJPROP_COLOR,tColor);  
      }
             
   return(0);
}

//+------------------------------------------------------------------+

int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf<Period()) tf=Period();
  return(tf);
}
//+------------------------------------------------------------------+
string TimeFrameToString(int tf)
{
   string tfs="Current time frame";
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}


//+------------------------------------------------------------------+
string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   
   while(lenght >= 0)
      {
         char = StringGetChar(s, lenght);

         
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                  s = StringSetChar(s, lenght, char - 32);
         else 
              if(char > -33 && char < 0)
                  s = StringSetChar(s, lenght, char + 224);
         lenght--;
   }
   

   
   return(s);
}
//+------------------------------------------------------------------+