Esempio n. 1
0
        public void Init(ServerInfoField server, UserInfoField user)
        {
            if (Api != null)
            {
                return;
            }
            if (!CheckSettings(server, user))
            {
                return;
            }
            _queryManager = new CtpQueryManager(this);
            _processor    = new CtpDealProcessor(this);
            User          = user;
            Server        = server;
            Api           = new CtpTraderApi(GetFlowPath(server, user));
            InitHandler();
            var items = server.Address.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var item in items)
            {
                Api.RegisterFront(item);
            }
            Api.SubscribePrivateTopic(CtpConvert.GetCtpResumeType(server.PrivateTopicResumeType));
            Api.SubscribePublicTopic(CtpConvert.GetCtpResumeType(server.PublicTopicResumeType));
            _publisher.Post(ConnectionStatus.Connecting);
            Api.Init();
        }
        private void ProcessCancelOrder(string id)
        {
            if (_cancelPendings.Contains(id))
            {
                return;
            }

            if (!_orders.TryGetById(id, out var order))
            {
                return;
            }

            if (string.IsNullOrEmpty(order.OrderID))
            {
                _cancelPendings.Add(id);
                return;
            }

            var action = new CtpInputOrderAction();

            (action.FrontID, action.SessionID, action.OrderRef) = CtpConvert.ParseId(order.ID);
            action.OrderSysID     = order.OrderID;
            action.ExchangeID     = order.ExchangeID.ToUpper();
            action.InstrumentID   = order.InstrumentID;
            action.ActionFlag     = CtpActionFlagType.Delete;
            action.OrderActionRef = _client.GetNextRequestId();
            action.InvestorID     = _client.CtpLoginInfo.UserID;
            action.BrokerID       = _client.CtpLoginInfo.BrokerID;
            _client.Api.ReqOrderAction(action, _client.GetNextRequestId());
        }
Esempio n. 3
0
        protected override bool ProcessInstrument(CtpResponse?rsp)
        {
            if (!rsp.HasValue)
            {
                return(true);
            }
            try {
                var data = rsp.Value.Item1.AsInstrument;
                if (data == null)
                {
                    if (rsp.Value.IsLast)
                    {
                        _client.spi.ProcessQryInstrument(null, true);
                    }
                    return(true);
                }

                if (CtpConvert.CheckRspInfo(rsp.Value.Item2))
                {
                    _client.spi.ProcessQryInstrument(CtpConvert.GetInstrumentField(data), rsp.Value.IsLast);
                    return(rsp.Value.IsLast);
                }
                else
                {
                    _client.SendError(rsp.Value.Item2, nameof(ProcessInstrument));
                    _client.spi.ProcessQryInstrument(null, true);
                }
            }
            catch (Exception e) {
                _client.SendError(-1, e.Message);
                _client.spi.ProcessQryInstrument(null, true);
            }
            return(true);
        }
Esempio n. 4
0
        private void OnRspUserLogin(ref CtpResponse rsp)
        {
            ctpLoginInfo = rsp.Item1.AsRspUserLogin;
            if (ctpLoginInfo != null && CtpConvert.CheckRspInfo(rsp.Item2))
            {
                if (!Connected)
                {
                    UserLogin            = new RspUserLoginField();
                    UserLogin.TradingDay = CtpConvert.GetDate(ctpLoginInfo.TradingDay);
                    UserLogin.LoginTime  = CtpConvert.GetTime(ctpLoginInfo.LoginTime);
                    UserLogin.UserID     = ctpLoginInfo.UserID;
                    UserLogin.SessionID  = $"{ctpLoginInfo.FrontID}:{ctpLoginInfo.SessionID}";
                    UserLogin.Text       = string.IsNullOrEmpty(ctpLoginInfo.MaxOrderRef) ? "1" : ctpLoginInfo.MaxOrderRef;
                    _orderRef            = int.Parse(UserLogin.Text);
                    _publisher.Post(ConnectionStatus.Logined, UserLogin);
#if CTPMINI
                    Connected = true;
                    _publisher.Post(ConnectionStatus.Confirmed);
                    _publisher.Post(ConnectionStatus.Done, UserLogin);
#else
                    DoSettlementInfoConfirm();
#endif
                }
                else
                {
                    spi.ProcessLog(new LogField(LogLevel.Debug, $"Ctpse trader({User.UserID}) login success"));
                }
            }
            else
            {
                SendError(rsp.Item2, nameof(OnRspUserLogin));
                _publisher.Post(ConnectionStatus.Disconnected);
            }
        }
 private void OnRspUserLogin(object sender, CtpRspUserLogin response, CtpRspInfo rspInfo, int requestId, bool isLast)
 {
     if (response != null && CtpConvert.CheckRspInfo(rspInfo))
     {
         if (Connected)
         {
             Resubscribe();
             return;
         }
         Connected = true;
         UserLogin = new RspUserLoginField();
         var now = DateTime.Now;
         UserLogin.TradingDay = now.Year * 10000 + now.Month * 100 + now.Day;
         UserLogin.LoginTime  = now.Hour * 10000 + now.Minute * 100 + now.Second;
         UserLogin.UserID     = User.UserID;
         UserLogin.SessionID  = string.Empty;
         _publisher.Post(ConnectionStatus.Logined, UserLogin);
         _publisher.Post(ConnectionStatus.Done);
     }
     else
     {
         SendError(rspInfo, nameof(OnRspUserLogin));
         _publisher.Post(ConnectionStatus.Disconnected);
     }
 }
Esempio n. 6
0
 private void OnRspUserLogin(ref CtpResponse rsp)
 {
     CtpLoginInfo = rsp.Item1.AsRspUserLogin;
     if (CtpLoginInfo != null && CtpConvert.CheckRspInfo(rsp.Item2))
     {
         UserLogin            = new RspUserLoginField();
         UserLogin.TradingDay = CtpConvert.GetDate(CtpLoginInfo.TradingDay);
         UserLogin.LoginTime  = CtpConvert.GetTime(CtpLoginInfo.LoginTime);
         UserLogin.UserID     = CtpLoginInfo.UserID;
         UserLogin.SessionID  = $"{CtpLoginInfo.FrontID}:{CtpLoginInfo.SessionID}";
         _publisher.Post(ConnectionStatus.Logined, UserLogin);
         if (string.IsNullOrEmpty(CtpLoginInfo.MaxOrderRef))
         {
             _orderRefId = 1;
         }
         else
         {
             _orderRefId = int.Parse(CtpLoginInfo.MaxOrderRef) + 1;
         }
         DoSettlementInfoConfirm();
     }
     else
     {
         SendError(rsp.Item2, nameof(OnRspUserLogin));
         _publisher.Post(ConnectionStatus.Disconnected);
     }
 }
        private void ProcessOrderReject(CtpInputOrder input, CtpRspInfo rspInfo)
        {
            var login = _client.CtpLoginInfo;
            var id    = CtpConvert.GetId(input, login);

            ProcessOrderReject(id, rspInfo);
        }
Esempio n. 8
0
        private void OnRtnInstrumentStatus(ref CtpResponse rsp)
        {
            var status = rsp.Item1.AsInstrumentStatus;
            var field  = CtpConvert.GetInstrumentStatus(status);

            Spi.ProcessRtnInstrumentStatus(field);
        }
        private void ProcessNewOrder(NewOrderEvent e)
        {
            var data = CtpConvert.GetCtpOrder(e.Order);

            data.InvestorID = _client.CtpLoginInfo.UserID;
            data.BrokerID   = _client.CtpLoginInfo.BrokerID;
            _client.Api.ReqOrderInsert(data, _client.GetNextRequestId());
            _orders.AddOrder(e.Order);
        }
        private void ProcessRtnTrade(CtpTrade data)
        {
            var sysId = $"{data.ExchangeID}:{data.InstrumentID}:{data.OrderSysID}";

            if (_orders.TryGetBySysId(sysId, out var order))
            {
                var trade = CtpConvert.GetTrade(data);
                trade.ID = order.ID;
                _client.Spi.ProcessRtnTrade(trade);
            }
        }
 private void ProcessNewOrder(NewOrderEvent e)
 {
     _orders.AddOrder(e.Order);
     if (e.Order.Status == OrderStatus.NotSent)
     {
         var data = CtpConvert.GetInputOrder(e.Order);
         data.InvestorID = _client.CtpLoginInfo.UserID;
         data.BrokerID   = _client.CtpLoginInfo.BrokerID;
         _client.Api.ReqOrderInsert(data, _client.GetNextRequestId());
     }
 }
 private void ProcessRtnTrade(CtpTrade data)
 {
     if (_orders.TryGetBySysId(data.OrderSysID, out var order))
     {
         var trade = CtpConvert.GetTrade(data);
         trade.ID = order.ID;
         _client.Spi.ProcessRtnTrade(trade);
     }
     else
     {
         _tradePendings.Add(data);
     }
 }
Esempio n. 13
0
        protected override bool ProcessOrder(CtpResponse?rsp)
        {
            if (rsp == null)
            {
                return(true);
            }

            if (CtpConvert.CheckRspInfo(rsp.Value.Item2))
            {
                _client.spi.ProcessQryOrder(CtpConvert.GetOrder(rsp.Value.Item1.AsOrder), rsp.Value.IsLast);
                return(rsp.Value.IsLast);
            }
            return(true);
        }
 private void ProcessRtnTrade(CtpTrade data)
 {
     _client.spi.ProcessLog(new LogField(LogLevel.Trace, "RtnTrade"));
     if (_orders.TryGetBySysId(data.OrderSysID, out var order))
     {
         var trade = CtpConvert.GetTrade(data);
         trade.ID = order.ID;
         _client.spi.ProcessRtnTrade(trade);
     }
     else
     {
         _tradePending.Add(data);
     }
 }
Esempio n. 15
0
        private void OnRspAuthenticate(ref CtpResponse rsp)
        {
            var data = rsp.Item1.AsRspAuthenticate;

            if (data != null && CtpConvert.CheckRspInfo(rsp.Item2))
            {
                _publisher.Post(ConnectionStatus.Authorized);
                DoLogin();
            }
            else
            {
                SendError(rsp.Item2, nameof(OnRspAuthenticate));
                _publisher.Post(ConnectionStatus.Disconnected);
            }
        }
Esempio n. 16
0
        protected override bool ProcessTrade(CtpResponse?rsp)
        {
            if (rsp == null)
            {
                return(true);
            }

            if (CtpConvert.CheckRspInfo(rsp.Value.Item2))
            {
                _client.Spi.ProcessQryTrade(CtpConvert.GetTrade(rsp.Value.Item1.AsTrade), rsp.Value.IsLast);
                return(rsp.Value.IsLast);
            }

            return(true);
        }
Esempio n. 17
0
        private void OnRspSettlementInfoConfirm(ref CtpResponse rsp)
        {
            var data = rsp.Item1.AsSettlementInfoConfirm;

            if (data != null && CtpConvert.CheckRspInfo(rsp.Item2))
            {
                Connected = true;
                _publisher.Post(ConnectionStatus.Confirmed);
                _publisher.Post(ConnectionStatus.Done, UserLogin);
            }
            else
            {
                SendError(rsp.Item2, nameof(OnRspSettlementInfoConfirm));
                _publisher.Post(ConnectionStatus.Disconnected);
            }
        }
        private void ProcessPosition(CtpResponse rsp)
        {
            var data = rsp.Item1.AsInvestorPosition;

            if (data == null)
            {
                return;
            }
            if (CtpConvert.CheckRspInfo(rsp.Item2))
            {
                _client.Spi.ProcessQryPosition(CtpConvert.GetPositionField(data), rsp.IsLast);
            }
            else
            {
                _client.SendError(rsp.Item2, nameof(ProcessPosition));
                _client.Spi.ProcessQryPosition(null, true);
            }
        }
Esempio n. 19
0
        protected override void ProcessTradingAccount(CtpResponse?rsp)
        {
            var data = rsp?.Item1.AsTradingAccount;

            if (data == null)
            {
                return;
            }
            if (CtpConvert.CheckRspInfo(rsp.Value.Item2))
            {
                _client.Spi.ProcessQryAccount(CtpConvert.GetAccountField(data), rsp.Value.IsLast);
            }
            else
            {
                _client.SendError(rsp.Value.Item2, nameof(ProcessTradingAccount));
                _client.Spi.ProcessQryAccount(null, true);
            }
        }
 private void ProcessNewOrder(NewOrderEvent e)
 {
     _orders.AddOrder(e.Order);
     if (e.Order.Status == OrderStatus.NotSent)
     {
         var data = CtpConvert.GetInputOrder(e.Order);
         data.InvestorID = _client.ctpLoginInfo.UserID;
         data.BrokerID   = _client.ctpLoginInfo.BrokerID;
         data.OrderRef   = e.Order.LocalID;
         var ret = _client.api.ReqOrderInsert(data, _client.GetNextRequestId());
         if (ret == 0)
         {
             return;
         }
         ProcessOrderReject(data, new CtpRspInfo {
             ErrorID = -1, ErrorMsg = "连接中断发送失败"
         });
     }
 }
        private void ProcessRtnOrder(CtpOrder data)
        {
            var localId = $"{data.FrontID}:{data.SessionID}:{data.OrderRef}";

            if (!_orders.TryGetByLocalId(localId, out var order))
            {
                return;
            }

            if (!string.IsNullOrEmpty(data.OrderSysID) && string.IsNullOrEmpty(order.OrderID))
            {
                order.OrderID = $"{data.ExchangeID}:{data.InstrumentID}:{data.OrderSysID}";
                _orders.SetOrderSysId(order.OrderID, localId);
            }
            ReportOrder(order, CtpConvert.GetExecType(data), CtpConvert.GetOrderStatus(data), 0, 0, data.StatusMsg);
            if (_cancelPendings.Contains(localId))
            {
                _cancelPendings.Remove(localId);
                ProcessCancelOrder(localId);
            }
        }
        private void ProcessRtnOrder(CtpOrder data)
        {
            var id = CtpConvert.GetId(data);

            if (!_orders.TryGetById(id, out var order))
            {
                return;
            }
            if (!string.IsNullOrEmpty(data.OrderSysID) && string.IsNullOrEmpty(order.OrderID))
            {
                order.OrderID    = data.OrderSysID;
                order.ExchangeID = data.ExchangeID;
                _orders.SetMap(order);
                ProcessTradePending();
            }
            ReportOrder(order, CtpConvert.GetExecType(data), CtpConvert.GetOrderStatus(data), 0, 0, data.StatusMsg);
            if (_cancelPendings.Contains(id))
            {
                _cancelPendings.Remove(id);
                ProcessCancelOrder(id);
            }
        }
Esempio n. 23
0
 protected override void ProcessInvestorPosition(CtpResponse?rsp)
 {
     if (!rsp.HasValue)
     {
         return;
     }
     if (CtpConvert.CheckRspInfo(rsp.Value.Item2))
     {
         var           data     = rsp.Value.Item1.AsInvestorPosition;
         PositionField position = null;
         if (data != null)
         {
             position = CtpConvert.GetPositionField(data);
         }
         _client.Spi.ProcessQryPosition(position, rsp.Value.IsLast);
     }
     else
     {
         _client.SendError(rsp.Value.Item2, nameof(ProcessInvestorPosition));
         _client.Spi.ProcessQryPosition(null, true);
     }
 }
Esempio n. 24
0
        private void OnRspAuthenticate(ref CtpResponse rsp)
        {
            var data = rsp.Item1.AsRspAuthenticate;

            if (data != null && CtpConvert.CheckRspInfo(rsp.Item2))
            {
                if (!Connected)
                {
                    _publisher.Post(ConnectionStatus.Authorized);
                }
                else
                {
                    spi.ProcessLog(new LogField(LogLevel.Debug, $"Ctp trader({User.UserID}) authorized"));
                }

                DoLogin();
            }
            else
            {
                SendError(rsp.Item2, nameof(OnRspAuthenticate));
                _publisher.Post(ConnectionStatus.Disconnected);
            }
        }
 private void ProcessInstrument(CtpResponse rsp)
 {
     try {
         var data = rsp.Item1.AsInstrument;
         if (data == null)
         {
             return;
         }
         if (CtpConvert.CheckRspInfo(rsp.Item2))
         {
             _client.Spi.ProcessQryInstrument(CtpConvert.GetInstrumentField(data), rsp.IsLast);
         }
         else
         {
             _client.SendError(rsp.Item2, nameof(ProcessInstrument));
             _client.Spi.ProcessQryInstrument(null, true);
         }
     }
     catch (Exception e) {
         _client.SendError(-1, e.Message);
         _client.Spi.ProcessQryInstrument(null, true);
     }
 }
Esempio n. 26
0
 internal void SendError(int reason, string source)
 {
     SendError(new ErrorField(reason, reason, CtpConvert.GetReasonMsg(reason), source));
 }
Esempio n. 27
0
        private void OnRtnDepthMarketData(object sender, CtpDepthMarketData data)
        {
            //Thread.Sleep(5000);
            var market = new DepthMarketDataField();

            market.InstrumentID = data.InstrumentID;
            market.ExchangeID   = data.ExchangeID;
            //market.Exchange = CtpConvert.GetExchangeType(data.ExchangeID);
            CtpConvert.SetExchangeTime(data, market);

            market.LastPrice    = CtpConvert.IsInvalid(data.LastPrice) ? 0 : data.LastPrice;
            market.Volume       = data.Volume;
            market.OpenInterest = data.OpenInterest;
            market.Turnover     = data.Turnover;
            market.AveragePrice = data.AveragePrice;
            if (CtpConvert.IsInvalid(data.OpenPrice))
            {
                market.OpenPrice    = 0;
                market.HighestPrice = 0;
                market.LowestPrice  = 0;
                //market.TradingPhase = TradingPhaseType.BeforeTrading;
            }
            else
            {
                market.OpenPrice    = data.OpenPrice;
                market.HighestPrice = data.HighestPrice;
                market.LowestPrice  = data.LowestPrice;
                //market.TradingPhase = TradingPhaseType.Continuous;
            }

            if (CtpConvert.IsInvalid(data.ClosePrice))
            {
                market.ClosePrice = 0;
            }
            else
            {
                market.ClosePrice = data.ClosePrice;
                //market.TradingPhase = TradingPhaseType.Closed;
            }
            market.SettlementPrice    = CtpConvert.IsInvalid(data.SettlementPrice) ? 0 : data.SettlementPrice;
            market.UpperLimitPrice    = data.UpperLimitPrice;
            market.LowerLimitPrice    = data.LowerLimitPrice;
            market.PreClosePrice      = data.PreClosePrice;
            market.PreSettlementPrice = data.PreSettlementPrice;
            market.PreOpenInterest    = data.PreOpenInterest;
            market.Asks = new DepthField[5];
            market.Bids = new DepthField[5];
            if (!CtpConvert.IsInvalid(data.AskPrice1))
            {
                market.Asks[0].Price = data.AskPrice1;
                market.Asks[0].Size  = data.AskVolume1;
                if (!CtpConvert.IsInvalid(data.AskPrice2))
                {
                    market.Asks[1].Price = data.AskPrice2;
                    market.Asks[1].Size  = data.AskVolume2;
                    if (!CtpConvert.IsInvalid(data.AskPrice3))
                    {
                        market.Asks[2].Price = data.AskPrice3;
                        market.Asks[2].Size  = data.AskVolume3;
                        if (!CtpConvert.IsInvalid(data.AskPrice4))
                        {
                            market.Asks[3].Price = data.AskPrice4;
                            market.Asks[3].Size  = data.AskVolume4;
                            if (!CtpConvert.IsInvalid(data.AskPrice5))
                            {
                                market.Asks[4].Price = data.AskPrice5;
                                market.Asks[4].Size  = data.AskVolume5;
                            }
                        }
                    }
                }
            }

            if (!CtpConvert.IsInvalid(data.BidPrice1))
            {
                market.Bids[0].Price = data.BidPrice1;
                market.Bids[0].Size  = data.BidVolume1;
                if (!CtpConvert.IsInvalid(data.BidPrice2))
                {
                    market.Bids[1].Price = data.BidPrice2;
                    market.Bids[1].Size  = data.BidVolume2;
                    if (!CtpConvert.IsInvalid(data.BidPrice3))
                    {
                        market.Bids[2].Price = data.BidPrice3;
                        market.Bids[2].Size  = data.BidVolume3;
                        if (!CtpConvert.IsInvalid(data.BidPrice4))
                        {
                            market.Bids[3].Price = data.BidPrice4;
                            market.Bids[3].Size  = data.BidVolume4;
                            if (!CtpConvert.IsInvalid(data.BidPrice5))
                            {
                                market.Bids[4].Price = data.BidPrice5;
                                market.Bids[4].Size  = data.BidVolume5;
                            }
                        }
                    }
                }
            }
            _spi.ProcessDepthMarketData(market);
        }
Esempio n. 28
0
        private void OnRtnDepthMarketData(object sender, CtpDepthMarketData data)
        {
            var market = new DepthMarketDataField();

            market.InstrumentID = data.InstrumentID;
            market.ExchangeID   = data.ExchangeID;
            market.Exchange     = CtpConvert.GetExchangeType(data.ExchangeID);
            switch (market.Exchange)
            {
            case ExchangeType.CZCE:
                CtpConvert.SetCzceExchangeTime(data, market);
                break;

            case ExchangeType.DCE:
                CtpConvert.SetDceExchangeTime(data, market);
                break;

            default:
                CtpConvert.SetExchangeTime(data, market);
                break;
            }

            market.LastPrice    = CtpConvert.IsMax(data.LastPrice) ? 0 : data.LastPrice;
            market.Volume       = data.Volume;
            market.OpenInterest = data.OpenInterest;
            market.Turnover     = data.Turnover;
            market.AveragePrice = data.AveragePrice;
            if (CtpConvert.IsMax(data.OpenPrice))
            {
                market.OpenPrice    = 0;
                market.HighestPrice = 0;
                market.LowestPrice  = 0;
                market.TradingPhase = TradingPhaseType.BeforeTrading;
            }
            else
            {
                market.OpenPrice    = data.OpenPrice;
                market.HighestPrice = data.HighestPrice;
                market.LowestPrice  = data.LowestPrice;
                market.TradingPhase = TradingPhaseType.Continuous;
            }

            if (CtpConvert.IsMax(data.ClosePrice))
            {
                market.ClosePrice = 0;
            }
            else
            {
                market.ClosePrice   = data.ClosePrice;
                market.TradingPhase = TradingPhaseType.Closed;
            }
            market.SettlementPrice    = CtpConvert.IsMax(data.SettlementPrice) ? 0 : data.SettlementPrice;
            market.UpperLimitPrice    = data.UpperLimitPrice;
            market.LowerLimitPrice    = data.LowerLimitPrice;
            market.PreClosePrice      = data.PreClosePrice;
            market.PreSettlementPrice = data.PreSettlementPrice;
            market.PreOpenInterest    = data.PreOpenInterest;
            if (!CtpConvert.IsMax(data.AskPrice1))
            {
                market.Asks = new[] { new DepthField {
                                          Price = data.AskPrice1, Size = data.AskVolume1
                                      } };
            }
            else
            {
                market.Asks = new DepthField[0];
            }

            if (!CtpConvert.IsMax(data.BidPrice1))
            {
                market.Bids = new[] { new DepthField {
                                          Price = data.BidPrice1, Size = data.BidVolume1
                                      } };
            }
            else
            {
                market.Bids = new DepthField[0];
            }
            _spi.ProcessDepthMarketData(market);
        }