internal void InitTIKReader(int Index, string filepath, bool saveticks, int interval) { tr = new TikReader(filepath); if (_saveticks) { ticks = new List <Tick>(tr.ApproxTicks); } else { ticks = new List <Tick>(); } _saveticks = saveticks; idx = Index; if (interval == 0) { tr.gotTick += new TickDelegate(tr_gotTick); } else { bl = new BarListImpl(tr.Symbol, interval, BarInterval.CustomTime); bl.GotNewBar += new SymBarIntervalDelegate(bl_GotNewBar); tr.gotTick += new TickDelegate(tr_gotTick2); } }
/// <summary> /// load a security from a historical tick file /// </summary> /// <param name="filename"></param> /// <returns></returns> public static SecurityImpl FromTIK(string filename) { TikReader tr = new TikReader(filename); SecurityImpl s = (SecurityImpl)tr.ToSecurity(); if (s.isValid && tr.isValid) { s._hashist = true; s.HistSource = tr; s._approxticks = s.HistSource.ApproxTicks; } return(s); }
internal static void ThreadPoolGo(object info) { tickreader reader = (tickreader)info; reader.IsBusy = true; TikReader tr = reader.tr; //int i = 0; while (!reader.Cancel && tr.NextTick()) { ; } //if (i++ % 10 == 0) // Console.WriteLine("i:" + reader.idx + " c:" + i + " f:"+reader.file) ; reader.count = tr.Count; reader.finished = !reader.Cancel; tr.Close(); reader.IsBusy = false; }
public void CreateRead() { readdata.Clear(); readdata2.Clear(); FILE = TikWriter.SafeFilename(SYM, PATH, DATE); TestTikWriterReader.removefile(FILE); { Tick[] data = RandomTicks.GenerateSymbol(SYM, MAXTICKS); TickArchiver ta = new TickArchiver(Environment.CurrentDirectory); for (int i = 0; i < data.Length; i++) { data[i].date = DATE; data[i].time = Util.DT2FT(DateTime.Now); ta.newTick(data[i]); } ta.Stop(); // read file back in from file TikReader tr = new TikReader(FILE); tr.gotTick += new TickDelegate(tr_gotTick); while (tr.NextTick()) ; // verify length Assert.AreEqual(data.Length, readdata.Count); // verify content bool equal = true; for (int i = 0; i < MAXTICKS; i++) equal &= data[i].trade == readdata[i].trade; tr.Close(); readdata.Clear(); Assert.IsTrue(equal, "ticks did not matched archive."); TestTikWriterReader.removefile(FILE); } }
internal tickreader(int Index, string filepath, bool saveticks, int interval) { _int = interval; file = filepath; finished = false; count = 0; Cancel = false; IsBusy = false; _saveticks = false; datetimes = new List <long>(EXPECTTICKS); index = new List <int>(EXPECTTICKS); tr = new TikReader(filepath); if (_saveticks) { ticks = new List <Tick>(tr.ApproxTicks); } else { ticks = new List <Tick>(); } _saveticks = saveticks; idx = Index; if (interval == 0) { tr.gotTick += new TickDelegate(tr_gotTick); } else { bl = new BarListImpl(tr.Symbol, interval, BarInterval.CustomTime); bl.GotNewBar += new SymBarIntervalDelegate(bl_GotNewBar); tr.gotTick += new TickDelegate(tr_gotTick2); } ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolGo), this); IsBusy = true; }
/// <summary> /// play simulation until specified day/time is hit /// </summary> /// <param name="datetime"></param> virtual public void PlayTo(long datetime) { // check for event if (GotTick == null) { debug("Not handling ticks, quitting..."); return; } // handle non binary indicies if (!myhsi.isBinaryIndex) { // make sure inited if (!hsipinited) { Initialize(); // wait a moment System.Threading.Thread.Sleep(WaitRead * 2); } // ensure reader is done while (_reader.IsBusy) { _reader.CancelAsync(); } // start reading _reader.RunWorkerAsync(datetime); stopwatch(); // start writing while (hsipplay && (hsip_nexttime <= datetime)) { if (_buf.hasItems) { Tick k = _buf.Read(); gotnewtick(k); } else if (!_reader.IsBusy) break; System.Threading.Thread.Sleep(_writewait); } stopwatch(); } else { debug("Playing index containing " + myhsi.TicksPresent.ToString("N0") + " ticks."); TikReader tw = new TikReader(myhsi.BinaryIndex); // reset counters br = 0; tw.gotTick += new TickDelegate(tw_gotTick); // get symbols for every toc string[] syms = new string[myhsi.TOCTicks.Length]; for (int i = 0; i < syms.Length; i++) syms[i] = SecurityImpl.SecurityFromFileName(myhsi.TOCTicks[i]).symbol; // get symbol for every tick symidx = new string[TicksPresent]; for (int i = 0; i < symidx.Length; i++) symidx[i] = syms[myhsi.Playindex[i]]; // playback ticks stopwatch(); while (hsipplay && (hsip_nexttime <= datetime) && tw.NextTick()) { } stopwatch(); } }
/// <summary> /// load a security from a historical tick file /// </summary> /// <param name="filename"></param> /// <returns></returns> public static SecurityImpl FromTIK(string filename) { TikReader tr = new TikReader(filename); SecurityImpl s = (SecurityImpl)tr.ToSecurity(); if (s.isValid && tr.isValid) { s._hashist = true; s.HistSource = tr; s._approxticks = s.HistSource.ApproxTicks; } return s; }
/// <summary> /// play simulation until specified day/time is hit /// </summary> /// <param name="datetime"></param> virtual public void PlayTo(long datetime) { // check for event if (GotTick == null) { debug("Not handling ticks, quitting..."); return; } // handle non binary indicies if (!myhsi.isBinaryIndex) { // make sure inited if (!hsipinited) { Initialize(); // wait a moment System.Threading.Thread.Sleep(WaitRead * 2); } // ensure reader is done while (_reader.IsBusy) { _reader.CancelAsync(); } // start reading _reader.RunWorkerAsync(datetime); stopwatch(); // start writing while (hsipplay && (hsip_nexttime <= datetime)) { if (_buf.hasItems) { Tick k = _buf.Read(); gotnewtick(k); } else if (!_reader.IsBusy) { break; } System.Threading.Thread.Sleep(_writewait); } stopwatch(); } else { debug("Playing index containing " + myhsi.TicksPresent.ToString("N0") + " ticks."); TikReader tw = new TikReader(myhsi.BinaryIndex); // reset counters br = 0; tw.gotTick += new TickDelegate(tw_gotTick); // get symbols for every toc string[] syms = new string[myhsi.TOCTicks.Length]; for (int i = 0; i < syms.Length; i++) { syms[i] = SecurityImpl.SecurityFromFileName(myhsi.TOCTicks[i]).symbol; } // get symbol for every tick symidx = new string[TicksPresent]; for (int i = 0; i < symidx.Length; i++) { symidx[i] = syms[myhsi.Playindex[i]]; } // playback ticks stopwatch(); while (hsipplay && (hsip_nexttime <= datetime) && tw.NextTick()) { } stopwatch(); } }
double writeandread(Tick[] data, int date, bool printperf) { // clear out the read buffer readdata.Clear(); // keep track of time double elapms; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); // write new file from data TikWriter tw = new TikWriter(PATH,data[0].symbol,date==0 ? data[0].date: date); string FILE = tw.Filepath; sw.Start(); foreach (Tick k in data) tw.newTick(k); sw.Stop(); tw.Close(); elapms = (double)sw.ElapsedMilliseconds; if (printperf) Console.WriteLine("write speed (ticks/sec): " + (data.Length / (elapms / 1000)).ToString("n0")); // read file back in from file TikReader tr = new TikReader(FILE); tr.gotTick += new TickDelegate(tr_gotTick); sw.Reset(); sw.Start(); while (tr.NextTick()) ; sw.Stop(); tr.Close(); elapms = (double)sw.ElapsedMilliseconds; if (printperf) Console.WriteLine("read speed (ticks/sec): " + (data.Length/(elapms/1000)).ToString("n0")); // remove file removefile(FILE); return elapms; }
public void Multiday() { readdata.Clear(); readdata2.Clear(); int d = 20100223; int t = 235900; int t1 = 0; const decimal p = 50; int s = 100; string FILE1 = TikWriter.SafeFilename(SYM, PATH, d); TestTikWriterReader.removefile(FILE1); string FILE2 = TikWriter.SafeFilename(SYM, PATH, d+1); TestTikWriterReader.removefile(FILE2); Tick[] data = new Tick[] { TickImpl.NewTrade(SYM,d,t++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t++,p,s,string.Empty), // day two TickImpl.NewTrade(SYM,++d,t1++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t1++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t1++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t1++,p,s,string.Empty), TickImpl.NewTrade(SYM,d,t1++,p,s,string.Empty), }; TickArchiver ta = new TickArchiver(Environment.CurrentDirectory); for (int i = 0; i < data.Length; i++) { ta.newTick(data[i]); } ta.Stop(); // read file back in from files if (System.IO.File.Exists(FILE1)) { TikReader tr = new TikReader(FILE1); tr.gotTick += new TickDelegate(tr_gotTick); while (tr.NextTick()) ; tr.Close(); } if (System.IO.File.Exists(FILE2)) { TikReader tr2 = new TikReader(FILE2); tr2.gotTick += new TickDelegate(tr2_gotTick); while (tr2.NextTick()) ; tr2.Close(); } // verify length Assert.AreEqual(5,readdata2.Count); Assert.AreEqual(5, readdata.Count); TestTikWriterReader.removefile(FILE1); TestTikWriterReader.removefile(FILE2); }
internal tickreader(int Index, string filepath, bool saveticks, int interval) { _int = interval; file = filepath; finished = false; count = 0; Cancel = false; IsBusy = false; _saveticks = false; datetimes = new List<long>(EXPECTTICKS); index = new List<int>(EXPECTTICKS); tr = new TikReader(filepath); if (_saveticks) ticks = new List<Tick>(tr.ApproxTicks); else ticks = new List<Tick>(); _saveticks = saveticks; idx = Index; if (interval == 0) tr.gotTick += new TickDelegate(tr_gotTick); else { bl = new BarListImpl(tr.Symbol, interval, BarInterval.CustomTime); bl.GotNewBar += new SymBarIntervalDelegate(bl_GotNewBar); tr.gotTick += new TickDelegate(tr_gotTick2); } ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolGo), this); IsBusy = true; }