Metatrader 5 - Recognizing certain bar of current day

 

Hello everyone, I am new user.

I have a doubt, and if someone could help me I would be very grateful.


I want to recognize (intraday chart) certain bar of THIS day


Is it possible to create a function that recognize hour of bar?


I bought the book of Andrew Young, and he creates the class Price, and inside there is a reference "(bar[pShift) .time)". Is it possible to do what I want with that?

Example:

Bar.time(16h).close

Something like this?

Or, there is another option to recognize without using time ?



Thanks so much

 

Hi.

Use the CopyTime function to copy the time of the BARS to an array.

https://www.mql5.com/en/docs/series/copytime


then convert this time value to an mqldatetime variable.

https://www.mql5.com/en/docs/constants/structures/mqldatetime


This MQL struct variable is useful for "splitting" the time into several components like "day" or "hour"

You can now check with a loop if the date/time is switching to a new day.

I hope this information is enough to solve your problem.


Good luck

Documentation on MQL5: Timeseries and Indicators Access / CopyTime
Documentation on MQL5: Timeseries and Indicators Access / CopyTime
  • www.mql5.com
Timeseries and Indicators Access / CopyTime - Reference on algorithmic/automated trading language for MetaTrader 5
 

Thanks for the answer snelle_moda.

I was thinking better, and I notice that working with time is not so simple because each market opens at a given time, and consequently,  it may cause some problems of recognition.

What I am trying to do is a way to recognize the value (price) of closing in the First bar of the day.

Do you suggests some other way to recognize given bar of the day?


Thanks so much

 
gui_moniz:

Thanks for the answer snelle_moda.

I was thinking better, and I notice that working with time is not so simple because each market opens at a given time, and consequently,  it may cause some problems of recognition.

What I am trying to do is a way to recognize the value (price) of closing in the First bar of the day.

Do you suggests some other way to recognize given bar of the day?


Thanks so much

Please show the code with your attempts.
 
angevoyageur:
Please show the code with your attempts.
I am trying to create two conditions:


Using a 15 minutes Chart


1- A Gap (up) between First Bar of day open and last bar of previous day.(15 min chart)


2- Low of the First 4 bars of day higher than High of last bar of previous day. (15 min chart)
   

Condition

1- I started this way:





   double close[];
   ArraySetAsSeries(close,true);
   CopyClose(_Symbol,PERIOD_D1,0,1,close);
   double pbar = close[1];



   double open[];
   ArraySetAsSeries(open,true);
   CopyClose(_Symbol,PERIOD_D1,0,1,open);
   double curbar = open[0];


 
Then:

  Gap = curbar > pbar



The second condition, I don´t know how to copy Low value price of specific time bar.
 

Hi


I have made a custom indicator that detects the LAST bar of the previous day and the first 4 bars of the current day. It draws a line under the LOW prices of these BARS.

You can also use this code for solving your problem for checking the opening gap because you now know which bars are the first bars of the day.

Copy this code into a new custom indicator file and compile it. The first part of the code is not relevant for your problem. It only checks when a new bar has formed.

The relevant part of the code starts at line 125. 



//+------------------------------------------------------------------+
//|                                            Opening_Bar_Check.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
#property indicator_label1  "Relevant BARS"

   int nMaxNumber_Of_Bars = 1750; // Max number of minute BARS in a day plus a little more.

// Chart Time frame period.
   int nChartTimeFrame;

// Indicator buffer array's.
   double Relevant_BARS[];   

// Value for translating point value between index futures and forex pairs.
   double nPointSize_Correction; 

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
   {

// This example only works with a MAX chart timeframe between 1 minute to 1 hour BARS 

// Chart timeframe. 
   nChartTimeFrame = Period();  

// Correct the 1 hour bar timeframe.
   if(nChartTimeFrame > 60)
         {         
         if(nChartTimeFrame == 16385) {nChartTimeFrame = 60;} // Correct the value.         
         }

// Indicator buffers mapping.
   SetIndexBuffer(0,  Relevant_BARS, INDICATOR_DATA);

   MqlTick Price;
   SymbolInfoTick(Symbol(), Price);     

           
   if(Price.bid > 1000) // index future or gold.
         {
         nPointSize_Correction = 1;           
         }
         else if(Price.bid > 10) // stocks or yen/dollar
         {
         nPointSize_Correction = 100;
         }
         else // other forex pairs.
         {
         if(_Digits == 2) {nPointSize_Correction = 100;}                  
         if(_Digits == 3) {nPointSize_Correction = 100;}                     
         if(_Digits == 4) {nPointSize_Correction = 10000;}                 
         if(_Digits == 5) {nPointSize_Correction = 10000;}        
         if(_Digits == 6) {nPointSize_Correction = 10000;}                     
         }

   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[])
   {

// Rates structure array for price data of last two bars.  
   MqlRates mrate[2];  

// Copy data of last to BARS to rates structure array.               
   CopyRates(Symbol(), Period(), 0, 2, mrate);

// NEW BAR CHECK.
//------------------
   static double   dBar_Open;     
   static double   dBar_High;
   static double   dBar_Low;
   static double   dBar_Close;
   static long     lBar_Volume;

// Boolean for confirmation of a new BAR.
   bool bStart_NewBar = false;

// Check if a new BAR has formed if the data of the old BAR is different compared to the data of the new BAR.   
   if(mrate[0].open != dBar_Open || mrate[0].high != dBar_High || mrate[0].low != dBar_Low || mrate[0].close != dBar_Close || mrate[0].tick_volume != lBar_Volume || prev_calculated == 0)
         {
         bStart_NewBar = true;         
     
         dBar_Open   = mrate[0].open;      
         dBar_High   = mrate[0].high;
         dBar_Low    = mrate[0].low;
         dBar_Close  = mrate[0].close;                 
         lBar_Volume = mrate[0].tick_volume;
         }

//---------------------------------------------------

// New BAR has formed, this check is only updated if a new BAR has formed to save CPU resources.   
   if(bStart_NewBar == true)
         {

// Number of BARS that will be used, corrected for your timeframe.          
         int nNumber_Of_Bars_Corrected = nMaxNumber_Of_Bars / nChartTimeFrame;  
                          
         datetime TimeBars[];             // Array for Time Values      
         ArraySetAsSeries(TimeBars,true); // Revert index numbers.  
         CopyTime(Symbol(), Period(), 0, nNumber_Of_Bars_Corrected, TimeBars); // Copy the Time variables to the array.            

// This data array is used for storing the prices of the BARS in the past.
         MqlRates mBars[];             // Rates structure array for price data   
         ArraySetAsSeries(mBars,true); // Revert index numbers.              
         CopyRates(Symbol(), Period(), 0, nNumber_Of_Bars_Corrected, mBars); // Copy data of the BAR prices to data array. 

         int    nIndex_Value_LastBar_PreviousDay = 0; // Index number for data array for detecting the LAST bar of the previous day.
         double dHigh_Value_LastBar_PreviousDay = 0;  // HIGH value of the LAST bar of the previous day.         
         double dOpen_Value_FirstBar_CurrentDay = 0;  // OPEN value of the first bar of the current day.

// Reset the values in the indicator buffer.
         ArrayInitialize(Relevant_BARS, EMPTY_VALUE);  

// Check loop         
         for(int i = 0; i < nNumber_Of_Bars_Corrected; i++)
               {
         
               MqlDateTime PastTimeBars_LastBAR;                // Time data of the LAST BAR in the past
               TimeToStruct(TimeBars[i], PastTimeBars_LastBAR); // Translate time variable into a MQL struct variable. 

               MqlDateTime PastTimeBars_PreviousBAR;                    // Time data of the previous BAR before the LAST BAR in the past.
               TimeToStruct(TimeBars[i + 1], PastTimeBars_PreviousBAR); // Translate time variable into a MQL struct variable. 

// Check start after the first four bars has formed.
// Check if the date of the BAR in is different compared to the date of the previous BAR.               
               if(i > 4 && PastTimeBars_LastBAR.day_of_week != PastTimeBars_PreviousBAR.day_of_week) // Last BAR of previous DAY detected!.
                     {
                                                                               
                     dHigh_Value_LastBar_PreviousDay  = mBars[i + 1].high; // HIGH value of the LAST BAR of the previous DAY.
                     dOpen_Value_FirstBar_CurrentDay  = mBars[i].open;     // OPEN value of the first BAR if the current DAY.
                     nIndex_Value_LastBar_PreviousDay = i + 1;             // Index value for the data array of the LAST BAR of the previous day. 
                     
                     Alert("Bar number = ", nIndex_Value_LastBar_PreviousDay, " is the last BAR of the previous day: Open ",mBars[i + 1].open, " High", mBars[i + 1].high, " Low ", mBars[i + 1].low, " Close ", mBars[i + 1].close);                     
                     Alert("Open price of current DAY is: ", mBars[i].open);
                     break;
                     }               
               }

// Index number for indicator buffer.        
         int y = rates_total - 1 - nIndex_Value_LastBar_PreviousDay; // Index number for the LAST BAR of the previous day.
         
         Relevant_BARS[y]     = mBars[nIndex_Value_LastBar_PreviousDay].low     - 15/nPointSize_Correction; // Last BAR previous DAY.
         Relevant_BARS[y + 1] = mBars[nIndex_Value_LastBar_PreviousDay - 1].low - 15/nPointSize_Correction; // 1e BAR this DAY. 
         Relevant_BARS[y + 2] = mBars[nIndex_Value_LastBar_PreviousDay - 2].low - 15/nPointSize_Correction; // 2e BAR this DAY.         
         Relevant_BARS[y + 3] = mBars[nIndex_Value_LastBar_PreviousDay - 3].low - 15/nPointSize_Correction; // 3e BAR this DAY.         
         Relevant_BARS[y + 4] = mBars[nIndex_Value_LastBar_PreviousDay - 4].low - 15/nPointSize_Correction; // 4e BAR this DAY.
         }
    
   return(rates_total);
   }
 
snelle_moda:

Hi


I have made a custom indicator that detects the LAST bar of the previous day and the first 4 bars of the current day. It draws a line under the LOW prices of these BARS.

You can also use this code for solving your problem for checking the opening gap because you now know which bars are the first bars of the day.

Copy this code into a new custom indicator file and compile it. The first part of the code is not relevant for your problem. It only checks when a new bar has formed.

The relevant part of the code starts at line 125. 



Thank you so much for helping me, snelle_moda.

This indicator will be extremely useful for me, and certainly will solve my problem.


Thank you for everything