Download MetaTrader 5

showing current high-low of week - page 2

To add comments, please log in or register
whroeder1
16802
whroeder1  

aristid:

My initial thought was to use (DayOfWeek[0] < DayOfWeek[1]) to find the first day of the week.

My problem is that I cannot use this on backdata, only realtime.

  1. Not sure that is correct anymore. I think they were fixed. But you can always use
    datetime now = Time[0];  int nowDOW = TimeDayOfWeek(now);
  2. Alphabetic Index of MQL4 Functions (600+) - MQL4 forum
  3. In any case you don't want to be looping back from i to i+n to find the start of the week. A) Time of bar, B) iBarshift to find the W1 bar, C) iTime of the W1 bar, D) ibarshift to find the chart bar at BOW.
Keith Watford
Moderator
10536
Keith Watford  
aristid:

thanks for your reply, I understand.

My initial thought was to use (DayOfWeek[0] < DayOfWeek[1]) to find the first day of the week.

My problem is that I cannot use this on backdata, only realtime.

There is something I'm not getting with MQL4.

Any suggestions, I'd appreciate.

thanks again. 

Try this, it may give you a start.

I think that it should work in real time as well

 

//+------------------------------------------------------------------+
//|                                                    Week HiLo.mq4 |
//|                                                           GumRai |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "GumRai"
#property link      "none"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot WeekHigh
#property indicator_label1  "WeekHigh"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot WeekLow
#property indicator_label2  "WeekLow"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters

//--- indicator buffers
double         WeekHighBuffer[];
double         WeekLowBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,WeekHighBuffer);
   SetIndexBuffer(1,WeekLowBuffer);
   
//---
   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[])
  {
//---
  if(Period()>PERIOD_D1)
     return(0);
  int limit;
  if(prev_calculated==0)
     limit=rates_total-2;
  else
     limit=1;
  
  for(int index=limit;index>=0;index--)
     {
     //if(TimeDayOfWeek(Time[index+1])>=5 && TimeDayOfWeek(Time[index])<5)
     if(TimeDayOfWeek(Time[index+1])>TimeDayOfWeek(Time[index]))
        {
        WeekHighBuffer[index]=High[index];
        WeekLowBuffer[index]=Low[index];
        }
     else
        {
        if(High[index]>WeekHighBuffer[index+1])
           WeekHighBuffer[index]=High[index];   
        else
           WeekHighBuffer[index]=WeekHighBuffer[index+1];
        if(Low[index]<WeekLowBuffer[index+1])
           WeekLowBuffer[index]=Low[index];   
        else
           WeekLowBuffer[index]=WeekLowBuffer[index+1];
        }
     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
valgoih
26
valgoih  
you seems to be using donchian channel.use that on one week TF and you will see when price close above or below the channel!
Keith Watford
Moderator
10536
Keith Watford  
valgoih:
you seems to be using donchian channel.use that on one week TF and you will see when price close above or below the channel!
It is nothing like a Donchian channel. A Donchian channel plots the high and low from a set number of previous bars.
aristides
35
aristides  

GumRai,

 

thank you very much,

it certainly looks excellent!

I will have to look into OnCalculate to better understand.

 

thanks again!

aristides. 

 

(MT4 screenshot:)

 

Keith Watford
Moderator
10536
Keith Watford  
aristid:

GumRai,

 

thank you very much,

it certainly looks excellent!

I will have to look into OnCalculate to better understand.

 

thanks again!

aristides. 

 

(MT4 screenshot:)

 

You're welcome :)

Don't let all the const arrays after OnCalculate distract you. None of those arrays are used in the code. Just concentrate on the logic, you were almost there when you said

"My initial thought was to use (DayOfWeek[0] < DayOfWeek[1]) to find the first day of the week." 

salexes
812
salexes  
Keith Watford:

Try this, it may give you a start.

I think that it should work in real time as well

 


Could anyone tell me how I can change the indicator by Keith Watford to previous weekly high low ?

Files:
whroeder1
16802
whroeder1  
salexes: Could anyone tell me how I can change the indicator by Keith Watford to previous weekly high low ?

Why do you need to change anything? Look at the last lines for the previous week.

salexes
812
salexes  
whroeder1:

Why do you need to change anything? Look at the last lines for the previous week.

I need them. And want them drawn. So that when I apply it to the chart it shows/draws in the current week.. also at newest bar the high low from the previous week.

whroeder1
16802
whroeder1  
salexes: So that when I apply it to the chart it shows/draws in the current week.. also at newest bar the high low from the previous week.
     if(TimeDayOfWeek(Time[index+1])>TimeDayOfWeek(Time[index]))
        {
        WeekHighBuffer[index]=High[index];
        WeekLowBuffer[index]=Low[index];
        }
     else
        {
        if(High[index]>WeekHighBuffer[index+1])
           WeekHighBuffer[index]=High[index];   
        else
           WeekHighBuffer[index]=WeekHighBuffer[index+1];
        if(Low[index]<WeekLowBuffer[index+1])
           WeekLowBuffer[index]=Low[index];   
        else
           WeekLowBuffer[index]=WeekLowBuffer[index+1];
        }
     }

You want the previous week's H/L drawn on the current week. That isn't what your code does. It will have to be completely rewritten.

  1. Update the W1 chart. (One function call at the start)
  2. Find the W1 shift for the bar index. (One function call.)
  3. Get the H/L of the previous W1 bar and store. (Two assignments.)
17 lines become 4.
12
To add comments, please log in or register