//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color5  clrMediumSeaGreen
#property indicator_color6  clrMediumSeaGreen
#property indicator_color7  C'225,172,183'
#property indicator_color8  C'225,172,183'

#property indicator_width5  1
#property indicator_width6  1
#property indicator_width7  1
#property indicator_width8  1

#property indicator_style5  STYLE_DOT
#property indicator_style6  STYLE_DOT
#property indicator_style7  STYLE_DOT
#property indicator_style8  STYLE_DOT
#property strict

//
//
//
//
//

extern ENUM_TIMEFRAMES   TimeFrame   = PERIOD_CURRENT;  // Time frame
extern int                SLength              = 40;    // Slow period
extern double             SDeviation           = 1.0;    // Slow deviation
extern int                FLength              = 20;    // Fast period
extern double             FDeviation           = 1.0;    // Fast deviation
extern ENUM_APPLIED_PRICE Price               = PRICE_CLOSE; 
extern bool                showRIBBON    =   true;  // Show ribbon?
input int              RibbonWidth      =   1;                // Width of ribbon
extern color           ribbonUP                = clrMediumSeaGreen;      // Bullish ribbon  color
extern color           ribbonDN                = C'225,172,183';;      // Bearish ribbon  color      
input bool               Interpolate = true;            // Interpolate in multi time frame mode on/off?

double ssmh1[],ssmh2[],ssml1[],ssml2[],slope1[],slope2[],U1[],L1[],U2[],L2[],count[];
string indicatorFileName;
#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,SLength,SDeviation,FLength,FDeviation,Price,showRIBBON,RibbonWidth,ribbonUP,ribbonDN,_buff,_ind)

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnInit()
{
   IndicatorBuffers(11);
   SetIndexBuffer(0,ssmh1); SetIndexStyle(0,showRIBBON  ? DRAW_HISTOGRAM :  DRAW_NONE,0, RibbonWidth,ribbonDN);
   SetIndexBuffer(1,ssmh2); SetIndexStyle(1,showRIBBON  ? DRAW_HISTOGRAM :  DRAW_NONE,0, RibbonWidth,ribbonUP);
   SetIndexBuffer(2,ssml1); SetIndexStyle(2,showRIBBON  ? DRAW_HISTOGRAM :  DRAW_NONE,0, RibbonWidth,ribbonDN);
   SetIndexBuffer(3,ssml2); SetIndexStyle(3,showRIBBON  ? DRAW_HISTOGRAM :  DRAW_NONE,0, RibbonWidth,ribbonUP);
   
   SetIndexBuffer(4,U1,   INDICATOR_DATA); SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(5,L1, INDICATOR_DATA); SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(6,U2, INDICATOR_DATA); SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(7,L2, INDICATOR_DATA); SetIndexStyle(7,DRAW_LINE); 
   
   SetIndexBuffer(8,slope1);
   SetIndexBuffer(9,slope2);
   
   SetIndexBuffer(10,count);
   
   indicatorFileName = WindowExpertName();
   TimeFrame         = fmax(TimeFrame,_Period); 
   
  
   
   IndicatorSetString(INDICATOR_SHORTNAME,timeFrameToString(TimeFrame)+"  ("+(string)FLength+")");
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=fmin(rates_total-prev_calculated+1,rates_total-1); count[0] = limit;
  
      if (TimeFrame!=_Period)
      {
         limit = (int)fmax(limit,fmin(rates_total-1,_mtfCall(10,0)*TimeFrame/_Period));
       
         for (i=limit;i>=0 && !_StopFlag; i--)
         {
            int y = iBarShift(NULL,TimeFrame,time[i]);
               ssmh1[i]   = _mtfCall(0,y);
               ssmh2[i]   = _mtfCall(1,y);
               ssml1[i]   = _mtfCall(2,y);
               ssml2[i]   = _mtfCall(3,y);
               
               U1[i]   = _mtfCall(4,y);
               L1[i] = _mtfCall(5,y);
               U2[i] = _mtfCall(6,y);
               L2[i] = _mtfCall(7,y);
               slope1[i] = _mtfCall(8,y);
               slope2[i] = _mtfCall(9,y);      
               //
               //
               //
                if (slope1[i] == 1)
            {
               ssmh1[i] = U1[i];
               ssmh2[i] = U2[i];
            }
            else
            {
               ssmh2[i] = U2[i];
               ssmh1[i] = U1[i];
            }               
            if (slope2[i] == 1)
            {
               ssml1[i] = L1[i];
               ssml2[i] = L2[i];
            }
            else
            {
               ssml2[i] = L2[i];
               ssml1[i] = L1[i];
            }  
            
                  
               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 dtime = iTime(NULL,TimeFrame,y);
                  for(n = 1; (i+n)<rates_total && time[i+n] >= dtime; n++) continue;	
                  for(k = 1; k<n && (i+n)<rates_total && (i+k)<rates_total; k++) 
                  {
                     _interpolate(U1); 
                     _interpolate(L1);
                     _interpolate(U2);  
                     _interpolate(L2); 
                     
                      if (slope1[i] == 1)
            {
               ssmh1[i+k] = U1[i+k];
               ssmh2[i+k] = U2[i+k];
            }
            else
            {
               ssmh2[i+k] = U2[i+k];
               ssmh1[i+k] = U1[i+k];
            }               
            if (slope2[i] == 1)
            {
               ssml1[i+k] = L1[i+k];
               ssml2[i+k] = L2[i+k];
            }
            else
            {
               ssml2[i+k] = L2[i+k];
               ssml1[i+k] = L1[i+k];
            }  
            
                  }                    
         } 
        
   return(rates_total);
   } 
      
   //
   //
   //
          
  
   for(i=limit;i>=0; i--)
   {  
      U1[i]    = iBands(NULL,0,SLength,SDeviation,0,Price,MODE_UPPER,i); // 1 
      L1[i]    = iBands(NULL,0,SLength,SDeviation,0,Price,MODE_LOWER,i); // 2 
      
      U2[i]    = iBands(NULL,0,FLength,FDeviation,0,Price,MODE_UPPER,i); // 1 
      L2[i]    = iBands(NULL,0,FLength,FDeviation,0,Price,MODE_LOWER,i); // 2
      
       slope1[i] = (i<rates_total-1) ? (U1[i]>U2[i]) ? 1 : (U1[i]<U2[i]) ? -1  : slope1[i+1]  : 0; 
       slope2[i] = (i<rates_total-1) ? (L1[i]>L2[i]) ? 1 : (L1[i]<L2[i]) ? -1  : slope2[i+1]  : 0; 
      
     if (slope1[i] == 1)
            {
               ssmh1[i] = U1[i];
               ssmh2[i] = U2[i];
            }
            else
            {
               ssmh2[i] = U2[i];
               ssmh1[i] = U1[i];
            }               
            if (slope2[i] == 1)
            {
               ssml1[i] = L1[i];
               ssml2[i] = L2[i];
            }
            else
            {
               ssml2[i] = L2[i];
               ssml1[i] = L1[i];
            }  
   }
return(rates_total);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//


//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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("");
}
 

