Questions from Beginners MQL5 MT5 MetaTrader 5 - page 530

 
-Aleks-:

Please help! (MQL4)

I need to get the opening time of the Day on M15 chart taking into account each bar - for the indicator.

I decided to calculate in seconds, taking into account the current bar (0), but I must be mistaken.

D_Shift=(Open_time-pos*15*60-(Open_time-Open_timeTF))/1440*60;

If for the indicator, then:

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[])
  {
   int            temp_day;
   MqlDateTime    dt_struct;
   TimeToStruct(time[0],dt_struct);
   temp_day=dt_struct.day;

   for(int i=0;i<rates_total-1;i++)
     {
      TimeToStruct(time[i],dt_struct);
      if(dt_struct.day<temp_day)
        {
         Print("Start day ",time[i-1]);
         break;
        }
     }

//--- done
   return(rates_total);
  }

But: this code will be executed each time OnCalculate() is entered - depending on your needs, you can add bool flag or place code in separate function.

 
Karputov Vladimir:

If for indicator, then:

But: this code will be executed each time OnCalculate() - depending on your needs, or add a bool flag or place the code in a separate function.

Thanks, but at every tick to do recalculation is not good - I need to analyze your code, however, while I want to understand why my code does not work, in the log are returned strange numbers

Open_timeTF 1454715900
Open_time 1454284800
Delta_1S 431100
Delta_M15 479

it turns out that there is a difference of 479 bars between the first bar of the day and the current bar - why is that?

 
-Aleks-:

Thanks, but recalculating on every tick is not good - I need to think about your code, however, while I want to understand why my code does not work, the log returns strange numbers

Open_timeTF 1454715900
Open_time 1454284800
Delta_1S 431100
Delta_M15 479

it turns out that there is a difference of 479 bars between the first bar of the day and the current bar - why is that?

I did indicate that I'm not psychic and that's why:

But: this code will be executed each time OnCalculate() is entered- here depending on your needs either add abool flagor put the code in a separate function.

 
Karputov Vladimir:

I did point out that I'm not psychic and that's why:

Why is psychic ability required here? There are variables in the code above, I showed what those variables return in numbers and asked why this is the case and not the other way around - according to the code.
 
Karputov Vladimir, I don't understand your code - because I'm a beginner in this business, Ihaven't used OnCalculate function before, so I can't understand whatrates_totalis calculated there, and most importantly, where to return it? I don't understand how to make it all into a separate function either... it's hard to understand without comments.
 
-Aleks-:
Karputov Vladimir, I don't understand your code - I'm a beginner in this business and Ihaven't used functionOnCalculate before, so I can't understand whatrates_totalis calculated there and, more importantly, where it should be returned to? I don't understand how to make it all into a separate function either... it's hard to understand without comments.

Here is the full code of the MQL4 indicator:

//+------------------------------------------------------------------+
//|                                                    Start day.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//--- input parameters
input int      timer=10; // seconds
//--- parameter
bool           IsDefineTime=true; // true -> you can define time
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   EventSetTimer(timer);
//---
   IsDefineTime=true;
//---
   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[])
  {
//---
   int            temp_day;
   MqlDateTime    dt_struct;
   TimeToStruct(time[0],dt_struct);
   temp_day=dt_struct.day;

   if(IsDefineTime)
     {
      for(int i=0;i<rates_total-1;i++)
        {
         TimeToStruct(time[i],dt_struct);
         if(dt_struct.day<temp_day)
           {
            Print("Start day ",time[i-1]);
            IsDefineTime=false;
            break;
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   IsDefineTime=true; // true -> you can define time
  }
//+------------------------------------------------------------------+

Algorithm of its work: When you start indicator, you set timer time interval (for example, 10 seconds) - it means that every 10 seconds timer will be triggered:

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   IsDefineTime=true; // true -> you can define time
  }

and theIsDefineTime variable is assigned the value true. What is it for? When IsDefineTime=true we can, in OnCalculate(), enter a loop to determine the start time of the day:

   if(IsDefineTime)
     {
      for(int i=0;i<rates_total-1;i++)
        {
         TimeToStruct(time[i],dt_struct);
         if(dt_struct.day<temp_day)
           {
            Print("Start day ",time[i-1]);
            IsDefineTime=false;
            break;
           }
        }
     }

In this loop We start traversing the time[] array (this array stores the opening time values) from the rightmost bar (its index is "0"). When the beginning of the day is found, we unset its value and assign to the variableIsDefineTime the valuefalse - in this way we protect ourselves from further entering into the cycle of determining the starting time of the day, until the timer runs again.

Files:
Start_day.mq4  3 kb
 

Karputov Vladimir , thanks for the clarification, however, how does this solve the problem?

Roughly speaking we need to know the bar of the current timeframe that corresponds to the bar at the beginning of the day. We need to know on each bar and calculate and draw the indicator accordingly.

 
-Aleks-:

Karputov Vladimir , thanks for the clarification, however, how does this solve the problem?

Roughly speaking we need to know the bar of the current timeframe that corresponds to the bar at the beginning of the day. You need to know it on every bar and calculate and draw the indicator accordingly.

That's how you asked and that's how you were answered :) . And if you want not the time of the first bar of this day, but the index of this bar - it is here, when you print the beginning of the day:

.
.
.
Print("Start day ",time[i-1]);
.
.
.

You use the index [i-1] - this is the number of the first bar of the day.

 
Karputov Vladimir:

As you asked, you got your answer :) . And if you need not the time of the first bar of the day, but the index of this bar, it is here, when you print the beginning of the day:

You use the index [i-1] - this is the number of the first bar of the day.

I agree - I asked confusingly.

There's an indicator in the app that I made, but it doesn't count correctly. I'm terribly dumb and do not understand, even after your explanations, how to make it correctly determine the index corresponding to the first bar at the beginning of the day.

I apologize for the insolence, but maybe you can show me how to combine your code and mine?

 

-Aleks-:

How do I get it to correctly detect the index corresponding to the first bar at the start of the day?

It's a bit of a mess:

datetime Day00=86400*(TimeCurrent()/86400);//начало дня
int bar_shift=iBarShift(_Symbol,PERIOD_CURRENT,Day00);//индекс первого бара текущего дня
Reason: