Time and Price

 

Hello everyone,

I recently wrote this indicator to return buffers of the previous weekly high and low, but for some reason, it keeps being off.

Some bars on the lower timeframe do not have buffers that correspond to the weekly high/ low. 

I tried to correct this by using this

PeriodSeconds()


but it still gets them wrong. Can anyone please direct me on any indicator that has this functionality or what I am doing wrong?

I attached the part of the code. Thanks!

 input int Barscalc = 10; //Days to Draw

for(int j = Barscalc + 1; j > 0 ; j--)
     {
      datetime dl_start = iTime(_Symbol, PERIOD_D1, j) - (j * (PeriodSeconds(PERIOD_D1) / PeriodSeconds(PERIOD_CURRENT)));
      datetime dl_end = iTime(_Symbol, PERIOD_D1, j + 1) - (j * (PeriodSeconds(PERIOD_D1) / PeriodSeconds(PERIOD_CURRENT)));
      int first_bars = Bars(_Symbol, PERIOD_CURRENT, dl_start, dl_end);
      int barstouse = (first_bars * j) ;
      for(int i = barstouse; i >= 0; i--)
        {
         buf3[i] = iHigh(_Symbol, PERIOD_D1, j);
         buf4[i] = iLow(_Symbol, PERIOD_D1, j);
        }
     }
//====
   for(int j = Barscalc + 1  ; j > 0 ; j--)
     {
      datetime weekl_start = iTime(_Symbol, PERIOD_W1, j) - (j * (PeriodSeconds(PERIOD_W1) / PeriodSeconds(PERIOD_CURRENT)));
      datetime weekl_end = iTime(_Symbol, PERIOD_W1, j + 1) - (j * (PeriodSeconds(PERIOD_W1) / PeriodSeconds(PERIOD_CURRENT)));
      int first_bars = Bars(_Symbol, PERIOD_CURRENT, weekl_start, weekl_end);
      int barstouse = (first_bars * j) ;
      for(int i = barstouse; i >= 0; i--)
        {
         buf1[i] = iHigh(_Symbol, PERIOD_W1, j);
         buf2[i] = iLow(_Symbol, PERIOD_W1, j);
        }
     }
 

example attached. The whole zone marked out should have the same previous week buffer but some candles do, and some start returning the buffer from 2 weeks ago


 
I think maybe you could use iBarShift together with iTime

I published a code which draws lines on the high and low with user input for the period to select. by default it's on D1 but can be changed to weekly bar time. It's simple code if you use iTime together with iBarShift
 
Chioma Obunadike:

Hello everyone,

Hi , i don't understand the point of this calculation , you want the time in seconds 1 week ago for example minus the ratio of the weekly timeframe seconds to the current timeframe seconds , in seconds ?

datetime weekl_start = iTime(_Symbol, PERIOD_W1, j) - (j * (PeriodSeconds(PERIOD_W1) / PeriodSeconds(PERIOD_CURRENT)));
 
Conor Mcnamara #:
I think maybe you could use iBarShift together with iTime

I published a code which draws lines on the high and low with user input for the period to select. by default it's on D1 but can be changed to weekly bar time. It's simple code if you use iTime together with iBarShift

perfect. Thanks  Conor Mcnamara

Conor Mcnamara
Conor Mcnamara
  • 2023.10.14
  • www.mql5.com
Trader's profile
Reason: