//------------------------------------------------------------------
#property copyright "www.forex-station.cm"
#property link      "www.forex-station.cm"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  CLR_NONE
#property indicator_color2  Blue
#property indicator_color3  clrOrange
#property indicator_color4  clrOrange
#property indicator_color5  CLR_NONE
#property indicator_width2  8
#property indicator_width3  8
#property indicator_width4  8
#property indicator_style1  STYLE_DOT
#property indicator_style5  STYLE_DOT
#property strict

//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame     = PERIOD_CURRENT; // Time frame
input int              HighLowPeriod = 34;             // High
input bool             Interpolate   = true;           // Interpolate values on/off?

double bandUp[],bandMi[],bandMida[],bandMidb[],bandDn[],trend[],count[];
string indicatorFileName;
#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,HighLowPeriod,_buff,_ind)


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnInit()
{
   IndicatorBuffers(7);
   SetIndexBuffer(0,bandUp);
   SetIndexBuffer(1,bandMi);
   SetIndexBuffer(2,bandMida);
   SetIndexBuffer(3,bandMidb);
   SetIndexBuffer(4,bandDn);
   SetIndexBuffer(5,trend);
   SetIndexBuffer(6,count);
   indicatorFileName = WindowExpertName();
   TimeFrame         = fmax(TimeFrame,_Period);  
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) { }

//
//
//
//
//

int OnCalculate (const int       rates_total,
                 const int       prev_calculated,
                 const datetime& time[],
                 const double&   open[],
                 const double&   high[],
                 const double&   low[],
                 const double&   close[],
                 const long&     tick_volume[],
                 const long&     volume[],
                 const int&      spread[])
{
   int i,limit=rates_total-prev_calculated+1; if (limit>=rates_total) limit=rates_total-1; count[0] = limit;
      if (TimeFrame!=_Period)
      {
         limit = (int)fmax(limit,fmin(rates_total-1,_mtfCall(6,0)*TimeFrame/_Period));
         if (trend[limit]==-1) CleanPoint(limit,bandMida,bandMidb);
         for (i=limit;i>=0 && !_StopFlag; i--)
         {
            int y = iBarShift(NULL,TimeFrame,time[i]);
               bandUp[i]   = _mtfCall(0,y);
               bandMi[i]   = _mtfCall(1,y);
               bandMida[i] = bandMidb[i] = EMPTY_VALUE;
               bandDn[i]   = _mtfCall(4,y);
               trend[i]    = _mtfCall(5,y);
                
               //
               //
               //
               //
               //
                  
               if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,time[i-1]))) continue;
                  #define _interpolate(buff) buff[i+k] = buff[i]+(buff[i+n]-buff[i])*k/n
                  int n,k; datetime ctime = iTime(NULL,TimeFrame,y);
                     for(n = 1; (i+n)<rates_total && time[i+n] >= ctime; n++) continue;
                     for(k = 1; k<n && (i+n)<rates_total && (i+k)<rates_total; k++)
                     {
                         _interpolate(bandUp);
                         _interpolate(bandMi);
                         _interpolate(bandDn);      
                     }
               }
               for (i=limit;i>=0; i--) if (trend[i] == -1) PlotPoint(i,bandMida,bandMidb,bandMi);
   return(rates_total);
   } 
   
   //
   //
   //
   //
   //
   
   if (trend[limit]==-1) CleanPoint(limit,bandMida,bandMidb);
   for (i=limit;i>=0 && !_StopFlag; i--)
   {           
      bandUp[i] = iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,HighLowPeriod,i));
      bandDn[i] = iLow (NULL,0,iLowest (NULL,0,MODE_LOW ,HighLowPeriod,i));
      bandMi[i] = (bandUp[i]+bandDn[i])/2.0;
      trend[i]  = (i<rates_total-1) ? (bandMi[i]>bandMi[i+1]) ? 1 : (bandMi[i]<bandMi[i+1]) ? -1 : trend[i+1] : 0;
      bandMida[i] = bandMidb[i] = EMPTY_VALUE; if (trend[i] == -1) PlotPoint(i,bandMida,bandMidb,bandMi);
   }
return(rates_total);
}

//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>=Bars-3) return;
   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 (i>=Bars-2) return;
   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; }
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
