mql4 how to get datetime of iHigh?

 
double highestPrice = iHigh(Symbol(),0,iHighest(NULL,0,MODE_HIGH,1900,2));

How to get what is the datetime of the iHigh in format YYYY-mm-dd 03:12:01?

 

I think answer is :

datetime dtTime = iTime(_Symbol,_Period, highestPrice);
 
Eng Keat Ang:

How to get what is the datetime of the iHigh in format YYYY-mm-dd 03:12:01?

If you already know the shift (bar/candle) of the high that you're interested in, which is necessary if you're calling iHigh, then what you need is to call iTime for that shift to retrieve the datetime value, and then call TimeToString with the 2nd parameters being TIME_DATE|TIME_SECONDS and save it into a string variable, which will look like:

YYYY.mm.dd hh:mm:ss

For the specific format that you want, you would have to do a StringReplace and replace "." with "-"

Another alternative is to use the MqlDateTime struct. This is done by first getting the datettime value, declaring a variable of MqlDateTime type, then calling TimeToStruct to fill the values of the MqlDateTime variable with the datetime value.

This method is recommend if you require more advanced date formatting.

https://www.mql5.com/en/docs/constants/structures/mqldatetime
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Date Type Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Date Type Structure
  • www.mql5.com
Date Type Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
double highestPrice= iHigh(Symbol(),0,iHighest(NULL,0,MODE_HIGH,1900,2));
double lowestPrice= iLow(Symbol(),0,iLowest(NULL,0,MODE_LOW,1900,2));

datetime highestDateTime = iTime(_Symbol,_Period, highestPrice ); // this output 2015.12.01 03:05
datetime lowestDateTime = iTime(_Symbol,_Period, lowestPrice ); // this output 1420141800 

How to make lowestDateTime be the same format as highestDateTime?

 
Eng Keat Ang #:

How to make lowestDateTime be the same format as highestDateTime?

From the code you posted, highestDateTime is the same format as lowestDateTime. This is because they're both of the datetime type.

datetime values in MQL are seconds elapsed starting from January 1st, 1970.

To convert it to something we can understand, therefore, you must use TimeToString

string highestDate = TimeToString(highestDateTime); // date in format of YYYY.mm.dd hh:mm
string lowestDate  = TimeToString(lowestDateTime);  // date in format of YYYY.mm.dd hh:mm

https://www.mql5.com/en/docs/convert/timetostring

Documentation on MQL5: Conversion Functions / TimeToString
Documentation on MQL5: Conversion Functions / TimeToString
  • www.mql5.com
TimeToString - Conversion Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Alexander Martinez #:

From the code you posted, highestDateTime is the same format as lowestDateTime. This is because they're both of the datetime type.

datetime values in MQL are seconds elapsed starting from January 1st, 1970.

To convert it to something we can understand, therefore, you must use TimeToString

https://www.mql5.com/en/docs/convert/timetostring

Thank you very much.

 
Eng Keat Ang #:

I think answer is : 

datetime dtTime = iTime(_Symbol,_Period, highestPrice);

This return the latest 2nd bar time, not return the highest bar time. How to return the highest bar time?

 
Eng Keat Ang #:

This return the latest 2nd bar time, not return the highest bar time. How to return the highest bar time?

Ok found the solution :

double HighPrice = iHigh(Symbol(),0,iHighest(NULL,0,MODE_HIGH,1900,2));
double LowPrice = iLow(Symbol(),0,iLowest(NULL,0,MODE_LOW,1900,2));

int forHighTime   = iHighest( NULL,0,MODE_HIGH,1900);  
int forLowTime   = iLowest( NULL,0,MODE_LOW,1900); 
datetime highestDateTime = iTime(_Symbol,_Period, forHighTime);
datetime lowestDateTime = iTime(_Symbol,_Period, forLowTime);
 
Eng Keat Ang #:

Thank you very much.

Glad it could help. 👍

Reason: