Prossible -> Read data from history file

 

Hello, i trie to read data´s high,low,open,close... from the 1 min History chart, but it is not understandable for me how I can select the values that i realy ned, with my script example i just get so much values i dont know what to do with them, can one of you show me please how i can read the values high,low,open,close,time and value from a .hst history file in.


my script example is that:


  int handlwe = FileOpenHistory("EURUSD1.HST",FILE_BIN|FILE_CSV|FILE_READ);
  if(handlwe<1)
    {
     Print("Cannot read file EURUSD1.HST");
     return(false);
    }
    else
    {
     for(int iw=0; iw<90; iw++)
     {
     Print(FileReadInteger(handlwe,LONG_VALUE));
     Print(FileReadDouble(handlwe,DOUBLE_VALUE)); 
     Print(FileReadNumber(handlwe));
     Print(FileReadString(handlwe));
     }
    }
  FileClose(handlwe);
But the results are very confused..
 

The history files are not updated real time.

Open the history folder in Explorer and check the "modified date".

I think the bars for this MetaTrader "session" are in memory, and aren't written until MT4 does a clean exit,
or some other trigger to save to history occurs.

For example, I have a 15 minute Gold chart I opened today, but there is no 15 min Gold file in
my history folder, at all.

It is now 9:35pm, and the latest history file update was at 1:09 this afternoon, on EURUSD daily.

---

As for reading a HST file, you also need to know the format of the file:

First comes a header, then the individual bar data.

Here is a sample script that reads the header and the first few bars of data. The data read can be in a loop, of course.
This code was just used for verifying headers.

int start()
  {
 string filename = "GBPJPY.hst";
 int i_unused[30];
   
 int FILE = FileOpenHistory(filename, FILE_READ|FILE_BIN);
 FileSeek(FILE, 0, SEEK_SET);
 
 int version    = FileReadInteger (FILE, LONG_VALUE);
 string c_copyright  = FileReadString (FILE, 64);
 string name    = FileReadString (FILE, 12);
 int period     = FileReadInteger (FILE, LONG_VALUE);
 int i_digits   = FileReadInteger (FILE, LONG_VALUE);
 int timesign    = FileReadInteger (FILE, LONG_VALUE);       //timesign
 datetime last_sync   = FileReadInteger (FILE, LONG_VALUE);       //last_sync
 FileReadArray (FILE, i_unused, 0, 13);
 
 Print("Version = ", version);
 Print("c_copyright = ", c_copyright);
 Print("Equity = ", name);
 Print("period = ", period);
 Print("i_digits = ", i_digits);
 Print("timesign = ", TimeToStr(timesign, TIME_DATE|TIME_SECONDS));
 Print("last_sync = ", last_sync);
 Print("i_unused = ", i_unused[0]);
 Print("i_unused = ", i_unused[1]);
 Print("i_unused = ", i_unused[2]);
 Print("i_unused = ", i_unused[3]);
 Print("i_unused = ", i_unused[4]);
 Print("i_unused = ", i_unused[5]);
 Print("i_unused = ", i_unused[6]);
 Print("i_unused = ", i_unused[7]);
 Print("i_unused = ", i_unused[8]);
 Print("i_unused = ", i_unused[9]);
 Print("i_unused = ", i_unused[0]);
 Print("i_unused = ", i_unused[11]);
 Print("i_unused = ", i_unused[12]);
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 FileClose(FILE);   
 return(0);
}
 
Hello Phy, thank you for the example, I think it will be very exakt helpfull and tried it, and its god to know from you that the datas there in this files dont be updated realtime.
 
phy:

The history files are not updated real time.

Open the history folder in Explorer and check the "modified date".

I think the bars for this MetaTrader "session" are in memory, and aren't written until MT4 does a clean exit,
or some other trigger to save to history occurs.

For example, I have a 15 minute Gold chart I opened today, but there is no 15 min Gold file in
my history folder, at all.

It is now 9:35pm, and the latest history file update was at 1:09 this afternoon, on EURUSD daily.

---

As for reading a HST file, you also need to know the format of the file:

First comes a header, then the individual bar data.

Here is a sample script that reads the header and the first few bars of data. The data read can be in a loop, of course.
This code was just used for verifying headers.

int start()
  {
 string filename = "GBPJPY.hst";
 int i_unused[30];
   
 int FILE = FileOpenHistory(filename, FILE_READ|FILE_BIN);
 FileSeek(FILE, 0, SEEK_SET);
 
 int version    = FileReadInteger (FILE, LONG_VALUE);
 string c_copyright  = FileReadString (FILE, 64);
 string name    = FileReadString (FILE, 12);
 int period     = FileReadInteger (FILE, LONG_VALUE);
 int i_digits   = FileReadInteger (FILE, LONG_VALUE);
 int timesign    = FileReadInteger (FILE, LONG_VALUE);       //timesign
 datetime last_sync   = FileReadInteger (FILE, LONG_VALUE);       //last_sync
 FileReadArray (FILE, i_unused, 0, 13);
 
 Print("Version = ", version);
 Print("c_copyright = ", c_copyright);
 Print("Equity = ", name);
 Print("period = ", period);
 Print("i_digits = ", i_digits);
 Print("timesign = ", TimeToStr(timesign, TIME_DATE|TIME_SECONDS));
 Print("last_sync = ", last_sync);
 Print("i_unused = ", i_unused[0]);
 Print("i_unused = ", i_unused[1]);
 Print("i_unused = ", i_unused[2]);
 Print("i_unused = ", i_unused[3]);
 Print("i_unused = ", i_unused[4]);
 Print("i_unused = ", i_unused[5]);
 Print("i_unused = ", i_unused[6]);
 Print("i_unused = ", i_unused[7]);
 Print("i_unused = ", i_unused[8]);
 Print("i_unused = ", i_unused[9]);
 Print("i_unused = ", i_unused[0]);
 Print("i_unused = ", i_unused[11]);
 Print("i_unused = ", i_unused[12]);
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Time    = ", FileReadInteger (FILE, LONG_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Price    = ", FileReadDouble (FILE, DOUBLE_VALUE));
 Print("Volume   = ", FileReadDouble (FILE, DOUBLE_VALUE));
 FileClose(FILE);   
 return(0);
}

Now I can read the datas, but the aktuallest are at the end from the file, is it possible to read the file from bottom to top?

 

Yes, using FileSeek() to set the file pointer to the starting point of the record you want to read.

Open the file.

Use FileSeek() to go to the end of the file.

Use FileSeek() to step back the length of one record. Read it.

Continue stepping backwards X records and reading until you have read the data you need.

--
Another way, read all the data records into an array and read from the array.

 
phy wrote >>

As for reading a HST file, you also need to know the format of the file:

First comes a header, then the individual bar data.

Here is a sample script that reads the header and the first few bars of data. The data read can be in a loop, of course.
This code was just used for verifying headers.

phy, is there any chance you could provide a similar code-snippet for reading through the header and into the first few bars of data in an FXT file?

I've tried coding my owner FXT header parser starting with the FXT header generator (the include file) as well as the readandcheck routine but I'm not having much luck with it (a reflection of my coding inadequacies no doubt). Many thanks in advance for your time and assistance here.

 
  string FileName="EURUSD240.hst";
  Print(Handle);
  if(Handle<1) 
    { 
      Print("Error:",FileName);
      return 0;
    }   
    Print("This file is open",FileName);


This is my code but always return -1 WHY??????