showing current high-low of week

 

Hello,

 I would appreciate some help.

I can program tradestation but I am having difficulties with some aspects of MQL.

I would like to be able to send an alert when the weekly high is broken.

It would be grand if was able to plot the highs and lows of the week like this: (this is 4H chart)

highs and lows of week 

I would then test to see if the current high High[1] of the bar just completed is above the weekly high of [2] (the bar before it, as the weekly high will now be changed if a new one is achieved) 

I have tried using

     double wHigh = iHigh(Symbol(),PERIOD_W1,0);

    double wLow = iLow(Symbol(),PERIOD_W1,0);

but I always get the current week high, not the high as it was two bars ago. 

I am suspecting iBarShift may have something to do with the solution but I can't get my head around it.

 

hopefully someone can help.

thank you all in advance,

aristides. 

 

If you code the indicator, then you could use iCustom.

If just using code, you could use something like this 

Just quickly typed, not checked thoroughly. 

  ResetLastError(); 
  datetime week_start=iTime(Symbol(),PERIOD_W1,0);
  if(GetLastError()==4066)
     {
     Print("Error - Weekly Timeframe Updating");
     return;
     }
  else
     {
     int week_start_shift=iBarShift(Symbol(),0,week_start,true);
     if(week_start_shift==-1)
        week_start_shift=iBarShift(Symbol(),0,week_start,false)-1;
     if(week_start_shift>1)
        {
        int highest=iHighest(Symbol(),0,MODE_HIGH,week_start_shift,1);
        //Print("week_start_shift=",week_start_shift);
        //Print("highest=",highest);
        if(highest==1)
           {
           Print("Bar[1] is the highest of the week excluding the current Bar");
           }
        }
     
     }
 
 

GumRai,

 

thank you very much for your reply, you were very helpful.

 here is the code I have now.

does this look like it would work? it compiles ok.

is there any way to test it apart from having the computer on for a week? :)

#property copyright "Copyright © 2014, Aristides Mytaras"
#property link      ""
// 
/*
    alert new extremes of week after opposing bar has printed
*/
extern bool Enable_Alerts = true;
extern bool Enable_EMails = false;
extern bool signal_newWlow = true;
extern bool signal_newWhigh = true;

/////////////////////////////////////
int ThisBarTrade        =      0;
    
int init() {
}

int start() {
      
    string sAlert, sMail, sCandleTime;
    string subject="alert "+Symbol();
    //double wHigh = iHigh(Symbol(),PERIOD_W1,0);
    //double wLow = iLow(Symbol(),PERIOD_W1,0);

    bool newHigh = false;
    bool newLow = false;
    
    if ( ThisBarTrade != Bars) {
         // calculate only when there is a new bar
         ThisBarTrade = Bars;
         
         //inserted code by GumRai
      ResetLastError(); 
      datetime week_start=iTime(Symbol(),PERIOD_W1,0);
      if(GetLastError()==4066)
         {Print("Error - Weekly Timeframe Updating");
         return;
            }
       else
         {
         int week_start_shift=iBarShift(Symbol(),0,week_start,true);
         if(week_start_shift==-1)
            week_start_shift=iBarShift(Symbol(),0,week_start,false)-1;
         if(week_start_shift>1)
             {
            int highest=iHighest(Symbol(),0,MODE_HIGH,week_start_shift,1);
            int lowest=iLowest(Symbol(),0,MODE_HIGH,week_start_shift,1);
            
            // if highest or lowest achieved during last two bars assume there is "new" high/low of week
            if(highest==1 || highest==2) newHigh==true;
            if(lowest==1 || lowest==2) newLow==true;
             }
     
          }
         
         
         sCandleTime=" Candle="+TimeToStr(Time[1]);
         
         if (Close[1]<Open[1] && newHigh && signal_newWhigh) {
            // DOWN (red) bar after new Weekly High
            newHigh==false;
            sAlert=Symbol()+" W high + DOWN bar "+sCandleTime;
            sMail =sAlert + "  Time: " + TimeToStr(TimeCurrent());
            subject="W low "+Symbol();
            if (Enable_Alerts) Alert(sAlert);
            if (Enable_EMails) SendMail(subject, sMail);
         }
         
           if (Close[1]>Open[1] && newLow && signal_newWlow) {
            // UP (green) bar after new weekly Low
            newLow==false;
            sAlert=Symbol()+" W low + UP bar "+sCandleTime;
            sMail =sAlert + "  Time: " + TimeToStr(TimeCurrent());
            subject="W high "+Symbol();
            if (Enable_Alerts) Alert(sAlert);
            if (Enable_EMails) SendMail(subject, sMail);
         }      
    }
}

 

thank you once again.

aristides. 

 
aristid:

I have tried using

     double wHigh = iHigh(Symbol(),PERIOD_W1,0);

    double wLow = iLow(Symbol(),PERIOD_W1,0);

but I always get the current week high, not the high as it was two bars ago.

Which is exactly what your image shows. So what's the problem?
 
WHRoeder:
aristid:

I have tried using

     double wHigh = iHigh(Symbol(),PERIOD_W1,0);

    double wLow = iLow(Symbol(),PERIOD_W1,0);

but I always get the current week high, not the high as it was two bars ago.

Which is exactly what your image shows. So what's the problem?

Sorry, perhaps I am not clear. The image is from a plugin I did on tradestation. It is very easy to do there (only 2 lines). In tradestation you program the logic and it automatically goes through all bars on the chart, no need to set up a loop.

I only wish I could make MT4 show this image. 

 
aristid: It is very easy to do there (only 2 lines). In tradestation you program the logic and it automatically goes through all bars on the chart, no need to set up a loop.
  1. you have no buffers, so you get no lines
  2. Set up two buffers in init and fill them in start
    for(int iBar = Bars - 1 - IndicatorCounted(); iBar >= 0; --iBar){
       BufferA[iBar] = iHigh(Symbol(),PERIOD_W1, iBar);
       BufferB ...
    }
  3. Is that so hard? Two extra lines not counting init? Read the documentation, look at some code examples.
 

WHRoeder,


I am sure you can understand that what you are suggesting will plot the current week's hi-lo on the current bar, then last week's hi-lo on the previous bar, then week[2]'s hi-lo on bar[2] etc.  

weekly hilos on 4h 

It's not hard, it just doesn't work. And thanks for the other recommendations.

 
aristid: I am sure you can understand that what you are suggesting will plot the current week's hi-lo on the current bar, then last week's hi-lo on the previous bar, then week[2]'s hi-lo on bar[2] etc. 
Doh, too late for me when I posted. I was just thinking of your loop comment., not the indexing. Try:
for(int iBar = Bars - 1 - IndicatorCounted(); iBar >= 0; --iBar){
   int      iW1  = iBarShift(Symbol(),PERIOD_W1, Time[iBar]);
   BufferA[iBar] = iHigh(Symbol(),PERIOD_W1, iW1);
   BufferB ...
}
 

Thank you, this also doesn't work, although it's much better..

You get highs and lows of weeks displayed properly, but not as they happen.

hilos 

What I am after is to plot the current hi-lo of the week on each bar.

After the first 4H bar of the week prints the current hi-lo of the new week is simply the high-low of the 1st bar, and it goes from there.. 

The first image of the post illustrates this. 

 
aristid: What I am after is to plot the current hi-lo of the week on each bar.

That is exactly what it is doing. Let it run and next week you will see.

What you want is for it to plot what the weekly hi-lo was at that bar in time. That is much harder, so I didn't try to confuse you with it.

  1. You must find the weekly bar from the current chart bar,
  2. then find the chart bar that starts that week,
  3. then find the high between (inclusive) the chart bars.
 
WHRoeder:

That is exactly what it is doing. Let it run and next week you will see.

What you want is for it to plot what the weekly hi-lo was at that bar in time. That is much harder, so I didn't try to confuse you with it.

  1. You must find the weekly bar from the current chart bar,
  2. then find the chart bar that starts that week,
  3. then find the high between (inclusive) the chart bars.

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. 

Reason: