Esempio n. 1
0
        /// <summary>
        /// get tick files created on a certain date
        /// </summary>
        /// <param name="tickfolder"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static List <string> GetFilesFromDate(string tickfolder, int date)
        {
            string[]      files    = TikUtil.GetFiles(tickfolder, TikConst.WILDCARD_EXT);
            List <string> matching = new List <string>();

            foreach (string file in files)
            {
                SecurityImpl sec    = SecurityImpl.SecurityFromFileName(file);
                string       symfix = System.IO.Path.GetFileNameWithoutExtension(sec.Name);
                if (sec.Date == date)
                {
                    matching.Add(file);
                }
            }
            return(matching);
        }
Esempio n. 2
0
        public static bool PlayHistoricalTicks(Response r, GenericTracker <string> symbols, DateTime start, DateTime endexclusive, int expecteddays, DebugDelegate deb)
        {
            bool skipexpected = expecteddays == 0;
            // prepare to track actual days for each symbol
            GenericTracker <int> actualdays = new GenericTracker <int>();

            foreach (string sym in symbols)
            {
                actualdays.addindex(sym, 0);
            }
            // prepare to track all tickfiles
            Dictionary <string, List <string> > files = new Dictionary <string, List <string> >();
            // get all required tickfiles for each symbol on each date
            DateTime now = new DateTime(start.Ticks);
            int      tfc = 0;

            while (now < endexclusive)
            {
                // get the tick files
                List <string> allfiles = TikUtil.GetFilesFromDate(Util.TLTickDir, Util.ToTLDate(now));
                // go through them all and see if we find expected number
                foreach (string fn in allfiles)
                {
                    // get security
                    SecurityImpl sec = SecurityImpl.FromTIK(fn);
                    // see if we want this symbol
                    int idx = symbols.getindex(sec.HistSource.RealSymbol);
                    if (idx < 0)
                    {
                        idx = symbols.getindex(sec.Symbol);
                    }
                    sec.HistSource.Close();
                    // skip if we don't
                    if (idx < 0)
                    {
                        continue;
                    }
                    string sym = symbols.getlabel(idx);
                    // if we have it, count actual day
                    actualdays[idx]++;
                    // save file and symbol
                    if (!files.ContainsKey(sym))
                    {
                        files.Add(sym, new List <string>());
                    }
                    files[sym].Add(fn);
                    // count files
                    tfc++;
                }
                // add one day
                now = now.AddDays(1);
            }
            // notify
            if (deb != null)
            {
                deb("found " + tfc + " tick files matching dates: " + Util.ToTLDate(start) + "->" + Util.ToTLDate(endexclusive) + " for: " + string.Join(",", symbols.ToArray()));
            }
            // playback when actual meets expected
            bool allok = true;

            foreach (string sym in symbols)
            {
                if (skipexpected || (actualdays[sym] >= expecteddays))
                {
                    // get tick files
                    string[] tf = files[sym].ToArray();
                    // notify
                    if (deb != null)
                    {
                        deb(sym + " playing back " + tf.Length + " tick files.");
                    }
                    // playback
                    HistSim h = new SingleSimImpl(tf);
                    h.GotTick += new TickDelegate(r.GotTick);
                    h.PlayTo(MultiSimImpl.ENDSIM);
                    h.Stop();
                    // notify
                    if (deb != null)
                    {
                        deb(sym + " completed playback. ");
                    }
                }
                else
                {
                    allok = false;
                }
            }


            return(allok);
        }
Esempio n. 3
0
        public bool newTick(Tick t)
        {
            if (_stopped)
            {
                return(false);
            }
            if ((t.symbol == null) || (t.symbol == ""))
            {
                return(false);
            }
            TikWriter tw;
            // prepare last date of tick
            int lastdate = 0;
            // get last date
            bool havedate = datedict.TryGetValue(t.symbol, out lastdate);

            // if we don't have date, use present date
            if (!havedate)
            {
                lastdate = t.date;
                datedict.Add(t.symbol, t.date);
            }
            // see if we need a new day
            bool samedate = lastdate == t.date;
            // see if we have stream already
            bool havestream = filedict.TryGetValue(t.symbol, out tw);

            // if no changes, just save tick
            if (samedate && havestream)
            {
                try
                {
                    tw.newTick((TickImpl)t);
                    return(true);
                }
                catch (IOException) { return(false); }
            }
            else
            {
                try
                {
                    // if new date, close stream
                    if (!samedate)
                    {
                        try
                        {
                            tw.Close();
                        }
                        catch (IOException) { }
                    }
                    // ensure file is writable
                    string fn = TikWriter.SafeFilename(t.symbol, _path, t.date);
                    if (TikUtil.IsFileWritetable(fn))
                    {
                        // open new stream
                        tw = new TikWriter(_path, t.symbol, t.date);
                        // save tick
                        tw.newTick((TickImpl)t);
                        // save stream
                        if (!havestream)
                        {
                            filedict.Add(t.symbol, tw);
                        }
                        else
                        {
                            filedict[t.symbol] = tw;
                        }
                        // save date if changed
                        if (!samedate)
                        {
                            datedict[t.symbol] = t.date;
                        }
                    }
                }
                catch (IOException) { return(false); }
                catch (Exception) { return(false); }
            }

            return(false);
        }