Finding High or Low between two points in time

 

Trying to figure out how to find the high between two points in time.  From January 1st to March 1st.  Going in circles.  Perhaps someone could look at the following code and tell me if I'm headed in the right direction.


Thanks!

//+------------------------------------------------------------------+
//|                                               Quarterly High.mq4 |
//|                                                               me |
//|                                                                  |
//+------------------------------------------------------------------+


#property indicator_chart_window

extern int Hour_Num = 0;
extern int Minute_Num = 0;
extern int Month_Num = 1;
extern int Day_Num = 1;

    int qh = 0;
    
    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----

     
 
    
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
   {
   
    datetime BeginTime, EndTime;
    int Counted_bars=IndicatorCounted();                                                          
    int i=Bars-Counted_bars-1;                                                                    
                                                                                                   
    while(i>=0)                                                                                        
    {    
    
    if( TimeMonth(Time[i]) == Month_Num && TimeDayOfYear(Time[i]) == Day_Num )   
    
    { qh=qh+1;
                                                                                              
     double vqh=iHigh(NULL, PERIOD_MN1, qh);  
                                                                                            
    if(i > 0)   
    { 
    BeginTime = iTime( NULL, PERIOD_MN1, qh );  
                                
    EndTime   = BeginTime + 7776000;                                                                
    }
    
      
    ObjectDelete("Qh_Line");  
    ObjectCreate("Qh_Line", OBJ_TREND, 0, BeginTime, vqh, EndTime, vqh );                           
    ObjectSet("Qh_Line", OBJPROP_COLOR, Magenta);
    ObjectSet("Qh_Line", OBJPROP_STYLE, STYLE_SOLID);
    ObjectSet("Qh_Line", OBJPROP_WIDTH, 3);
    ObjectSet("Qh_Line", OBJPROP_RAY, False);  
    
    }
    i--;  
    ObjectSet("Qh_Line",OBJ_TREND,EndTime);
    
    
    ChartRedraw(ChartID());

     
     
  }
 
//----
   return(0);
}
//+------------------------------------------------------------------+




 
Yellowbeard1781:

Trying to figure out how to find the high between two points in time.  From January 1st to March 1st.  Going in circles.  Perhaps someone could look at the following code and tell me if I'm headed in the right direction.


Thanks!


Try this :)

void OnStart()
{
   string   s           = Symbol();
   int      p           = Period();
   datetime t1          = D'2017.01.01';
   datetime t2          = D'2017.03.01';
   int      t1_shift    = iBarShift(s,p,t1);
   int      t2_shift    = iBarShift(s,p,t2);
   int      bar_count   = t1_shift-t2_shift;
   int      high_shift  = iHighest(s,p,MODE_HIGH,bar_count,t2_shift);
   int      low_shift   = iLowest(s,p,MODE_LOW,bar_count,t2_shift);
   double   high        = iHigh(s,p,high_shift);
   double   low         = iLow(s,p,low_shift);
   Print(t1," -> ",t2,":: High = ",high," Low = ",low);
}
//+------------------------------------------------------------------+
 
nicholishen:

Try this :)

Thanks, nicholishen!  I'm getting closer with the following code.  Need to adapt for Low of the quarter.

//+------------------------------------------------------------------+
//|                                               Quarterly High.mq4 |
//|                                                               me |
//|                                                                  |
//+------------------------------------------------------------------+


#property indicator_chart_window

extern int Hour_Num = 0;
extern int Minute_Num = 0;
extern int Month_Num = 1;
extern int Day_Num = 1;

    int qh = 0;
    
    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----

     
 
    
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
   {
   
    datetime BeginTime, EndTime;
    int Counted_bars=IndicatorCounted();                                                          
    int i=Bars-Counted_bars-1;                                                                    
                                                                                                   
    while(i>=0)                                                                                        
    {    
    
    if( TimeDayOfYear(Time[i]) == Day_Num )   // TimeMonth(Time[i]) == Month_Num && 
    
    { qh=qh+1;
                                                                                              
    
    
 
   
   int CntBarsz = 1441;
   
   int lowshiftl = iLowest(NULL,0,MODE_LOW,CntBarsz,925);
   
    double Minimuml = Low[lowshiftl];                                                           
    datetime lowtimel = Time[lowshiftl];
                        
   int maxshifth = iHighest(NULL,0,MODE_HIGH,CntBarsz,925);
   
    double Maximumh = High[maxshifth];                                                     
    datetime maxtimeh = Time[maxshifth];
        

   for(int j=0;j<CntBarsz-1;j++)                                                                           
     {                                                                                                                       
      if (Low[j]< Minimuml)                                                                          
         {Minimuml = Low[j]; }                                                          
 
                          
      if (High[j]> Maximumh)                                                                                                                     
         {Maximumh = High[j];}                                                                                                            
   
      }
   
    
      
    
    Alert("Maximumh =  ",Maximumh,"     Minimuml =  ",Minimuml,"     ","     J =  ",j,"     CntBarsz =  ",CntBarsz,"     ",DayOfYear());     
    
                                                                                           
    if(i > 0)   
    { 
                                             
    
    
    BeginTime = maxtimeh;              
    EndTime   = BeginTime + 7776000;                                                                
    }
    
      
    ObjectDelete("Qh_Line");  
    ObjectCreate("Qh_Line", OBJ_TREND, 0, BeginTime, Maximumh, EndTime, Maximumh );                           
    ObjectSet("Qh_Line", OBJPROP_COLOR, Magenta);
    ObjectSet("Qh_Line", OBJPROP_STYLE, STYLE_SOLID);
    ObjectSet("Qh_Line", OBJPROP_WIDTH, 3);
    ObjectSet("Qh_Line", OBJPROP_RAY, False); 
    
    
    
       ObjectDelete("DnSymbolBz");
       ObjectCreate("DnSymbolBz", OBJ_ARROW, 0, maxtimeh,Maximumh);         
       ObjectSet("DnSymbolBz", OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
       ObjectSet("DnSymbolBz", OBJPROP_WIDTH,3);       
       ObjectSet("DnSymbolBz", OBJPROP_COLOR,Red);
       ObjectSet("DnSymbolBz", OBJPROP_ANCHOR, ANCHOR_BOTTOM); 
    
    
    
    
    
    
     
    
    }
    i--;  
        
    
    ChartRedraw(ChartID());

     
     
  }
 
//----
   return(0);
}
//+------------------------------------------------------------------+




 
nicholishen:

Try this :)

How would I break down the components  of time, so that I can plot out each occurrence of this period of time, from all the bars on a chart?  In short, I'm trying to plot the high and low of each first quarter going back as far as I can, to 1970.
//+------------------------------------------------------------------+
//|                                               Quarterly High.mq4 |
//|                                                               me |
//|                                                                  |
//+------------------------------------------------------------------+


#property indicator_chart_window

    
    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----

     
 
    
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
   {


   string   s           = Symbol();
   int      p           = Period();
   
   datetime t1          = D'2017.01.01';   
   datetime t2          = D'2017.03.31';   



   MqlDateTime stm;
   TimeToStruct(t1,stm);
   
   Alert("Year: "        +(string)stm.year);
   Alert("Month: "      +(string)stm.mon);
   Alert("Day: "      +(string)stm.day);   
   
   


   int      t1_shift    = iBarShift(s,p,t1);
   int      t2_shift    = iBarShift(s,p,t2);
   
   
   
   int      bar_count   = t1_shift-t2_shift;
   
   
   
   int      high_shift  = iHighest(s,p,MODE_HIGH,bar_count,t2_shift);
   
   int      low_shift   = iLowest(s,p,MODE_LOW,bar_count,t2_shift);
   
   double   high        = iHigh(s,p,high_shift);
   
   double   low         = iLow(s,p,low_shift);
   
  
 
//+------------------------------------------------------------------+



    ObjectDelete("Qh_LineA");  
    ObjectCreate("Qh_LineA", OBJ_TREND, 0, t1, high, t2, high );                           
    ObjectSet("Qh_LineA", OBJPROP_COLOR, Magenta);
    ObjectSet("Qh_LineA", OBJPROP_STYLE, STYLE_SOLID);
    ObjectSet("Qh_LineA", OBJPROP_WIDTH, 3);
    ObjectSet("Qh_LineA", OBJPROP_RAY, False); 




      
    ObjectDelete("Ql_LineA");  
    ObjectCreate("Ql_LineA", OBJ_TREND, 0, t1, low, t2, low );                           
    ObjectSet("Ql_LineA", OBJPROP_COLOR, Aqua);
    ObjectSet("Ql_LineA", OBJPROP_STYLE, STYLE_SOLID);
    ObjectSet("Ql_LineA", OBJPROP_WIDTH, 3);
    ObjectSet("Ql_LineA", OBJPROP_RAY, False); 
    
    
//----
   return(0);
}
//+------------------------------------------------------------------+




 
Yellowbeard1781:
How would I break down the components  of time, so that I can plot out each occurrence of this period of time, from all the bars on a chart?  In short, I'm trying to plot the high and low of each first quarter going back as far as I can, to 1970.

//+------------------------------------------------------------------+
//|                                                       PlotQ1.mq4 |
//|                                                      nicholishen |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <ChartObjects\ChartObjectsLines.mqh>
#include <Arrays\List.mqh>

CList list;
int last_year  = 0;
double price[2]={0};
color  col[2]  ={clrMagenta,clrAqua};

int OnInit()
{
   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[])
{
   string   s           = Symbol();
   int      p           = PERIOD_D1;
   datetime _first      = (datetime)SeriesInfoInteger(s,p,SERIES_FIRSTDATE);
   datetime _last       = (datetime)SeriesInfoInteger(s,p,SERIES_LASTBAR_DATE);
   MqlDateTime first,last;
   TimeToStruct(_first,first);
   TimeToStruct(_last,last);
   if(last.year == last_year)
      return(rates_total);
   list.Clear();
   last_year = last.year;
   if(first.mon > 1)
      first.year++;
   int y1    = first.year;
   int y2    = last.year;
   first.day = 1;
   first.mon = 1;
   last.day  = 30;
   last.mon  = 3;
   for(int yy = y1;yy<=y2;yy++)
   {
      first.year = yy;
      last.year  = yy;
      datetime t1          = StructToTime(first);
      datetime t2          = StructToTime(last);
      int      t1_shift    = iBarShift(s,p,t1);
      int      t2_shift    = iBarShift(s,p,t2);
      int      bar_count   = t1_shift-t2_shift;
      int      high_shift  = iHighest(s,p,MODE_HIGH,bar_count,t2_shift);
      int      low_shift   = iLowest(s,p,MODE_LOW,bar_count,t2_shift);
      price[0]             = iHigh(s,p,high_shift);
      price[1]             = iLow(s,p,low_shift);
      for(int i=0;i<2;i++)
      {
         CChartObjectTrend *line = new CChartObjectTrend;
         line.Create(0,"line"+(string)price[i]+(string)t1,0,t1,price[i],t2,price[i]);
         line.Width(3);
         line.Color(col[i]);
         line.RayRight(false);
         list.Add(line);
      }
      t2+=PeriodSeconds(p)-1;
      Print(t1," -> ",t2,":: High = ",price[0]," Low = ",price[1]);
   }
   return(rates_total);
}
 

Hello all,


This is very interesting to me for I am trying to find the high and low over a variable period of time.

For instance: In M1 chart I would like to know the high and low where the bars (history) should be variable adjustable; 6 bars back or 12 bars back etc.

And this should be possible different charts: M5, M15, M30 etc.


Can this be achieved with the code above?


Sincerely PH

 
PaulHewson:

Hello all,


This is very interesting to me for I am trying to find the high and low over a variable period of time.

For instance: In M1 chart I would like to know the high and low where the bars (history) should be variable adjustable; 6 bars back or 12 bars back etc.

And this should be possible different charts: M5, M15, M30 etc.


Can this be achieved with the code above?


Sincerely PH

You will need to modify it, but yes, all the core components are there. 
 
nicholi shen:

Try this :)

I just want to hop in and say your post helped me a lot to understand how all this works in mql as a newbie and to thank you for it. Also to thank OP for asking the queston.

With the base you provided i was able to achieve exactly what i wanted and learned a lot.


Cheers :)

Reason: