Example #1
0
        int SESSION_ID; //会话编号

        #endregion Fields

        #region Methods

        public void Run()
        {
            api = new KSFTTraderAdapter();
            cosApi = api.LoadExtApi(COS_API);

            api.OnFrontConnected += new FrontConnected(OnFrontConnected);
            api.OnRspUserLogin += new RspUserLogin(OnRspUserLogin);

            cosApi.OnRspInitInsertConditionalOrder += new RspInitInsertConditionalOrder(OnRspInitInsertConditionalOrder);
            cosApi.OnRspQueryConditionalOrder += new RspQueryConditionalOrder(OnRspQueryConditionalOrder);
            cosApi.OnRspInsertProfitAndLossOrder += new RspInsertProfitAndLossOrder(OnRspInsertProfitAndLossOrder);
            cosApi.OnRspModifyConditionalOrder += new RspModifyConditionalOrder(OnRspModifyConditionalOrder);
            cosApi.OnRspModifyProfitAndLossOrder += new RspModifyProfitAndLossOrder(OnRspModifyProfitAndLossOrder);
            cosApi.OnRspPauseConditionalOrder += new RspPauseConditionalOrder(OnRspPauseConditionalOrder);
            cosApi.OnRspQueryProfitAndLossOrder += new RspQueryProfitAndLossOrder(OnRspQueryProfitAndLossOrder);
            cosApi.OnRspRemoveConditionalOrder += new RspRemoveConditionalOrder(OnRspRemoveConditionalOrder);
            cosApi.OnRspRemoveProfitAndLossOrder += new RspRemoveProfitAndLossOrder(OnRspRemoveProfitAndLossOrder);
            cosApi.OnRspSelectConditionalOrder += new RspSelectConditionalOrder(OnRspSelectConditionalOrder);
            cosApi.OnRtnCOSAskSelect += new RtnCOSAskSelect(OnRtnCOSAskSelect);
            cosApi.OnRtnCOSStatus += new RtnCOSStatus(OnRtnCOSStatus);
            cosApi.OnRtnPLStatus += new RtnPLStatus(OnRtnPLStatus);

            api.SubscribePublicTopic(EnumTeResumeType.THOST_TERT_QUICK);					// 注册公有流
            api.SubscribePrivateTopic(EnumTeResumeType.THOST_TERT_QUICK);					// 注册私有流

            try
            {
                api.RegisterFront(FRONT_ADDR);
                api.Init();
                api.Join(); // 阻塞直到关闭或者CTRL+C
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                api.Release();
            }
        }
Example #2
0
        private IDictionary <int, ICollection <ThostFtdcInvestorPositionField> > _positions = new Dictionary <int, ICollection <ThostFtdcInvestorPositionField> >();  // 键是reqeust id

        public void Initialize()
        {
            var configPath = Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "Kingstar.yaml");

            if (!File.Exists(configPath))
            {
                throw new Exception(string.Format("{0}初始化失败,配置文件{1}不存在", this.GetType().FullName, configPath));
            }

            using (var reader = new StreamReader(configPath))
            {
                var deserializer = new Deserializer(namingConvention: new UnderscoredNamingConvention());
                this._config = deserializer.Deserialize <Config>(reader);
            }

            // ================================================================================
            // 交易初始化
            // ================================================================================
            this._trade = new KSFTTraderAdapter(Path.GetDirectoryName(this.GetType().Assembly.Location));
            this._trade.OnFrontConnected       += _trade_OnFrontConnected;
            this._trade.OnRspUserLogin         += _trade_OnRspUserLogin;
            this._trade.OnRspQryInstrument     += _trade_OnRspQryInstrument;
            this._trade.OnRspError             += OnRspError;
            this._trade.OnRspQryTradingAccount += _trade_OnRspQryTradingAccount;
            this._trade.OnRspQryOrder          += _trade_OnRspQryOrder;
            this._trade.OnRspOrderInsert       += (order, rsp, _3, _4) =>
            {
                if (rsp.ErrorID != 0)
                {
                    this.OnLog($"ERROR: OnRspOrderInsert: {nameof(rsp.ErrorID) + rsp.ErrorID}, {nameof(rsp.ErrorMsg) + rsp.ErrorMsg}");
                }
            };
            this._trade.OnRtnOrder += _trade_OnRtnOrder;
            this._trade.OnRtnTrade += _ =>
            {
                this.QueryPositions();
                this.OnTrade(new Trade {
                    InstrumentID = _.InstrumentID, Price = _.Price, Qty = _.Volume
                });
            };
            this._trade.OnRspQryInvestorPosition += (positionField, rspField, requestId, isLast) =>
            {
                if (positionField != null)
                {
                    this._positions[requestId].Add(positionField);
                }
                if (isLast)
                {
                    if (rspField.ErrorID != 0)
                    {
                        throw new Exception($"查询持仓失败,代码:{rspField.ErrorID},描述:{rspField.ErrorMsg}");
                    }

                    this.Account.Positions = this._positions[requestId].Select(_ => _.ToOptionPosition()).ToList();
                    this._positions.Remove(requestId);
                }
            };

            foreach (var f in this._config.TradingFrontends)
            {
                this._trade.RegisterFront(f);
            }
            this._trade.SubscribePrivateTopic(EnumTeResumeType.THOST_TERT_QUICK);
            this._trade.Init();

            this.OnLog("等待交易登录响应。。。");
            this._asyncWaiter.WaitOne();

            // ================================================================================
            // 行情初始化
            // ================================================================================
            this._market                       = new KSFTMDAdapter();
            this._market.OnRspError           += OnRspError;
            this._market.OnFrontConnected     += _market_OnFrontConnected;
            this._market.OnRspUserLogin       += _market_OnRspUserLogin;
            this._market.OnRtnDepthMarketData += _market_OnRtnDepthMarketData;
            foreach (var f in this._config.MarketFrontends)
            {
                this._market.RegisterFront(f);
            }
            this._market.Init();

            this.OnLog("等待行情登录响应。。。");
            this._asyncWaiter.WaitOne();

            this.OnLog("查询合约并订阅行情。。。");
            var ret = this._trade.ReqQryInstrument(new ThostFtdcQryInstrumentField {
                ExchangeID = "SSE"
            }, this._session.NextRequestID());

            this._asyncWaiter.WaitOne();
        }
Example #3
0
        int SESSION_ID; //会话编号

        #endregion Fields

        #region Methods

        public void Run()
        {
            api = new KSFTTraderAdapter();
            api.OnFrontConnected += new FrontConnected(OnFrontConnected);
            api.OnFrontDisconnected += new FrontDisconnected(OnFrontDisconnected);
            api.OnHeartBeatWarning += new HeartBeatWarning(OnHeartBeatWarning);
            api.OnRspError += new RspError(OnRspError);
            api.OnRspUserLogin += new RspUserLogin(OnRspUserLogin);
            api.OnRspUserLogout += new RspUserLogout(OnRspUserLogout);
            api.OnRspUserPasswordUpdate += new RspUserPasswordUpdate(OnRspUserPasswordUpdate);
            api.OnRspTradingAccountPasswordUpdate += new RspTradingAccountPasswordUpdate(OnRspTradingAccountPasswordUpdate);
            api.OnRspOrderAction += new RspOrderAction(OnRspOrderAction);
            api.OnRspOrderInsert += new RspOrderInsert(OnRspOrderInsert);
            api.OnRspQueryMaxOrderVolume += new RspQueryMaxOrderVolume(OnRspQueryMaxOrderVolume);
            api.OnRspQryOrder += new RspQryOrder(OnRspQryOrder);
            api.OnRspQryTrade += new RspQryTrade(OnRspQryTrade);
            api.OnRspQryInvestor += new RspQryInvestor(OnRspQryInvestor);
            api.OnRspQryTradingCode += new RspQryTradingCode(OnRspQryTradingCode);
            api.OnRspQryInstrumentMarginRate += new RspQryInstrumentMarginRate(OnRspQryInstrumentMarginRate);
            api.OnRspQryInstrumentCommissionRate += new RspQryInstrumentCommissionRate(OnRspQryInstrumentCommissionRate);
            api.OnRspQryExchange += new RspQryExchange(OnRspQryExchange);
            api.OnRspQryDepthMarketData += new RspQryDepthMarketData(OnRspQryDepthMarketData);
            api.OnRspQrySettlementInfo += new RspQrySettlementInfo(OnRspQrySettlementInfo);
            api.OnRspQryInvestorPositionDetail += new RspQryInvestorPositionDetail(OnRspQryInvestorPositionDetail);
            api.OnRspQryNotice += new RspQryNotice(OnRspQryNotice);
            api.OnRspQrySettlementInfoConfirm += new RspQrySettlementInfoConfirm(OnRspQrySettlementInfoConfirm);
            api.OnRspQryInvestorPositionCombineDetail += new RspQryInvestorPositionCombineDetail(OnRspQryInvestorPositionCombineDetail);
            api.OnRspQryEWarrantOffset += new RspQryEWarrantOffset(OnRspQryEWarrantOffset);
            api.OnRspQryTransferSerial += new RspQryTransferSerial(OnRspQryTransferSerial);
            api.OnRspQryAccountregister += new RspQryAccountregister(OnRspQryAccountregister);
            api.OnRspQryInstrument += new RspQryInstrument(OnRspQryInstrument);
            api.OnRspQryInvestorPosition += new RspQryInvestorPosition(OnRspQryInvestorPosition);
            api.OnRspQryTradingAccount += new RspQryTradingAccount(OnRspQryTradingAccount);
            api.OnRspSettlementInfoConfirm += new RspSettlementInfoConfirm(OnRspSettlementInfoConfirm);
            api.OnRtnOrder += new RtnOrder(OnRtnOrder);
            api.OnRtnTrade += new RtnTrade(OnRtnTrade);
            api.OnErrRtnOrderInsert += new ErrRtnOrderInsert(OnErrRtnOrderInsert);
            api.OnErrRtnOrderAction += new ErrRtnOrderAction(OnErrRtnOrderAction);
            api.OnRtnInstrumentStatus += new RtnInstrumentStatus(OnRtnInstrumentStatus);
            api.OnRtnTradingNotice += new RtnTradingNotice(OnRtnTradingNotice);
            api.OnRspQryContractBank += new RspQryContractBank(OnRspQryContractBank);
            api.OnRspQryTradingNotice += new RspQryTradingNotice(OnRspQryTradingNotice);
            api.OnRspQryBrokerTradingParams += new RspQryBrokerTradingParams(OnRspQryBrokerTradingParams);
            api.OnRspQryBrokerTradingAlgos += new RspQryBrokerTradingAlgos(OnRspQryBrokerTradingAlgos);
            api.OnRspFromBankToFutureByFuture += new RspFromBankToFutureByFuture(OnRspFromBankToFutureByFuture);
            api.OnRspFromFutureToBankByFuture += new RspFromFutureToBankByFuture(OnRspFromFutureToBankByFuture);
            api.OnRspQueryBankAccountMoneyByFuture += new RspQueryBankAccountMoneyByFuture(OnRspQueryBankAccountMoneyByFuture);
            api.OnRspQryInvestorOpenPosition += new RspQryInvestorOpenPosition(OnRspQryInvestorOpenPosition);
            api.OnRspQryInvestorOpenCombinePosition += new RspQryInvestorOpenCombinePosition(OnRspQryInvestorOpenCombinePosition);
            api.OnRspBulkCancelOrder += new RspBulkCancelOrder(OnRspBulkCancelOrder);

            api.SubscribePublicTopic(EnumTeResumeType.THOST_TERT_QUICK);					     // 注册公有流
            api.SubscribePrivateTopic(EnumTeResumeType.THOST_TERT_QUICK);					// 注册私有流

            try
            {
                api.RegisterFront(FRONT_ADDR);
                api.Init();
                api.Join(); // 阻塞直到关闭或者CTRL+C
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                api.Release();
            }
        }