Example #1
0
        private void TradeExt_OnRtnTrade(object sender, TradeArgs e)
        {
            OrderField of;

            if (!DicOrderField.TryGetValue(e.Value.OrderID, out of))
            {
                return;
            }
            if (!of.IsLocal)
            {
                return;
            }
        }
Example #2
0
        private void CTPOnErrRtnOrderAction(ref CThostFtdcOrderActionField pOrderAction, ref CThostFtdcRspInfoField pRspInfo)
        {
            if (pRspInfo.ErrorID == 0)
            {
                return;
            }

            string     id = string.Format("{0}|{1}|{2}", pOrderAction.SessionID, pOrderAction.FrontID, pOrderAction.OrderRef);
            OrderField of;

            if (IsLogin && DicOrderField.TryGetValue(id, out of))
            {
                _OnRtnErrCancel?.Invoke(this, new ErrOrderArgs {
                    ErrorID = pRspInfo.ErrorID, ErrorMsg = pRspInfo.ErrorMsg, Value = of
                });
            }
        }
Example #3
0
        public override int ReqOrderAction(string pOrderId)
        {
            OrderField of;

            if (!DicOrderField.TryGetValue(pOrderId, out of))
            {
                _OnRtnError?.Invoke(this, new ErrorEventArgs
                {
                    ErrorID  = -1,
                    ErrorMsg = string.Format("OrderActionError:[OrderID:{0}][ErrorMsg:no orderid.]", pOrderId),
                });
                return(-1);
            }

            return((int)_t.ReqOrderAction(_broker, _investor, InstrumentID: of.InstrumentID,
                                          OrderRef: pOrderId.Split('|')[2],
                                          FrontID: int.Parse(pOrderId.Split('|')[1]),
                                          SessionID: int.Parse(pOrderId.Split('|')[0]),
                                          ActionFlag: TThostFtdcActionFlagType.THOST_FTDC_AF_Delete));
        }
Example #4
0
        private void CTPOnRtnTrade(ref CThostFtdcTradeField pTrade)
        {
            if (!IsLogin)
            {
                _rtnOrderTime = DateTime.Now;
            }

            string     id;
            OrderField of = null;

            if (!(_dicSysidSfrId.TryGetValue(pTrade.OrderSysID, out id) && DicOrderField.TryGetValue(id, out of)))
            {
                CThostFtdcTradeField fReTrade = pTrade;
                var list = _sysidTrade.GetOrAdd(pTrade.OrderSysID, new List <CThostFtdcTradeField>());
                list.Add(fReTrade);
                return;
            }

            TradeField f = new TradeField
            {
                Hedge = pTrade.HedgeFlag == TThostFtdcHedgeFlagType.THOST_FTDC_HF_Speculation ? HedgeType.Speculation
                    : pTrade.HedgeFlag == TThostFtdcHedgeFlagType.THOST_FTDC_HF_Arbitrage ? HedgeType.Arbitrage : HedgeType.Hedge,
                Direction = pTrade.Direction == TThostFtdcDirectionType.THOST_FTDC_D_Buy ? DirectionType.Buy : DirectionType.Sell,

                //ExchangeID = pTrade.ExchangeID,
                InstrumentID = pTrade.InstrumentID,
                Offset       = pTrade.OffsetFlag == TThostFtdcOffsetFlagType.THOST_FTDC_OF_Open ? OffsetType.Open
                    : pTrade.OffsetFlag == TThostFtdcOffsetFlagType.THOST_FTDC_OF_CloseToday ? OffsetType.CloseToday : OffsetType.Close,
                Price      = pTrade.Price,
                TradeID    = pTrade.TradeID + (char)pTrade.Direction,
                TradeTime  = pTrade.TradeTime,
                TradingDay = pTrade.TradingDay,
                Volume     = pTrade.Volume,
                SysID      = pTrade.OrderSysID,
            };
            Exchange exc;

            if (Enum.TryParse(pTrade.ExchangeID, out exc))
            {
                f.ExchangeID = exc;
            }
            if (DicTradeField.TryAdd(f.TradeID, f)) // string.Format("{0}_{1}", f.TradeID, f.Direction), f))
            {
                f.OrderID      = id;                //更新成交对应的委托ID
                of.TradeTime   = pTrade.TradeTime;
                of.AvgPrice    = (of.AvgPrice * (of.Volume - of.VolumeLeft) + pTrade.Price * pTrade.Volume) / (of.Volume - of.VolumeLeft + pTrade.Volume);
                of.TradeVolume = pTrade.Volume;
                of.VolumeLeft -= of.TradeVolume;
                if (of.VolumeLeft == 0)
                {
                    of.Status    = OrderStatus.Filled;
                    of.StatusMsg = "全部成交";
                }
                else
                {
                    of.Status    = OrderStatus.Partial;
                    of.StatusMsg = "部分成交";
                }

                if (IsLogin)
                {
                    #region 更新持仓
                    PositionField pf;
                    //处理持仓
                    if (f.Offset == OffsetType.Open)
                    {
                        pf = DicPositionField.GetOrAdd(f.InstrumentID + "_" + f.Direction, new PositionField());
                        pf.InstrumentID = f.InstrumentID;
                        pf.Direction    = f.Direction;
                        pf.Hedge        = f.Hedge;
                        pf.Price        = (pf.Price * pf.Position + f.Price * f.Volume) / (pf.Position + f.Volume);
                        pf.TdPosition  += f.Volume;
                        pf.Position    += f.Volume;
                    }
                    else
                    {
                        pf = this.DicPositionField.GetOrAdd(f.InstrumentID + "_" + (f.Direction == DirectionType.Buy ? "Sell" : "Buy"), new PositionField());
                        if (f.Offset == OffsetType.CloseToday)
                        {
                            pf.TdPosition -= f.Volume;
                        }
                        else
                        {
                            int tdClose = Math.Min(pf.TdPosition, f.Volume);
                            if (pf.TdPosition > 0)
                            {
                                pf.TdPosition -= tdClose;
                            }
                            pf.YdPosition -= Math.Max(0, f.Volume - tdClose);
                        }
                        pf.Position -= f.Volume;
                    }
                    #endregion

                    //委托响应
                    _OnRtnOrder?.Invoke(this, new OrderArgs {
                        Value = of
                    });
                    //成交响应
                    _OnRtnTrade?.Invoke(this, new TradeArgs {
                        Value = f
                    });
                }
            }
        }