Example #1
0
        private void OnAddRow(int OpenID, int RowID, object Fields)
        {
            object[] fields = (object[])Fields;

            if (_all_trades_table != null && OpenID == _all_trades_table.ID)
            {
                if (RowID <= _allTrades_lastRowID)
                {
                    return;
                }
                _allTrades_lastRowID = RowID;

                string secboard = Convert.ToString(fields[5]).Trim();
                if (secboard != _secBoard)
                {
                    return;
                }

                string   ticker  = Convert.ToString(fields[6]).Trim();
                long     tradeNo = Convert.ToInt64(fields[2]);
                DateTime msktime = Convert.ToDateTime(fields[3]).AddHours(_addHours);
                if (msktime.TimeOfDay < _startSessionMskTime)
                {
                    msktime = new DateTime(msktime.Year, msktime.Month, msktime.Day, _startSessionMskTime.Hours, _startSessionMskTime.Minutes, _startSessionMskTime.Seconds);
                }
                if (msktime.TimeOfDay > _endSessionMskTime)
                {
                    msktime = new DateTime(msktime.Year, msktime.Month, msktime.Day, _endSessionMskTime.Hours, _endSessionMskTime.Minutes, _endSessionMskTime.Seconds);
                }

                int    lots  = Convert.ToInt32(fields[8]);
                double price = Convert.ToDouble(fields[9]);

                Common.Data.Instrum ins = _instrumTable.GetInstrum(ticker);
                if (ins == null)
                {
                    return;
                }

                _tickDispatcher.AddTick(new Tick(tradeNo, msktime, ins.InsID, lots, (decimal)price));
            }
            else if (_securities_table != null && OpenID == _securities_table.ID)
            {
                if (RowID <= _securities_lastRowID)
                {
                    return;
                }
                _securities_lastRowID = RowID;

                string secboard = Convert.ToString(fields[1]).Trim();
                if (secboard != _secBoard)
                {
                    return;
                }

                string  ticker    = Convert.ToString(fields[2]).Trim();
                string  name      = Convert.ToString(fields[3]).Trim();
                string  shortname = Convert.ToString(fields[4]).Trim();
                int     lotsize   = Convert.ToInt32(fields[13]);
                decimal pricestep = Convert.ToDecimal(fields[15]);
                int     decimals  = Convert.ToInt32(fields[17]);

                _instrumTable.SyncInstrum(ticker, shortname, name, lotsize, decimals, pricestep);
            }
            else if (_trdacc_table != null && OpenID == _trdacc_table.ID)
            {
                string account = Convert.ToString(fields[1]).Trim();
                string name    = Convert.ToString(fields[2]).Trim();
                _accountTable.AddAccount(account, name);
            }
            else if (_orders_table != null && OpenID == _orders_table.ID)
            {
                var ticker = Convert.ToString(fields[21]).Trim();
                var ins    = _instrumTable.GetInstrum(ticker);
                if (ins == null)
                {
                    return;
                }

                string accCode = Convert.ToString(fields[19]).Trim();
                var    acc     = _accountTable.GetAccount(accCode);
                if (acc == null)
                {
                    return;
                }

                Order order = new Order();
                order.OrderNo   = Convert.ToInt64(fields[1]);
                order.Time      = Convert.ToDateTime(fields[3]).AddHours(_addHours);
                order.InsID     = ins.InsID;
                order.AccountID = acc.AccountID;

                var bs = Convert.ToString(fields[12]).Trim();
                order.BuySell = bs == "B" ? BuySell.Buy : BuySell.Sell;

                order.LotCount = Convert.ToInt32(fields[25]);

                order.Price = Convert.ToDecimal(fields[23]);
                if (order.Price == 0)
                {
                    order.Price = null;
                }

                var status = Convert.ToString(fields[10]).Trim();
                if (status == "M")
                {
                    order.Status = OrderStatus.Trade;
                }
                else if (status == "C")
                {
                    order.Status = OrderStatus.EndTime;
                }
                else if (status == "N")
                {
                    order.Status = OrderStatus.Reject;
                }
                else if (status == "W")
                {
                    order.Status = OrderStatus.Remove;
                }
                else
                {
                    order.Status = OrderStatus.Active; // A, O
                }
                _orderTable.AddOrder(order);
            }
            else if (_stoporders_table != null && OpenID == _stoporders_table.ID)
            {
                string secboard = Convert.ToString(fields[4]).Trim();
                if (secboard != _secBoard)
                {
                    return;
                }

                var ticker = Convert.ToString(fields[5]).Trim();
                var ins    = _instrumTable.GetInstrum(ticker);
                if (ins == null)
                {
                    return;
                }

                string accCode = Convert.ToString(fields[21]).Trim();
                var    acc     = _accountTable.GetAccount(accCode);
                if (acc == null)
                {
                    return;
                }

                StopOrder so = new StopOrder();
                so.StopOrderNo = Convert.ToInt64(fields[3]);
                so.Time        = Convert.ToDateTime(fields[1]).AddHours(_addHours);
                so.InsID       = ins.InsID;
                so.AccountID   = acc.AccountID;

                var bs = Convert.ToString(fields[6]).Trim();
                so.BuySell = bs == "B" ? BuySell.Buy : BuySell.Sell;

                var st = Convert.ToString(fields[10]).Trim();
                so.StopType = StopOrderType.TakeProfit;
                if (st == "L")
                {
                    so.StopType = StopOrderType.StopLoss;
                }
                else if (st == "P")
                {
                    so.StopType = StopOrderType.TakeProfit;
                }

                so.AlertPrice = Convert.ToDecimal(fields[13]);

                so.Price = Convert.ToDecimal(fields[14]);
                if (so.Price == 0)
                {
                    so.Price = null;
                }

                so.LotCount = Convert.ToInt32(fields[15]);
                var status = Convert.ToString(fields[18]).Trim();
                if (status == "M")
                {
                    so.Status = StopOrderStatus.Order;
                }
                else if (status == "E")
                {
                    so.Status = StopOrderStatus.EndTime;
                }
                else if (status == "R")
                {
                    so.Status = StopOrderStatus.Reject;
                }
                else if (status == "W")
                {
                    so.Status = StopOrderStatus.Remove;
                }
                else
                {
                    so.Status = StopOrderStatus.Active; // A, O, space
                }
                so.EndTime = (DateTime?)fields[11];     // если время не указать, то сначала оно будет null, а потом сработает update, поменяется статус на Активный и время завершения установится на конец торговой сессии
                if (so.EndTime != null)
                {
                    so.EndTime = so.EndTime.Value.AddHours(_addHours);
                }
                _stopOrderTable.AddStopOrder(so, GetCurrentTime());
            }
            else if (_trades_table != null && OpenID == _trades_table.ID)
            {
                var ticker = Convert.ToString(fields[6]).Trim();
                var ins    = _instrumTable.GetInstrum(ticker);
                if (ins == null)
                {
                    return;
                }

                string accCode = Convert.ToString(fields[29]).Trim();
                var    acc     = _accountTable.GetAccount(accCode);
                if (acc == null)
                {
                    return;
                }

                long  orderNo = Convert.ToInt64(fields[2]);
                Order order   = _orderTable.GetOrder(orderNo);
                if (order == null)
                {
                    return;
                }

                Trade trade = new Trade();
                trade.TradeNo   = Convert.ToInt64(fields[1]);
                trade.OrderID   = order.OrderID;
                trade.Time      = Convert.ToDateTime(fields[3]).AddHours(_addHours);
                trade.InsID     = ins.InsID;
                trade.AccountID = acc.AccountID;
                var bs = Convert.ToString(fields[8]).Trim();
                trade.BuySell  = bs == "B" ? BuySell.Buy : BuySell.Sell;
                trade.LotCount = Convert.ToInt32(fields[9]);
                trade.Price    = Convert.ToDecimal(fields[10]);

                _tradeTable.AddTrade(trade);
            }
            else if (_holding_table != null && OpenID == _holding_table.ID)
            {
                var ticker = Convert.ToString(fields[5]).Trim();
                var ins    = _instrumTable.GetInstrum(ticker);
                if (ins == null)
                {
                    return;
                }

                var acc = _accountTable.GetDefaultAccount();
                if (acc == null)
                {
                    return;
                }

                int lots = Convert.ToInt32(fields[8]);
                _holdingTable.SetHolding(acc.AccountID, ins.InsID, lots);
            }
            else if (_positions_table != null && OpenID == _positions_table.ID)
            {
                string accCode = Convert.ToString(fields[2]).Trim();
                var    acc     = _accountTable.GetAccount(accCode);
                if (acc == null)
                {
                    return;
                }

                double  pos    = Convert.ToDouble(fields[11]);
                decimal curPos = (decimal)pos;
                _positionTable.SetPosition(acc.AccountID, curPos);
            }
        }