//+------------------------------------------------------------------+
//|                                                    SMA shift.mq4 |
//|                                    copyleft 2012, Diksy Media F. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "copyleft 2012, Diksy Media F."
#property link      ""

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_style2 STYLE_SOLID
#property indicator_style3 STYLE_SOLID
#property indicator_style4 STYLE_SOLID
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_color2 Aqua
#property indicator_color3 Magenta
#property indicator_color4 Lime

static int OPEN = 0,    MONTHLY = 1,    WEEKLY = 2,    DAILY = 3;
double     open_buff[], monthly_buff[], weekly_buff[], daily_buff[];
//--- input parameters
extern int       MonthlyPeriod=480;
extern int       WeeklyPeriod=120;
extern int       DailyPeriod=24;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorDigits(Digits);
   SetIndexBuffer(OPEN, open_buff);
   SetIndexBuffer(MONTHLY, monthly_buff);
   SetIndexBuffer(WEEKLY, weekly_buff);
   SetIndexBuffer(DAILY, daily_buff);
   SetIndexStyle(OPEN, DRAW_NONE);
   SetIndexStyle(MONTHLY, DRAW_LINE);
   SetIndexStyle(WEEKLY, DRAW_LINE);
   SetIndexStyle(DAILY, DRAW_LINE);
   SetIndexLabel(MONTHLY, "monthly");
   SetIndexLabel(WEEKLY, "weekly");
   SetIndexLabel(DAILY, "daily");
   SetLevelValue(0, 0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   int index;
   for(index = Bars; index >= 0; index--) open_buff[index] = Open[index];
   for(index = Bars - MonthlyPeriod; index >= 0; index--)
   {
      monthly_buff[index] = (Close[index] - open_buff[index + MonthlyPeriod - 1]) / Point;
      weekly_buff[index] = (Close[index] - open_buff[index + WeeklyPeriod - 1]) / Point;
      daily_buff[index] = (Close[index] - open_buff[index + DailyPeriod - 1]) / Point;
   }
   for(index = Bars; index >= 0; index--) open_buff[index] = EMPTY_VALUE;
   
   int monthly, weekly, daily, average = 0;
   // Monthly
   for(index = 1; index <= 12; index++) average += (iHigh(NULL, PERIOD_MN1, index) - iLow(NULL, PERIOD_MN1, index)) / Point;
   monthly = average / 12;
   // Weekly
   average = 0;
   for(index = 1; index <= 16; index++) average += (iHigh(NULL, PERIOD_W1, index) - iLow(NULL, PERIOD_W1, index)) / Point;
   weekly = average / 16;
   // Daily
   average = 0;
   for(index = 1; index <= 20; index++) average += (iHigh(NULL, PERIOD_D1, index) - iLow(NULL, PERIOD_D1, index)) / Point;
   daily = average / 20;
   
   IndicatorShortName("SMA 1 Shift [MN1: "+monthly+"] [W1: "+weekly+"] [D1: "+daily+"]");
//----
   return(0);
  }
//+------------------------------------------------------------------+