/*
      подправленный +ST.mq4 (значение limit)
*/
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_width1 1
#property indicator_color2 Red
#property indicator_width2 1
#property indicator_color3 Black

input ENUM_TIMEFRAMES timeFrame = 0;
extern string sTFs = "0 - введённый TF, 1 - на один выше, 2 - на 2 выше";
extern int TFs  = 0;
extern int width = 2;
extern color ColorUp=clrDodgerBlue;
extern color ColorDn=clrOrange;

double   Trend[];
double   TrendDoA[];
double   TrendDoB[];
double   Direction[];
int      TimeFrame;
string   IndicatorFileName;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//

int init()
{
   TimeFrame = stringToTimeFrame(timeFrame, TFs);   
      IndicatorBuffers(4);
      SetIndexBuffer(0, Trend);     SetIndexStyle(0, DRAW_LINE, 0, width,  ColorUp);  SetIndexLabel(0, TimeFrameToString(TimeFrame));
      SetIndexBuffer(1, TrendDoA);   SetIndexStyle(1, DRAW_LINE, 0, width, ColorDn);  SetIndexLabel(1, TimeFrameToString(TimeFrame));
      SetIndexBuffer(2, TrendDoB);  // SetIndexStyle(2, DRAW_LINE, 0, width, ColorDn);  SetIndexLabel(2, "ST");
      SetIndexBuffer(3, Direction);
         
       
        
   IndicatorFileName = WindowExpertName();
   IndicatorShortName("SuperTrend "+TimeFrameToString(TimeFrame));
    return(0);
   
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//

int start()
{
   int      counted_bars = IndicatorCounted();
   int      limit,i;

   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
     // limit = MathMax(Bars-counted_bars,Bars-1); //Было
      limit = MathMin(Bars-counted_bars,Bars-1);   //Стало  +ST_m
      
      
      //Print(" limit = ", limit);
      if (Direction[limit] == -1) CleanPoint(limit,TrendDoA,TrendDoB);

   //

      if (TimeFrame != Period())
      {
         limit = MathMax(limit,TimeFrame/Period());

            //
         
            for(i=limit; i>=0; i--)
            {
               int y = iBarShift(NULL,TimeFrame,Time[i]);
               double cciTrend = iCCI(NULL, TimeFrame, 50, PRICE_TYPICAL, y);
                  
               //
                  
               TrendDoA[i]  = EMPTY_VALUE;
               TrendDoB[i]  = EMPTY_VALUE;
               Trend[i]     = iCustom(NULL,TimeFrame,IndicatorFileName,0,y);
               Direction[i] = Direction[i+1];
                  if (cciTrend>0) Direction[i] =  1;
                  if (cciTrend<0) Direction[i] = -1;
                  if (Direction[i]==-1) PlotPoint(i,TrendDoA,TrendDoB,Trend);
            }
         return(0);         
      }

   //
      
   for(i = limit; i >= 0; i--)
   {
         cciTrend = iCCI(NULL, 0, 50, PRICE_TYPICAL, i);
         
         //
         
         TrendDoA[i]  = EMPTY_VALUE;
         TrendDoB[i]  = EMPTY_VALUE;
         Trend[i]     = Trend[i+1];
         Direction[i] = Direction[i+1];
            if (cciTrend > 0) { Trend[i] = MathMax(Low[i]  - iATR(NULL, 0, 5, i),Trend[i+1]); Direction[i] =  1; }
            if (cciTrend < 0) { Trend[i] = MathMin(High[i] + iATR(NULL, 0, 5, i),Trend[i+1]); Direction[i] = -1; }
            if (Direction[i]==-1) PlotPoint(i,TrendDoA,TrendDoB,Trend);
   }
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
      if (first[i+2] == EMPTY_VALUE) {
          first[i]    = from[i];
          first[i+1]  = from[i+1];
          second[i]   = EMPTY_VALUE;
         }
      else {
          second[i]   = from[i];
          second[i+1] = from[i+1];
          first[i]    = EMPTY_VALUE;
         }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}

//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+


int stringToTimeFrame(string tfs, int next)
{
   int tf=0;
         if (tfs==0      &&  next==0)  tf=_Period;
         if (tfs==1      &&  next==0)  tf=PERIOD_M1;
         if (tfs==5      &&  next==0)  tf=PERIOD_M5;
         if (tfs==15     &&  next==0)  tf=PERIOD_M15;
         if (tfs==30     &&  next==0)  tf=PERIOD_M30;
         if (tfs==60     &&  next==0)  tf=PERIOD_H1;
         if (tfs==240    &&  next==0)  tf=PERIOD_H4;
         if (tfs==1440   &&  next==0)  tf=PERIOD_D1;
         if (tfs==10080  &&  next==0)  tf=PERIOD_W1;
         if (tfs==43200  &&  next==0)  tf=PERIOD_MN1;
         if (tfs==0      &&  next==1)  {  width = 3;  ColorUp=clrBlue;   ColorDn=clrTomato;  tf=NextTF(_Period);  }
         if (tfs==0      &&  next==2)  {  width = 4;  ColorUp=clrGreen;  ColorDn=clrFireBrick;    tf=NextNextTF(_Period);  }
   return(tf);
}

string TimeFrameToString(int tf)
{
   string tfs="";
//   if (Period()!=tf)
   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      chhar;
   
   while(lenght >= 0)
      {
         chhar = StringGetChar(s, lenght);
         
         //
         
         if((chhar > 96 && chhar < 123) || (chhar > 223 && chhar < 256))
                  s = StringSetChar(s, lenght, chhar - 32);
         else 
              if(chhar > -33 && chhar < 0)
                  s = StringSetChar(s, lenght, chhar + 224);
         lenght--;
   }
   
   //
   
   return(s);
}

int NextTF(int per)  {
  int tf;
  if (per==1      )  tf=PERIOD_M5;
  if (per==5      )  tf=PERIOD_M15;
  if (per==15     )  tf=PERIOD_H1;
  if (per==30     )  tf=PERIOD_H1;
  if (per==60     )  tf=PERIOD_H4;
  if (per==240    )  tf=PERIOD_D1;
  if (per==1440   )  tf=PERIOD_W1;
  if (per==10080  )  tf=PERIOD_MN1;
  if (per==43200  )  tf=PERIOD_MN1;
  return(tf);
}  
      
int NextNextTF(int per)  {
  int tf;
  if (per==1      )  tf=PERIOD_M15;
  if (per==5      )  tf=PERIOD_H1;
  if (per==15     )  tf=PERIOD_H4;
  if (per==30     )  tf=PERIOD_D1;
  if (per==60     )  tf=PERIOD_D1;
  if (per==240    )  tf=PERIOD_W1;
  if (per==1440   )  tf=PERIOD_MN1;
  if (per==10080  )  tf=PERIOD_MN1;
  if (per==43200  )  tf=PERIOD_MN1;
  return(tf);
}  

