mql5-tick-compression/scripts/TkzToCustom.mq5
88 라인
3.1 KiB
MQL5
//+------------------------------------------------------------------+
//| TkzToCustom.mq5 |
//| Loads a .tkz archive into a custom symbol, so the stored tick |
//| history can be used in charts and in the Strategy Tester. |
//+------------------------------------------------------------------+
#property copyright "TKZ tick compression"
#property version "1.1"
#property description "Loads the ticks of a .tkz archive into a custom symbol (CustomTicksReplace)."
#property script_show_inputs
#include "../lib/TkzArchive.mqh"
input string InpFile=""; // Archive file (empty = <chart symbol>.tkz)
input bool InpCommon=false; // File is in the common data folder
input string InpCustom=""; // Custom symbol name (empty = <archive symbol>.tkz)
input string InpOrigin=""; // Copy symbol properties from (empty = archive symbol)
//+------------------------------------------------------------------+
void OnStart()
{
string file=(InpFile=="") ? _Symbol+".tkz" : InpFile;
CTkzReader reader;
if(!reader.Open(file,InpCommon))
{
PrintFormat("TKZ: cannot open %s: %s",file,reader.LastError());
return;
}
string base=reader.ArchiveSymbol();
string custom=(InpCustom=="") ? base+".tkz" : InpCustom;
string origin=(InpOrigin=="") ? base : InpOrigin;
bool is_custom=false;
if(SymbolExist(custom,is_custom))
{
if(!is_custom)
{
PrintFormat("TKZ: %s exists and is not a custom symbol",custom);
return;
}
}
else
{
bool origin_custom=false;
bool have_origin=SymbolExist(origin,origin_custom);
ResetLastError();
bool created=have_origin ? CustomSymbolCreate(custom,"TKZ",origin) : CustomSymbolCreate(custom,"TKZ");
if(!created)
{
PrintFormat("TKZ: CustomSymbolCreate(%s) failed, error %d",custom,GetLastError());
return;
}
if(!have_origin && reader.ArchiveDigits()>=0)
CustomSymbolSetInteger(custom,SYMBOL_DIGITS,reader.ArchiveDigits());
}
MqlTick ticks[];
long total=0;
int failed=0;
ulong t0=GetMicrosecondCount();
for(int i=0; i<reader.ChunkCount() && !IsStopped(); i++)
{
STkzChunkInfo info;
reader.GetChunkInfo(i,info);
int n=reader.ReadChunk(i,ticks);
if(n<0)
{
PrintFormat("TKZ: chunk %d: decode error: %s",i,reader.LastError());
failed++;
continue;
}
if(n==0)
continue;
ResetLastError();
int r=CustomTicksReplace(custom,info.min_msc,info.max_msc,ticks);
if(r<0)
{
PrintFormat("TKZ: chunk %d: CustomTicksReplace failed, error %d",i,GetLastError());
failed++;
continue;
}
total+=n;
Comment(StringFormat("TKZ: %s, chunk %d of %d, %I64d ticks",custom,i+1,reader.ChunkCount(),total));
}
reader.Close();
Comment("");
SymbolSelect(custom,true);
PrintFormat("TKZ: %I64d ticks loaded into %s in %.1f s, %d chunks failed",
total,custom,(double)(GetMicrosecondCount()-t0)/1e6,failed);
}
//+------------------------------------------------------------------+