Author: 2009-2017, MetaQuotes Software Corp.
Price Data Components
Series array that contains close prices for each bar
0 Views
0 Downloads
0 Favorites
TMA Band
#property copyright   "2009-2017, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "Triple Exponential Moving Average"

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   3
#property indicator_type1   DRAW_LINE
#property indicator_color1  Yellow
#property indicator_width1  1
#property indicator_label1  "TMA"
#property indicator_type2   DRAW_LINE
#property indicator_color2  Red
#property indicator_width2  1
#property indicator_label2  "Up Band TMA"
#property indicator_type3   DRAW_LINE
#property indicator_color3  Red
#property indicator_width3  1
#property indicator_label3  "Down Band TMA"
//#property indicator_applied_price PRICE_CLOSE
//--- input parameters
input int InpPeriodTMA = 20;
input double BandsDeviations = 2.0;
//--- indicator buffers
double BufferTMA[];
double upBuffer[];
double dnBuffer[];
double wuBuffer[];
double wdBuffer[];
void OnInit()
  {
   SetIndexBuffer(0, BufferTMA, INDICATOR_DATA);
   SetIndexBuffer(1, upBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, dnBuffer, INDICATOR_DATA);
   SetIndexBuffer(3, wuBuffer, INDICATOR_CALCULATIONS);
   SetIndexBuffer(4, wdBuffer, INDICATOR_CALCULATIONS);
  }
  
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   int start;
   if(prev_calculated==0)
      start=0;
   else
      start=prev_calculated-1;

   double FullLength = 2.0 * InpPeriodTMA + 1.0;
   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      int NewIndex = rates_total - 1 - i;
      double sumw = (InpPeriodTMA + 1);
      double sum  = sumw * iClose(NULL,PERIOD_CURRENT,NewIndex);

      for(int j = 1, k = InpPeriodTMA; j <= InpPeriodTMA; j ++, k --)
        {
         sum  += k * iClose(NULL,PERIOD_CURRENT,NewIndex + j);
         sumw += k;
         if(j <= NewIndex)
           {
            sum  += k * iClose(NULL,PERIOD_CURRENT,NewIndex - j);
            sumw += k;
           }
        }
        
      BufferTMA[i] = sum/sumw;

      double diff = iClose(NULL,PERIOD_CURRENT,NewIndex) - BufferTMA[i];

      if(i < InpPeriodTMA+1)
         continue;

      static bool FirstStart = true;

      if(FirstStart)
        {
         upBuffer[i-1] = upBuffer[i] = BufferTMA[i];
         dnBuffer[i-1] = upBuffer[i] = BufferTMA[i];
         if(diff>=0)
           {
            wuBuffer[i-1] = wuBuffer[i] = MathPow(diff,2);
            wdBuffer[i-1] = wdBuffer[i] = 0;
           }
         else
           {
            wdBuffer[i-1] = wdBuffer[i] = MathPow(diff,2);
            wuBuffer[i-1] = wuBuffer[i] = 0;
           }
         FirstStart=false;
         continue;
        }

      if(diff>=0)
        {
         wuBuffer[i] = (wuBuffer[i-1] * (FullLength-1) + MathPow(diff,2))/FullLength;
         wdBuffer[i] =  wdBuffer[i-1] * (FullLength-1) / FullLength;
        }
      else
        {
         wdBuffer[i] = (wdBuffer[i-1] * (FullLength-1) + MathPow(diff,2))/FullLength;
         wuBuffer[i] =  wuBuffer[i-1] * (FullLength-1) / FullLength;
        }

      upBuffer[i] = BufferTMA[i] + BandsDeviations * MathSqrt(wuBuffer[i]);
      dnBuffer[i] = BufferTMA[i] - BandsDeviations * MathSqrt(wdBuffer[i]);
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments