Example #1
0
 private void OnRtnOrder_callback(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder)
 {
     if (null != OnRtnOrder)
     {
         OnRtnOrder(this, new OnRtnOrderArgs(pTraderApi, ref pOrder));
     }
 }
Example #2
0
 public static extern void TD_CancelOrder(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder);
Example #3
0
        public void CancelOrder(ref CThostFtdcOrderField pOrder)
        {
            if (null == m_pTdApi || IntPtr.Zero == m_pTdApi)
            {
                return;
            }

            TraderApi.TD_CancelOrder(m_pTdApi, ref pOrder);
        }
Example #4
0
 private void OnRspQryOrder_callback(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
 {
     if (null != OnRspQryOrder)
     {
         OnRspQryOrder(this, new OnRspQryOrderArgs(pTraderApi, ref pOrder, ref pRspInfo, nRequestID, bIsLast));
     }
 }
        private void OnRtnOrderFirstStatus(GenericOrderItem item, CThostFtdcOrderField pOrder, string OrderSysID, string Key)
        {
            // 向上层报告保单引用
            if(OrderSysID.Length>0)
            {
                orderMap.OrderSysID_OrderRef[OrderSysID] = Key;

                foreach (var o in item.GetLegs())
                {
                    // 这个地方要再查一查,是不是要移动出来?
                    o.Order.OrderID = pOrder.OrderSysID;
                }
            }
            
            // 判断是第一次报单,还是只是撤单时的第一条记录
            if (!orderMap.OrderItem_OrderField.ContainsKey(item))
            {
                // 在Order_OrderItem中记录绑定关系
                foreach (var o in item.GetLegs())
                {
                    orderMap.Order_OrderItem[o.Order] = item;
                }

                EmitAccepted(item);
            }

            // 在OrderItem_OrderField中记录与API的对应关系
            orderMap.OrderItem_OrderField[item] = pOrder;
        }
        private void OnRtnOrderLastStatus(GenericOrderItem item, CThostFtdcOrderField pOrder, string OrderSysID, string Key)
        {
            // 给成交回报用的,由于成交回报返回慢,所以不能删
            //orderMap.OrderSysID_OrderRef.Remove(OrderSysID);

            // 已经是最后状态,不能用于撤单了
            // 这个功能交给用户上层处理,报个重复撤单也没啥
            //orderMap.OrderItem_OrderField.Remove(item);

            OnLastStatus(item, OrderSysID, Key);
        }
Example #7
0
        private void OnRtnOrder(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder)
        {
            tdlog.Info("{0},{1},{2},开平{3},价{4},原量{5},成交{6},提交{7},状态{8},引用{9},报单编号{10},{11}",
                    pOrder.InsertTime, pOrder.InstrumentID, pOrder.Direction, pOrder.CombOffsetFlag, pOrder.LimitPrice,
                    pOrder.VolumeTotalOriginal, pOrder.VolumeTraded, pOrder.OrderSubmitStatus, pOrder.OrderStatus,
                    pOrder.OrderRef, pOrder.OrderSysID, pOrder.StatusMsg);

            SingleOrder order;
            string strKey = string.Format("{0}:{1}:{2}", _RspUserLogin.FrontID, _RspUserLogin.SessionID, pOrder.OrderRef);
            if (_OrderRef2Order.TryGetValue(strKey, out order))
            {
                order.Text = string.Format("{0}|{1}", order.Text.Substring(0,Math.Min(order.Text.Length,64)), pOrder.StatusMsg);

                //找到对应的报单回应
                Dictionary<string, CThostFtdcOrderField> _Ref2Action;
                if (!_Orders4Cancel.TryGetValue(order, out _Ref2Action))
                {
                    //没找到,自己填一个
                    _Ref2Action = new Dictionary<string, CThostFtdcOrderField>();
                    _Orders4Cancel[order] = _Ref2Action;
                }

                lock (_Ref2Action)
                {
                    switch (pOrder.OrderStatus)
                    {
                        case TThostFtdcOrderStatusType.AllTraded:
                            //已经是最后状态,不能用于撤单了
                            _Ref2Action.Remove(strKey);
                            break;
                        case TThostFtdcOrderStatusType.PartTradedQueueing:
                            //只是部分成交,还可以撤单,所以要记录下来
                            _Ref2Action[strKey] = pOrder;
                            break;
                        case TThostFtdcOrderStatusType.PartTradedNotQueueing:
                            //已经是最后状态,不能用于撤单了
                            _Ref2Action.Remove(strKey);
                            break;
                        case TThostFtdcOrderStatusType.NoTradeQueueing:
                            if (0 == _Ref2Action.Count())
                            {
                                EmitAccepted(order);
                            }
                            _Ref2Action[strKey] = pOrder;
                            break;
                        case TThostFtdcOrderStatusType.NoTradeNotQueueing:
                            //已经是最后状态,不能用于撤单了
                            _Ref2Action.Remove(strKey);
                            break;
                        case TThostFtdcOrderStatusType.Canceled:
                            //已经是最后状态,不能用于撤单了
                            _Ref2Action.Remove(strKey);
                            //分析此报单是否结束,如果结束分析整个Order是否结束
                            switch (pOrder.OrderSubmitStatus)
                            {
                                case TThostFtdcOrderSubmitStatusType.InsertRejected:
                                    //如果是最后一个的状态,同意发出消息
                                    if (0 == _Ref2Action.Count())
                                        EmitRejected(order, pOrder.StatusMsg);
                                    else
                                        Cancel(order);
                                    break;
                                default:
                                    //如果是最后一个的状态,同意发出消息
                                    if (0 == _Ref2Action.Count())
                                        EmitCancelled(order);
                                    else
                                        Cancel(order);
                                    break;
                            }
                            break;
                        case TThostFtdcOrderStatusType.Unknown:
                            switch (pOrder.OrderSubmitStatus)
                            {
                                case TThostFtdcOrderSubmitStatusType.InsertSubmitted:
                                    //新单,新加入记录以便撤单
                                    if (0 == _Ref2Action.Count())
                                    {
                                        EmitAccepted(order);
                                    }
                                    _Ref2Action[strKey] = pOrder;
                                    break;
                            }
                            break;
                        case TThostFtdcOrderStatusType.NotTouched:
                            //没有处理
                            break;
                        case TThostFtdcOrderStatusType.Touched:
                            //没有处理
                            break;
                    }

                    //已经是最后状态了,可以去除了
                    if (0 == _Ref2Action.Count())
                    {
                        _Orders4Cancel.Remove(order);
                    }
                }
            }
            else
            {
                //由第三方软件发出或上次登录时的剩余的单子在这次成交了,先不处理,当不存在
            }
        }
        private void OnRtnOrder(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder)
        {
            tdlog.Info("{0},{1},{2},开平{3},价{4},原量{5},成交{6},提交{7},状态{8},前置{9},会话{10},引用{11},报单编号{12},{13}",
                    pOrder.InsertTime, pOrder.InstrumentID, pOrder.Direction, pOrder.CombOffsetFlag, pOrder.LimitPrice,
                    pOrder.VolumeTotalOriginal, pOrder.VolumeTraded, pOrder.OrderSubmitStatus, pOrder.OrderStatus,
                    pOrder.FrontID,pOrder.SessionID,pOrder.OrderRef, pOrder.OrderSysID, pOrder.StatusMsg);

            // 加上这句只是为了在一个账号多会话高频交易时提前过滤
            if (pOrder.SessionID != _RspUserLogin.SessionID || pOrder.FrontID != _RspUserLogin.FrontID)
            {
                return;
            }

            GenericOrderItem item;
            string strKey = string.Format("{0}:{1}:{2}", pOrder.FrontID, pOrder.SessionID, pOrder.OrderRef);
            if (orderMap.TryGetValue(strKey, out item))
            {
                string strSysID = string.Format("{0}:{1}", pOrder.ExchangeID, pOrder.OrderSysID);

                switch (pOrder.OrderStatus)
                {
                        /// 不用处理
                    case TThostFtdcOrderStatusType.PartTradedQueueing:
                        break;
                        /// 第一个状态,要注册
                    case TThostFtdcOrderStatusType.NoTradeQueueing:
                        OnRtnOrderFirstStatus(item, pOrder,strSysID,strKey);
                        break;
                        /// 最后一个状态,要清理
                    case TThostFtdcOrderStatusType.AllTraded:
                    case TThostFtdcOrderStatusType.PartTradedNotQueueing:
                    case TThostFtdcOrderStatusType.NoTradeNotQueueing:
                        OnRtnOrderLastStatus(item, pOrder, strSysID, strKey);
                        break;
                        /// 其它情况
                    case TThostFtdcOrderStatusType.Canceled:
                        //分析此报单是否结束
                        switch (pOrder.OrderSubmitStatus)
                        {
                            case TThostFtdcOrderSubmitStatusType.InsertRejected:
                                EmitRejected(item,pOrder.StatusMsg);
                                break;
                            default:
                                EmitCancelled(item);
                                break;
                        }
                        OnRtnOrderLastStatus(item, pOrder, strSysID, strKey);
                        break;
                    case TThostFtdcOrderStatusType.Unknown:
                        switch (pOrder.OrderSubmitStatus)
                        {
                            case TThostFtdcOrderSubmitStatusType.InsertSubmitted:
                                OnRtnOrderFirstStatus(item, pOrder, strSysID, strKey);
                                break;
                        }
                        break;
                    case TThostFtdcOrderStatusType.NotTouched:
                        //没有处理
                        break;
                    case TThostFtdcOrderStatusType.Touched:
                        //没有处理
                        break;
                }
            }
        }
Example #9
0
 public OnRtnOrderArgs(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder)
 {
     this.pTraderApi = pTraderApi;
     this.pOrder = pOrder;
 }
Example #10
0
 public OnRspQryOrderArgs(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
 {
     this.pTraderApi = pTraderApi;
     this.pOrder = pOrder;
     this.pRspInfo = pRspInfo;
     this.nRequestID = nRequestID;
     this.bIsLast = bIsLast;
 }
Example #11
0
        public void UpdateOrderRef(CThostFtdcOrderField pOrder)
        {
            sw.EnterWriteLock();

            var order = orders.FirstOrDefault(
                o => o.OrderRef == pOrder.OrderRef && o.FrontID == pOrder.FrontID && o.SessionID == pOrder.SessionID);

            if (order != null)
            {
                order.OrderSysID = pOrder.OrderSysID;
                order.ExchangeID = pOrder.ExchangeID;
                OrderRepository.Update(order);
            }
            else
            {
                foreach (var o in orders)
                {
                    var closeorder = o.CloseOrders.FirstOrDefault(c => c.OrderRef == pOrder.OrderRef &&
                                                      c.FrontID == pOrder.FrontID && c.SessionID == pOrder.SessionID);

                    if (closeorder == null) continue;

                    closeorder.OrderSysID = pOrder.OrderSysID;
                    OrderRepository.Update(o);
                    break;
                }
            }

            sw.ExitWriteLock();
        }
Example #12
0
 // 通过报单组合,实际的向交易所撤单
 public bool TryGetValue(GenericOrderItem key, out CThostFtdcOrderField value)
 {
     return OrderItem_OrderField.TryGetValue(key, out value);
 }
        private void OnRtnOrder(IntPtr pTraderApi, ref CThostFtdcOrderField pOrder)
        {
            tdlog.Info("{0},{1},{2},开平{3},价{4},原量{5},成交{6},提交{7},状态{8},前置{9},会话{10},引用{11},报单编号{12},{13}",
                    pOrder.InsertTime, pOrder.InstrumentID, pOrder.Direction, pOrder.CombOffsetFlag, pOrder.LimitPrice,
                    pOrder.VolumeTotalOriginal, pOrder.VolumeTraded, pOrder.OrderSubmitStatus, pOrder.OrderStatus,
                    pOrder.FrontID,pOrder.SessionID,pOrder.OrderRef, pOrder.OrderSysID, pOrder.StatusMsg);

            // 加上这句只是为了在一个账号多会话高频交易时提前过滤
            if (pOrder.SessionID != _RspUserLogin.SessionID || pOrder.FrontID != _RspUserLogin.FrontID)
            {
                return;
            }

            SingleOrder order;
            string strKey = string.Format("{0}:{1}:{2}", pOrder.FrontID, pOrder.SessionID, pOrder.OrderRef);
            if (_OrderRef2Order.TryGetValue(strKey, out order))
            {
                order.Text = string.Format("{0}|{1}", order.Text.Substring(0, Math.Min(order.Text.Length, 64)), pOrder.StatusMsg);
                order.OrderID = pOrder.OrderSysID;

                //找到对应的报单回应
                Dictionary<string, CThostFtdcOrderField> _Ref2Action;
                if (!_Orders4Cancel.TryGetValue(order, out _Ref2Action))
                {
                    //没找到,自己填一个
                    _Ref2Action = new Dictionary<string, CThostFtdcOrderField>();
                    _Orders4Cancel[order] = _Ref2Action;
                }

                // 有对它进行过撤单操作,这地方是为了加正在撤单状态
                OrdStatus status;
                if (_PendingCancelFlags.TryGetValue(order, out status))
                {
                    // 记下当时的状态
                    _PendingCancelFlags[order] = order.OrdStatus;
                    EmitPendingCancel(order);
                }

                lock (_Ref2Action)
                {
                    string strSysID = string.Format("{0}:{1}", pOrder.ExchangeID, pOrder.OrderSysID);

                    switch (pOrder.OrderStatus)
                    {
                        case TThostFtdcOrderStatusType.AllTraded:
                            //已经是最后状态,不能用于撤单了
                            _PendingCancelFlags.Remove(order);
                            _Ref2Action.Remove(strKey);
                            break;
                        case TThostFtdcOrderStatusType.PartTradedQueueing:
                            //只是部分成交,还可以撤单,所以要记录下来
                            _Ref2Action[strKey] = pOrder;
                            break;
                        case TThostFtdcOrderStatusType.PartTradedNotQueueing:
                            //已经是最后状态,不能用于撤单了
                            _PendingCancelFlags.Remove(order);
                            _Ref2Action.Remove(strKey);
                            break;
                        case TThostFtdcOrderStatusType.NoTradeQueueing:
                            // 用于收到成交回报时定位
                            _OrderSysID2OrderRef[strSysID] = strKey;

                            if (0 == _Ref2Action.Count())
                            {
                                _Ref2Action[strKey] = pOrder;
                                EmitAccepted(order);
                            }
                            else
                            {
                                _Ref2Action[strKey] = pOrder;
                            }
                            break;
                        case TThostFtdcOrderStatusType.NoTradeNotQueueing:
                            //已经是最后状态,不能用于撤单了
                            _PendingCancelFlags.Remove(order);
                            _Ref2Action.Remove(strKey);
                            break;
                        case TThostFtdcOrderStatusType.Canceled:
                            // 将撤单中记录表清理下
                            _PendingCancelFlags.Remove(order);
                            //已经是最后状态,不能用于撤单了
                            _Ref2Action.Remove(strKey);
                            //分析此报单是否结束,如果结束分析整个Order是否结束
                            switch (pOrder.OrderSubmitStatus)
                            {
                                case TThostFtdcOrderSubmitStatusType.InsertRejected:
                                    //如果是最后一个的状态,同意发出消息
                                    if (0 == _Ref2Action.Count())
                                    {
                                        //唯一的从OnRtnOrder中就返回拒绝
                                        EmitRejected(order, pOrder.StatusMsg);
                                    }
                                    else
                                        Cancel(order);
                                    break;
                                default:
                                    //如果是最后一个的状态,同意发出消息
                                    if (0 == _Ref2Action.Count())
                                        EmitCancelled(order);
                                    else
                                        Cancel(order);
                                    break;
                            }
                            break;
                        case TThostFtdcOrderStatusType.Unknown:
                            switch (pOrder.OrderSubmitStatus)
                            {
                                case TThostFtdcOrderSubmitStatusType.InsertSubmitted:
                                    // 有可能头两个报单就这状态,就是报单编号由空变为了有。为空时,也记,没有关系
                                    _OrderSysID2OrderRef[strSysID] = strKey;

                                    //新单,新加入记录以便撤单
                                    if (0 == _Ref2Action.Count())
                                    {
                                        _Ref2Action[strKey] = pOrder;
                                        EmitAccepted(order);
                                    }
                                    else
                                    {
                                        _Ref2Action[strKey] = pOrder;
                                    }
                                    break;
                            }
                            break;
                        case TThostFtdcOrderStatusType.NotTouched:
                            //没有处理
                            break;
                        case TThostFtdcOrderStatusType.Touched:
                            //没有处理
                            break;
                    }

                    //已经是最后状态了,可以去除了
                    if (0 == _Ref2Action.Count())
                    {
                        _Orders4Cancel.Remove(order);
                    }
                }
            }
            else
            {
                //由第三方软件发出或上次登录时的剩余的单子在这次成交了,先不处理,当不存在
            }
        }