Пример #1
0
        public static TradeLinkMessage ToTradeLinkMessage(ref System.Windows.Forms.Message windowsmessageref)
        {
            if (windowsmessageref.Msg != WM_COPYDATA)
            {
                return(null);
            }
            TradeLinkMessage m   = new TradeLinkMessage();
            COPYDATASTRUCT   cds = new COPYDATASTRUCT();

            cds = (COPYDATASTRUCT)Marshal.PtrToStructure(windowsmessageref.LParam, typeof(COPYDATASTRUCT));
            if (cds.cbData > 0)
            {
                m.body = Marshal.PtrToStringAnsi(cds.lpData);
                m.type = (MessageTypes)(int)cds.dwData;
            }
            return(m);
        }
Пример #2
0
 public static TradeLinkMessage ToTradeLinkMessage(ref System.Windows.Forms.Message windowsmessageref)
 {
     if (windowsmessageref.Msg != WM_COPYDATA)
         return null;
     TradeLinkMessage m = new TradeLinkMessage();
     COPYDATASTRUCT cds = new COPYDATASTRUCT();
     cds = (COPYDATASTRUCT)Marshal.PtrToStructure(windowsmessageref.LParam, typeof(COPYDATASTRUCT));
     if (cds.cbData > 0)
     {
         m.body = Marshal.PtrToStringAnsi(cds.lpData);
         m.type = (MessageTypes)(int)cds.dwData;
     }
     return m;
 }
Пример #3
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;
        }
Пример #4
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;
        }
Пример #5
0
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            TradeLinkMessage tlm = WMUtil.ToTradeLinkMessage(ref m);

            if (tlm == null)
            {
                base.WndProc(ref m);
                return;
            }
            string msg = tlm.body;

            long result = (long)MessageTypes.OK;

            switch (tlm.type)
            {
            case MessageTypes.ACCOUNTREQUEST:
                if (newAcctRequest == null)
                {
                    break;
                }
                string accts = newAcctRequest();
                TLSend(accts, MessageTypes.ACCOUNTRESPONSE, msg);
                break;

            case MessageTypes.POSITIONREQUEST:
                if (newPosList == null)
                {
                    break;
                }
                string [] pm = msg.Split('+');
                if (pm.Length < 2)
                {
                    break;
                }
                string     client = pm[0];
                string     acct   = pm[1];
                Position[] list   = newPosList(acct);
                foreach (Position pos in list)
                {
                    TLSend(PositionImpl.Serialize(pos), MessageTypes.POSITIONRESPONSE, client);
                }
                break;

            case MessageTypes.ORDERCANCELREQUEST:
            {
                long id = 0;
                if (long.TryParse(msg, out id) && (newOrderCancelRequest != null))
                {
                    newOrderCancelRequest(id);
                }
            }
            break;

            case MessageTypes.SENDORDER:
                SrvDoExecute(msg);
                break;

            case MessageTypes.REGISTERCLIENT:
                SrvRegClient(msg);
                break;

            case MessageTypes.REGISTERSTOCK:
                string[] m2 = msg.Split('+');
                SrvRegStocks(m2[0], m2[1]);
                break;

            case MessageTypes.CLEARCLIENT:
                SrvClearClient(msg);
                break;

            case MessageTypes.CLEARSTOCKS:
                SrvClearStocks(msg);
                break;

            case MessageTypes.HEARTBEATREQUEST:
                SrvBeatHeart(msg);
                break;

            case MessageTypes.BROKERNAME:
                result = (long)newProviderName;
                break;

            case MessageTypes.IMBALANCEREQUEST:
                if (newImbalanceRequest != null)
                {
                    newImbalanceRequest();
                }
                break;

            case MessageTypes.FEATUREREQUEST:
                string msf            = "";
                List <MessageTypes> f = new List <MessageTypes>();
                f.Add(MessageTypes.HEARTBEATREQUEST);
                f.Add(MessageTypes.CLEARCLIENT);
                f.Add(MessageTypes.CLEARSTOCKS);
                f.Add(MessageTypes.REGISTERCLIENT);
                f.Add(MessageTypes.REGISTERSTOCK);
                f.Add(MessageTypes.FEATUREREQUEST);
                f.Add(MessageTypes.FEATURERESPONSE);
                f.Add(MessageTypes.VERSION);
                f.Add(MessageTypes.BROKERNAME);
                List <string> mf = new List <string>();
                foreach (MessageTypes t in f)
                {
                    int ti = (int)t;
                    mf.Add(ti.ToString());
                }
                if (newFeatureRequest != null)
                {
                    MessageTypes[] f2 = newFeatureRequest();
                    foreach (MessageTypes t in f2)
                    {
                        int ti = (int)t;
                        mf.Add(ti.ToString());
                    }
                }
                msf = string.Join(",", mf.ToArray());
                TLSend(msf, MessageTypes.FEATURERESPONSE, msg);
                break;

            case MessageTypes.VERSION:
                result = (long)MinorVer;
                break;

            default:
                if (newUnknownRequest != null)
                {
                    result = newUnknownRequest(tlm.type, msg);
                }
                else
                {
                    result = (long)MessageTypes.FEATURE_NOT_IMPLEMENTED;
                }
                break;
            }
            m.Result = (IntPtr)result;
        }