如何利用量化交易平台获取实时行情数据进行分析之代码分享

量化交易平台之行情数据获取方式续
通过开放的方式提供全球股票(A股、港股、美股)、期货(国内期货、国际期货)等历史数据查询及实盘实时行情订阅
平台特色:
全球大多数行情一次购买即可享受全部数据行情订阅。
历史数据可以提供下载服务方便使用
云端自定义指数合成能力
自定义品种的支持(如不同品种的价差K线等)
实时行情部分时效性强
行情数据接口,分享代码如下:

        tick.AskPrice = f.AskPrice1;
        tick.AskVolume = f.AskVolume1;
        tick.AveragePrice = f.AveragePrice;
        tick.BidPrice = f.BidPrice1;
        tick.BidVolume = f.BidVolume1;
        tick.LastPrice = f.LastPrice;
        tick.OpenInterest = f.OpenInterest;
        tick.UpdateMillisec = f.UpdateMillisec;
        tick.UpdateTime = f.UpdateTime;
        tick.Volume = f.Volume;
        tick.UpperLimitPrice = f.UpperLimitPrice;
        tick.LowerLimitPrice = f.LowerLimitPrice;
        tick.Turnover = f.Turnover;
        tick.HighestPrice = f.HighestPrice;
        tick.LowestPrice = f.LowestPrice;
        tick.SettlementPrice = f.SettlementPrice;
        tick.OpenPrice = f.OpenPrice;
        tick.ClosePrice = f.ClosePrice;
        tick.PreClosePrice = f.PreClosePrice;

        this.DicTick[tick.InstrumentID] = tick;

        if (_OnRtnTick == null) return;
        _OnRtnTick(this, new TickEventArgs
        {
            Tick = tick
        });
    }

    private void CTPOnRspSubMarketData(ref CThostFtdcSpecificInstrumentField pSpecificInstrument, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
    {
    }

    private void CTPOnFrontDisconnected(int nReason)
    {
        this.IsLogin = false;
        _OnRspUserLogout?.Invoke(this, new IntEventArgs { Value = nReason });
        //SetCallBack();
    }
    private void HeartBeat()
    {
        int threeMinutes = 1000 * 60 * 3;
        while (_doHeartBeatThread.IsBackground)
        {
            if (!this.IsLogin)
            {
                //SetCallBack();
            }
            Thread.Sleep(threeMinutes);
        }
    }

    private void CTPOnRspError(ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
    {
        _OnRtnError?.Invoke(this, new ErrorEventArgs { ErrorID = pRspInfo.ErrorID, ErrorMsg = pRspInfo.ErrorMsg });
    }

    private void CTPOnRspUserLogin(ref CThostFtdcRspUserLoginField pRspUserLogin, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
    {
        //避免登录错误后不断重连
        //if (pRspInfo.ErrorID != 0)
        //    _q.SetOnFrontConnected(null);
        //else //正常登录时注册连接事件(后续自动重连时可自行登录)
        //{
        //    this.IsLogin = true;

// _q.SetOnFrontConnected((DeleOnFrontConnected)AddDele(new DeleOnFrontConnected(CTPOnFrontConnected)));
// }

        this.IsLogin = true;

        _OnRspUserLogin?.Invoke(this, new IntEventArgs { Value = pRspInfo.ErrorID });
    }

    private void CTPOnFrontConnected()
    {
        _OnFrontConnected?.Invoke(this, new EventArgs());
    }

    public override bool IsLogin { get; protected set; }

    public override int ReqConnect()
    {
        _q.RegisterFront(this.FrontAddr);
        return (int)_q.Init();
        //_q.Init();            
        //return (int)_q.Join(); //会造成阻塞
    }

    public override int ReqSubscribeMarketData(params string[] pInstrument)
    {
        int size = Marshal.SizeOf(typeof(IntPtr));
        IntPtr insts = Marshal.AllocHGlobal(size * pInstrument.Length);
        var tmp = insts;
        for (int i = 0; i < pInstrument.Length; i++, tmp += size)
        {
            Marshal.StructureToPtr(Marshal.StringToHGlobalAnsi(pInstrument[i]), tmp, false);
        }
        return (int)_q.SubscribeMarketData(insts, pInstrument.Length);
    }

    public override int ReqUnSubscribeMarketData(params string[] pInstrument)
    {
        int size = Marshal.SizeOf(typeof(IntPtr));
        IntPtr insts = Marshal.AllocHGlobal(size * pInstrument.Length);
        var tmp = insts;
        for (int i = 0; i < pInstrument.Length; i++, tmp += size)
        {
            Marshal.StructureToPtr(Marshal.StringToHGlobalAnsi(pInstrument[i]), tmp, false);
        }
        return (int)_q.UnSubscribeMarketData(insts, pInstrument.Length);
    }

    public override int ReqUserLogin()
    {
        return (int)_q.ReqUserLogin(BrokerID: this.Broker, UserID: this.Investor, Password: this.Password);
    }

    public override void ReqUserLogout()
    {
        this.IsLogin = false;
        //上面的disconnect注销掉,需要主动调用此回调函数
        _OnRspUserLogout?.Invoke(this, new IntEventArgs { Value = 0 });
        //取消连接响应,避免重连后的再登录.(release中已处理)
        //_q.SetOnFrontDisconnected(null);
        //_q.SetOnFrontConnected(null);
        _q.Release();
    }
}

}

你可能感兴趣的:(如何利用量化交易平台获取实时行情数据进行分析之代码分享)