Exemplo n.º 1
0
        public void OnRecv(byte[] data)
        {
            if (data == null)
            {
                return;
            }

            string str;

            int[] ids;

            try
            {
                str = Encoding.UTF8.GetString(data);
            }
            catch
            {
                return;
            }

            string cmd = CmdParse(str, out ids);

            if (string.IsNullOrEmpty(cmd))
            {
                return;
            }

            if (cmd == "GetInstrumList")
            {
                var instrums = _instrumTable.GetInstrums();
                var json     = JsonConvert.SerializeObject(instrums);
                var bytes    = Encoding.UTF8.GetBytes(json);
                _core.SendResponseAsync(this, bytes).Wait();
            }
            else if (cmd == "GetAccountList")
            {
                var accounts = _accountTable.GetAccounts();
                var json     = JsonConvert.SerializeObject(accounts);
                var bytes    = Encoding.UTF8.GetBytes(json);
                _core.SendResponseAsync(this, bytes).Wait();
            }
            else if (cmd == "GetStopOrders")
            {
                if (ids.Length == 2)
                {
                    var list  = _stopOrderTable.GetStopOrders(ids[0], ids[1]);
                    var json  = JsonConvert.SerializeObject(list);
                    var bytes = Encoding.UTF8.GetBytes(json);
                    _core.SendResponseAsync(this, bytes).Wait();
                }
                else
                {
                    _core.SendResponseAsync(this, null).Wait();
                }
            }
            else if (cmd == "GetStopOrdersByIds")
            {
                if (ids.Length > 0)
                {
                    var list  = _stopOrderTable.GetStopOrdersByIds(ids);
                    var json  = JsonConvert.SerializeObject(list);
                    var bytes = Encoding.UTF8.GetBytes(json);
                    _core.SendResponseAsync(this, bytes).Wait();
                }
                else
                {
                    _core.SendResponseAsync(this, null).Wait();
                }
            }
            else if (cmd == "GetOrders")
            {
                if (ids.Length == 2)
                {
                    var list  = _orderTable.GetOrders(ids[0], ids[1]);
                    var json  = JsonConvert.SerializeObject(list);
                    var bytes = Encoding.UTF8.GetBytes(json);
                    _core.SendResponseAsync(this, bytes).Wait();
                }
                else
                {
                    _core.SendResponseAsync(this, null).Wait();
                }
            }
            else if (cmd == "GetOrdersByIds")
            {
                if (ids.Length > 0)
                {
                    var list  = _orderTable.GetOrdersByIds(ids);
                    var json  = JsonConvert.SerializeObject(list);
                    var bytes = Encoding.UTF8.GetBytes(json);
                    _core.SendResponseAsync(this, bytes).Wait();
                }
                else
                {
                    _core.SendResponseAsync(this, null).Wait();
                }
            }
            else if (cmd == "GetTrades") // GetTrades accountId fromId
            {
                if (ids.Length == 2)
                {
                    var list  = _tradeTable.GetTrades(ids[0], ids[1]);
                    var json  = JsonConvert.SerializeObject(list);
                    var bytes = Encoding.UTF8.GetBytes(json);
                    _core.SendResponseAsync(this, bytes).Wait();
                }
                else
                {
                    _core.SendResponseAsync(this, null).Wait();
                }
            }
            else if (cmd == "GetCash")
            {
                if (ids.Length == 1)
                {
                    var cash  = _cashTable.GetCash(ids[0]);
                    var json  = JsonConvert.SerializeObject(cash);
                    var bytes = Encoding.UTF8.GetBytes(json);
                    _core.SendResponseAsync(this, bytes).Wait();
                }
                else
                {
                    _core.SendResponseAsync(this, null).Wait();
                }
            }
            else if (cmd == "GetHoldingList")
            {
                if (ids.Length == 1)
                {
                    var list  = _holdingTable.GetHoldings(ids[0]);
                    var json  = JsonConvert.SerializeObject(list);
                    var bytes = Encoding.UTF8.GetBytes(json);
                    _core.SendResponseAsync(this, bytes).Wait();
                }
                else
                {
                    _core.SendResponseAsync(this, null).Wait();
                }
            }
            else
            {
                _core.SendResponseAsync(this, null).Wait();
            }
        }
Exemplo n.º 2
0
        public void OnRecv(byte[] data)
        {
            if (data == null)
            {
                return;
            }

            string cmd = Encoding.UTF8.GetString(data);

            string[] args = Regex.Split(cmd, @"\s+");

            if (args.Length == 2 && args[0] == "GetLastPrices")
            {
                var tickers             = Regex.Split(args[1], @"\s*,\s*");
                List <LastPrice> prices = new List <LastPrice>();
                foreach (var t in tickers)
                {
                    var instrum = _instrumTable.GetInstrum(t);
                    if (instrum == null)
                    {
                        continue;
                    }

                    var tick = _tickDisp.GetLastTick(instrum.InsID);
                    if (tick.InsID == 0)
                    {
                        continue;                  // диспетчер вернул default value структуры Tick, значит тика еще нет
                    }
                    prices.Add(new LastPrice(instrum.Ticker, tick.Time, tick.Price, tick.Lots));
                }

                var json  = JsonConvert.SerializeObject(prices);
                var bytes = Encoding.UTF8.GetBytes(json);
                _core.SendResponseAsync(this, bytes).Wait();
            }

            if (args.Length == 3 && args[0] == "GetLastTicks")
            {
                var ticker  = args[1];
                var instrum = _instrumTable.GetInstrum(ticker);
                if (instrum == null)
                {
                    _core.SendResponseAsync(this, new byte[] { 0xff });
                    return;
                }

                int skip;
                if (!int.TryParse(args[2], out skip))
                {
                    _core.SendResponseAsync(this, new byte[] { 0xfe }).Wait();
                    return;
                }

                var ticks = _tickDisp.GetLastTicks(instrum.InsID, skip);
                if (ticks.Length == 0)
                {
                    _core.SendResponseAsync(this, new byte[] { 0x0 }).Wait();
                    return;
                }

                AllTradesEncoder encoder = new AllTradesEncoder(instrum.Decimals);
                using (MemoryStream ms = new MemoryStream())
                {
                    foreach (var tick in ticks)
                    {
                        uint   seconds = (uint)(tick.Time.Hour * 60 * 60 + tick.Time.Minute * 60 + tick.Time.Second);
                        byte[] buf     = encoder.AddTick(seconds, tick.Price, tick.Lots);
                        ms.Write(buf, 0, buf.Length);
                    }
                    _core.SendResponseAsync(this, ms.ToArray()).Wait();
                }
            }
        }