GainTicks2Allfxt_V2_1

Author: Copyright � 2006, MetaQuotes Software Corp.
Miscellaneous
It reads information from a fileIt reads information from a fileIt writes information to fileIt writes information to fileUses files from the file systemIt writes information to fileIt writes information to file
0 Views
0 Downloads
0 Favorites
GainTicks2Allfxt_V2_1
//+------------------------------------------------------------------+
//|                                        GainTicks2Allfxt_V2_1.mq4 |
//|									                    Paul Hampton-Smith |
//|																						|
//|    									 Modification of simple_csv2fxt.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//|																						|
//| V2: uses FXTHeader403.mqh for build 200 									|
//|	  Removes filter for ticks with the same time   					|
//|																						|
//| V2_1: Fixes error in hst file header																						|
//|																						|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property show_inputs

#include <FXTHeader403.mqh>

string strScriptName = "GainTicks2Allfxt_V2_1";

extern string ExtCsvFile_Is = "Gain ticks file to be converted";
extern string ExtCsvFile="";
extern string Either = "Select MakeAllTimeFrames = true";
extern bool MakeAllTimeFrames = true;
extern string Or = "Select individual timeframes = true";
extern bool MakeM1 = false;
extern bool MakeM5 = false;
extern bool MakeM15 = false;
extern bool MakeM30 = false;
extern bool MakeH1 = false;
extern bool MakeH4 = false;
extern bool MakeD1 = false;
extern bool MakeW1 = false;

int      ExtTicks;
int      ExtBars;
int      ExtCsvHandle=-1;
int      ExtHstHandle=-1;
int      ExtHandle=-1;
string   ExtFileName;
int      ExtPeriodSeconds;
datetime ExtLastTime;
datetime ExtLastBarTime;
double   ExtLastOpen;
double   ExtLastLow;
double   ExtLastHigh;
double   ExtLastClose;
double   ExtLastVolume;
int nPeriod;

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
	if (MakeAllTimeFrames)
	{
		MakeM1 = true;
		MakeM5 = true;
		MakeM15 = true;
		MakeM30 = true;
		MakeH1 = true;
		MakeH4 = true;
		MakeD1 = true;
		MakeW1 = true;
	}
	if (MakeM1) MakeFXT(PERIOD_M1);
	if (MakeM5)	MakeFXT(PERIOD_M5);
	if (MakeM15)MakeFXT(PERIOD_M15);
	if (MakeM30)MakeFXT(PERIOD_M30);
	if (MakeH1)	MakeFXT(PERIOD_H1);
	if (MakeH4)	MakeFXT(PERIOD_H4);
	if (MakeD1)	MakeFXT(PERIOD_D1);
	if (MakeW1)	MakeFXT(PERIOD_W1);
	Comment(strScriptName," completed");
}


// Gain tick data format:
// int,pairname,yyyy-mm-dd hh:mm:ss,bid,ask,D
bool ReadNextTick(datetime& cur_time, double& tick_price)
  {
//----
	// initial id number is discarded
  	double dblID = FileReadNumber(ExtCsvHandle);
	// pairname is discarded
  	string strPairname = FileReadString(ExtCsvHandle);
	// yyyy-mm-dd hh:mm:ss     	
  	string date_time = FileReadString(ExtCsvHandle);
  	date_time = StringSetChar(date_time,4,'.');
  	date_time = StringSetChar(date_time,7,'.');
  	// yyyy.mm.dd hh:mm:ss
   if(FileIsEnding(ExtCsvHandle)) return(false);
//   cur_time=StrToTimeWithSeconds(date_time);
   cur_time=StrToTime(date_time);
//   Print(TimeToStr(cur_time,TIME_DATE|TIME_SECONDS));
   //---- read tick price
   tick_price=FileReadNumber(ExtCsvHandle);
   // discard Ask and "D"
   double dblAsk = FileReadNumber(ExtCsvHandle);
   string strD = FileReadString(ExtCsvHandle);
 
   if(FileIsEnding(ExtCsvHandle)) return(false);
 
//---- price must be normalized
   tick_price=NormalizeDouble(tick_price,Digits);
   ExtLastTime=cur_time;
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void WriteTick()
  {
//---- current bar state
   FileWriteInteger(ExtHandle, ExtLastBarTime, LONG_VALUE);
   FileWriteDouble(ExtHandle, ExtLastOpen, DOUBLE_VALUE);
   FileWriteDouble(ExtHandle, ExtLastLow, DOUBLE_VALUE);
   FileWriteDouble(ExtHandle, ExtLastHigh, DOUBLE_VALUE);
   FileWriteDouble(ExtHandle, ExtLastClose, DOUBLE_VALUE);
   FileWriteDouble(ExtHandle, ExtLastVolume, DOUBLE_VALUE);
//---- incoming tick time
   FileWriteInteger(ExtHandle, ExtLastTime, LONG_VALUE);
//---- flag 4 (it must be not equal to 0)
   FileWriteInteger(ExtHandle, 4, LONG_VALUE);
//---- ticks counter
   ExtTicks++;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void WriteHstHeader()
  {
//---- History header
   int    i_version=400;
   string c_copyright;
   string c_symbol=Symbol();
   int    i_period=nPeriod;
   int    i_digits=Digits;
   int    i_unused[16];
//----  
   ExtHstHandle=FileOpen(c_symbol+i_period+".hst", FILE_BIN|FILE_WRITE);
   if(ExtHstHandle < 0) return;
//---- write history file header
   c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";
   FileWriteInteger(ExtHstHandle, i_version, LONG_VALUE);
   FileWriteString(ExtHstHandle, c_copyright, 64);
   FileWriteString(ExtHstHandle, c_symbol, 12);
   FileWriteInteger(ExtHstHandle, i_period, LONG_VALUE);
   FileWriteInteger(ExtHstHandle, i_digits, LONG_VALUE);
  	FileWriteInteger(ExtHstHandle, 0, LONG_VALUE);       //timesign
   FileWriteInteger(ExtHstHandle, 0, LONG_VALUE);       //last_sync
   FileWriteArray(ExtHstHandle, i_unused, 0, 13);  }
//+------------------------------------------------------------------+
//| write corresponding hst-file                                     |
//+------------------------------------------------------------------+
void WriteBar()
  {
   if(ExtHstHandle>0)
     {
      FileWriteInteger(ExtHstHandle, ExtLastBarTime, LONG_VALUE);
      FileWriteDouble(ExtHstHandle, ExtLastOpen, DOUBLE_VALUE);
      FileWriteDouble(ExtHstHandle, ExtLastLow, DOUBLE_VALUE);
      FileWriteDouble(ExtHstHandle, ExtLastHigh, DOUBLE_VALUE);
      FileWriteDouble(ExtHstHandle, ExtLastClose, DOUBLE_VALUE);
      FileWriteDouble(ExtHstHandle, ExtLastVolume, DOUBLE_VALUE);
     }
  }
//+------------------------------------------------------------------+

void MakeFXT(int period)
{
	nPeriod = period;
   datetime cur_time,cur_open;
   double   tick_price;
   int      delimiter=';';
//----
   ExtTicks=0;
   ExtBars=0;
   ExtLastTime=0;
   ExtLastBarTime=0;
//---- open input csv-file
//   if(StringLen(ExtCsvFile)<=0)  ExtCsvFile=Symbol()+"_ticks.csv";
//   if(StringLen(ExtDelimiter)>0) delimiter=StringGetChar(ExtDelimiter,0);
//   if(delimiter==' ')  delimiter=';';
//   if(delimiter=='\\') delimiter='\t';
   ExtCsvHandle=FileOpen(ExtCsvFile,FILE_CSV|FILE_READ,',');
   if(ExtCsvHandle<0) return(-1);
   // discard header
   while(!FileIsLineEnding(ExtCsvHandle)) FileReadString(ExtCsvHandle);
   int nCsvStartPos = FileTell(ExtCsvHandle);
   // get start time
   ReadNextTick(t_fromdate,tick_price);
   // go back to read again
   FileSeek(ExtCsvHandle,nCsvStartPos,SEEK_SET);

//---- open output fxt-file
   ExtFileName=Symbol()+nPeriod+"_0.fxt";
   ExtHandle=FileOpen(ExtFileName,FILE_BIN|FILE_WRITE);
   if(ExtHandle<0) return(-1);
   Print("Creating ",Symbol(),nPeriod,"_0.fxt");
   Comment("Creating ",Symbol(),nPeriod,"_0.fxt");
//----
   ExtPeriodSeconds=nPeriod*60;
   WriteHeader(ExtHandle,Symbol(),nPeriod,0);
//---- open hst-file and write it's header
   WriteHstHeader();
//---- csv read loop
   while(!IsStopped())
     {
      //---- if end of file reached exit from loop
      if(!ReadNextTick(cur_time,tick_price)) break;
      //---- calculate bar open time from tick time
      cur_open=cur_time/ExtPeriodSeconds;
      cur_open*=ExtPeriodSeconds;
      //---- new bar?
      if(ExtLastBarTime!=cur_open)
        {
         if(ExtBars>0) WriteBar();
         ExtLastBarTime=cur_open;
         ExtLastOpen=tick_price;
         ExtLastLow=tick_price;
         ExtLastHigh=tick_price;
         ExtLastClose=tick_price;
         ExtLastVolume=1;
         WriteTick();
         ExtBars++;
         if (MathMod(ExtBars,MathMax(9000/nPeriod,1)) < 1) Comment("Creating tick file ",Symbol(),nPeriod,"_0.fxt and writing bar ",ExtBars," to history file ",Symbol(),nPeriod,".hst");
        }
      else
        {
         //---- check for minimum and maximum
         if(ExtLastLow>tick_price)  ExtLastLow=tick_price;
         if(ExtLastHigh<tick_price) ExtLastHigh=tick_price;
         ExtLastClose=tick_price;
         ExtLastVolume++;
         WriteTick();
        }
     }
//---- finalize
   WriteBar();
   if(ExtHstHandle>0) FileClose(ExtHstHandle);
   FileClose(ExtCsvHandle);
//---- store processed bars amount
   FileFlush(ExtHandle);
   FileSeek(ExtHandle,88,SEEK_SET);
   FileWriteInteger(ExtHandle,ExtBars,LONG_VALUE);
   FileClose(ExtHandle);
   Print(ExtTicks," ticks added. ",ExtBars," bars finalized in the header");
//----
   return;
}

/*datetime StrToTimeWithSeconds(string strDateTime)
{
	// converts string date time with seconds because StrToTime does not
	int nSseconds = 0;
	datetime dt = StrToTime(strDateTime);
	// allow for potential fix of bug in future release
	if (MathMod(dt,60) == 0)
	{
		nSseconds = StrToInteger(StringSubstr(strDateTime,17,2));
	}
	return(dt + nSseconds);
}*/

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 ---