コード例 #1
0
ファイル: MarketApiTest.cs プロジェクト: followbin/CTPSharp
        public void TestSubscribeMarketData()
        {
            string instrumentID = "IF1709";

            _api.OnRspSubMarketData += new MarketApi.RspSubMarketData((ref CThostFtdcSpecificInstrumentField pSpecificInstrument,
                                                                       ref CThostFtdcRspInfoField pRspInfo, int nRequestID, byte bIsLast) =>
            {
                Console.WriteLine("订阅{0}成功", instrumentID);
                Assert.AreEqual(instrumentID, pSpecificInstrument.InstrumentID);

                //退订行情
                _api.UnsubscribeMarketData(instrumentID);
                Thread.Sleep(50);
            });
            _api.OnRspUnSubMarketData += new MarketApi.RspUnSubMarketData((ref CThostFtdcSpecificInstrumentField pSpecificInstrument,
                                                                           ref CThostFtdcRspInfoField pRspInfo, int nRequestID, byte bIsLast) =>
            {
                Console.WriteLine("退订{0}成功", instrumentID);
                Assert.AreEqual(instrumentID, pSpecificInstrument.InstrumentID);
            });
            _api.OnRtnDepthMarketData += new MarketApi.RtnDepthMarketData((ref CThostFtdcDepthMarketDataField pDepthMarketData) =>
            {
                Console.WriteLine("昨收价:{0},现价:{1}", pDepthMarketData.PreClosePrice, pDepthMarketData.LastPrice);
                Assert.AreEqual(instrumentID, pDepthMarketData.InstrumentID);
            });

            //订阅行情
            _api.SubscribeMarketData(instrumentID);
            Thread.Sleep(50);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Tendx/Ctp.Net
 private static void OnRspUserLogin(RspUserLoginField RspUserLogin, RspInfoField RspInfo, int RequestID, bool IsLast)
 {
     _mdapi.SubscribeMarketData(new[] { "rb2109" }, 1);
 }
コード例 #3
0
        public void Initialize()
        {
            Console.WriteLine("CTP行情订阅初始化");
            _api             = new MarketApi(_brokerID, _frontAddr);
            _api.OnRspError += new MarketApi.RspError((ref CThostFtdcRspInfoField pRspInfo, int nRequestID, byte bIsLast) =>
            {
                Console.WriteLine("ErrorID: {0}, ErrorMsg: {1}", pRspInfo.ErrorID, pRspInfo.ErrorMsg);
            });

            _api.OnFrontConnected += new MarketApi.FrontConnected(() =>
            {
                Console.WriteLine("连接成功");
                _api.UserLogin(-3, _investor, _password);
            });

            _api.OnRspUserLogin += new MarketApi.RspUserLogin((ref CThostFtdcRspUserLoginField pRspUserLogin,
                                                               ref CThostFtdcRspInfoField pRspInfo, int nRequestID, byte bIsLast) =>
            {
                if (_isLogin == false)
                {
                    _isLogin = true;
                    _api.SubscribeMarketData(this._codeList);
                    Console.WriteLine("   ");
                    Console.WriteLine("***********************************************");
                    Console.WriteLine("*****  CTP行情接收中,请勿关闭窗口!!!  *****");
                    Console.WriteLine("*****  CTP行情接收中,请勿关闭窗口!!!  *****");
                    Console.WriteLine("*****  CTP行情接收中,请勿关闭窗口!!!  *****");
                    Console.WriteLine("***********************************************");
                    Console.WriteLine("   ");
                }
            });

            _api.OnRspUserLogout += new MarketApi.RspUserLogout((ref CThostFtdcUserLogoutField pRspUserLogout,
                                                                 ref CThostFtdcRspInfoField pRspInfo, int nRequestID, byte bIsLast) =>
            {
                _isLogin = false;
                _api.Disconnect();
            });

            _api.OnFrontDisconnected += new MarketApi.FrontDisconnected((int nReasion) =>
            {
                _isLogin = false;
            });

            _api.OnHeartBeatWarning += new MarketApi.HeartBeatWarning((int nTimeLapse) =>
            {
                Console.WriteLine(DateTime.Now.ToString());
                _api.Connect();
            });

            _api.OnRspSubMarketData += new MarketApi.RspSubMarketData((ref CThostFtdcSpecificInstrumentField pSpecificInstrument,
                                                                       ref CThostFtdcRspInfoField pRspInfo, int nRequestID, byte bIsLast) =>
            {
                Console.WriteLine("订阅{0}成功", pSpecificInstrument.InstrumentID);
                Thread.Sleep(10);
            });

            _api.OnRtnDepthMarketData += new MarketApi.RtnDepthMarketData((ref CThostFtdcDepthMarketDataField pDepthMarketData) =>
            {
                try
                {
                    OnReceiveHQ(pDepthMarketData);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            });

            _api.Connect();
            Console.WriteLine("初始化完成");
        }
コード例 #4
0
 //订阅行情
 public void SubscribeFutureMarketData(string[] CodeList)
 {
     _api.SubscribeMarketData(CodeList);
 }
コード例 #5
0
 /// <summary>
 /// 订阅行情
 /// </summary>
 /// <param name="instruments">合约列表,传空订阅所有</param>
 public void SubscribeMarket(params string[] instruments)
 {
     _api.SubscribeMarketData(instruments);
 }