Questions from Beginners MQL5 MT5 MetaTrader 5 - page 375

 
Tapochun:

1. Determine the opening time of the daily candle;

Determine the number of bars num on the TF you want (if you want to check the extremum of the day every 15 minutes - M15 bars);

3. Receive the data high for num bars;

3. In the loop, compare all the values of high bars with the variable dayHigh for the desired day candle (initially dayHigh = 0);

4. If high > dayHigh, you store the new value in the dayHigh variable;

This is understandable, thanks. But so far it is complicated for me... or rather point 2, it should be calculated for each TF and put in ranges to find the number of candlesticks?

Can't we just find out the date of the current timeframe and look through all values for the date, without counting the number of candles that have passed?

 
-Aleks-:

That's understandable, thank you. But so far it's complicated for me... or more precisely point 2, is it to do a calculation for each TF and put it into ranges to find the number of candlesticks?

Can't we just find out the date of the current timeframe and look through all values for the date, without determining the number of candles that have passed?

It is easy to determinethe number of bars within the day. For example, for a 15-minute chart. Divide the number of minutes in one day by 15 (1440 / 15 = 96 bars or another PERIOD_D1/PERIOD_M15 = 96)
 
Vitalii Ananev:
The number of intraday bars is simple to determine. For example for a 15 minute chart. Divide the number of minutes in one day by 15 (1440 / 15 = 96 bars or another PERIOD_D1/PERIOD_M15 = 96)
You should not do this! Never accept as a fact that the number of bars in any given day on the TF will be the same!
 
Vitalii Ananev:
It is easy to determine the number of bars in a day. For example for 15 min chart. Divide the number of minutes in one day by 15 (1440 / 15 = 96 bars or another PERIOD_D1/PERIOD_M15 = 96).

I understand that mathematically, but how to do it with functions - that's the question! Suppose I get the current time using Time, and then how

I think to make a counter for each TF, with a new bar, which will be reset at the start of a new day, and reset to zero for a day maximum, and before that to do a comparison of each bar, but how to program it?

 
Tapochun:
You should not do that! It should never be taken as a fact that the number of bars on any given day by TF will be the same!

Yes, for example there may be an early closing time on a Friday...

So you have to define the day, and take an array to find the maximum for the day without taking into account the number of candlesticks, how do you do that?

 
-Aleks-:

I understand that mathematically, but how to do it with functions - that's the question! Suppose I get the current time using Time, and then how

I think I should make a counter for each TF, with a new bar, which will be reset to zero in case of a new day. I want to reset it to zero for one day maximum and compare each bar before that; but how should I program it?

This is approximately how we can find the first bar corresponding to the beginning of the day.

 datetime BeginDay = iTime(Symbol(), PERIOD_D1, iBarShift(Symbol(), PERIOD_D1, Time[NumberBar]));  

 int StartBar = iBarShift(Symbol(), 0, BeginDay);
 
Vitalii Ananev:

This is roughly how to find the first bar corresponding to the start of the day.

For example we have this algorithm.

We have found the first bar - at the moment when it is formed this bar will be the high of the day. Then we get the next bar and check its maximum, if it is higher than the previous one, it means it is a new high of the day. And so on till the last bar of the day.

 
#property script_show_inputs
#property strict
//+------------------------------------------------------------------+
//| Входные параметры                                                |
//+------------------------------------------------------------------+
input ENUM_TIMEFRAMES inpUserTimeframe=PERIOD_M15;   // Пользовательский ТФ
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
// Определяем время открытия текущего дня
   datetime open_D1=iTime(_Symbol,PERIOD_D1,0);

// Определяем номер первого сформированного бара по пользовательскому ТФ за текущий день
   int last_utf = iBarShift( _Symbol, inpUserTimeframe, open_D1, true );
   if( last_utf == -1 )
     {
      Alert(EnumToString(inpUserTimeframe)+" ОШИБКА #",GetLastError(),": номер бара не определен! "+TimeToStr(open_D1));
      return;
     }

// Определяем максимум дня
   double dayHigh=0;
   double high;
   int highNum=0;

   for(int i=1; i<=last_utf; i++) // Цикл по сформированным свечам за текущий день
     {
      high = iHigh( _Symbol, inpUserTimeframe, i );   // Максимум на i свече
      if( high > dayHigh )                            // Если максимум дня превышен
        {
         dayHigh = high;                              // Запоминаем новый максимум
         highNum = i;                                 // Запоминаем номер свечи
        }
     }

   Alert(EnumToString(inpUserTimeframe)+": максимум дня = ",DoubleToString(dayHigh,_Digits)," обнаружен на свече #",highNum);
  }
Files:
toAleks.mq4  3 kb
 
Tapochun, Vitalii Ananev- Thank you! I will now try to apply what I have learned.
 
Tapochun:
Can I see an indicator instead of a script? I'm just having a hard time with all these time arrays... I'd like to learn from an example.
Reason: