SaveHistoryToHST

Author: avoitenko
Miscellaneous
Uses files from the file systemIt writes information to fileIt writes information to fileIt writes information to fileIt writes information to file
0 Views
0 Downloads
0 Favorites
SaveHistoryToHST
//+------------------------------------------------------------------+
//|                                             SaveHistoryToHST.mq5 |
//|                                                        avoitenko |
//|                        https://login.mql5.com/ru/users/avoitenko |
//+------------------------------------------------------------------+
#property copyright "avoitenko"
#property link      "https://login.mql5.com/ru/users/avoitenko"
#property version   "1.00"

#property script_show_inputs

#define OFFLINE_HEADER_SIZE 148 // LONG_VALUE + 64 + 12 + 4 * LONG_VALUE + 13 * LONG_VALUE
#define OFFLINE_RECORD_SIZE 44  // 5 * DOUBLE_VALUE + LONG_VALUE

input uint DATA_COUNT=5000;  // History depth in bars

MqlRates rates[];
//+------------------------------------------------------------------+
//|   OnStart()                                                      |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- checking of DATA_COUNT input parameter
   if(DATA_COUNT==0)
     {
      printf("Error, invalid value of  DATA_COUNT (%d), valid values: >0",DATA_COUNT);
      return;
     }
//--- checking of data
   uint count=CopyRates(_Symbol,_Period,0,DATA_COUNT,rates);
   if(count<DATA_COUNT)
     {
      printf("Insufficient data in history (%d), %d bars needed",count,DATA_COUNT);
      return;
     }

   int period=PeriodSeconds(_Period)/60;

//--- save header of HST file
   if(!WriteOfflineHeader("!"+_Symbol,period,_Digits)) return;

//--- save data to HST file
   for(uint i=0;i<DATA_COUNT;i++)
     {
      if(!WriteOfflineBar("!"+_Symbol,period,0,rates[i]))return;
     }
//--- show path to saved file
   Print("Path to saved file: ",TerminalInfoString(TERMINAL_DATA_PATH),"MQL5\\Files\\",OfflineFileName("!"+_Symbol,period));
  }
//+------------------------------------------------------------------+
bool WriteOfflineHeader(string symbol,int period,int digits)
//+------------------------------------------------------------------+
  {
   int    version=400;
   string c_copyright="(C)opyright 2011, Andrey Voytenko";
   int    i_unused[13];

   ResetLastError();
   int F=FileOpen(OfflineFileName(symbol,period),FILE_BIN|FILE_ANSI|FILE_WRITE);
   if(F==INVALID_HANDLE)
     {
      Print(__FUNCTION__," Error in FileOpen, error ",GetLastError());
      return(false);
     }

   FileSeek(F,0,SEEK_SET);
   FileWriteInteger(F,version,INT_VALUE);
   FileWriteString(F,c_copyright,64);
   FileWriteString(F,symbol,12);
   FileWriteInteger(F,period,INT_VALUE);
   FileWriteInteger(F,digits,INT_VALUE);
   FileWriteInteger(F,(int)TimeCurrent(),INT_VALUE); // timesign
   FileWriteInteger(F,(int)TimeCurrent(),INT_VALUE); // last_sync
   FileWriteArray(F,i_unused,0,13);

   FileClose(F);
   return(true);
  }
//+------------------------------------------------------------------+
bool WriteOfflineBar(string symbol,int period,int bars_back,MqlRates &data)
//+------------------------------------------------------------------+
  {
   ResetLastError();
   int F=FileOpen(OfflineFileName(symbol,period),FILE_BIN|FILE_ANSI|FILE_READ|FILE_WRITE);
   if(F==INVALID_HANDLE)
     {
      Print(__FUNCTION__," Error in FileOpen, error ",GetLastError());
      return(false);
     }

   int position=bars_back*OFFLINE_RECORD_SIZE;
   FileSeek(F,-position,SEEK_END);

   if(FileTell(F)>=OFFLINE_HEADER_SIZE)
     {
      FileWriteInteger(F,(int)data.time,INT_VALUE);
      FileWriteDouble(F, data.open);
      FileWriteDouble(F, data.low);
      FileWriteDouble(F, data.high);
      FileWriteDouble(F, data.close);
      FileWriteDouble(F, data.tick_volume);
     }

   FileClose(F);
   return(true);
  }
//+------------------------------------------------------------------+
string OfflineFileName(string symbol,int period)
//+------------------------------------------------------------------+
  {
   return(StringSubstr(symbol,0,12)+IntegerToString(period)+".hst");
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---