#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  LightSeaGreen
#property  indicator_color2  Green
#property  indicator_color3  Red

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int period = 13, metod = MODE_SMA, price = PRICE_CLOSE;

double ExtBuffer0[];
double ExtBuffer1[];
double ExtBuffer2[];
double trend[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
int    timeFrame;


int init()
  {
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,trend);
   
   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
   timeFrame         = stringToTimeFrame(TimeFrame);
   IndicatorShortName(timeFrameToString(timeFrame)+" Force mod ("+period+","+metod+","+price+")");
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
//---- initialization done
   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) { ExtBuffer0[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]);            
                     ExtBuffer0[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",period,metod,price,0,y);
                     ExtBuffer1[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",period,metod,price,1,y);
                     ExtBuffer2[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",period,metod,price,2,y);
               }
            return(0);
            }
             
             //
             //
             //
             //
             //
             
             for (i=limit; i>=0; i--) ExtBuffer0[i]=iVolume(NULL,0,i)*(iMA(NULL,0,period,0,metod,price,i) - iMA(NULL,0,period,0,metod,price,i+1));                 
             for(i=limit-1; i>=0; i--)
             {
                ExtBuffer1[i] = EMPTY_VALUE;
                ExtBuffer2[i] = EMPTY_VALUE;
                trend[i] = trend[i+1];
                if(ExtBuffer0[i]>ExtBuffer0[i+1]) trend[i] = 1;
                if(ExtBuffer0[i]<ExtBuffer0[i+1]) trend[i] =-1;
                if (trend[i] == 1) ExtBuffer1[i] = ExtBuffer0[i];
                if (trend[i] ==-1) ExtBuffer2[i] = ExtBuffer0[i];
             }
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);
}

