Пример #1
0
        void _import_OnRspQryTrade(TradeField pTrade, bool pLast)
        {
            //无数据时,也会返回一条空记录
            if (string.IsNullOrEmpty(pTrade.InstrumentID))
            {
                return;
            }
            TradeField f = DicTradeField.GetOrAdd(pTrade.TradeID + (int)pTrade.Direction, new TradeField
            {
                TradeID = pTrade.TradeID + (int)pTrade.Direction,
            });

            foreach (var info in pTrade.GetType().GetFields())
            {
                if (info.Name == "TradeID")
                {
                    continue;
                }
                f.GetType().GetField(info.Name).SetValue(f, Convert.ChangeType(info.GetValue(pTrade), f.GetType().GetField(info.Name).FieldType));
            }
            //OrderField of;
            //if (DicOrderField.TryGetValue(pTrade.OrderID, out of))
            //{
            //	int preTrade = of.Volume - of.VolumeLeft;
            //	of.AvgPrice = (of.AvgPrice * preTrade + pTrade.Price * pTrade.Volume) / (preTrade + pTrade.Volume);
            //	of.TradeTime = pTrade.TradeTime;
            //	of.VolumeLeft -= pTrade.Volume;
            //}
        }
Пример #2
0
        private void _import_OnRtnTrade(TradeField pTrade)
        {
            //tradeid增加方向标识(自成交冲突)
            TradeField f = DicTradeField.GetOrAdd(pTrade.TradeID + (int)pTrade.Direction, new TradeField
            {
                TradeID = pTrade.TradeID + (int)pTrade.Direction,
            });
            foreach (var info in pTrade.GetType().GetFields())
            {
                if (info.Name == "TradeID")
                {
                    continue;
                }
                f.GetType().GetField(info.Name).SetValue(f, Convert.ChangeType(info.GetValue(pTrade), f.GetType().GetField(info.Name).FieldType));
            }

            PositionField pf;
            //处理持仓
            if (pTrade.Offset == OffsetType.Open)
            {
                pf = this.DicPositionField.GetOrAdd(pTrade.InstrumentID + "_" + pTrade.Direction, new PositionField());
                pf.InstrumentID = pTrade.InstrumentID;
                pf.Direction = pTrade.Direction;
                pf.Hedge = pTrade.Hedge;
                pf.Price = (pf.Price * pf.Position + pTrade.Price * pTrade.Volume) / (pf.Position + pTrade.Volume);
                pf.TdPosition += pTrade.Volume;
                pf.Position += pTrade.Volume;
            }
            else
            {
                pf = this.DicPositionField.GetOrAdd(pTrade.InstrumentID + "_" + (pTrade.Direction == DirectionType.Buy ? "Sell" : "Buy"), new PositionField());
                if (pf.TdPosition > 0)// pTrade.ExchangeID == "SHFE" && pTrade.Offset == OffsetType.CloseToday)
                {
                    pf.TdPosition -= Math.Min(pf.TdPosition, pTrade.Volume);
                }
                pf.Position -= pTrade.Volume;
            }
            //有关orderfield中的avgprice和tradetime字段,已在C++层进行处理
            //OrderField of;
            //if (DicOrderField.TryGetValue(f.OrderID, out of))
            //{
            //	int preTrade = of.Volume - of.VolumeLeft;
            //	of.AvgPrice = (of.AvgPrice * preTrade + pTrade.Price * pTrade.Volume) / (preTrade + pTrade.Volume);
            //	of.TradeTime = pTrade.TradeTime;
            //	of.VolumeLeft -= pTrade.Volume;
            //	of.Status = of.VolumeLeft == 0 ? OrderStatus.Filled : OrderStatus.Partial;
            //	if (_OnRtnTrade != null)
            //	{
            //		_OnRtnTrade(this, new TradeArgs
            //		{
            //			Value = f,
            //		});
            //	}
            //}
            if (_OnRtnTrade != null)
            {
                _OnRtnTrade(this, new TradeArgs
                {
                    Value = f,
                });
            }
        }
Пример #3
0
 void _import_OnRspQryTrade(TradeField pTrade, bool pLast)
 {
     //无数据时,也会返回一条空记录
     if (string.IsNullOrEmpty(pTrade.InstrumentID))
     {
         return;
     }
     TradeField f = DicTradeField.GetOrAdd(pTrade.TradeID + (int)pTrade.Direction, new TradeField
     {
         TradeID = pTrade.TradeID + (int)pTrade.Direction,
     });
     foreach (var info in pTrade.GetType().GetFields())
     {
         if (info.Name == "TradeID")
             continue;
         f.GetType().GetField(info.Name).SetValue(f, Convert.ChangeType(info.GetValue(pTrade), f.GetType().GetField(info.Name).FieldType));
     }
     OrderField of;
     if (DicOrderField.TryGetValue(pTrade.OrderID, out of))
     {
         int preTrade = of.Volume - of.VolumeLeft;
         of.AvgPrice = (of.AvgPrice * preTrade + pTrade.Price * pTrade.Volume) / (preTrade + pTrade.Volume);
         of.TradeTime = pTrade.TradeTime;
         of.VolumeLeft -= pTrade.Volume;
     }
 }
Пример #4
0
        void _import_OnRtnTrade(TradeField pTrade)
        {
            //tradeid增加方向标识(自成交冲突)
            TradeField f = DicTradeField.GetOrAdd(pTrade.TradeID + (int)pTrade.Direction, new TradeField
            {
                TradeID = pTrade.TradeID + (int)pTrade.Direction,
            });

            foreach (var info in pTrade.GetType().GetFields())
            {
                if (info.Name == "TradeID")
                {
                    continue;
                }
                f.GetType().GetField(info.Name).SetValue(f, Convert.ChangeType(info.GetValue(pTrade), f.GetType().GetField(info.Name).FieldType));
            }

            PositionField pf;

            //处理持仓
            if (pTrade.Offset == OffsetType.Open)
            {
                pf = this.DicPositionField.GetOrAdd(pTrade.InstrumentID + "_" + pTrade.Direction, new PositionField());
                pf.InstrumentID = pTrade.InstrumentID;
                pf.Direction    = pTrade.Direction;
                pf.Hedge        = pTrade.Hedge;
                pf.Price        = (pf.Price * pf.Position + pTrade.Price * pTrade.Volume) / (pf.Position + pTrade.Volume);
                pf.TdPosition  += pTrade.Volume;
                pf.Position    += pTrade.Volume;
            }
            else
            {
                pf = this.DicPositionField.GetOrAdd(pTrade.InstrumentID + "_" + (pTrade.Direction == DirectionType.Buy ? "Sell" : "Buy"), new PositionField());
                if (pTrade.Offset == OffsetType.CloseToday)
                {
                    pf.TdPosition -= pTrade.Volume;
                }
                else
                {
                    int tdClose = Math.Min(pf.TdPosition, pTrade.Volume);
                    if (pf.TdPosition > 0)
                    {
                        pf.TdPosition -= tdClose;
                    }
                    pf.YdPosition -= Math.Max(0, pTrade.Volume - tdClose);
                }
                pf.Position -= pTrade.Volume;
            }
            //有关orderfield中的avgprice和tradetime字段,已在C++层进行处理
            //OrderField of;
            //if (DicOrderField.TryGetValue(f.OrderID, out of))
            //{
            //	int preTrade = of.Volume - of.VolumeLeft;
            //	of.AvgPrice = (of.AvgPrice * preTrade + pTrade.Price * pTrade.Volume) / (preTrade + pTrade.Volume);
            //	of.TradeTime = pTrade.TradeTime;
            //	of.VolumeLeft -= pTrade.Volume;
            //	of.Status = of.VolumeLeft == 0 ? OrderStatus.Filled : OrderStatus.Partial;
            //	if (_OnRtnTrade != null)
            //	{
            //		_OnRtnTrade(this, new TradeArgs
            //		{
            //			Value = f,
            //		});
            //	}
            //}
            if (_OnRtnTrade != null)
            {
                _OnRtnTrade(this, new TradeArgs
                {
                    Value = f,
                });
            }
        }