How do I get Open,Low,High,Close parameters in MQL5? - page 10

 
//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   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 q = rates_total;
   static datetime w = 0;
   
   if(w != time[q-1])
    { 
     w = time[q-1];
     Print("-------------------------------------------------------------------------------");
     Print("  time[q-1]  ",time[q-1],"  time[q-2]  ",time[q-2],"  time[q-3]  ",time[q-3],"  time[q-4]  ",time[q-4],"  time[q-5]  ",time[q-5]);
     Print("  open[q-1]  ",open[q-1],"  open[q-2]  ",open[q-2],"  open[q-3]  ",open[q-3],"  open[q-4]  ",open[q-4],"  open[q-5]  ",open[q-5]);
     Print("  high[q-1]  ",high[q-1],"  high[q-2]  ",high[q-2],"  high[q-3]  ",high[q-3],"  high[q-4]  ",high[q-4],"  high[q-5]  ",high[q-5]);
     Print("  low[q-1]  ",low[q-1],"  low[q-2]  ",low[q-2],"  low[q-3]  ",low[q-3],"  low[q-4]  ",low[q-4],"  low[q-5]  ",low[q-5]);
     Print("  close[q-1]  ",close[q-1],"  close[q-2]  ",close[q-2],"  close[q-3]  ",close[q-3],"  close[q-4]  ",close[q-4],"  close[q-5]  ",close[q-5]);
    }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I wonder why a bar with index [rates_total - 1] always has all open, close, high, low values equal? Doesn't the index open[rates_total - 1] correspond to the index 1 in the timeseries? That is, it is the same as Open[1], Close[1],... in MT4.

Although, maybe index [rates_total - 1] corresponds to index 0 in timeseries? Then it means that [rates_total - 1] is the current bar and because it has not yet formed, its OCHL parameters are equal?

 
Реter Konow:

I wonder why a bar with index [rates_total - 1] always has all open, close, high, low values equal? Doesn't the index open[rates_total - 1] correspond to the index 1 in the timeseries? That is, it is the same as Open[1], Close[1],... in MT4.

Although, maybe the index [rates_total - 1] corresponds to the index 0 in the timeseries? Then it means that [rates_total - 1] is the current bar and because it has not yet formed, its OCHL parameters are equal?

By default the MQL5 indicators have the following indexing (by the example of "0" and "rates_total-1")

/*
   time[0]              -> D'2018.01.02 00:00:00'
   time[rates_total-1]  -> D'2018.12.31 22:00:00'
*/

As you can see the rightmost bar in the chart has the "rates_total-1" index. But index "0" will correspond to the leftmost bar in the chart.

 
Vladimir Karputov:

By default the MQL5 indicators have the following indexing (using "0" and "rates_total-1" as examples)

As you can see, the rightmost bar in the chart has the "rates_total-1" index. The "0" index will correspond to the leftmost bar in the chart.

The one to the right is the current one? The one that has not formed?

In general, as I understand it, rates_total-1 points to the current bar, and since it has not formed, its OHCL parameters in the timers are equal.

 
Реter Konow:
The rightmost one - that is the current one? The one that has not formed?

Look at any chart - the rightmost bar on the GUIDE is the current bar, which is in the process of forming (i.e. all its prices and volume can be changed at any time).

 
Vladimir Karputov:

Look at any chart - the rightmost bar on the GUIDE is the current bar, which is in the process of forming (i.e. all its prices and volume can be changed at any time).

Yes, I understand that. I just didn't realise that it has all the values in the timeseries equal before it gets an index of 1. That is, it stops being current.
 
Реter Konow:
Yes, I understand that. I was just surprised that it has all values equal before it gets index 1 in the timeseries. That is, it stops being current.

What is the other index one? The rightmost bar on the chart in the MQL5 indicators has an index of "rates_total-1".

 
Vladimir Karputov:

What is the other index one? The rightmost bar on the chart in the MQL5 indicators has an index of "rates_total-1".

You don't understand what I'm talking about. rates_total-1 is the current bar. I know that. BUT. THE OCHL VALUES IN THE TIMESERIES ARE EQUAL. Check the code I posted. That's exactly what I didn't know.
 
Реter Konow:
You don't understand what I'm talking about. rates_total-1 THIS IS the current bar. I know that. BUT. THE OCHL VALUES IN THE TIMESERIES ARE EQUAL. Check the code I posted. That's exactly what I didn't know.
Your code only takes the values at the moment the bar opens
 
Реter Konow:
You do not understand what I am talking about. rates_total-1 THIS is the current bar. I know that. BUT. THE OCHL VALUES IN THE TIMESERIES ARE EQUAL. Check the code I posted. That's exactly what I didn't know.

The current bar (the rightmost on the chart) will have EQUAL prices at the moment the bar is born - this is the basics: the bar has just been born and all prices are the same. With the arrival of new ticks the prices of the current bar (the rightmost bar in the chart) will start to change.


I hope it is clear? At the moment of bar birth and its OHLC are equal.

 
Andrey Barinov:
Your code only takes values when the bar opens

Ouch, hasty, the man himself should have realised that ...

Reason: