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

文章介绍了通过开放API实现的全球股票和期货市场历史数据查询以及实时行情订阅平台,强调了数据的全面性和实时性,以及CTP_quote类的使用方法和数据处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过开放的方式提供全球股票(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 _listDele = new List();
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;
		}

完整下续
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值