#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Tomato
#property indicator_color3 Teal
#property indicator_color4 Teal
#property indicator_color5 Black

extern string TimeFrame = "Current time frame";   
extern int    Amplitude = 2;

bool nexttrend;
double minh, maxl, up[], down[], trend[], atrlo[], atrhi[];

string indicatorFileName;
bool   returnBars;
int    timeFrame;


int init () {
  SetIndexBuffer (0, up);
  SetIndexStyle (0, DRAW_LINE);
  SetIndexBuffer (1, down);
  SetIndexStyle (1, DRAW_LINE);
  SetIndexBuffer (2, atrlo);
  SetIndexStyle (2, DRAW_LINE);
  SetIndexBuffer (3, atrhi);
  SetIndexStyle (3, DRAW_LINE);
  SetIndexBuffer (4, trend);
  SetIndexEmptyValue (0, 0.0);
  SetIndexEmptyValue (1, 0.0);
  SetIndexEmptyValue (4, 0.0);
  nexttrend = 0;
  minh = High[Bars - 1];
  maxl = Low[Bars - 1];
  indicatorFileName = WindowExpertName();
  returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
  timeFrame         = stringToTimeFrame(TimeFrame);
  IndicatorShortName(timeFrameToString(timeFrame)+" Ozymandias");
  return (0);
}

int start () 
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { up[0] = limit+1; return(0); }
   
            if (timeFrame!=Period())
            {
               limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
               for (int i=limit; i>=0; i--)
               {
                  int y = iBarShift(NULL,timeFrame,Time[i]);           
                     up[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Amplitude,0,y);
                     down[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Amplitude,1,y);
                     atrlo[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Amplitude,2,y);
                     atrhi[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Amplitude,3,y);
               }
               return(0);
            }
  double atr, ll, hh, lma, hma;
  int workbar = 1;
  int c = IndicatorCounted ();

  if (c < 0) {
    return (- 1);
  }

  for (i = Bars - 1 - c; i >= workbar; i --) {
    ll = iLow (Symbol (), Period (),
      iLowest (Symbol (), Period (), MODE_LOW, Amplitude, i));
    hh = iHigh (Symbol (), Period (),
      iHighest (Symbol (), Period (), MODE_HIGH, Amplitude, i));
    lma = iMA (NULL, 0, Amplitude, 0, MODE_SMA, PRICE_LOW, i);
    hma = iMA (NULL, 0, Amplitude, 0, MODE_SMA, PRICE_HIGH, i);
    trend[i] = trend[i + 1];
    atr = iATR (Symbol (), 0, 100, i) / 2;

    if (nexttrend == 1) {
      maxl = MathMax (ll, maxl);

      if (hma < maxl
      && Close[i] < Low[i + 1]) {
        trend[i] = 1;
        nexttrend = 0;
        minh = hh;
      }
    }

    if (nexttrend == 0) {
      minh = MathMin (hh, minh);

      if (lma > minh
      && Close[i] > High[i + 1]) {
        trend[i] = 0;
        nexttrend = 1;
        maxl = ll;
      }
    }

    if (trend[i] == 0.0) {
      if (trend[i + 1] != 0.0) {
        up[i] = down[i + 1];
        up[i + 1] = up[i];
      } else {
        up[i] = MathMax (maxl, up[i + 1]);
      }

      atrhi[i] = up[i] + atr;
      atrlo[i] = up[i] - atr;
      down[i] = 0.0;
    } else {
      if (trend[i + 1] != 1.0) {
        down[i] = up[i + 1];
        down[i + 1] = down[i];
      } else {
        down[i] = MathMin (minh, down[i + 1]);
      }

      atrhi[i] = down[i] + atr;
      atrlo[i] = down[i] - atr;
      up[i] = 0.0;
    }
  }

  return (0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}



