量化交易平台之行情数据获取方式

孤翁 后端 2023-08-16

量化交易平台之行情数据获取方式

通过开放的方式提供全球股票(A股、港股、美股)、期货(国内期货、国际期货)等历史数据查询及实盘实时行情订阅

平台特色:

全球大多数行情一次购买即可享受全部数据行情订阅。

历史数据可以提供下载服务方便使用

云端自定义指数合成能力

自定义品种的支持(如不同品种的价差K线等)

实时行情部分时效性强

行情数据接口,分享代码如下:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

using static RndInvest.DataPlatform.Ctp.ctp_quote;

 

namespace RndInvest.DataPlatform.Ctp

{

public abstract class CTPQuote : Quote

{

ctp_quote _q = null;

private readonly List<Delegate> _listDele = new List<Delegate>();

        public Thread _doHeartBeatThread;

 

 

        /// <summary>

        ///

        /// </summary>

public CTPQuote()

{

_q = new ctp_quote();

SetCallBack();

            _doHeartBeatThread = new Thread(new ThreadStart(HeartBeat));

            _doHeartBeatThread.IsBackground = true;

            _doHeartBeatThread.Start();

        }

 

        /// <summary>

        /// 前置地址端口

        /// </summary>

        public override string FrontAddr { get; set; }

 

        /// <summary>

        /// 帐号 guweng22346

        /// </summary>

        public override string Investor { get; set; }

 

        /// <summary>

        /// 密码

        /// </summary>

        public override string Password { get; set; }

 

        /// <summary>

        /// 经纪商代码

        /// </summary>

        public override string Broker { get; set; }

 

        Delegate AddDele(Delegate d) { _listDele.Add(d); return d; }

 

void SetCallBack()

{

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

_q.SetOnRspUserLogin((DeleOnRspUserLogin)AddDele(new DeleOnRspUserLogin(CTPOnRspUserLogin)));

_q.SetOnFrontDisconnected((DeleOnFrontDisconnected)AddDele(new DeleOnFrontDisconnected(CTPOnFrontDisconnected)));

_q.SetOnRspSubMarketData((DeleOnRspSubMarketData)AddDele(new DeleOnRspSubMarketData(CTPOnRspSubMarketData)));

_q.SetOnRtnDepthMarketData((DeleOnRtnDepthMarketData)AddDele(new DeleOnRtnDepthMarketData(CTPOnRtnDepthMarketData)));

_q.SetOnRspError((DeleOnRspError)AddDele(new DeleOnRspError(CTPOnRspError)));

}

 

private void CTPOnRtnDepthMarketData(ref CThostFtdcDepthMarketDataField pDepthMarketData)

{

CThostFtdcDepthMarketDataField f = pDepthMarketData;

 

if (string.IsNullOrEmpty(f.InstrumentID) || string.IsNullOrEmpty(f.UpdateTime) || double.IsInfinity(f.UpperLimitPrice))//过滤无穷大/

{

return;

}

//修正last=double.max

if (Math.Abs(f.LastPrice - double.MaxValue) < double.Epsilon)

{

if (Math.Abs(f.AskPrice1 - double.MaxValue) > double.Epsilon)

{

f.LastPrice = f.AskPrice1;

}

else if (Math.Abs(f.BidPrice1 - double.MaxValue) > double.Epsilon)

{

f.LastPrice = f.BidPrice1;

}

else

return;

}

 

//去掉tradingday字段

//if (string.IsNullOrEmpty(f.TradingDay))

//{

// f.TradingDay = this.TradingDay; //日期:实盘中某些交易所,此字段为空

//}

//if (string.IsNullOrEmpty(f.ActionDay)) //此字段可能为空

//{

// f.ActionDay = this.TradingDay;

//}

//f.ExchangeID = instrument.ExchangeID;

//处理,单边有挂边的情况

if (f.AskPrice1 > f.UpperLimitPrice) //未赋值的数据

{

f.AskPrice1 = f.LastPrice;

}

if (f.BidPrice1 > f.UpperLimitPrice)

{

f.BidPrice1 = f.LastPrice;

}

//修最高/最低

if (Math.Abs(f.HighestPrice - double.MaxValue) < double.Epsilon)

{

f.HighestPrice = f.AskPrice1;

}

if (Math.Abs(f.LowestPrice - double.MaxValue) < double.Epsilon)

{

f.LowestPrice = f.BidPrice1;

}

 

MarketData tick = DicTick.GetOrAdd(f.InstrumentID, new MarketData

{

InstrumentID = f.InstrumentID,

});

 

 

if (f.UpdateMillisec == 0 && f.UpdateTime == tick.UpdateTime && tick.UpdateMillisec < 990)  //某些交易所(如郑商所)相同秒数的ms均为0

{

f.UpdateMillisec = tick.UpdateMillisec + 10;

}

完整下续

Apipost 私有化火热进行中

评论