how to get the opening value of the first candle of the previous day in period m15

 
hey how toget the opening value of the first candle of the previioius day in period m15 with MQ5?
 
galdinotrader :
hey how toget the opening value of the first candle of the previioius day in period m15 with MQ5?

Algorithm:

we get the opening time of the previous candle on PERIOD_D1 (we request two elements ) - we get the opening time of the current and previous day

we get the opening time of the first candle at PERIOD_M15 (we request from the time of the previous day's opening, we request one element )

//+------------------------------------------------------------------+
//|                                                      Test_en.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   MqlRates rates_D1[],rates_M15[];
   ArraySetAsSeries(rates_D1,true);  // rates_D1[0] - current day, rates_D1[1] - previous day
   ArraySetAsSeries(rates_M15,true);
   int start_pos=0,count=2;
   if(CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_D1)!=count)
      return;
   count=1;
   if(CopyRates(Symbol(),PERIOD_M15,rates_D1[1].time,count,rates_M15)!=count)
      return;
//---
   Print("first bar M15 previous day opened ",TimeToString(rates_M15[0].time),", price open ",DoubleToString(rates_M15[0].open,Digits()));
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
Files:
Test_en.mq5  3 kb
 
Vladimir Karputov:

Algorithm:

we get the opening time of the previous candle on PERIOD_D1 (we request two elements ) - we get the opening time of the current and previous day

we get the opening time of the first candle at PERIOD_M15 (we request from the time of the previous day's opening, we request one element )

Thx but i got the following questions: how to  get the closing value of the first candle of the current day in m15 period?

rates_M15[0]  represents the first candle of today or the day before?

 

As the first tick of a day is the identical opening price for all candles up to D1, yesterday's first M15 opening and D1 open share the same price, so why not simply take

open_firstM15_yesterday=iOpen(_Symbol,PERIOD_D1,1);

?

"Thx but i got the following questions: how to  get the closing value of the first candle of the current day in m15 period?"

Here it get a little more complicated. Maybe what helps is the fact that - if we go back in time - this is also the last M15 candle that has a candle time after or identical to todays D1 opening, so you can try this:

int index=0;
while (iTime(_Symbol,PERIOD_M15,index+1)>=iTime(_Symbol,PERIOD_D1,0)){index++;}
double close_firstM15_today=iClose(_Symbol,PERIOD_M15,index);

// (not tested)
 
galdinotrader :

Thx but i got the following questions: how to  get the closing value of the first candle of the current day in m15 period?

We use a similar algorithm:

we get the opening time of the current candle on PERIOD_D1 (we request one element ) - we get the opening time of the current

we get the first candle at PERIOD_M15 (we  request from the time of the current day's opening, we request one element  )

//+------------------------------------------------------------------+
//|                                                      Test_en.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.001"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   MqlRates rates_D1[],rates_M15[];
   ArraySetAsSeries(rates_D1,true);
   ArraySetAsSeries(rates_M15,true);
   int start_pos=0,count=1;
   if(CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_D1)!=count)
      return;
   if(CopyRates(Symbol(),PERIOD_M15,rates_D1[0].time,count,rates_M15)!=count)
      return;
//---
   Print("first bar M15 current day closed ",TimeToString(rates_M15[0].time),", price open ",DoubleToString(rates_M15[0].close,Digits()));
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
Files:
Test_en.mq5  3 kb
 
Vladimir Karputov:

We use a similar algorithm:

we get the opening time of the current candle on PERIOD_D1 (we request one element ) - we get the opening time of the current

we get the first candle at PERIOD_M15 (we  request from the time of the current day's opening, we request one element  )

solved! thank you very much

Reason: