Beispiel #1
0
        public void Parsing()
        {
            // tests to parse and generate user-supplied security specifiers
            SecurityImpl nyse = new SecurityImpl("LVS");
            string p = nyse.ToString();

            SecurityImpl t = SecurityImpl.Parse(p);
            Assert.That(t.Symbol == nyse.Symbol, t.Symbol);
            Assert.That(!t.hasDest, t.DestEx);
            Assert.That(t.Type == nyse.Type, t.Type.ToString());

            SecurityImpl crude = SecurityImpl.Parse("CLV8 FUT GLOBEX");
            Assert.That(crude.Symbol == "CLV8", crude.Symbol);
            Assert.That(crude.hasDest, crude.DestEx);
            Assert.That(crude.Type == SecurityType.FUT, crude.Type.ToString());
            SecurityImpl goog = SecurityImpl.Parse("GOOG");
            Assert.AreEqual("GOOG", goog.FullName);

            Security opt = SecurityImpl.Parse("IBM PUT 201004 100.00");
            Assert.That(opt.Type == SecurityType.OPT);
            Assert.AreEqual(100, opt.Strike);
            Assert.AreEqual("PUT", opt.Details);
            Assert.AreEqual(201004, opt.Date);


            
        }
Beispiel #2
0
 public static SecurityImpl Parse(string msg, int date)
 {
     string[] r = msg.Split(' ');
     SecurityImpl sec = new SecurityImpl();
     sec.Symbol = r[0];
     if (r.Length > 2)
     {
         int f2id = SecurityID(r[2]);
         int f1id = SecurityID(r[1]);
         if (f2id != -1)
         {
             sec.Type = (SecurityType)f2id;
             sec.DestEx = r[1];
         }
         else if (f1id != -1)
         {
             sec.Type = (SecurityType)f1id;
             sec.DestEx = r[2];
         }
     }
     else if (r.Length > 1)
     {
         int f1id = SecurityID(r[1]);
         if (f1id != -1)
             sec.Type = (SecurityType)f1id;
         else sec.DestEx = r[1];
     }
     else
         sec.Type = SecurityType.STK;
     sec.Date = date;
     if (sec.hasDest && !sec.hasType)
         sec.Type = TypeFromExchange(sec.DestEx);
     return sec;
 }
Beispiel #3
0
        public bool Watch(Tick tick, int NoTicksAlertWait)
        {
            int last = tick.time;

            if (!_last.ContainsKey(tick.symbol))
            {
                _last.Add(tick.symbol, last);
                if (_alertonfirst) // if we're notifying when first tick arrives, do it.
                {
                    if (FirstTick != null)
                    {
                        FirstTick(SecurityImpl.Parse(tick.symbol));
                    }
                }
                return(true);
            }
            int  span  = Util.FTDIFF(_last[tick.symbol], last);
            bool alert = span > NoTicksAlertWait;

            _last[tick.symbol] = last;
            if (alert && (Alerted != null))
            {
                Alerted(SecurityImpl.Parse(tick.symbol, _last[tick.symbol]));
            }
            return(!alert);
        }
Beispiel #4
0
 public void BasketBasics()
 {
     BasketImpl mb = new BasketImpl();
     
     Assert.That(mb != null);
     SecurityImpl i = new SecurityImpl("IBM");
     mb = new BasketImpl(i);
     mb.SendDebugEvent += new DebugDelegate(rt.d);
     Assert.That(mb.isNotEmpty);
     Assert.That(mb.isSecurityPresent(i), "missing ibm security");
     Assert.That(mb.isSymbolPresent("IBM"), "missing ibm symbol");
     Assert.IsFalse(mb.isSymbolPresent("LVS"), "had lvs before added");
     mb.Remove(i);
     Assert.That(!mb.isNotEmpty);
     mb.Add(new SecurityImpl("LVS"));
     Assert.That(mb[0].symbol=="LVS",mb[0].ToString());
     mb.Add(new SecurityImpl("IBM"));
     Assert.That(mb[1].symbol=="IBM");
     mb.Add("CLV8 FUT NYMEX");
     Assert.AreEqual(3, mb.Count,"missing futures symbol");
     Assert.IsTrue(mb[1].Type == SecurityType.STK, "not a equities type:" + mb[1].Type);
     Assert.IsTrue(mb[2].Type == SecurityType.FUT, "not a futures type:"+mb[2].Type);
     Security ts;
     Assert.IsTrue(mb.TryGetSecurityAnySymbol("IBM", out ts), "ibm fetch failed");
     Assert.IsTrue(mb.TryGetSecurityAnySymbol("LVS", out ts), "lvs fetch failed");
     Assert.IsTrue(mb.TryGetSecurityAnySymbol("CLV8", out ts), "CLV8 short fetch failed");
     Assert.IsTrue(mb.TryGetSecurityAnySymbol("CLV8 NYMEX FUT", out ts), "CLV8 short fetch failed");
     BasketImpl newbasket = new BasketImpl(new SecurityImpl("FDX"));
     newbasket.Add(mb);
     var orgcount = mb.Count;
     mb.Clear();
     Assert.That(mb.Count==0,"basket clear did not work");
     Assert.AreEqual(orgcount+1,newbasket.Count,"new basket missing symbols");
 }
Beispiel #5
0
 void ReadHeader()
 {
     // get version id
     ReadByte();
     // get version
     _filever = ReadInt32();
     if (_filever != TikConst.FILECURRENTVERSION)
     {
         throw new BadTikFile("version: " + _filever + " expected: " + TikConst.FILECURRENTVERSION);
     }
     // get real symbol
     _realsymbol = ReadString();
     // get security from symbol
     _sec      = TradeLink.Common.SecurityImpl.Parse(_realsymbol);
     _sec.Date = SecurityImpl.SecurityFromFileName(_path).Date;
     // get short symbol
     _sym = _sec.symbol;
     // get end of header
     ReadByte();
     // make sure we read something
     if (_realsymbol.Length <= 0)
     {
         throw new BadTikFile("no symbol defined in tickfile");
     }
     // flag header as read
     _haveheader = true;
 }
Beispiel #6
0
 public void Basics()
 {
     SecurityImpl s = new SecurityImpl("");
     Assert.That(s != null);
     Assert.That(!s.isValid);
     s = new SecurityImpl("TST");
     Assert.That(s.isValid);
 }
Beispiel #7
0
        /// <summary>
        /// Reinitialize the cache
        /// </summary>
        public void Initialize()
        {
            if (_inited)
            {
                return;          // only init once
            }
            if (_tickfiles.Length == 0)
            {
                // get our listings of historical files (idx and epf)
                string[] files = Directory.GetFiles(_folder, tickext);
                _tickfiles = _filter.Allows(files);
            }

            // now we have our list, initialize instruments from files
            for (int i = 0; i < _tickfiles.Length; i++)
            {
                try
                {
                    SecurityImpl s = getsec(i);
                    if ((s != null) && s.isValid && s.HistSource.isValid)
                    {
                        Workers.Add(new simworker(s));
                    }
                }
                catch (Exception ex)
                {
                    D("Unable to initialize: " + _tickfiles[i] + " error: " + ex.Message + ex.StackTrace);
                    continue;
                }
            }
            // setup our initial index
            idx  = genidx(Workers.Count);
            cidx = new int[Workers.Count];

            D("Initialized " + (_tickfiles.Length) + " instruments.");
            D(string.Join(Environment.NewLine.ToString(), _tickfiles));
            // read in single tick just to get first time for user
            FillCache(1);

            // get total ticks represented by files
            _availticks = 0;
            for (int i = 0; i < Workers.Count; i++)
            {
                if (Workers[i].workersec != null)
                {
                    _availticks += Workers[i].workersec.ApproxTicks;
                }
            }

            D("Approximately " + TicksPresent + " ticks to process...");
            _inited = true;
            // set first time as hint for user
            setnexttime();
        }
Beispiel #8
0
 public simworker(SecurityImpl sec)
 {
     workersec = sec;
     WorkerSupportsCancellation = true;
     RunWorkerCompleted        += new RunWorkerCompletedEventHandler(simworker_RunWorkerCompleted);
     // if we're multi-core start reading into cache immediately in background
     if (Environment.ProcessorCount > 1)
     {
         DoWork += new DoWorkEventHandler(simworker_DoWork);
     }
 }
Beispiel #9
0
 public void OptionOSIParse()
 {
     string osi = "YHOO100416C00020000";
     Security sec = new SecurityImpl();
     Assert
         .IsTrue(SecurityImpl.ParseOptionOSI(osi, ref sec,rt.d), "parsing osi had an error");
     Assert.AreEqual("YHOO", sec.Symbol,"symbol incorrect");
     Assert.AreEqual(20100416, sec.Date,"date incorrect");
     Assert.AreEqual(20, sec.Strike,"strike incorrect");
     Assert.IsTrue(sec.isCall, "not a call");
 }
Beispiel #10
0
        public static BarList FromTIK(string filename)
        {
            SecurityImpl s = SecurityImpl.FromTIK(filename);

            s.HistSource.gotTick += new TickDelegate(HistSource_gotTick);
            _fromepf              = new BarListImpl(s.Symbol);
            while (s.HistSource.NextTick())
            {
                ;
            }
            return(_fromepf);
        }
Beispiel #11
0
        /// <summary>
        /// Build a barlist using an EPF file as the source
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns>barlist</returns>
        public static BarList FromEPF(string filename)
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(filename);
            SecurityImpl           s  = eSigTick.InitEpf(sr);
            BarList b = new BarListImpl(s.Symbol);

            while (!sr.EndOfStream)
            {
                b.newTick(eSigTick.FromStream(s.Symbol, sr));
            }
            return(b);
        }
Beispiel #12
0
        /// <summary>
        /// attempt to get security by short symbol
        /// </summary>
        /// <param name="sym"></param>
        /// <param name="sec"></param>
        /// <returns></returns>
        public bool TryGetSecurityShortSymbol(string sym, out Security sec)
        {
            var idx = shortsym2secidx.getindex(sym);

            sec = new SecurityImpl();
            if (idx < 0)
            {
                return(false);
            }
            sec = this[idx];
            return(true);
        }
Beispiel #13
0
 /// <summary>
 /// determine security from the filename, without opening file
 /// (use SecurityImpl.FromFile to actually read it in)
 /// </summary>
 /// <param name="filename"></param>
 /// <returns></returns>
 public static SecurityImpl SecurityFromFileName(string filename)
 {
     try
     {
         string       ds  = System.Text.RegularExpressions.Regex.Match(filename, "([0-9]{8})[.]", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Result("$1");
         string       sym = filename.Replace(ds, "").Replace(TikConst.DOT_EXT, "");
         SecurityImpl s   = new SecurityImpl(sym);
         s.Date = Convert.ToInt32(ds);
         return(s);
     }
     catch (Exception) { }
     return(new SecurityImpl());
 }
Beispiel #14
0
        /// <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);
        }
Beispiel #15
0
 SecurityImpl getsec(string file)
 {
     try
     {
         SecurityImpl s = SecurityImpl.FromTIK(file);
         return(s);
     }
     catch (Exception ex)
     {
         debug("error reading TIK file: " + file + " err: " + ex.Message + ex.StackTrace);
         return(null);
     }
 }
Beispiel #16
0
 /// <summary>
 /// Sends the alerts for tickstreams who have gone idle based on the provided datetime.
 /// </summary>
 /// <param name="date">The datetime.</param>
 /// <param name="AlertSecondsWithoutTick">The alert seconds without tick.</param>
 public void SendAlerts(DateTime date, int AlertSecondsWithoutTick)
 {
     foreach (string sym in _last.Keys)
     {
         if (Alerted != null)
         {
             if (Util.FTDIFF(_last[sym], Util.DT2FT(date)) > AlertSecondsWithoutTick)
             {
                 Alerted(SecurityImpl.Parse(sym, _last[sym]));
             }
         }
     }
 }
Beispiel #17
0
        /// <summary>
        /// create barlist from a tik file using given intervals/types
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="uselast"></param>
        /// <param name="usebid"></param>
        /// <param name="intervals"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public static BarList FromTIK(string filename, bool uselast, bool usebid, int[] intervals, BarInterval[] types)
        {
            _uselast = uselast;
            _usebid  = usebid;
            SecurityImpl s = SecurityImpl.FromTIK(filename);

            s.HistSource.gotTick += new TickDelegate(HistSource_gotTick);
            _fromepf              = new BarListImpl(s.Symbol, intervals, types);
            while (s.HistSource.NextTick())
            {
                ;
            }
            return(_fromepf);
        }
Beispiel #18
0
        /// <summary>
        /// Reinitialize the cache
        /// </summary>
        public void Initialize()
        {
            if (_inited)
            {
                return;          // only init once
            }
            if (_tickfiles.Length == 0)
            {
                // get our listings of historical files (idx and epf)
                string[] files = Directory.GetFiles(_folder, tickext);
                _tickfiles = _filter.Allows(files);
            }

            // now we have our list, initialize instruments from files
            foreach (string file in _tickfiles)
            {
                SecurityImpl s = SecurityImpl.FromFile(file);
                if (s != null)
                {
                    Workers.Add(new simworker(s));
                }
            }
            // setup our initial index
            idx  = genidx(Workers.Count);
            cidx = new int[Workers.Count];

            D("Initialized " + (_tickfiles.Length) + " instruments.");
            D(string.Join(Environment.NewLine.ToString(), _tickfiles));
            // read in single tick just to get first time for user
            FillCache(1);

            // get total bytes represented by files
            DirectoryInfo di = new DirectoryInfo(_folder);

            FileInfo[] fi = di.GetFiles(tickext, SearchOption.AllDirectories);
            foreach (FileInfo thisfi in fi)
            {
                foreach (string file in _tickfiles)
                {
                    if (thisfi.FullName == file)
                    {
                        _bytestoprocess += thisfi.Length;
                    }
                }
            }
            D("Approximately " + TicksPresent + " ticks to process...");
            _inited = true;
            // set first time as hint for user
            setnexttime();
        }
Beispiel #19
0
 /// <summary>
 /// converts EPF files to tradelink tick files in current directory
 /// </summary>
 /// <param name="args"></param>
 public static void EPF2TIK(string[] args)
 {
     // get a list of epf files
     foreach (string file in args)
     {
         SecurityImpl sec = SecurityImpl.FromTIK(file);
         sec.HistSource.gotTick += new TradeLink.API.TickDelegate(HistSource_gotTick);
         _tw = new TikWriter(sec.Symbol);
         while (sec.NextTick())
         {
             _tw.Close();
         }
     }
 }
Beispiel #20
0
        public void OptionSec2OSI()
        {
            string osi1 = "YHOO100416C00020000";
            string osi2 = "AAPL 111022P420000";
            Security sec = new SecurityImpl();
            Assert
                .IsTrue(SecurityImpl.ParseOptionOSI(osi1, ref sec,rt.d), "parsing osi1 had an error");
            Assert.AreEqual("YHOO 100416C20000", SecurityImpl.ToOSISymbol(sec), "converting to osi1 failed");

            Assert
    .IsTrue(SecurityImpl.ParseOptionOSI(osi2, ref sec, rt.d), "parsing osi2 had an error");
            Assert.AreEqual("AAPL 111022P420000", SecurityImpl.ToOSISymbol(sec), "converting to osi2 failed");

        }
Beispiel #21
0
        /// <summary>
        /// Initilize the reading of an EPF file and return the header as a Stock object.
        /// </summary>
        /// <param name="EPFfile">The EP ffile.</param>
        /// <returns></returns>
        public static SecurityImpl InitEpf(StreamReader EPFfile)
        {
            StreamReader    cf       = EPFfile;
            string          symline  = cf.ReadLine();
            string          dateline = cf.ReadLine();
            Regex           se       = new Regex("=[$^a-z-A-Z0-9]+");
            Regex           dse      = new Regex(@"; Date=([0-9]+)/([0-9]+)/([0-9]+).*$");
            MatchCollection r        = se.Matches(symline, 0);
            string          t        = r[0].Value;
            string          symbol   = t.Substring(1, t.Length - 1);
            SecurityImpl    s        = SecurityImpl.Parse(symbol.ToUpper());
            string          date     = dateline.Contains("/") ? dse.Replace(dateline, "20$3$1$2") : Regex.Match(dateline, "[0-9]+").ToString();

            s.Date = Convert.ToInt32(date);
            return(s);
        }
Beispiel #22
0
 public simworker(SecurityImpl sec)
 {
     workersec = sec;
     WorkerSupportsCancellation = true;
     RunWorkerCompleted        += new RunWorkerCompletedEventHandler(simworker_RunWorkerCompleted);
     // if we're multi-core prepare to start I/O thread for this security
     if (Environment.ProcessorCount > 1)
     {
         DoWork += new DoWorkEventHandler(simworker_DoWork);
         workersec.HistSource.gotTick += new TickDelegate(HistSource_gotTick2);
     }
     else
     {
         workersec.HistSource.gotTick += new TickDelegate(HistSource_gotTick);
     }
 }
Beispiel #23
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);
        }
Beispiel #24
0
        /// <summary>
        /// Initializes a security with historical data from tick archive file
        /// </summary>
        public static SecurityImpl FromFile(string filename)
        {
            SecurityImpl s = null;

            try
            {
                System.IO.FileInfo     fi = new System.IO.FileInfo(filename);
                System.IO.StreamReader sr = new System.IO.StreamReader(filename);
                s           = eSigTick.InitEpf(sr);
                s._histfile = sr;
                // for epf files
                s._approxticks = (int)(fi.Length / 40);
            }
            catch (Exception) { }
            return(s);
        }
 public static SecurityImpl SecurityFromFileName(string strFile)
 {
     try
     {
         string strDate   = strFile.Substring(15, 8);
         int    symLength = strFile.Length - 41;
         string strSym    = strFile.Substring(30, symLength);
         //string sym = strFile.Replace(ds, "").Replace(TikConst.DOT_EXT, "");
         SecurityImpl s = new SecurityImpl(strSym);
         s.Date = Convert.ToInt32(strDate);
         return(s);
     }
     catch (System.Exception ex)
     {
     }
     return(new SecurityImpl());
 }
Beispiel #26
0
        public static SecurityImpl Parse(string msg, int date)
        {
            string[]     r   = msg.Split(' ');
            SecurityImpl sec = new SecurityImpl();

            sec.Symbol = r[0];
            if (r.Length > 2)
            {
                int f2id = SecurityID(r[2]);
                int f1id = SecurityID(r[1]);
                if (f2id != -1)
                {
                    sec.Type   = (SecurityType)f2id;
                    sec.DestEx = r[1];
                }
                else if (f1id != -1)
                {
                    sec.Type   = (SecurityType)f1id;
                    sec.DestEx = r[2];
                }
            }
            else if (r.Length > 1)
            {
                int f1id = SecurityID(r[1]);
                if (f1id != -1)
                {
                    sec.Type = (SecurityType)f1id;
                }
                else
                {
                    sec.DestEx = r[1];
                }
            }
            else
            {
                sec.Type = SecurityType.STK;
            }
            sec.Date = date;
            if (sec.hasDest && !sec.hasType)
            {
                sec.Type = TypeFromExchange(sec.DestEx);
            }
            return(sec);
        }
Beispiel #27
0
 public void BasketBasics()
 {
     BasketImpl mb = new BasketImpl();
     Assert.That(mb != null);
     SecurityImpl i = new SecurityImpl("IBM");
     mb = new BasketImpl(i);
     Assert.That(mb.hasStock);
     mb.Remove(i);
     Assert.That(!mb.hasStock);
     mb.Add(new SecurityImpl("LVS"));
     Assert.That(mb[0].Symbol=="LVS",mb[0].ToString());
     mb.Add(new SecurityImpl("IBM"));
     Assert.That(mb[1].Symbol=="IBM");
     BasketImpl newbasket = new BasketImpl(new SecurityImpl("FDX"));
     newbasket.Add(mb);
     mb.Clear();
     Assert.That(mb.Count==0);
     Assert.That(newbasket.Count==3);
 }
Beispiel #28
0
        bool SaveTick(Tick t)
        {
            if ((t.symbol == null) || (t.symbol == ""))
            {
                return(false);
            }
            if (filedict.ContainsKey(t.symbol))
            {
                try
                {
                    filedict[t.symbol].WriteLine(eSigTick.ToEPF(t));
                }
                catch (IOException) { return(false); }
            }
            else
            {
                string fn        = _path + @"/" + t.symbol + t.date + ".EPF";
                bool   hasheader = false;
                try
                {
                    if (File.Exists(fn))
                    {
                        StreamReader sr = new StreamReader(fn);
                        SecurityImpl s  = eSigTick.InitEpf(sr);
                        if (s.isValid)
                        {
                            hasheader = true;
                        }
                        sr.Close();
                    }
                    filedict.Add(t.symbol, new StreamWriter(fn, true));
                    if (!hasheader)
                    {
                        filedict[t.symbol].Write(eSigTick.EPFheader(t.symbol, t.date));
                    }
                    filedict[t.symbol].WriteLine(eSigTick.ToEPF(t));
                }
                catch (IOException) { return(false); }
                catch (Exception) { return(false); }
            }

            return(true);
        }
Beispiel #29
0
        public static Tick Deserialize(string msg)
        {
            string [] r = msg.Split(',');
            Tick      t = new TickImpl();

            t.Sec      = SecurityImpl.Parse(r[(int)TickField.symbol]);
            t.symbol   = t.Sec.Symbol;
            t.trade    = Convert.ToDecimal(r[(int)TickField.trade]);
            t.size     = Convert.ToInt32(r[(int)TickField.tsize]);
            t.bid      = Convert.ToDecimal(r[(int)TickField.bid]);
            t.ask      = Convert.ToDecimal(r[(int)TickField.ask]);
            t.os       = Convert.ToInt32(r[(int)TickField.asksize]);
            t.bs       = Convert.ToInt32(r[(int)TickField.bidsize]);
            t.ex       = r[(int)TickField.tex];
            t.be       = r[(int)TickField.bidex];
            t.oe       = r[(int)TickField.askex];
            t.time     = Convert.ToInt32(r[(int)TickField.time]);
            t.date     = Convert.ToInt32(r[(int)TickField.date]);
            t.datetime = t.date * 1000000 + t.time;
            t.depth    = Convert.ToInt32(r[(int)TickField.tdepth]);
            return(t);
        }
        public static TickFileInfo ParseFile(string filepath)
        {
            TickFileInfo tfi;

            tfi.type   = TickFileType.Invalid;
            tfi.date   = DateTime.MinValue;
            tfi.symbol = "";

            try
            {
                string fn  = System.IO.Path.GetFileNameWithoutExtension(filepath);
                string ext = System.IO.Path.GetExtension(filepath).Replace(".", "");

                SecurityImpl s = SecurityFromFileName(filepath);

                tfi.type   = (TickFileType)Enum.Parse(typeof(TickFileType), ext.ToUpper());
                tfi.date   = Util.TLD2DT(s.Date);
                tfi.symbol = s.symbol;
            }
            catch (Exception) { tfi.type = TickFileType.Invalid; }
            return(tfi);
        }
Beispiel #31
0
        public static BasketImpl Deserialize(string serialBasket)
        {
            BasketImpl mb = new BasketImpl();

            if ((serialBasket == null) || (serialBasket == ""))
            {
                return(mb);
            }
            string[] r = serialBasket.Split(',');
            for (int i = 0; i < r.Length; i++)
            {
                if (r[i] == "")
                {
                    continue;
                }
                SecurityImpl sec = SecurityImpl.Parse(r[i]);
                if (sec.isValid)
                {
                    mb.Add(sec);
                }
            }
            return(mb);
        }
Beispiel #32
0
        SecurityImpl getsec(string file)
        {
            SecurityImpl s = SecurityImpl.FromTIK(file);

            return(s);
        }
Beispiel #33
0
 /// <summary>
 /// try to fetch security using long then short symbol
 /// </summary>
 /// <param name="sym"></param>
 /// <param name="sec"></param>
 /// <returns></returns>
 public bool TryGetSecurityAnySymbol(string sym, out Security sec)
 {
     var idx = longsym2secidx.getindex(sym);
     sec = new SecurityImpl();
     if (idx < 0)
     {
         idx = shortsym2secidx.getindex(sym); 
         if (idx<0)
             return false;
     }
     sec = this[idx];
     return true;
 }
Beispiel #34
0
        /// <summary>
        /// determine security from the filename, without opening file
        /// (use SecurityImpl.FromFile to actually read it in)
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SecurityImpl SecurityFromFileName(string filename)
        {
            try
            {
                filename = System.IO.Path.GetFileName(filename);
                string ds = System.Text.RegularExpressions.Regex.Match(filename, "([0-9]{8})[.]", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Result("$1");
                string sym = filename.Replace(ds, "").Replace(TikConst.DOT_EXT, "");
                SecurityImpl s = new SecurityImpl(sym);
                s.Date = Convert.ToInt32(ds);
                return s;
            }
            catch (Exception) { }
            return new SecurityImpl();

        }
Beispiel #35
0
        /// <summary>
        /// get a security form a user-specified string
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static SecurityImpl Parse(string msg, int date)
        {
            string[] r = msg.Split(' ');
            SecurityImpl sec = new SecurityImpl();
            sec.Symbol = r[0];
            // look for option first
            if (msg.Contains("OPT") || msg.Contains("PUT") || msg.Contains("CALL") || msg.Contains("FOP") || msg.Contains("FUT"))
            {
                if (msg.Contains("OPT") || msg.Contains("PUT") || msg.Contains("CALL")) 
                    sec.Type = SecurityType.OPT;
                
            	if(msg.Contains("FUT")) sec.Type = SecurityType.FUT;
            	if(msg.Contains("FOP")) sec.Type = SecurityType.FOP;
            	   
                msg = msg.ToUpper();
                sec.Details = msg.Contains("PUT") ? "PUT" : (msg.Contains("CALL") ? "CALL" : string.Empty);
                msg = msg.Replace("CALL", "");
                msg = msg.Replace("PUT", "");
                msg = msg.Replace("OPT", "");
                msg=msg.Replace("FOP","");
                r = msg.Split(' ');
                sec.Symbol = r[0];
                sec.Date = ExpirationDate(ref r);
                sec.Strike = StrikePrice(ref r);
                sec.DestEx = Ex(sec.Symbol,ref r);

            }
            else if (r.Length > 2)
            {
                int f2id = SecurityID(r[2]);
                int f1id = SecurityID(r[1]);
                if (f2id != -1)
                {
                    sec.Type = (SecurityType)f2id;
                    sec.DestEx = r[1];
                }
                else if (f1id != -1)
                {
                    sec.Type = (SecurityType)f1id;
                    sec.DestEx = r[2];
                }
            }
            else if (r.Length > 1)
            {
                int f1id = SecurityID(r[1]);
                if (f1id != -1)
                    sec.Type = (SecurityType)f1id;
                else sec.DestEx = r[1];
            }
            else
                sec.Type = SecurityType.STK;
            if (date!=0)
                sec.Date = date;
            if (sec.hasDest && !sec.hasType)
                sec.Type = TypeFromExchange(sec.DestEx);
            return sec;
        }
Beispiel #36
0
        public simworker(SecurityImpl sec)
        {
            workersec = sec;
            WorkerSupportsCancellation = true;
            RunWorkerCompleted += new RunWorkerCompletedEventHandler(simworker_RunWorkerCompleted);
            // if we're multi-core prepare to start I/O thread for this security
            if (Environment.ProcessorCount > 1)
            {
                DoWork += new DoWorkEventHandler(simworker_DoWork);
                workersec.HistSource.gotTick += new TickDelegate(HistSource_gotTick2);
            }
            else
            {
                workersec.HistSource.gotTick += new TickDelegate(HistSource_gotTick);
            }

 
        }
Beispiel #37
0
 /// <summary>
 /// Create a basket of securities
 /// </summary>
 /// <param name="firstsec">security</param>
 public BasketImpl(SecurityImpl firstsec)
 {
     Add(firstsec);
 }
Beispiel #38
0
 /// <summary>
 /// Create a basket of securities
 /// </summary>
 /// <param name="firstsec">security</param>
 public BasketImpl(SecurityImpl firstsec)
 {
     Add(firstsec);
 }
Beispiel #39
0
        /// <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();
            }
        }
Beispiel #40
0
        /// <summary>
        /// Reinitialize the cache
        /// </summary>
        public void Initialize()
        {
            if (_inited)
            {
                return;          // only init once
            }
            if (_tickfiles.Length == 0)
            {
                // get our listings of historical files (idx and epf)
                string[] files = Directory.GetFiles(_folder, tickext);
                _tickfiles = _filter.Allows(files);
            }
            List <long>         d  = new List <long>(_tickfiles.Length);
            List <SecurityImpl> ss = new List <SecurityImpl>(_tickfiles.Length);

            // now we have our list, initialize instruments from files
            for (int i = 0; i < _tickfiles.Length; i++)
            {
                SecurityImpl s = getsec(i);
                if ((s != null) && s.isValid && s.HistSource.isValid)
                {
                    s.HistSource.gotTick += new TickDelegate(HistSource_gotTick);
                    ss.Add(s);
                    d.Add(s.Date);
                }
            }
            // setup our initial index
            long[]         didx = d.ToArray();
            SecurityImpl[] sidx = ss.ToArray();
            Array.Sort(didx, sidx);
            // save index and objects in order
            secs.Clear();
            dates.Clear();
            secs.AddRange(sidx);
            dates.AddRange(didx);
            doneidx = _tickfiles.Length - 1;


            D("Initialized " + (_tickfiles.Length) + " instruments.");
            D(string.Join(Environment.NewLine.ToString(), _tickfiles));
            // check for event
            if (GotTick != null)
            {
                hasevent = true;
            }
            else
            {
                D("No GotTick event defined!");
            }
            // read in single tick just to get first time for user
            isnexttick();

            // get total ticks represented by files
            _availticks = 0;
            for (int i = 0; i < secs.Count; i++)
            {
                if (secs[i] != null)
                {
                    _availticks += secs[i].ApproxTicks;
                }
            }

            D("Approximately " + TicksPresent + " ticks to process...");
            _inited = true;
        }
Beispiel #41
0
 /// <summary>
 /// Create a basket of securities
 /// </summary>
 /// <param name="securities"></param>
 public BasketImpl(SecurityImpl[] securities)
 {
     foreach (SecurityImpl s in securities)
         Add(s);
 }
Beispiel #42
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);
        }
Beispiel #43
0
 /// <summary>
 /// Create an epf file header
 /// </summary>
 /// <param name="sec">The security</param>
 /// <returns></returns>
 public static string EPFheader(SecurityImpl sec)
 {
 	return EPFheader(sec.Symbol,sec.Date);
 }
Beispiel #44
0
        public simworker(SecurityImpl sec)
        {
            workersec = sec;
            WorkerSupportsCancellation = true;
            RunWorkerCompleted += new RunWorkerCompletedEventHandler(simworker_RunWorkerCompleted);
            // if we're multi-core start reading into cache immediately in background
            if (Environment.ProcessorCount>1)
                DoWork += new DoWorkEventHandler(simworker_DoWork);
 
        }
Beispiel #45
0
        /// <summary>
        /// get a security form a user-specified string
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static SecurityImpl Parse(string msg, int date)
        {
            string[]     r   = msg.Split(' ');
            SecurityImpl sec = new SecurityImpl();

            sec.symbol = r[0];
            // look for option first
            if (msg.Contains("OPT") || msg.Contains("PUT") || msg.Contains("CALL") || msg.Contains("FOP") || msg.Contains("FUT"))
            {
                if (msg.Contains("OPT") || msg.Contains("PUT") || msg.Contains("CALL"))
                {
                    sec.Type = SecurityType.OPT;
                }

                if (msg.Contains("FUT"))
                {
                    sec.Type = SecurityType.FUT;
                }
                if (msg.Contains("FOP"))
                {
                    sec.Type = SecurityType.FOP;
                }

                msg         = msg.ToUpper();
                sec.Details = msg.Contains("PUT") ? "PUT" : (msg.Contains("CALL") ? "CALL" : string.Empty);
                msg         = msg.Replace("CALL", "");
                msg         = msg.Replace("PUT", "");
                msg         = msg.Replace("OPT", "");
                msg         = msg.Replace("FOP", "");
                msg         = msg.Replace("FUT", "");
                r           = msg.Split(' ');
                sec.symbol  = r[0];
                sec.Date    = ExpirationDate(ref r);
                sec.Strike  = StrikePrice(ref r);
                sec.DestEx  = Ex(sec.symbol, ref r);
            }
            else if (r.Length > 2)
            {
                int f2id = SecurityID(r[2]);
                int f1id = SecurityID(r[1]);
                if (f2id != -1)
                {
                    sec.Type   = (SecurityType)f2id;
                    sec.DestEx = r[1];
                }
                else if (f1id != -1)
                {
                    sec.Type   = (SecurityType)f1id;
                    sec.DestEx = r[2];
                }
            }
            else if (r.Length > 1)
            {
                int f1id = SecurityID(r[1]);
                if (f1id != -1)
                {
                    sec.Type = (SecurityType)f1id;
                }
                else
                {
                    sec.DestEx = r[1];
                }
            }
            else
            {
                sec.Type = SecurityType.STK;
            }
            if (date != 0)
            {
                sec.Date = date;
            }
            if (sec.hasDest && !sec.hasType)
            {
                sec.Type = TypeFromExchange(sec.DestEx);
            }
            return(sec);
        }