Get Open Price of Current Timeframe

 

I recently formatted my computer and lost a script code that did this function. I tried to recreate it but was unsuccessful.

it was like this


datetime now = TimeCurrent();
datetime nowTF = now - now % (60 * time[0]);
datetime period = iBarShift(NULL,0, nowTF);
datetime openprice = iOpen(Symbol(),PERIOD_M5,period);
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
guilherme15990:

I recently formatted my computer and lost a script code that did this function. I tried to recreate it but was unsuccessful.

it was like this


datetime now = TimeCurrent();
datetime nowTF = now - now % (60 * time[0]);
datetime period = iBarShift(NULL,0, nowTF);
datetime openprice = iOpen(Symbol(),PERIOD_M5,period);

This will print the open price of the current timeframe


 Print("Open Price is " + DoubleToString(iOpen(NULL,0,0),Digits()));

or put it in a variable

 double OPrice = iOpen(NULL,0,0);
 
guilherme15990:

I recently formatted my computer and lost a script code that did this function. I tried to recreate it but was unsuccessful.

it was like this


datetime now = TimeCurrent();
datetime nowTF = now - now % (60 * time[0]);
datetime period = iBarShift(NULL,0, nowTF);
datetime openprice = iOpen(Symbol(),PERIOD_M5,period);

int period = iBarShift(NULL,0, nowTF);

should be integer

 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. int period = iBarShift(NULL,0, nowTF);
    A shift is not a datetime, it's an int as Sardion stated.

  3. datetime nowTF = now - now % (60 * time[0]);
    And that is a very big number; result total garbage. You want:
    datetime nowTF = now - now % PeriodSeconds(PERIOD_H1);
              Find bar of the same time one day ago - MQL4 programming forum 2017.10.06

  4. int      period = iBarShift(NULL,0, nowTF);
    datetime openprice = iOpen(Symbol(),PERIOD_M5,period);
    You are getting the shift for the current chart but using it on the M5 chart. You are mixing apples and oranges.


 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. A shift is not a datetime, it's an int as Sardion stated.

  3. And that is a very big number; result total garbage. You want:           Find bar of the same time one day ago - MQL4 programming forum 2017.10.06

  4. You are getting the shift for the current chart but using it on the M5 chart. You are mixing apples and oranges.


I solved my problem with the code below


datetime tempo = TimeCurrent();
string DataHoraSegundosAtual = TimeToString(tempo,TIME_DATE|TIME_SECONDS);
string DataHoraSegundosDia = TimeToString(tempo,TIME_DATE) + " 00:00:00";
int diferenca = Bars(Symbol(),PERIOD_CURRENT,DataHoraSegundosDia,DataHoraSegundosAtual);
int BarraDeAbertura = iBarShift(NULL,0, DataHoraSegundosDia);
float PrecoDeAbertura = iOpen(Symbol(),PERIOD_CURRENT,BarraDeAbertura);
printf(PrecoDeAbertura);
Reason: