Beispiel #1
0
 private void OnRspQryTrade_callback(object sender, ref TradeField trade, int size1, bool bIsLast)
 {
     if (null != OnRspQryTrade)
     {
         OnRspQryTrade(this, new OnRspQryTradeEventArgs(ref trade, size1, bIsLast));
     }
 }
Beispiel #2
0
 private void OnRtnTrade_callback(object sender, ref TradeField trade)
 {
     if (null != OnRtnTrade)
     {
         OnRtnTrade(this, new OnRtnTradeEventArgs(ref trade));
     }
 }
        private void ProcessTrade(TradeField trade)
        {
            _map.TryPeek(trade.ID, out var record);
            if (record != null)
            {
                record.AddFill(trade.Price, trade.Qty);
                var status = (record.LeavesQty > 0) ? OrderStatus.PartiallyFilled : OrderStatus.Filled;
                var report = CreateReport(record, status, ExecType.ExecTrade);
                report.DateTime = trade.UpdateTime();
                report.LastPx   = trade.Price;
                report.LastQty  = trade.Qty;

                if (Math.Abs(trade.Commission) < double.Epsilon)
                {
                    report.Commission = _provider.GetCommission(report);
                }
                else
                {
                    report.Commission = trade.Commission;
                }

                _provider.OnMessage(report);
                if (status == OrderStatus.Filled)
                {
                    _map.RemoveFilled(trade.ID);
                }
            }
        }
Beispiel #4
0
 public TradingEvent(ExecType type, OrderField order)
 {
     Type        = type;
     OrderReturn = order;
     Trade       = null;
     Order       = null;
 }
Beispiel #5
0
 public TradingEvent(ExecType type)
 {
     Type        = type;
     Order       = null;
     Trade       = null;
     OrderReturn = null;
 }
        private string TradeFieldToLogString(TradeField field)
        {
            StringBuilder sb = new StringBuilder();

            try
            {
                sb.Append(string.Format("[BrokerID:{0}]", field.BrokerID));
                sb.Append(string.Format("[Direction:{0}]", field.Direction));
                sb.Append(string.Format("[ExchangeID:{0}]", field.ExchangeID));
                sb.Append(string.Format("[InstrumentID:{0}]", field.InstrumentID));
                sb.Append(string.Format("[OffsetFlag:{0}]", field.OffsetFlag));
                sb.Append(string.Format("[OrderRef:{0}]", field.OrderRef));
                sb.Append(string.Format("[OrderSysID:{0}]", field.OrderSysID));
                sb.Append(string.Format("[Price:{0}]", field.Price));
                sb.Append(string.Format("[TradeDate:{0}]", field.TradeDate));
                sb.Append(string.Format("[TradeID:{0}]", field.TradeID));
                sb.Append(string.Format("[TradeTime:{0}]", field.TradeTime));
                sb.Append(string.Format("[UserID:{0}]", field.UserID));
                sb.Append(string.Format("[Volume:{0}]", field.Volume));
            }
            catch (Exception ex)
            {
                sb.Append("TradeFieldToLogString() failed,Error:" + ex.Message);
            }

            return(sb.ToString());
        }
Beispiel #7
0
 public TradingEvent(ExecType type, TradeField trade)
 {
     Type        = type;
     Trade       = trade;
     Order       = null;
     OrderReturn = null;
 }
Beispiel #8
0
 private void XApi_OnRtnTrade(object sender, ref TradeField trade)
 {
     if (onReturnTrade != null)
     {
         TradeInfo tradeInfo = StructTransfer.TransferTradeInfo(trade);
         onReturnTrade(this, ref tradeInfo);
     }
 }
Beispiel #9
0
        private void OnRspQryTrade_callback(object sender, ref TradeField trade, int size1, bool bIsLast)
        {
            if (null == OnRspQryTrade)
            {
                return;
            }

            TradeClass cls = null;

            if (size1 > 0)
            {
                TradeField field = trade;

                cls = new TradeClass();

                cls.InstrumentName = field.InstrumentName();
                cls.Symbol         = field.Symbol;
                cls.InstrumentID   = field.InstrumentID;
                cls.ExchangeID     = field.ExchangeID;
                cls.ClientID       = field.ClientID;
                cls.AccountID      = field.AccountID;
                cls.Side           = (int)field.Side;
                cls.Side_String    = Enum <XAPI.OrderSide> .ToString(field.Side);

                cls.Qty              = field.Qty;
                cls.Price            = field.Price;
                cls.OpenClose        = (int)field.OpenClose;
                cls.OpenClose_String = Enum <XAPI.OpenCloseType> .ToString(field.OpenClose);

                cls.HedgeFlag        = (int)field.HedgeFlag;
                cls.HedgeFlag_String = Enum <XAPI.HedgeFlagType> .ToString(field.HedgeFlag);

                cls.Date          = field.Date;
                cls.Time          = field.Time;
                cls.ID            = field.ID;
                cls.TradeID       = field.TradeID;
                cls.ReserveInt32  = field.ReserveInt32;
                cls.ReserveChar64 = field.ReserveChar64;
            }

            if (null == OnRspQryTrade)
            {
                QueueData qd = new QueueData();
                qd.Type        = (int)ResponeType.OnRspQryTrade;
                qd.Type_String = Enum <XAPI.ResponeType> .ToString(ResponeType.OnRspQryTrade);

                qd.Sender = this;
                qd.Data1  = cls;
                qd.Data2  = size1;
                qd.Data3  = bIsLast;

                MessageQueue.Enqueue(qd);
            }
            else
            {
                OnRspQryTrade(this, ref cls, size1, bIsLast);
            }
        }
Beispiel #10
0
        private void OnRtnTrade_callback(object sender, [In] ref TradeField trade)
        {
            if (null == OnRtnTrade)
            {
                return;
            }

            TradeField field = trade;

            TradeClass cls = new TradeClass();

            cls.InstrumentName = field.InstrumentName();
            cls.Symbol         = field.Symbol;
            cls.InstrumentID   = field.InstrumentID;
            cls.ExchangeID     = field.ExchangeID;
            cls.ClientID       = field.ClientID;
            cls.AccountID      = field.AccountID;
            cls.Side           = (int)field.Side;
            cls.Side_String    = Enum <XAPI.OrderSide> .ToString(field.Side);

            cls.Qty              = field.Qty;
            cls.Price            = field.Price;
            cls.OpenClose        = (int)field.OpenClose;
            cls.OpenClose_String = Enum <XAPI.OpenCloseType> .ToString(field.OpenClose);

            cls.HedgeFlag        = (int)field.HedgeFlag;
            cls.HedgeFlag_String = Enum <XAPI.HedgeFlagType> .ToString(field.HedgeFlag);

            cls.Date          = field.Date;
            cls.Time          = field.Time;
            cls.ID            = field.ID;
            cls.TradeID       = field.TradeID;
            cls.ReserveInt32  = field.ReserveInt32;
            cls.ReserveChar64 = field.ReserveChar64;

            cls.PortfolioID1    = field.PortfolioID1;
            cls.PortfolioID2    = field.PortfolioID2;
            cls.PortfolioID3    = field.PortfolioID3;
            cls.Business        = (int)field.Business;
            cls.Business_String = Enum <XAPI.BusinessType> .ToString(field.Business);

            if (null == OnRtnTrade)
            {
                QueueData qd = new QueueData();
                qd.Type        = (int)ResponseType.OnRtnTrade;
                qd.Type_String = Enum <XAPI.ResponseType> .ToString(ResponseType.OnRtnTrade);

                qd.Sender = this;
                qd.Data1  = cls;

                MessageQueue.Enqueue(qd);
            }
            else
            {
                OnRtnTrade(this, cls);
            }
        }
Beispiel #11
0
 internal void OnMessage(TradeField field)
 {
     _processor.PostTrade(field);
     if (QueryTradingDataAfterTrade)
     {
         Trader.QueryAccount();
         Trader.QueryPositions();
     }
 }
Beispiel #12
0
        //交易回报
        private void _OnRtnTrade(object sender, TradeArgs e)
        {
            while (0 != Interlocked.Exchange(ref BaseStrategy.Locker, 1))
            {
            }
            TradeField trade = e.Value;

            LogUtils.EnginLog($"交易回报:{trade.OrderID}\t{trade.InstrumentID}\t{trade.Direction}\t{trade.Offset}\t{trade.Price}\t{trade.Volume}");
            Interlocked.Exchange(ref BaseStrategy.Locker, 0);
        }
Beispiel #13
0
        private void OnRspQryTrade_callback(object sender, ref TradeField trade, int size1, bool bIsLast)
        {
            if (size1 <= 0)
            {
                (sender as XApi).GetLog().Info("OnRspQryTrade");
                return;
            }

            (sender as XApi).GetLog().Info("OnRspQryTrade:" + trade.ToFormattedString());
        }
Beispiel #14
0
        private void _OnRtnTrade(IntPtr ptr1, int size1)
        {
            // 求快,不加
            //if (OnRtnTrade_ == null)
            //    return;

            //TradeField obj = PInvokeUtility.GetObjectFromIntPtr<TradeField>(ptr1);
            TradeField obj = (TradeField)Marshal.PtrToStructure(ptr1, typeof(TradeField));

            OnRtnTrade_(this, ref obj);
        }
Beispiel #15
0
        private void _OnRspQryTrade(IntPtr ptr1, int size1, double double1)
        {
            if (OnRspQryTrade_ == null)
            {
                return;
            }

            TradeField obj = PInvokeUtility.GetObjectFromIntPtr <TradeField>(ptr1);

            OnRspQryTrade_(this, ref obj, size1, double1 != 0);
        }
Beispiel #16
0
        private void _OnRtnTrade(IntPtr ptr1, int size1)
        {
            if (OnRtnTrade == null)
            {
                return;
            }

            TradeField obj = PInvokeUtility.GetObjectFromIntPtr <TradeField>(ptr1);

            OnRtnTrade(this, ref obj);
        }
 private void OnRtnTrade_callback(object sender, ref TradeField trade)
 {
     (sender as XApi).GetLog().Debug("OnRtnTrade:" + trade.ToFormattedString());
     try
     {
         orderMap.Process(ref trade);
     }
     catch (Exception ex)
     {
         (sender as XApi).GetLog().Error(ex);
     }
 }
Beispiel #18
0
        private void _OnRtnTrade(IntPtr ptr1, int size1)
        {
            // 求快,不加
            if (OnRtnTrade_ == null)
            {
                return;
            }

            TradeField obj = (TradeField)Marshal.PtrToStructure(ptr1, typeof(TradeField));

            OnRtnTrade_(this, ref obj);
        }
Beispiel #19
0
        private ConcurrentDictionary <string, TradeField> tradesDict;      // 防止重复收到成交回报
        private OrderRecord GetExternalOrder(ref TradeField field)
        {
            ExternalOrderRecord record;

            if (externalOrders.TryGetValue(field.InstrumentID, out record))
            {
                if (field.OpenClose == OpenCloseType.Open)
                {
                    return(field.Side == XAPI.OrderSide.Buy ? record.BuyOpen : record.SellOpen);
                }
                return(field.Side == XAPI.OrderSide.Buy ? record.BuyClose : record.SellClose);
            }
            return(null);
        }
Beispiel #20
0
        public void Process(ref TradeField trade, NLog.Logger log)
        {
            lock (this)
            {
                OrderRecord record;
                if (!workingOrders.TryGetValue(trade.ID, out record))
                {
                    record = GetExternalOrder(ref trade);
                }
                if (record != null)
                {
                    // CTP出现过重复发送委托与成交的情况,需要过滤。并提示
                    // 同一交易ID同一方向不会同时出现,出现表示系统重复提示
                    // 不向方向表示自成交了
                    TradeField field = null;
                    if (tradesDict.TryGetValue(trade.TradeID, out field))
                    {
                        // 有取出,可能是自成交,也可能是重复发单
                        if (trade.Side == field.Side)
                        {
                            log.Error("重复收到成交回报!");
                            // 后面不再处理
                            return;
                        }
                        else
                        {
                            log.Warn("出现自成交!");
                            // 自成交也得处理
                        }
                    }
                    else
                    {
                        tradesDict[trade.TradeID] = trade;
                    }

                    record.AddFill(trade.Price, (int)trade.Qty);
                    SQ.ExecType     execType    = SQ.ExecType.ExecTrade;
                    SQ.OrderStatus  orderStatus = (record.LeavesQty > 0) ? SQ.OrderStatus.PartiallyFilled : SQ.OrderStatus.Filled;
                    ExecutionReport report      = CreateReport(record, execType, orderStatus);
                    report.LastPx  = trade.Price;
                    report.LastQty = trade.Qty;
                    provider.EmitExecutionReport(report);
                }
                else
                {
                    // log.Warn("Trade,找不到订单,workingOrders.Count={0}", workingOrders.Count);
                }
            }
        }
Beispiel #21
0
 private void OnRspQryTrade_callback(object sender, ref TradeField trade, int size1, bool bIsLast)
 {
     if (size1 <= 0)
     {
         (sender as XApi).GetLog().Info("OnRspQryTrade");
     }
     else
     {
         (sender as XApi).GetLog().Info("OnRspQryTrade:" + trade.ToFormattedString());
     }
     if (OnRspQryTrade != null)
     {
         OnRspQryTrade(this, ref trade, size1, bIsLast);
     }
 }
Beispiel #22
0
 //交易回报
 public static void Body_OnRtnTrade(TradeField tf)
 {
     lock (OcAllTradeField)
     {
         int index = OcAllTradeField.IndexOf(tf);
         if (index == -1)
         {
             OcAllTradeField.Insert(0, tf);
         }
         else
         {
             OcAllTradeField[index].CThostFtdcTradeFieldInstance = tf.CThostFtdcTradeFieldInstance;
         }
     }
 }
Beispiel #23
0
        private void _onRtnTrade(object sender, ref TradeField field)
        {
            Instrument inst = InstrumentManager.GetInstrument(field.InstrumentID);

            if (inst == null)
            {
                return;
            }
            //
            BrokerTrade trade = new BrokerTrade(inst
                                                , ConvertUtil.ConvertExchange(field.ExchangeID)
                                                , ConvertUtil.ConvertDirectionType(field.Side)
                                                , (long)field.Qty
                                                , field.Price
                                                , ConvertUtil.ConvertOpenCloseType(field.OpenClose)
                                                , DateTime.Now);

            mOnTrade?.Invoke(trade);
        }
Beispiel #24
0
        public void Process(ref TradeField trade)
        {
            OrderRecord record;

            if (!workingOrders.TryGetValue(trade.ID, out record))
            {
                record = GetExternalOrder(ref trade);
            }
            if (record != null)
            {
                record.AddFill(trade.Price, (int)trade.Qty);
                SQ.ExecType     execType    = SQ.ExecType.ExecTrade;
                SQ.OrderStatus  orderStatus = (record.LeavesQty > 0) ? SQ.OrderStatus.PartiallyFilled : SQ.OrderStatus.Filled;
                ExecutionReport report      = CreateReport(record, execType, orderStatus);
                report.LastPx  = trade.Price;
                report.LastQty = trade.Qty;
                provider.EmitExecutionReport(report);
            }
        }
Beispiel #25
0
        private void ProcessTrade(TradeField trade)
        {
            _map.TryGetOrder(trade.ID, out var record);
            if (record != null)
            {
                record.AddFill(trade.Price, trade.Qty);
                var status = record.LeavesQty > 0 ? OrderStatus.PartiallyFilled : OrderStatus.Filled;
                var report = CreateReport(record, status, ExecType.ExecTrade);
                report.ExecId   = trade.TradeID;
                report.DateTime = trade.UpdateTime();
                if (report.DateTime.Date != _provider.trader.TradingDay)
                {
                    report.TransactTime = _provider.trader.TradingDay.Add(report.DateTime.TimeOfDay);
                }
                else
                {
                    report.TransactTime = report.DateTime;
                }

                report.LastPx  = trade.Price;
                report.LastQty = trade.Qty;

                if (Math.Abs(trade.Commission) < double.Epsilon)
                {
                    report.Commission = _provider.GetCommission(report);
                }
                else
                {
                    report.Commission = trade.Commission;
                }

                _provider.OnMessage(report);
                if (status == OrderStatus.Filled)
                {
                    _map.RemoveDone(trade.ID);
                }
            }
        }
Beispiel #26
0
        private void OnRtnTrade_callback(object sender, ref TradeField trade)
        {
            lock (this)
            {
                var log = (sender as XApi).GetLog();
                log.Debug("OnRtnTrade:" + trade.ToFormattedString());

                // 由策略来收回报
                if (OnRtnTrade != null)
                {
                    OnRtnTrade(sender, ref trade);
                }

                try
                {
                    orderMap.Process(ref trade, log);
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
            }
        }
Beispiel #27
0
 public OnRspQryTradeEventArgs(ref TradeField trade, int size1, bool bIsLast)
 {
     this.trade = trade;
     this.size1 = size1;
     this.bIsLast = bIsLast;
 }
Beispiel #28
0
 protected virtual void OnRtnTrade(object sender, TradeField trade)
 {
     Provider.OnMessage(trade);
 }
Beispiel #29
0
 public OnRtnTradeEventArgs(ref TradeField trade)
 {
     this.trade = trade;
 }
Beispiel #30
0
 public OnRtnTradeEventArgs(ref TradeField trade)
 {
     this.trade = trade;
 }
Beispiel #31
0
 internal void OnMessage(TradeField field)
 {
     _processor.PostTrade(field);
 }
Beispiel #32
0
 private void OnRspQryTrade_callback(object sender, ref TradeField trade, int size1, bool bIsLast)
 {
     if (null != OnRspQryTrade)
     {
         OnRspQryTrade(this, new OnRspQryTradeEventArgs(ref trade, size1, bIsLast));
     }
 }
Beispiel #33
0
        private void OnRspQryTrade_callback(object sender, ref TradeField trade, int size1, bool bIsLast)
        {
            if (null == OnRspQryTrade)
                return;

            TradeClass cls = null;

            if (size1 > 0)
            {
                TradeField field = trade;

                cls = new TradeClass();

                cls.InstrumentName = field.InstrumentName();
                cls.Symbol = field.Symbol;
                cls.InstrumentID = field.InstrumentID;
                cls.ExchangeID = field.ExchangeID;
                cls.ClientID = field.ClientID;
                cls.AccountID = field.AccountID;
                cls.Side = (int)field.Side;
                cls.Side_String = Enum<XAPI.OrderSide>.ToString(field.Side);
                cls.Qty = field.Qty;
                cls.Price = field.Price;
                cls.OpenClose = (int)field.OpenClose;
                cls.OpenClose_String = Enum<XAPI.OpenCloseType>.ToString(field.OpenClose);
                cls.HedgeFlag = (int)field.HedgeFlag;
                cls.HedgeFlag_String = Enum<XAPI.HedgeFlagType>.ToString(field.HedgeFlag);
                cls.Date = field.Date;
                cls.Time = field.Time;
                cls.ID = field.ID;
                cls.TradeID = field.TradeID;
                cls.ReserveInt32 = field.ReserveInt32;
                cls.ReserveChar64 = field.ReserveChar64;
            }

            if (null == OnRspQryTrade)
            {
                QueueData qd = new QueueData();
                qd.Type = (int)ResponeType.OnRspQryTrade;
                qd.Type_String = Enum<XAPI.ResponeType>.ToString(ResponeType.OnRspQryTrade);
                qd.Sender = this;
                qd.Data1 = cls;
                qd.Data2 = size1;
                qd.Data3 = bIsLast;

                MessageQueue.Enqueue(qd);
            }
            else
            {
                OnRspQryTrade(this, ref cls, size1, bIsLast);
            }
        }
Beispiel #34
0
 public void PostTrade(TradeField trade)
 {
     _orderBlock.Post(new TradingEvent(ExecType.ExecTrade, trade));
 }
Beispiel #35
0
 private void OnRtnTrade_callback(object sender, ref TradeField trade)
 {
     if (null != OnRtnTrade)
     {
         OnRtnTrade(this, new OnRtnTradeEventArgs(ref trade));
     }
 }