Esempio n. 1
0
        private void OnRtnTrade(IntPtr pTraderApi, ref CThostFtdcTradeField pTrade)
        {
            if (OutputLog)
            {
                Console.WriteLine("时{0},合约{1},方向{2},开平{3},价{4},量{5},引用{6}",
                                  pTrade.TradeTime, pTrade.InstrumentID, pTrade.Direction, pTrade.OffsetFlag, pTrade.Price, pTrade.Volume, pTrade.OrderRef);
            }

            SingleOrder order;

            //找到自己发送的订单,标记成交
            if (_OrderRef2Order.TryGetValue(string.Format("{0}:{1}:{2}", _RspUserLogin.FrontID, _RspUserLogin.SessionID, pTrade.OrderRef), out order))
            {
                if (TThostFtdcTradeTypeType.CombinationDerived == pTrade.TradeType)
                {
                    //组合,得特别处理
                    DbInMemTrade _trade;//用此对象维护组合对
                    if (!_Orders4Combination.TryGetValue(order, out _trade))
                    {
                        _trade = new DbInMemTrade();
                        _Orders4Combination[order] = _trade;
                    }

                    double Price  = 0;
                    int    Volume = 0;
                    //找到成对交易的,得出价差
                    if (_trade.OnTrade(ref order, ref pTrade, ref Price, ref Volume))
                    {
                        EmitFilled(order, Price, Volume);

                        //完成使命了,删除
                        if (_trade.isEmpty())
                        {
                            _Orders4Combination.Remove(order);
                        }
                    }
                }
                else
                {
                    //普通订单,直接通知即可
                    EmitFilled(order, pTrade.Price, pTrade.Volume);
                }
            }

            if (_dbInMemInvestorPosition.UpdateByTrade(pTrade))
            {
            }
            else
            {
                //本地计算更新失败,重查一次
                TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, pTrade.InstrumentID);
            }
        }
Esempio n. 2
0
        private void OnRtnTrade(IntPtr pTraderApi, ref CThostFtdcTradeField pTrade)
        {
            if (OutputLog)
            {
                Console.WriteLine("时{0},合约{1},方向{2},开平{3},价{4},量{5},引用{6}",
                    pTrade.TradeTime, pTrade.InstrumentID, pTrade.Direction, pTrade.OffsetFlag, pTrade.Price, pTrade.Volume, pTrade.OrderRef);
            }

            //将仓位计算提前,防止在OnPositionOpened中下平仓时与“C|”配合出错
            if (_dbInMemInvestorPosition.UpdateByTrade(pTrade))
            {
            }
            else
            {
                //本地计算更新失败,重查一次
                TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, pTrade.InstrumentID);
            }

            SingleOrder order;
            //找到自己发送的订单,标记成交
            if (_OrderRef2Order.TryGetValue(string.Format("{0}:{1}:{2}", _RspUserLogin.FrontID, _RspUserLogin.SessionID, pTrade.OrderRef), out order))
            {
                if (TThostFtdcTradeTypeType.CombinationDerived == pTrade.TradeType)
                {
                    //组合,得特别处理
                    DbInMemTrade _trade;//用此对象维护组合对
                    if (!_Orders4Combination.TryGetValue(order, out _trade))
                    {
                        _trade = new DbInMemTrade();
                        _Orders4Combination[order] = _trade;
                    }

                    double Price = 0;
                    int Volume = 0;
                    //找到成对交易的,得出价差
                    if (_trade.OnTrade(ref order, ref pTrade, ref Price, ref Volume))
                    {
                        EmitFilled(order, Price, Volume);

                        //完成使命了,删除
                        if (_trade.isEmpty())
                        {
                            _Orders4Combination.Remove(order);
                        }
                    }
                }
                else
                {
                    //普通订单,直接通知即可
                    EmitFilled(order, pTrade.Price, pTrade.Volume);
                }
            }
        }
Esempio n. 3
0
        private void OnRtnTrade(IntPtr pTraderApi, ref CThostFtdcTradeField pTrade)
        {
            tdlog.Info("时{0},合约{1},方向{2},开平{3},价{4},量{5},引用{6},成交编号{7}",
                       pTrade.TradeTime, pTrade.InstrumentID, pTrade.Direction, pTrade.OffsetFlag,
                       pTrade.Price, pTrade.Volume, pTrade.OrderRef, pTrade.TradeID);

            //将仓位计算提前,防止在OnPositionOpened中下平仓时与“C|”配合出错
            if (_dbInMemInvestorPosition.UpdateByTrade(pTrade))
            {
            }
            else
            {
                //本地计算更新失败,重查一次
                TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, pTrade.InstrumentID);
            }

            SingleOrder order;
            //找到自己发送的订单,标记成交
            string strSysID = string.Format("{0}:{1}", pTrade.ExchangeID, pTrade.OrderSysID);
            string strKey;

            if (!_OrderSysID2OrderRef.TryGetValue(strSysID, out strKey))
            {
                return;
            }

            if (_OrderRef2Order.TryGetValue(strKey, out order))
            {
                double Price  = 0;
                int    Volume = 0;

                if (TThostFtdcTradeTypeType.CombinationDerived == pTrade.TradeType)
                {
                    //组合,得特别处理
                    DbInMemTrade _trade;//用此对象维护组合对
                    if (!_Orders4Combination.TryGetValue(order, out _trade))
                    {
                        _trade = new DbInMemTrade();
                        _Orders4Combination[order] = _trade;
                    }

                    //找到成对交易的,得出价差
                    if (_trade.OnTrade(ref order, ref pTrade, ref Price, ref Volume))
                    {
                        //完成使命了,删除
                        //if (_trade.isEmpty())
                        //{
                        //    _Orders4Combination.Remove(order);
                        //}
                    }
                }
                else
                {
                    //普通订单,直接通知即可
                    Price  = pTrade.Price;
                    Volume = pTrade.Volume;
                }

                int LeavesQty = (int)order.LeavesQty - Volume;
                EmitFilled(order, Price, Volume);

                //成交完全,清理
                if (LeavesQty <= 0)
                {
                    _OrderRef2Order.Remove(strKey);
                    _OrderSysID2OrderRef.Remove(strSysID);
                    _Orders4Combination.Remove(order);
                    _Orders4Cancel.Remove(order);
                }
            }
        }
        private void OnRtnTrade(IntPtr pTraderApi, ref CThostFtdcTradeField pTrade)
        {
            tdlog.Info("时{0},合约{1},方向{2},开平{3},价{4},量{5},引用{6},成交编号{7}",
                    pTrade.TradeTime, pTrade.InstrumentID, pTrade.Direction, pTrade.OffsetFlag,
                    pTrade.Price, pTrade.Volume, pTrade.OrderRef, pTrade.TradeID);

            //将仓位计算提前,防止在OnPositionOpened中下平仓时与“C|”配合出错
            if (_dbInMemInvestorPosition.UpdateByTrade(pTrade))
            {
            }
            else
            {
                //本地计算更新失败,重查一次
                TraderApi.TD_ReqQryInvestorPosition(m_pTdApi, pTrade.InstrumentID);
            }

            SingleOrder order;
            //找到自己发送的订单,标记成交
            string strSysID = string.Format("{0}:{1}", pTrade.ExchangeID, pTrade.OrderSysID);
            string strKey;
            if (!_OrderSysID2OrderRef.TryGetValue(strSysID,out strKey))
            {
                return;
            }

            if (_OrderRef2Order.TryGetValue(strKey, out order))
            {
                double Price = 0;
                int Volume = 0;

                if (TThostFtdcTradeTypeType.CombinationDerived == pTrade.TradeType)
                {
                    //组合,得特别处理
                    DbInMemTrade _trade;//用此对象维护组合对
                    if (!_Orders4Combination.TryGetValue(order, out _trade))
                    {
                        _trade = new DbInMemTrade();
                        _Orders4Combination[order] = _trade;
                    }

                    //找到成对交易的,得出价差
                    if (_trade.OnTrade(ref order, ref pTrade, ref Price, ref Volume))
                    {
                        //完成使命了,删除
                        //if (_trade.isEmpty())
                        //{
                        //    _Orders4Combination.Remove(order);
                        //}
                    }
                }
                else
                {
                    //普通订单,直接通知即可
                    Price = pTrade.Price;
                    Volume = pTrade.Volume;
                }

                int LeavesQty = (int)order.LeavesQty - Volume;
                EmitFilled(order, Price, Volume);

                //成交完全,清理
                if (LeavesQty <= 0)
                {
                    _OrderRef2Order.Remove(strKey);
                    _OrderSysID2OrderRef.Remove(strSysID);
                    _Orders4Combination.Remove(order);
                    _Orders4Cancel.Remove(order);
                }
            }
        }