Previous Day High-Low

 

Hi, I am new to MetaTrader and to its programming language, I have some experience with VB and WealthLab, anyway is not the same. I was wondering if someone could help me with the following idea:


I want an indicator that plots the previous day high and low, in any time frame. For example, I use 1h chart, but I need the T-1 1day high-low, and hopefully I could change to any time frame and still getting the T-1 1day high-Low for any T.


There are some post on the mql4 forum regarding this, but I haven't found exactly what I need. There is this interesting post: 'Previous Day's High' but that code only plot yesterday high-low:





As you can see, no matter the time frame I can get yesterday's High-Low, what is nice, but only yesterday's.


What I am trying to achieve is a little different. Let's suppose there is a chart with T daily bars, Bar T should plot the high-low from T-1, Bar T-1 should plot the high-low from T-2, Bar T-2 should plot the high-low from T-3, and so on. Actually I have some code that does that, but only works correctly in daily charts, if I change the time frame, i.e. 1hour, it goes mad.


The code is:


    for(int i = 0; i < limit; i++) 
     {
       UpperBuf[i] = iHigh(NULL, PERIOD_D1, i+1);
       LowerBuf[i] = iLow(NULL, PERIOD_D1, i+1);
      }

with a daily chart I get:




As you can see, there are the daily high-low shifted I bar to the right. But using the same code in a lower time frame it goes mad, like this:




What i need is an indicator that in a daily chart looks as does my actual code, but in a lower time frame looks like this:




Note: I draw the lines by hand. :p

Note2: In that chart the indicator doesn't take into account weekend data, I guess I can filter that in the main loop.


I will really appreciate your collaboration with this idea.


Regards,


Lococo.


PD. I attach my actual code.

Files:
 
lococo:

Hi, I am new to MetaTrader and to its programming language, I have some experience with VB and WealthLab, anyway is not the same. I was wondering if someone could help me with the following idea:


I want an indicator that plots the previous day high and low, in any time frame. For example, I use 1h chart, but I need the T-1 1day high-low, and hopefully I could change to any time frame and still getting the T-1 1day high-Low for any T.


There are some post on the mql4 forum regarding this, but I haven't found exactly what I need. There is this interesting post: 'Previous Day's High' but that code only plot yesterday high-low:





As you can see, no matter the time frame I can get yesterday's High-Low, what is nice, but only yesterday's.


What I am trying to achieve is a little different. Let's suppose there is a chart with T daily bars, Bar T should plot the high-low from T-1, Bar T-1 should plot the high-low from T-2, Bar T-2 should plot the high-low from T-3, and so on. Actually I have some code that does that, but only works correctly in daily charts, if I change the time frame, i.e. 1hour, it goes mad.


The code is:


with a daily chart I get:




As you can see, there are the daily high-low shifted I bar to the right. But using the same code in a lower time frame it goes mad, like this:




What i need is an indicator that in a daily chart looks as does my actual code, but in a lower time frame looks like this:




Note: I draw the lines by hand. :p

Note2: In that chart the indicator doesn't take into account weekend data, I guess I can filter that in the main loop.


I will really appreciate your collaboration with this idea.


Regards,


Lococo.


PD. I attach my actual code.


//+------------------------------------------------------------------+
//|                                         Copyright © 2008, ledxep |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, ledzep"
#property link      "http://www.metaquotes.net/"
 
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
 
 
double               DayHigh,DayLow,DayHigh1,DayLow1;
int                  ObjectIdx;
int                  DayIdx;
int                  k;
string               ObjName;
datetime             StartTime;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   { 
  
   return(0);
   }
  
  
//+------------------------------------------------------------------+
//| Deinitialization function                                        |
//+------------------------------------------------------------------+
int deinit()
   {
   ObjectsDeleteAll();  
   return(0);
   }
 
 
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
int start()
  {
  
   static bool first=true;   
   
   
   int      i,counted_bars=IndicatorCounted();
   double   BarHour,BarMinute;
   int      WeekDay;
   
 
   if(Bars<3) return(0);
   i=Bars-3;
   
   if(counted_bars==0 || first)
      {
      first=false;     
      while(i>=0)
         {
        
         TrendLine(StartTime,DayHigh1,Time[i],DayHigh1,Red);
         TrendLine(StartTime,DayLow1,Time[i],DayLow1,Red);
        
         if(High[i+1] > DayHigh) DayHigh = High[i+1];
         if(Low[i+1] < DayLow)   DayLow  = Low[i+1];
        
      
         if(TimeDay(Time[i]) != TimeDay(Time[i+1]))        
            {            
            DayHigh1=DayHigh;
            DayLow1=DayLow;
            DayHigh=Open[i];
            DayLow=Open[i];
            StartTime=Time[i];
            }
            
         i--;
         
         }//while close
     
      }
  return(0);
  }//start close
//+------------------------------------------------------------------+
 
void TrendLine(datetime x1, double y1, datetime x2, double y2, color col)
   {
   ObjectIdx++;
   ObjName="Line" + DoubleToStr(ObjectIdx,0);   
   ObjectCreate(ObjName, OBJ_TREND, 0, x1, y1, x2, y2);
   ObjectSet(ObjName,OBJPROP_COLOR,col);
   ObjectSet(ObjName,OBJPROP_RAY,0);    
   }
 
ledzep:

Ledzep, thank you very much, it does exactly whta I was looking for. Very nice code, I will learn from it.


Thank you again.


Regards.

 
ledzep:

Ledzep, thank you very much, it does exactly what I was looking for. Very nice code, I will learn from it.


Thank you again.


Regards.

 

Hello ledzep,


is it possible to modify this code in way that sunday will be counted with monday?


So we can see High Low of Friday on (sunday + monday), and High Low (sunday + monday) on Tuesday?


Thank you

 
ledzep wrote >>

really good work. i emailed you

 
Yet, the interesting question is, why does the original posted by locoloco go mad??? It should be as simple as he did it!
 
Because he is plotting D1 Indicator data on top of a H1 chart . . .
 
    UpperBuf[i] = iHigh(NULL, PERIOD_D1, i+1);
UpperBuf[i] is the current chart. If i is two that's two hours ago and you display the daily high of three days go. You're mixing apples and oranges.
for (int iChart - Bars - 1 - indicatorCounted(); iChart >= 0; iChart--){

    // What daily bar corresponds to this chart's bar?
    int iDay = iBarShift(NULL, PERIOD_D1, Time[iChart]);

    // Get yesterdays daily high.
    UpperBuf[iChart] = iHigh(NULL, PERIOD_D1, iDay + 1);
}
 
lococo:

Hi, I am new to MetaTrader and to its programming language, I have some experience with VB and WealthLab, anyway is not the same. I was wondering if someone could help me with the following idea:

....

I want an indicator that plots the previous day high and low, in any time frame. For example, I use 1h chart, but I need the T-1 1day high-low, and hopefully I could change to any time frame and still getting the T-1 1day high-Low for any T.

....

I will really appreciate your collaboration with this idea.


Regards,

Lococo.



WHRoeder: " You're mixing apples and oranges."

That is exactly what he wants, both; apples AND oranges.

 
You misunderstood. He was using the variable i for BOTH the current chart AND the D1 chart.
Reason: