// Downloaded From https://www.WiseStockTrader.com
#property copyright "Copyright © 2011, Xaphod"
#property link      "http://wwww.xaphod.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 DimGray
#property indicator_color2 OrangeRed
#property indicator_color3 MediumSeaGreen
#property indicator_width1 1
#property indicator_width2 2
#property indicator_width3 2
#property indicator_style1 STYLE_DOT
#property indicator_maximum 1
#property indicator_minimum 0


#define INDICATOR_NAME "xSuperTrend MTF"
#define INDICATOR_VERSION "v1.01, www.xaphod.com"

// Indicator parameters
extern string Version_Info=INDICATOR_VERSION;
extern string SuperTrend_Settings="——————————————————————————————";
extern int    SuperTrend_Period    = 10;  // SuperTrend ATR Period
extern double SuperTrend_Multiplier= 1.7; // SuperTrend Multiplier
extern int    SuperTrend_TimeFrame = 0;   // SuperTrend Timeframe
extern bool   SuperTrend_AutoTF    = True;    // Select next higher TF. If TF=M15 then H1 is selected
extern bool   alertsOn             = False;
extern bool   alertsOnCurrentBar   = true;
extern bool   alertsMessage        = true;
extern bool   alertsSound          = false;
extern bool   alertsEmail          = false;


// Global module varables
double gadUpBuf[];
double gadDnBuf[];
double gadSuperTrend[];
int giTimeFrame;
int giRepaintBars;

//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init() {
  // Set buffers
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, gadSuperTrend);
  SetIndexLabel(0, "SuperTrend");
  SetIndexStyle(1, DRAW_LINE);
  SetIndexBuffer(1, gadDnBuf);
  SetIndexLabel(1, "SuperTrend Down");
  SetIndexStyle(2, DRAW_LINE);
  SetIndexBuffer(2, gadUpBuf);
  SetIndexLabel(2, "SuperTrend Up");
  
  // Set Timeframe
  if (SuperTrend_TimeFrame==0)
    SuperTrend_TimeFrame=Period();
  if (SuperTrend_AutoTF==True)
    SuperTrend_TimeFrame=NextHigherTF(Period());
  
  // Calculation call via iCustom
  if (SuperTrend_AutoTF==False && SuperTrend_Settings=="") {
    giRepaintBars=0;
  }
  // Higher Time-frame
  else if (SuperTrend_TimeFrame!=Period()) {
    IndicatorShortName(INDICATOR_NAME+" "+TF2Str(SuperTrend_TimeFrame) +"["+SuperTrend_Period+";"+DoubleToStr(SuperTrend_Multiplier,1)+"]");
    giRepaintBars=SuperTrend_TimeFrame/Period()*2+1;
  }
  // Current Time-frame
  else {
    IndicatorShortName(INDICATOR_NAME+" "+TF2Str(SuperTrend_TimeFrame) +"["+SuperTrend_Period+";"+DoubleToStr(SuperTrend_Multiplier,1)+"]");
    giRepaintBars=0;
  }   

  return(0);
}


//-----------------------------------------------------------------------------
// function: deinit()
// Description: Custom indicator deinitialization function.
//-----------------------------------------------------------------------------
int deinit() {
   return (0);
}


///-----------------------------------------------------------------------------
// function: start()
// Description: Custom indicator iteration function.
//-----------------------------------------------------------------------------
int start() {
  int iNewBars, iCountedBars, i;
  double dAtr,dUpperLevel, dLowerLevel;  
  
  // Get unprocessed ticks
  iCountedBars=IndicatorCounted();
  if(iCountedBars < 0) return (-1); 
  if(iCountedBars>0) iCountedBars--;
  iNewBars=MathMax(giRepaintBars,Bars-iCountedBars);
  
  for(i=iNewBars; i>=0; i--) {
    // Calc SuperTrend Locally
    if (SuperTrend_TimeFrame==Period()) {
      dAtr = iATR(NULL, 0, SuperTrend_Period, i);
      dUpperLevel=(High[i]+Low[i])/2+SuperTrend_Multiplier*dAtr;
      dLowerLevel=(High[i]+Low[i])/2-SuperTrend_Multiplier*dAtr;
    
      // Set supertrend levels
      if (Close[i]>gadSuperTrend[i+1] && Close[i+1]<=gadSuperTrend[i+1]) {
        gadSuperTrend[i]=dLowerLevel;
      }
      else if (Close[i]<gadSuperTrend[i+1] && Close[i+1]>=gadSuperTrend[i+1]) {
        gadSuperTrend[i]=dUpperLevel;
      }
      else if (gadSuperTrend[i+1]<dLowerLevel)
         gadSuperTrend[i]=dLowerLevel;
      else if (gadSuperTrend[i+1]>dUpperLevel)
        gadSuperTrend[i]=dUpperLevel;
      else
        gadSuperTrend[i]=gadSuperTrend[i+1];
      
      // Draw Histo
      gadUpBuf[i]=EMPTY_VALUE;
      gadDnBuf[i]=EMPTY_VALUE;
      if (Close[i]>gadSuperTrend[i] || (Close[i]==gadSuperTrend[i] && Close[i+1]>gadSuperTrend[i+1])) 
        gadUpBuf[i]=gadSuperTrend[i];
      else if (Close[i]<gadSuperTrend[i] || (Close[i]==gadSuperTrend[i] && Close[i+1]<gadSuperTrend[i+1])) 
        gadDnBuf[i]=gadSuperTrend[i];
        
    }
    // Calc higher TF SuperTrend via iCustom
    else {
      gadUpBuf[i]=EMPTY_VALUE;
      gadDnBuf[i]=EMPTY_VALUE;
      gadSuperTrend[i]=iCustom(Symbol(),SuperTrend_TimeFrame,WindowExpertName(),"","",SuperTrend_Period,
                        SuperTrend_Multiplier,SuperTrend_TimeFrame,False,alertsOn,alertsOnCurrentBar,alertsMessage,alertsSound,alertsEmail,0,iBarShift(Symbol(), SuperTrend_TimeFrame, Time[i]));
      gadDnBuf[i]=iCustom(Symbol(),SuperTrend_TimeFrame,WindowExpertName(),"","",SuperTrend_Period,
                        SuperTrend_Multiplier,SuperTrend_TimeFrame,False,alertsOn,alertsOnCurrentBar,alertsMessage,alertsSound,alertsEmail,1,iBarShift(Symbol(), SuperTrend_TimeFrame, Time[i]));
      gadUpBuf[i]=iCustom(Symbol(),SuperTrend_TimeFrame,WindowExpertName(),"","",SuperTrend_Period,
                        SuperTrend_Multiplier,SuperTrend_TimeFrame,False,alertsOn,alertsOnCurrentBar,alertsMessage,alertsSound,alertsEmail,2,iBarShift(Symbol(), SuperTrend_TimeFrame, Time[i]));                                                
      
    }  
  }
  if (SuperTrend_TimeFrame==Period()) manageAlerts();
  
  return(0);
}
//+------------------------------------------------------------------+


//-----------------------------------------------------------------------------
// function: TF2Str()
// Description: Convert time-frame to a string
//-----------------------------------------------------------------------------
string TF2Str(int iPeriod) {
  switch(iPeriod) {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN1");
    default: return("M"+iPeriod);
  }
}


//-----------------------------------------------------------------------------
// function: NextHigherTF()
// Description: Select the next higher time-frame. 
//              Note: M15 and M30 both select H1 as next higher TF. 
//-----------------------------------------------------------------------------
int NextHigherTF(int iPeriod) {
  if (iPeriod==0) iPeriod=Period();
  switch(iPeriod) {
    case PERIOD_M1: return(PERIOD_M5);
    case PERIOD_M5: return(PERIOD_M15);
    case PERIOD_M15: return(PERIOD_H1);
    case PERIOD_M30: return(PERIOD_H1);
    case PERIOD_H1: return(PERIOD_H4);
    case PERIOD_H4: return(PERIOD_D1);
    case PERIOD_D1: return(PERIOD_W1);
    case PERIOD_W1: return(PERIOD_MN1);
    case PERIOD_MN1: return(PERIOD_MN1);
    default: return(Period());
  }
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrentBar)
           int whichBar = 0;
      else     whichBar = 1;
      if ((gadUpBuf[whichBar] != EMPTY_VALUE && gadUpBuf[whichBar+1] == EMPTY_VALUE) || (gadDnBuf[whichBar]!=EMPTY_VALUE && gadDnBuf[whichBar+1]==EMPTY_VALUE))
      {
         if (gadUpBuf[whichBar] != EMPTY_VALUE) doAlert(whichBar,"up");
         if (gadDnBuf[whichBar] != EMPTY_VALUE) doAlert(whichBar,"down");
      }         
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message =  TF2Str(Period())+" "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" super trend signal changed to "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"super trend "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}