is there method to read last candle data from mql4 hst file?

 

hi, I am writing an offline chart in which I get OHLC data from an API.

I convert data to csv and convert the csv to MQLrates and then save it as hst.

this is the first step which is working fine.

now the issue is that sometimes because of network issues or latency or any unknow error there may be a gap in the data, and candles get wrong.


now I want to read the data from the hst file that I create in the first step with FileOpenHistory, and check the last candle data in the history ( without opening the offline chart and read candle 0 data )

and then download OHLC from that latest candle time to the current time from api again and update the hst file.

any suggestions?

I searched and read topics like these but I couldn't fine complete documentation info.

https://www.mql5.com/en/articles/1387

Offline Charts in the New MQL4
Offline Charts in the New MQL4
  • www.mql5.com
Updated MQL4 has the new format for storing historical data and provides the appropriate MqlRates structure for convenient storage of Time, Open, Low, High, Close and Volume values. For many years, traders have developed their MQL4 applications that collect and store their data in HST files for generating offline charts. We can assure you that all previously compiled EX4 files will work in the new MetaTrader 4 terminal the same way as before.
 

You can read the HST file in the same way you write to it. The FileOpenHistory function can be used for writing or for reading. Please reference the link to the documentation.

 
Fernando Carreiro #:

You can read the HST file in the same way you write to it. The FileOpenHistory function can be used for writing or for reading. Please reference the link to the documentation.

hi, thanks for answering. 

my problem is that I don't know how to get the last candle data from that hst file when its converted to Bin format without opening chart.

I think I should get the last pointer position with FileTell(hst-handle)... then use FileReadStruct( at some pointer location )

but the documentation is not clear written.

also I don't want to read whole hst file on every timer ... and convert whole data to candles arrays in memory again, for a simple comparison of only last candle.


in normal conditions , I save the last pointer position ,  download OHLC data from api again for last candle, and then I update the mqlrate struct from that last saved fpos... so last candle updates or one new candle is being gerenated.

but when there is gaps... I have to update more candles than simply last one by comparing data existing in history. 

maybe im confused?   how can I get Time[0] from hst file ? 

 
Farzin Sadeghi Bonjar #: hi, thanks for answering. my problem is that I don't know how to get the last candle data from that hst file when its converted to Bin format without opening chart.

I think I should get the last pointer position with FileTell(hst-handle)... then use FileReadStruct( at some pointer location ). but the documentation is not clear written. also I don't want to read whole hst file on every timer ... and convert whole data to candles arrays in memory again, for a simple comparison of only last candle. in normal conditions , I save the last pointer position ,  download OHLC data from api again for last candle, and then I update the mqlrate struct from that last saved fpos... so last candle updates or one new candle is being gerenated. but when there is gaps... I have to update more candles than simply last one by comparing data existing in history. maybe im confused?   how can I get Time[0] from hst file ? 

It is simple math. You know the header size and you know the record size.

There is only one header and multiple records, so ... [Header size] + [Record size] x [number of records] = [File size] (FileSize).

Calculate the [number of records] and then use FileSeek to exactly go to where you need to start reading the last record.

A quicker and simpler way (assuming the file is not corrupted), to knowing the starting position of the last record is simply [File size] - [Record size].

 
Fernando Carreiro #:

It is simple math. You know the header size and you know the record size.

There is only one header and multiple records, so ... [Header size] + [Record size] x [number of records] = [File size] (FileSize).

Calculate the [number of records] and then use FileSeek to exactly go to where you need to start reading the last record.

A quicker and simpler way (assuming the file is not corrupted), to knowing the starting position of the last record is simply [File size] - [Record size].

great. according to the table in this link for version 401 ... header is 148 bytes.

https://www.mql5.com/en/forum/149178

mqlrates 60 bytes * number of records.

I will test now. thanks.

.hst file format . . . Old and New (Jan 2014) - How to create a digital version of the MT4 and later
.hst file format . . . Old and New (Jan 2014) - How to create a digital version of the MT4 and later
  • 2014.01.29
  • www.mql5.com
Hst file format valid as of mt4 build 509. // lowest price 8 bytes double high. // highest price 8 bytes double close. // symbol name 12 bytes int period. // symbol timeframe 4 bytes int digits. // to be used in future 52 bytes
 
Fernando Carreiro #:

It is simple math. You know the header size and you know the record size.

There is only one header and multiple records, so ... [Header size] + [Record size] x [number of records] = [File size] (FileSize).

Calculate the [number of records] and then use FileSeek to exactly go to where you need to start reading the last record.

A quicker and simpler way (assuming the file is not corrupted), to knowing the starting position of the last record is simply [File size] - [Record size].

nice point :x

 ETHUSD,M15: line:9742 | TF:15m | 64060 record(s) written.| filesize:3843748 Bytes.

(64060*60)+148=3843748   its correct. 

now Im going to write the code you suggested and get the candle data and see if I can write it.

thanks again.


 
You are welcome!