Author: Copyright � 2010, WUCIWUG
Price Data Components
Series array that contains open time of each bar
Indicators Used
Moving average indicator
1 Views
0 Downloads
0 Favorites
VVolt
#property copyright "Copyright © 2010, WUCIWUG"
#property link      "wuciwu@gmail.com"

extern int     ATRPeriod = 14;
extern int     AvgPeriod = 20;
extern int     Smoothing = 15;

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 Red
#property indicator_color2 White

double ATR[];
double AvgATR[];
double Differ[];
double Smoothed[];
double Volumes[];

int init()
{
   IndicatorBuffers(5);
   
   SetIndexBuffer(0, Differ);
   SetIndexBuffer(1, Smoothed);
   SetIndexBuffer(2, ATR);
   SetIndexBuffer(3, AvgATR);
   SetIndexBuffer(4, Volumes);
   
   return(0);
}

int start()
{
   int toCount = MathMin(Bars, Bars - IndicatorCounted() + 1);
   
   for (int i = toCount - 1; i >= 0; i--)
   {
      Volumes[i] = Volume[i];
   }

   for (i = toCount - 1; i >= 0; i--)
   {
      ATR[i] = iMAOnArray(Volumes, 0, ATRPeriod, 0, MODE_EMA, i);
   }
   
   for (i = toCount - 1; i >= 0; i--)
   {
      AvgATR[i] = EMPTY_VALUE;
      double sum = 0;
      int dayShift = iBarShift(Symbol(), PERIOD_D1, Time[i], true);
      
      if (dayShift != -1 && ATR[i] != EMPTY_VALUE)
      {
         int offset = Time[i] - iTime(Symbol(), PERIOD_D1, dayShift);
         double lastValue = ATR[i];
         
         for (int j = 0; j < AvgPeriod; j++)
         {
            int elementShift = iBarShift(Symbol(), 0, iTime(Symbol(), PERIOD_D1, dayShift + j) + offset, true);
            if (elementShift == -1 || ATR[elementShift] == EMPTY_VALUE)
            {
               sum += lastValue;
            }
            else
            {
               sum += ATR[elementShift];
               lastValue = ATR[elementShift];
            }
         }
         AvgATR[i] = sum/AvgPeriod;
      }
   }
   
   for (i = toCount - 1; i >= 0; i--)
   {
      if (ATR[i] != EMPTY_VALUE && AvgATR[i] != EMPTY_VALUE)
      {
         Differ[i] = ATR[i] - AvgATR[i];
      }
      else
      {
         Differ[i] = 0;
      }
   }

   for (i = toCount - 1; i >= 0; i--)
   {
      Smoothed[i] = iMAOnArray(Differ, 0, Smoothing, 0, MODE_SMA, i);
   }
   
   return(0);
}

Comments