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

45次阅读

共计 3311 个字符,预计需要花费 9 分钟才能阅读完成。

量化交易平台之行情数据获取形式续
通过凋谢的形式提供寰球股票(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();}
}

}

正文完
 0