Yearly Hi/Lo channel problem

 

Hello all,


I'm trying to write up this indicator which will plot channels for the previous day/week/month and year. So far i have got the day/week/month all working but I am having some troubles with the yearly channel.

To do this for the previous 12 months doesn't seem too difficult but I would like to make the channel for the high/low of the past calander month (Jan-Dec) for all years in the chart history.

In it's present form it currently marks the high/low of the previous year but seems to be off by a month or two for some reason. Then after a couple of years the indi just runs flat.

Any ideas. Code Below:

#property strict
#property indicator_chart_window

#property indicator_buffers 8
#property indicator_color1 SkyBlue
#property indicator_color2 SkyBlue
#property indicator_color3 SeaGreen
#property indicator_color4 SeaGreen
#property indicator_color5 Coral
#property indicator_color6 Coral
#property indicator_color7 FireBrick
#property indicator_color8 FireBrick

int wkTFbars = 0, mnTFbars = 0;      //calculates the number of bars in a week for the current timeframe
int cur_day = 0, prev_day = 0, cur_year = 0, prev_year = 0, DAYshift = 0, WEEKshift = 0, MONTHshift = 0, cnt;
double DAYhigh[], DAYlow[], day_high = 0, day_low = 0, last_day_high = 0, last_day_low = 0;
double WEEKhigh[], WEEKlow[], week_high = 0, week_low = 0, last_week_high = 0, last_week_low = 0;
double MONTHhigh[], MONTHlow[], month_high = 0, month_low = 0, last_month_high = 0, last_month_low = 0;
double YEARhigh[], YEARlow[], year_low = 0, year_high = 0, last_year_high = 0, last_year_low;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(6);
   IndicatorDigits(Digits);
   
   SetIndexBuffer (0, DAYhigh);
   SetIndexBuffer (1, DAYlow);
   SetIndexBuffer (2, WEEKhigh);
   SetIndexBuffer (3, WEEKlow);
   SetIndexBuffer (4, MONTHhigh);
   SetIndexBuffer (5, MONTHlow);
   SetIndexBuffer (6, YEARhigh);
   SetIndexBuffer (7, YEARlow);
   
   SetIndexStyle (0, DRAW_LINE,2);
   SetIndexStyle (1, DRAW_LINE,2);
   SetIndexStyle (2, DRAW_LINE,2);
   SetIndexStyle (3, DRAW_LINE,2);
   SetIndexStyle (4, DRAW_LINE,2);
   SetIndexStyle (5, DRAW_LINE,2);
   SetIndexStyle (6, DRAW_LINE,2);
   SetIndexStyle (7, DRAW_LINE,2);
   
   SetIndexDrawBegin(0,0);
   SetIndexDrawBegin(1,0);
   SetIndexDrawBegin(2,0);
   SetIndexDrawBegin(3,0);
   SetIndexDrawBegin(4,0);
   SetIndexDrawBegin(5,0);
   SetIndexDrawBegin(6,0);
   SetIndexDrawBegin(7,0);
   
   SetIndexLabel(0,"DAYhigh");
   SetIndexLabel(1,"DAYlow");
   SetIndexLabel(2,"WEEKhigh");
   SetIndexLabel(3,"WEEKlow");   
   SetIndexLabel(4,"MONTHhigh");
   SetIndexLabel(5,"MONTHlow");
   SetIndexLabel(6,"YEARhigh");
   SetIndexLabel(7,"YEARlow");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
   for (cnt = Bars - 1; cnt >= 0 ;cnt--) {
   
      cur_day = TimeDay(Time[cnt]);
      cur_year = TimeYear(Time[cnt]);
   //Print("TFbars = ",TFbars);
      if (prev_day != cur_day) {
      
         DAYshift = iBarShift(Symbol(), PERIOD_D1, Time[cnt]);
         
         //Get the shift of the current Weeks bar
         WEEKshift = iBarShift(Symbol(), PERIOD_W1, Time[cnt]);
         
         //Get the shift of the current months bar
         MONTHshift = iBarShift(Symbol(), PERIOD_MN1, Time[cnt]);
         
         last_day_high = iHigh(Symbol(), PERIOD_D1, DAYshift + 1);
         last_day_low = iLow(Symbol(), PERIOD_D1, DAYshift + 1);
         
         last_week_high = iHigh(Symbol(), PERIOD_W1, WEEKshift + 1);
         last_week_low = iLow(Symbol(), PERIOD_W1, WEEKshift + 1);
         
         last_month_high = iHigh(Symbol(), PERIOD_MN1, MONTHshift + 1);
         last_month_low = iLow(Symbol(), PERIOD_MN1, MONTHshift +1);
          
         if (prev_year != cur_year) {
            last_year_high = iHigh(NULL, PERIOD_MN1, iHighest(NULL, PERIOD_MN1, MODE_HIGH, 12, MONTHshift)); 
            last_year_low = iLow(NULL, PERIOD_MN1, iLowest(NULL, PERIOD_MN1, MODE_LOW, 12, MONTHshift));
            
            prev_year = cur_year;
         } 
                    
         prev_day = cur_day;
      }
      
    /*  if (prev_year != cur_year) {
         last_year_high = iHigh(NULL, PERIOD_MN1, iHighest(NULL, PERIOD_MN1, MODE_HIGH, 12, Month())); 
         last_year_low = iLow(NULL, PERIOD_MN1, iLowest(NULL, PERIOD_MN1, MODE_LOW, 12, Month()));
            
         prev_year = cur_year;
      }*/
        
      
   day_high = last_day_high;
   day_low = last_day_low;   
      
   week_high = last_week_high;
   week_low = last_week_low;
   
   month_high = last_month_high;
   month_low = last_month_low;
   
   year_high = last_year_high;
   year_low = last_year_low;
   
   if (Period() == 1 || Period() == 5 || Period() == 15 || Period() == 30 || Period() == 60) {
      DAYhigh[cnt] = day_high;
      DAYlow[cnt] = day_low;
   }
   
   if (Period() == 1 || Period() == 5 || Period() == 15 || Period() == 30 || Period() == 60 || Period() == 240) {
         WEEKhigh[cnt] = week_high;
         WEEKlow[cnt] = week_low; 
   }
   
   if (Period() == 1 || Period() == 5 || Period() == 15 || Period() == 30 || Period() == 60 || Period() == 240 || 
       Period() == 1440) {
         MONTHhigh[cnt] = month_high;
         MONTHlow[cnt] = month_low;
   }
   
   YEARhigh[cnt] = year_high;
   YEARlow[cnt] = year_low;
      
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
SteepCurve:

Hello all,


I'm trying to write up this indicator which will plot channels for the previous day/week/month and year. So far i have got the day/week/month all working but I am having some troubles with the yearly channel.

To do this for the previous 12 months doesn't seem too difficult but I would like to make the channel for the high/low of the past calander month (Jan-Dec) for all years in the chart history.

In it's present form it currently marks the high/low of the previous year but seems to be off by a month or two for some reason. Then after a couple of years the indi just runs flat.

Any ideas. Code Below:

Nobody? I know it's something simple. It always is with me, but for the life of me I can't nut this one out :(
 
         if(prev_year!=cur_year) 
           {
            last_year_high= iHigh(NULL,PERIOD_MN1,iHighest(NULL,PERIOD_MN1,MODE_HIGH,12,MONTHshift+1));
            last_year_low = iLow(NULL,PERIOD_MN1,iLowest(NULL,PERIOD_MN1,MODE_LOW,12,MONTHshift+1));

            prev_year=cur_year;
           }
The +1 may make a difference
 
GumRai:
The +1 may make a difference

Thanks for the reply GumRai.

I would have thought it would definitely solve the problem but unfortunately it doesn't seem to make any major difference the to problem described above.

Do you (or anyone else) have any suggestions?

Sorry for the hassle

 
SteepCurve:

Thanks for the reply GumRai.

I would have thought it would definitely solve the problem but unfortunately it doesn't seem to make any major difference the to problem described above.

Do you (or anyone else) have any suggestions?

Sorry for the hassle

I stand corrected. It does fix the issue.

I was looking at MN1 and forgot to check the other timeframes.

The yearly channel plots fine now on everything but the monthly chart. Any ideas why this may be. I have data all the way back to 1995 on my chart so it's not like there is a shortage of bars.

Is it because I use PERIOD_MN1 in my calculations rather than the number of bars in a year for what ever period chart I am viewing at any given time?

 

It is because

TimeDay(Time[cnt])

 will always return 1 on the monthly chart

so

if(prev_day!=cur_day)

will never be true.

Move this out of the if condition and add the extra line to calculate MONTHshift

         if(prev_year!=cur_year) 
           {
            MONTHshift=iBarShift(Symbol(),PERIOD_MN1,Time[cnt]);
            last_year_high= iHigh(NULL,PERIOD_MN1,iHighest(NULL,PERIOD_MN1,MODE_HIGH,12,MONTHshift+1));
            last_year_low = iLow(NULL,PERIOD_MN1,iLowest(NULL,PERIOD_MN1,MODE_LOW,12,MONTHshift+1));

            prev_year=cur_year;
           }
 

GumRai!!

I'm sorry I missed your reply to this.

Thank you for your clear explanation. Your fix worked perfectly :)

It's folks like you that make this forum that much more enjoyable.

Cheers,

SteepCurve

Reason: