示例#1
0
        bool ORDER_ACTION_SENT = false; //是否发送了报单

        public void Run()
        {
            api = new CTPTraderAdapter();
            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.OnRspOrderAction           += new RspOrderAction(OnRspOrderAction);
            api.OnRspOrderInsert           += new RspOrderInsert(OnRspOrderInsert);
            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.SubscribePublicTopic(EnumTeResumeType.THOST_TERT_RESTART);                                      // 注册公有流
            api.SubscribePrivateTopic(EnumTeResumeType.THOST_TERT_RESTART);                                     // 注册私有流

            try
            {
                api.RegisterFront(FRONT_ADDR);
                api.Init();
                api.Join(); // 阻塞直到关闭或者CTRL+C
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                api.Release();
            }
        }
示例#2
0
 public void Release()
 {
     if (trader != null)
     {
         trader.Release();
         trader = null;
     }
 }
示例#3
0
        public void Init()
        {
            if (FrontAddrs == null || FrontAddrs.Length == 0)
            {
                HandleErrorInternal("CTP交易输入前置机地址不能为空!");
                return;
            }

            if (trader == null)
            {
                trader = new CTPTraderAdapter(".\\trade");
            }                                               // 创建trade目录存放流文件,避免与行情流文件冲突

            // 回调函数
            trader.OnFrontConnected           += new FrontConnected(OnFrontConnected);
            trader.OnFrontDisconnected        += new FrontDisconnected(OnFrontDisconnected);
            trader.OnHeartBeatWarning         += new HeartBeatWarning(OnHeartBeatWarning);
            trader.OnRspError                 += new RspError(OnRspError);
            trader.OnRspUserLogin             += new RspUserLogin(OnRspUserLogin);
            trader.OnRspOrderAction           += new RspOrderAction(OnRspOrderAction);
            trader.OnRspOrderInsert           += new RspOrderInsert(OnRspOrderInsert);
            trader.OnErrRtnOrderInsert        += new ErrRtnOrderInsert(OnErrRtnOrderInsert);
            trader.OnErrRtnOrderAction        += new ErrRtnOrderAction(OnErrRtnOrderAction);
            trader.OnRspQryInstrument         += new RspQryInstrument(OnRspQryInstrument);
            trader.OnRspQryInvestorPosition   += new RspQryInvestorPosition(OnRspQryInvestorPosition);
            trader.OnRspQryTradingAccount     += new RspQryTradingAccount(OnRspQryTradingAccount);
            trader.OnRspSettlementInfoConfirm += new RspSettlementInfoConfirm(OnRspSettlementInfoConfirm);
            trader.OnRtnOrder                 += new RtnOrder(OnRtnOrder);
            trader.OnRtnTrade                 += new RtnTrade(OnRtnTrade);


            // 订阅私有流
            trader.SubscribePrivateTopic(EnumTeResumeType.THOST_TERT_RESUME);
            // 订阅公共流
            trader.SubscribePublicTopic(EnumTeResumeType.THOST_TERT_RESUME);

            try
            {
                foreach (var frontAddr in FrontAddrs)
                {
                    trader.RegisterFront(frontAddr);
                }

                trader.Init();
            }
            catch (Exception ex)
            {
                HandleErrorInternal("CTP连接前置机发生错误:" + ex.Message);
            }
        }
示例#4
0
文件: Program.cs 项目: aoxiang310/CPS
        bool ORDER_ACTION_SENT = false; //是否发送了报单

        public void Run()
        {
            api = new CTPTraderAdapter();
            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.OnRspOrderAction           += new RspOrderAction(OnRspOrderAction);
            api.OnRspOrderInsert           += new RspOrderInsert(OnRspOrderInsert);
            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.SubscribePublicTopic(EnumTeResumeType.THOST_TERT_QUICK);                                        // 注册公有流
            api.SubscribePrivateTopic(EnumTeResumeType.THOST_TERT_QUICK);                                       // 注册私有流

            try
            {
                api.RegisterFront(FRONT_ADDR);
                //ThostFtdcUserPasswordUpdateField req = new ThostFtdcUserPasswordUpdateField();
                //req.BrokerID = BROKER_ID;
                //req.UserID = INVESTOR_ID;
                //req.OldPassword = PASSWORD;
                //req.NewPassword = "******";
                //int iResult = api.ReqUserPasswordUpdate(req, iRequestID);
                //Console.WriteLine("--->>> 修改密码: " + ((iResult == 0) ? "修改成功" : "修改失败"));

                api.Init();
                api.Join(); // 阻塞直到关闭或者CTRL+C

                //修改密码
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                api.Release();
            }
        }
示例#5
0
        public void Initialize()
        {
            var configPath = Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "CTP.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 CTPTraderAdapter(Path.GetDirectoryName(this.GetType().Assembly.Location) + '\\');
            this._trade.OnFrontConnected       += _trade_OnFrontConnected;
            this._trade.OnRspUserLogin         += _trade_OnRspUserLogin;
            this._trade.OnRspAuthenticate      += _trade_OnRspAuthenticate;
            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 != null && rspField.ErrorID != 0)
                    {
                        throw new Exception($"查询持仓失败,代码:{rspField.ErrorID},描述:{rspField.ErrorMsg}");
                    }

                    this.Account.Positions = this._positions[requestId].Select(_ => _.ToOptionPosition()).Where(_ => _.Qty > 0).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 CTPMDAdapter();
            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("查询合约并订阅行情。。。");
            this._ctpReqstr.Schedule(() => this._trade.ReqQryInstrument(new ThostFtdcQryInstrumentField {
                ExchangeID = "SSE"
            }, this._session.NextRequestID()));
            this._asyncWaiter.WaitOne();
        }
示例#6
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="trader"></param>
 public TradeManager(CTPTraderAdapter trader)
 {
     this.trader = trader;
     AddEvent();
 }
示例#7
0
 /// <summary>
 /// 释放资源
 /// </summary>
 public void Release()
 {
     if (trader != null)
     {
         trader.Release();
         trader = null;
     }
 }
示例#8
0
 protected override void OnLoad(EventArgs e)
 {
     base.OnLoad(e);
     this.MDFrontAddr = MDFrontAddress.Text;
     this.TDFrontAddr = TDFrontAddress.Text;
     this.BrokerID = brokerID.Text;
     this.InvestorID = investorID.Text;
     this.Password = password.Text;
     this.marketer = new CTPMDAdapter();
     this.trader = new CTPTraderAdapter();
     this.instrumentsList = new List<ThostFtdcInstrumentField>();
     this.investorPositionList = new List<ThostFtdcInvestorPositionField>();
 }