Пример #1
0
        public static Imbalance Deserialize(string msg)
        {
            ImbalanceImpl i = new ImbalanceImpl();

            string[] r = msg.Split(',');
            int      v = 0;

            if (int.TryParse(r[(int)ImbalanceField.IF_SIZE], out v))
            {
                i._size = v;
            }
            if (int.TryParse(r[(int)ImbalanceField.IF_TIME], out v))
            {
                i._time = v;
            }
            if (int.TryParse(r[(int)ImbalanceField.IF_PSIZE], out v))
            {
                i._psize = v;
            }
            if (int.TryParse(r[(int)ImbalanceField.IF_PTIME], out v))
            {
                i._ptime = v;
            }
            if (int.TryParse(r[(int)ImbalanceField.IF_INFO], out v))
            {
                i._info = v;
            }
            i._sym = r[(int)ImbalanceField.IF_SYM];
            i._ex  = r[(int)ImbalanceField.IF_EX];
            return(i);
        }
Пример #2
0
 public void newImbalance(Imbalance imb)
 {
     for (int i = 0; i < client.Count; i++)
     {
         TLSend(ImbalanceImpl.Serialize(imb), MessageTypes.IMBALANCERESPONSE, i);
     }
 }
Пример #3
0
        public void SerializeDeserilize()
        {
            // setup imbalance
            const string s = "IBM";
            const string e = "NYSE";
            const int x = 500000;
            const int t = 1550;
            const int p = 1540;
            const int px = 400000;
            Imbalance i = new ImbalanceImpl(s, e, x, t, px, p,999);

            // verify it's valid
            Assert.IsTrue(i.isValid);

            // serialize it
            string msg = ImbalanceImpl.Serialize(i);
            // deserialize it somewhere else
            Imbalance ni = ImbalanceImpl.Deserialize(msg);

            // make sure it's valid
            Assert.IsTrue(ni.isValid);

            // verify it's the same
            Assert.AreEqual(i.Symbol, ni.Symbol);
            Assert.AreEqual(i.Exchange, ni.Exchange);
            Assert.AreEqual(i.PrevImbalance, ni.PrevImbalance);
            Assert.AreEqual(i.PrevTime, ni.PrevTime);
            Assert.AreEqual(i.ThisImbalance, ni.ThisImbalance);
            Assert.AreEqual(i.ThisTime, ni.ThisTime);
            Assert.AreEqual(i.InfoImbalance, ni.InfoImbalance);
        }
Пример #4
0
 public void newImbalance(Imbalance imb)
 {
     for (int i = 0; i < client.Count; i++)
     {
         WMUtil.SendMsg(ImbalanceImpl.Serialize(imb), hims[i], Handle, (int)MessageTypes.IMBALANCERESPONSE);
     }
 }
Пример #5
0
        public static Imbalance[] SampleImbalanceData(int count)
        {
            Random r = new Random((int)DateTime.Now.Ticks);
            string[] syms = TradeLink.Research.RandomSymbol.GetSymbols((int)DateTime.Now.Ticks, 4,count);
            Imbalance[] imbs = new Imbalance[syms.Length];
            for (int j = 0; j < syms.Length; j++)
            {
                imbs[j] = new ImbalanceImpl(syms[j], "NYSE", r.Next(-1000, 1000) * r.Next(1000), 1550, r.Next(-1000, 1000) * r.Next(1000), 1540, 0);
            }
            return imbs;

        }
Пример #6
0
 public static Imbalance Deserialize(string msg)
 {
     ImbalanceImpl i = new ImbalanceImpl();
     string[] r = msg.Split(',');
     i._size= Convert.ToInt32(r[(int)ImbalanceField.IF_SIZE]);
     i._time= Convert.ToInt32(r[(int)ImbalanceField.IF_TIME]);
     i._psize= Convert.ToInt32(r[(int)ImbalanceField.IF_PSIZE]);
     i._ptime= Convert.ToInt32(r[(int)ImbalanceField.IF_PTIME]);
     i._sym= r[(int)ImbalanceField.IF_SYM];
     i._ex= r[(int)ImbalanceField.IF_EX];
     i._info = Convert.ToInt32(r[(int)ImbalanceField.IF_INFO]);
     return i;
 }
Пример #7
0
        public static Imbalance Deserialize(string msg)
        {
            ImbalanceImpl i = new ImbalanceImpl();

            string[] r = msg.Split(',');
            i._size  = Convert.ToInt32(r[(int)ImbalanceField.IF_SIZE]);
            i._time  = Convert.ToInt32(r[(int)ImbalanceField.IF_TIME]);
            i._psize = Convert.ToInt32(r[(int)ImbalanceField.IF_PSIZE]);
            i._ptime = Convert.ToInt32(r[(int)ImbalanceField.IF_PTIME]);
            i._sym   = r[(int)ImbalanceField.IF_SYM];
            i._ex    = r[(int)ImbalanceField.IF_EX];
            i._info  = Convert.ToInt32(r[(int)ImbalanceField.IF_INFO]);
            return(i);
        }
Пример #8
0
 public static Imbalance Deserialize(string msg)
 {
     ImbalanceImpl i = new ImbalanceImpl();
     string[] r = msg.Split(',');
     int v = 0;
     if (int.TryParse(r[(int)ImbalanceField.IF_SIZE], out v))
         i._size = v;
     if (int.TryParse(r[(int)ImbalanceField.IF_TIME], out v))
         i._time = v;
     if (int.TryParse(r[(int)ImbalanceField.IF_PSIZE], out v))
         i._psize = v;
     if (int.TryParse(r[(int)ImbalanceField.IF_PTIME], out v))
         i._ptime = v;
     if (int.TryParse(r[(int)ImbalanceField.IF_INFO], out v))
         i._info = v;
     i._sym= r[(int)ImbalanceField.IF_SYM];
     i._ex= r[(int)ImbalanceField.IF_EX];
     return i;
 }
Пример #9
0
        void handle(MessageTypes type, string msg)
        {
            long result = 0;

            switch (type)
            {
            case MessageTypes.TICKNOTIFY:
                Tick t;
                try
                {
                    _lastheartbeat = DateTime.Now.Ticks;
                    t = TickImpl.Deserialize(msg);
                }
                catch (Exception ex)
                {
                    _tickerrors++;
                    debug("Error processing tick: " + msg);
                    debug("TickErrors: " + _tickerrors);
                    debug("Error: " + ex.Message + ex.StackTrace);
                    break;
                }
                if (gotTick != null)
                {
                    gotTick(t);
                }
                break;

            case MessageTypes.IMBALANCERESPONSE:
                Imbalance i = ImbalanceImpl.Deserialize(msg);
                _lastheartbeat = DateTime.Now.Ticks;
                if (gotImbalance != null)
                {
                    gotImbalance(i);
                }
                break;

            case MessageTypes.ORDERCANCELRESPONSE:
            {
                long id = 0;
                _lastheartbeat = DateTime.Now.Ticks;
                if (gotOrderCancel != null)
                {
                    if (long.TryParse(msg, out id))
                    {
                        gotOrderCancel(id);
                    }
                    else if (SendDebugEvent != null)
                    {
                        SendDebugEvent("Count not parse order cancel: " + msg);
                    }
                }
            }
            break;

            case MessageTypes.EXECUTENOTIFY:
                _lastheartbeat = DateTime.Now.Ticks;
                // date,time,symbol,side,size,price,comment
                Trade tr = TradeImpl.Deserialize(msg);
                if (gotFill != null)
                {
                    gotFill(tr);
                }
                break;

            case MessageTypes.ORDERNOTIFY:
                _lastheartbeat = DateTime.Now.Ticks;
                Order o = OrderImpl.Deserialize(msg);
                if (gotOrder != null)
                {
                    gotOrder(o);
                }
                break;

            case MessageTypes.HEARTBEATRESPONSE:
            {
                _lastheartbeat = DateTime.Now.Ticks;
                v("got heartbeat response at: " + _lastheartbeat);
                _recvheartbeat = !_recvheartbeat;
            }
            break;

            case MessageTypes.POSITIONRESPONSE:
                try
                {
                    _lastheartbeat = DateTime.Now.Ticks;
                    Position pos = PositionImpl.Deserialize(msg);
                    if (gotPosition != null)
                    {
                        gotPosition(pos);
                    }
                }
                catch (Exception ex)
                {
                    if (SendDebugEvent != null)
                    {
                        SendDebugEvent(msg + " " + ex.Message + ex.StackTrace);
                    }
                }
                break;

            case MessageTypes.ACCOUNTRESPONSE:
                _lastheartbeat = DateTime.Now.Ticks;
                if (gotAccounts != null)
                {
                    gotAccounts(msg);
                }
                break;

            case MessageTypes.FEATURERESPONSE:
                _lastheartbeat = DateTime.Now.Ticks;
                string[]            p = msg.Split(',');
                List <MessageTypes> f = new List <MessageTypes>();
                _rfl.Clear();
                foreach (string s in p)
                {
                    try
                    {
                        MessageTypes mt = (MessageTypes)Convert.ToInt32(s);
                        f.Add(mt);
                        _rfl.Add(mt);
                    }
                    catch (Exception) { }
                }
                if (gotFeatures != null)
                {
                    gotFeatures(f.ToArray());
                }
                break;

            case MessageTypes.SERVERDOWN:
                if (gotServerDown != null)
                {
                    gotServerDown(msg);
                }
                break;

            case MessageTypes.SERVERUP:
                if (gotServerUp != null)
                {
                    gotServerUp(msg);
                }
                break;

            default:
                _lastheartbeat = DateTime.Now.Ticks;
                if (gotUnknownMessage != null)
                {
                    gotUnknownMessage(type, 0, 0, 0, string.Empty, ref msg);
                }
                break;
            }
            result = 0;
        }
Пример #10
0
        void doquote(ref structSTIQuoteUpdate q)
        {
            Tick k = new TickImpl(q.bstrSymbol);
            k.bid = (decimal)q.fBidPrice;
            k.ask = (decimal)q.fAskPrice;
            k.bs = q.nBidSize / 100;
            k.os = q.nAskSize / 100;
            k.ex = GetExPretty(q.bstrExch);
            k.be = GetExPretty(q.bstrBidExch);
            k.oe = GetExPretty(q.bstrAskExch);
            int now = Convert.ToInt32(q.bstrUpdateTime);
            k.date = Util.ToTLDate(DateTime.Now);
            //int sec = now % 100;
            k.time = now;

            // we don't want to simply return on out-of-order ticks because it'll prevent processing
            // of the mdx messages further in this function.
            if (!IgnoreOutOfOrderTicks || (k.time > _lasttime))
            {
                _lasttime = k.time;
                k.trade = (decimal)q.fLastPrice;
                k.size = q.nLastSize;
                // execute orders if papertrade is enabled
                if (isPaperTradeEnabled)
                    ptt.newTick(k);
                // notify clients of tick
                if (!_imbalance || (_imbalance && k.isValid))
                    tl.newTick(k);
            }

            /////////////////////////
            // MDX Processing
            /////////////////////////
            if (q.nMdxMsgType == 1)
            {
                if (VerboseDebugging)
                    debug(q.bstrUpdateTime
                    + "  Received Regulatory Imbalance for: " + q.bstrSymbol
                    + "  ValidIntradayMarketImb: " + q.bValidIntradayMktImb
                    + "  ValidMktImb: " + q.bValidMktImb
                    + "  Imbalance: " + q.nImbalance
                    + "  iMktImbalance: " + q.nIntradayMktImbalance
                    + "  MktImbalance: " + q.nMktImbalance);

                int time;
                if (int.TryParse(q.bstrUpdateTime, out time))
                {
                    Imbalance imb = new ImbalanceImpl(q.bstrSymbol, GetExPretty(q.bstrExch), q.nIntradayMktImbalance, time, 0, 0, 0);
                    tl.newImbalance(imb);
                }
            }
            else if (q.nMdxMsgType == 2)
            {
                if (VerboseDebugging)
                    debug(q.bstrUpdateTime
                    + "  Received Informational Imbalance for: " + q.bstrSymbol
                    + "  ValidIntradayMarketImb: " + q.bValidIntradayMktImb
                    + "  ValidMktImb: " + q.bValidMktImb
                    + "  Imbalance: " + q.nImbalance
                    + "  iMktImbalance: " + q.nIntradayMktImbalance
                    + "  MktImbalance: " + q.nMktImbalance);

                int time;
                if (int.TryParse(q.bstrUpdateTime, out time))
                {
                    Imbalance imb = new ImbalanceImpl(q.bstrSymbol, GetExPretty(q.bstrExch), 0, time, 0, 0, q.nIntradayMktImbalance);
                    tl.newImbalance(imb);
                }
            }
            else if (q.nMdxMsgType == 3)
            {
                if (VerboseDebugging)
                    debug(q.bstrUpdateTime
                    + "  Received Halt/Delay for: " + q.bstrSymbol
                    + "  Status: " + q.bstrHaltResumeStatus
                    + "  Reason: " + q.bstrHaltResumeReason);

                int time;
                if (int.TryParse(q.bstrUpdateTime, out time))
                {
                    HaltResume h = new HaltResumeImpl(q.bstrSymbol, GetExPretty(q.bstrExch), time, q.bstrHaltResumeStatus, q.bstrHaltResumeReason);
                    for (int clientNumber = 0; clientNumber < tl.NumClients; clientNumber++)
                        tl.TLSend(HaltResumeImpl.Serialize(h), MessageTypes.HALTRESUME, clientNumber);
                }
            }
            else if (q.nMdxMsgType == 4)
            {
                if (VerboseDebugging)
                    debug(q.bstrUpdateTime
                    + "  Received Indication for: " + q.bstrSymbol
                    + "  ValidIndicators: " + q.bValidIndicators
                    + "  IndicatorHigh: " + q.fIndicatorHigh
                    + "  IndicatorLow: " + q.fIndicatorLow);

                int time;
                if (int.TryParse(q.bstrUpdateTime, out time))
                {
                    Indication ind = new IndicationImpl(q.bstrSymbol, GetExPretty(q.bstrExch), time, q.bValidIndicators, (decimal)q.fIndicatorHigh, (decimal)q.fIndicatorLow);
                    for (int clientNumber = 0; clientNumber < tl.NumClients; clientNumber++)
                        tl.TLSend(IndicationImpl.Serialize(ind), MessageTypes.INDICATION, clientNumber);
                }
            }
        }
Пример #11
0
 void m_Session_OnNYSEImbalanceMessage(object sender, BWNYSEImbalance imbalanceMsg)
 {
     string s = imbalanceMsg.Symbol;
     int i = imbalanceMsg.ImbalanceVolume;
     int it = TradeLink.Common.Util.DT2FT(imbalanceMsg.Time);
     int pi = imbalanceMsg.InitImbalanceVolume;
     int pt = TradeLink.Common.Util.DT2FT(imbalanceMsg.InitTime);
     string ex = imbalanceMsg.FeedID.ToString();
     Imbalance imb = new ImbalanceImpl(s, ex, i, it, pi, pt, i);
     tl.newImbalance(imb);
 }
Пример #12
0
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            long             result = 0;
            TradeLinkMessage tlm    = WMUtil.ToTradeLinkMessage(ref m);

            if (tlm == null)         // if it's not a WM_COPYDATA message
            {
                base.WndProc(ref m); // let form process it
                return;              // we're done
            }

            string msg = tlm.body;

            switch (tlm.type)
            {
            case MessageTypes.TICKNOTIFY:
                Tick t;
                try
                {
                    t = TickImpl.Deserialize(msg);
                }
                catch (Exception ex)
                {
                    _tickerrors++;
                    debug("Error processing tick: " + msg);
                    debug("TickErrors: " + _tickerrors);
                    debug("Error: " + ex.Message + ex.StackTrace);
                    break;
                }
                if (gotTick != null)
                {
                    gotTick(t);
                }
                break;

            case MessageTypes.IMBALANCERESPONSE:
                Imbalance i = ImbalanceImpl.Deserialize(msg);
                if (gotImbalance != null)
                {
                    gotImbalance(i);
                }
                break;

            case MessageTypes.ORDERCANCELRESPONSE:
            {
                long id = 0;
                if (gotOrderCancel != null)
                {
                    if (long.TryParse(msg, out id))
                    {
                        gotOrderCancel(id);
                    }
                    else if (SendDebugEvent != null)
                    {
                        SendDebugEvent("Count not parse order cancel: " + msg);
                    }
                }
            }
            break;

            case MessageTypes.EXECUTENOTIFY:
                // date,time,symbol,side,size,price,comment
                try
                {
                    Trade tr = TradeImpl.Deserialize(msg);
                    if (gotFill != null)
                    {
                        gotFill(tr);
                    }
                }
                catch (Exception ex)
                {
                    debug("error deserializing fill: " + msg);
                    debug("error: " + ex.Message + ex.StackTrace);
                    debug("broker: " + BrokerName);
                }
                break;

            case MessageTypes.ORDERNOTIFY:
                try
                {
                    Order o = OrderImpl.Deserialize(msg);
                    if (gotOrder != null)
                    {
                        gotOrder(o);
                    }
                }
                catch (Exception ex)
                {
                    debug("error deserializing order: " + msg);
                    debug("error: " + ex.Message + ex.StackTrace);
                    debug("broker: " + BrokerName);
                }
                break;

            case MessageTypes.POSITIONRESPONSE:
                try
                {
                    Position pos = PositionImpl.Deserialize(msg);
                    if (gotPosition != null)
                    {
                        gotPosition(pos);
                    }
                }
                catch (Exception ex)
                {
                    if (SendDebugEvent != null)
                    {
                        SendDebugEvent(msg + " " + ex.Message + ex.StackTrace);
                    }
                }
                break;

            case MessageTypes.ACCOUNTRESPONSE:
                if (gotAccounts != null)
                {
                    gotAccounts(msg);
                }
                break;

            case MessageTypes.FEATURERESPONSE:
                string[]            p = msg.Split(',');
                List <MessageTypes> f = new List <MessageTypes>();
                foreach (string s in p)
                {
                    try
                    {
                        f.Add((MessageTypes)Convert.ToInt32(s));
                    }
                    catch (Exception) { }
                }
                if (gotFeatures != null)
                {
                    gotFeatures(f.ToArray());
                }
                if (gotUnknownMessage != null)
                {
                    gotUnknownMessage(tlm.type, 0, 0, 0, string.Empty, ref tlm.body);
                }
                break;

            case MessageTypes.SERVERDOWN:
                if (gotServerDown != null)
                {
                    gotServerDown(msg);
                }
                break;

            case MessageTypes.SERVERUP:
                if (gotServerUp != null)
                {
                    gotServerUp(msg);
                }
                break;

            default:
                if (gotUnknownMessage != null)
                {
                    gotUnknownMessage(tlm.type, 0, 0, 0, string.Empty, ref tlm.body);
                }
                break;
            }
            result   = 0;
            m.Result = (IntPtr)result;
        }
Пример #13
0
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            long             result = 0;
            TradeLinkMessage tlm    = WMUtil.ToTradeLinkMessage(ref m);

            if (tlm == null)         // if it's not a WM_COPYDATA message
            {
                base.WndProc(ref m); // let form process it
                return;              // we're done
            }

            string msg = tlm.body;

            string[] r = msg.Split(',');
            switch (tlm.type)
            {
            case MessageTypes.ORDERCANCELRESPONSE:
                if (gotOrderCancel != null)
                {
                    gotOrderCancel(Convert.ToUInt32(msg));
                }
                break;

            case MessageTypes.TICKNOTIFY:
                Tick t = TickImpl.Deserialize(msg);
                if (t.isTrade)
                {
                    try
                    {
                        if (t.trade > chighs[t.symbol])
                        {
                            chighs[t.symbol] = t.trade;
                        }
                        if (t.trade < clows[t.symbol])
                        {
                            clows[t.symbol] = t.trade;
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                        chighs.Add(t.symbol, 0);
                        clows.Add(t.symbol, decimal.MaxValue);
                    }
                }
                if (gotTick != null)
                {
                    gotTick(t);
                }
                break;

            case MessageTypes.EXECUTENOTIFY:
                // date,time,symbol,side,size,price,comment
                Trade tr = TradeImpl.Deserialize(msg);
                if (gotFill != null)
                {
                    gotFill(tr);
                }
                break;

            case MessageTypes.ORDERNOTIFY:
                Order o = OrderImpl.Deserialize(msg);
                if (gotOrder != null)
                {
                    gotOrder(o);
                }
                break;

            case MessageTypes.POSITIONRESPONSE:
                Position pos = PositionImpl.Deserialize(msg);
                if (gotPosition != null)
                {
                    gotPosition(pos);
                }
                break;

            case MessageTypes.ACCOUNTRESPONSE:
                if (gotAccounts != null)
                {
                    gotAccounts(msg);
                }
                break;

            case MessageTypes.FEATURERESPONSE:
                string[]            p = msg.Split(',');
                List <MessageTypes> f = new List <MessageTypes>();
                foreach (string s in p)
                {
                    try
                    {
                        f.Add((MessageTypes)Convert.ToInt32(s));
                    }
                    catch (Exception) { }
                }
                if (gotSupportedFeatures != null)
                {
                    gotSupportedFeatures(f.ToArray());
                }
                break;

            case MessageTypes.IMBALANCERESPONSE:
                Imbalance i = ImbalanceImpl.Deserialize(msg);
                if (gotImbalance != null)
                {
                    gotImbalance(i);
                }
                break;
            }
            result   = 0;
            m.Result = (IntPtr)result;
        }