量化交易之HFT篇 - 高频做市模型源码(.h文件)

本文档详细介绍了WtHftStraDemo类,一个用于高频交易的策略实现。该策略涉及到订单管理、市场数据处理、交易逻辑以及日志记录等功能。包括买入、卖出、平仓等操作的条件判断,以及价格获取、订单类型判断等关键函数。同时,文档还涵盖了会话状态管理、重新连接逻辑以及扫描间隔检查等核心部分。
摘要由CSDN通过智能技术生成
"""
事先声明, 模型源码仅作参考和交流使用, 不能直接用于实盘
"""
#pragma once
#include <unordered_set>
#include <memory>
#include <thread>
#include <mutex>
#include <iostream>

#include <time.h>
#include <string.h>
#include <vector>
#include <sstream>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>

#include "../Includes/HftStrategyDefs.h"
#include "../Includes/WTSTradeDef.hpp"

#include "../Share/JsonToVariant.hpp"
#include "../Share/BoostFile.hpp"


#include "../../include/rapidjson/document.h"
#include "../../include/rapidjson/writer.h"
#include "../../include/rapidjson/stringbuffer.h"
#include "../../include/rapidjson/filewritestream.h"
#include "../../include/rapidjson/prettywriter.h"
#include "../../include/rapidjson/filereadstream.h"

#include "TQZHftAlphas/TQZHftMa.h"
#include "TQZHftAlphas/TQZHftMacd.h"
#include "TQZHftAlphas/TQZHftDiff.h"
#include "TQZHftAlphas/TQZHftLastTradedQuantity.h"
#include "TQZHftAlphas/TQZHftBook.h"


using namespace boost::gregorian;
using namespace std;
using namespace hftalphas;


enum TQZOrderType {
	DEFAULT_ORDER_TYPE,
	NO_TYPE,

	BUY_TYPE,
	SELL_TYPE,
	SHORT_TYPE,
	COVER_TYPE
};

enum TQZSessionStatus {
	DEFAULT_STATUS,

	RE_CONNECT_STATUS,

	CLOSE_POSTIONS_STATUS,
	MARKET_MAKING_STATUS
};

enum TQZLogFileType {
	TRADE_CHANGE_TYPE,
	CANCEL_ORDER_COUNTS_TYPE
};

struct TQZLogMessage {

	TQZLogMessage() {};
	
	TQZLogMessage(std::string code, uint32_t orderid, double currentPrice, double orderPrice, std::string orderType, std::string currentMarketTime, std::string sendOrderTime, std::string orderComment)
		: code(code)
		, orderid(orderid)
		, currentPrice(currentPrice)
		, orderPrice(orderPrice)
		, orderType(orderType)
		, currentMarketTime(currentMarketTime)
		, sendOrderTime(sendOrderTime)
		, orderComment(orderComment)
	{
	}

	string code;

	uint32_t orderid;

	double currentPrice;
	double orderPrice;

	std::string orderType;
	std::string currentMarketTime;
	std::string sendOrderTime;

	std::string orderComment;
};


class WtHftStraDemo : public HftStrategy {
public:
	WtHftStraDemo(const char* id);
	~WtHftStraDemo();

public:
	virtual const char* getName() override;

	virtual const char* getFactName() override;

	virtual bool init(WTSVariant* cfg) override;

	virtual void on_init(HftStraBaseCtx* ctx) override;

	virtual void on_tick(HftStraBaseCtx* ctx, const char* code, WTSTickData* newTick) override;

	virtual void on_bar(HftStraBaseCtx* ctx, const char* code, const char* period, uint32_t times, WTSBarStruct* newBar) override;

	virtual void on_trade(HftStraBaseCtx* ctx, uint32_t localid, const char* stdCode, bool isBuy, double qty, double price, const char* userTag) override;

	virtual void on_order(HftStraBaseCtx* ctx, uint32_t localid, const char* stdCode, bool isBuy, double totalQty, double leftQty, double price, bool isCanceled, const char* userTag) override;

	virtual void on_position(HftStraBaseCtx* ctx, const char* stdCode, bool isLong, double prevol, double preavail, double newvol, double newavail) override;

	virtual void on_channel_ready(HftStraBaseCtx* ctx) override;

	virtual void on_channel_lost(HftStraBaseCtx* ctx) override;

	virtual void on_entrust(uint32_t localid, bool bSuccess, const char* message, const char* userTag) override;

private:
	typedef std::unordered_set<uint32_t> IDSet;
	typedef std::map<uint32_t, TQZLogMessage> MessageMap;
	typedef std::vector<WTSTickData> Ticks;
	typedef std::unique_ptr<Ticks> TicksUPtr;

	WTSTickData*	_last_tick;
	HftStraBaseCtx*	_ctx;

	std::string		_code;
	std::string		_tradeChange_log_filename;

	uint32_t		_marketMaking_scan_interval;

	uint32_t		_long_order_offset;
	uint32_t		_short_order_offset;

	uint32_t		_offset_open_minutes;
	uint32_t		_offset_close_minutes;

	IDSet			_code_buy_orders;
	IDSet			_code_short_orders;
	IDSet			_code_sell_orders;
	IDSet			_code_cover_orders;

	uint32_t		_code_buy_order;
	uint32_t		_code_short_order;
	uint32_t		_code_sell_order;
	uint32_t		_code_cover_order;

	uint64_t		_last_scan_time;
	double			_last_mid_price;

	bool			_channel_ready;
	bool			_re_connect_ready;

	bool			_code_buy_lock;
	bool			_code_sell_lock;
	bool			_code_short_lock;
	bool			_code_cover_lock;

	bool			_re_marketMaking;

	bool			_record_hft_log;

	uint32_t		_cancel_limit_counts;

	rapidjson::Document _sessionsDocument;
	rapidjson::Document _commoditiesDocument;

	TQZSessionStatus _current_session_status;

	MessageMap _log_message_map;

	TicksUPtr _ticks_uptr;


private:
	/// cancel order part.
	void __tqz_cancelOrder(const std::string code, const uint32_t orderId);

	void __tqz_cancelOrders(const std::string code, const IDSet ordersIds);
	
	void __tqz_cancelAllOrders();

	
	/// get askPrice | bidPrice | midPrice | upperlimit | lowerlimit | orderType.
	double __tqz_getAskPrice(const std::string code);
	
	double __tqz_getBidPrice(const std::string code);

	double __tqz_getCurrentMidPrice(const std::string code);

	double __tqz_getLongPrice(const std::string code, const int offsetTicks);

	double __tqz_getShortPrice(const std::string code, const int offsetTicks);

	double __tqz_getUpperlimitPrice(const std::string code);

	double __tqz_getLowerlimitPrice(const std::string code);

	double __tqz_getMarketMakingLongPrice(const std::string code, const double currentMidPrice, const int offsetTicks);

	double __tqz_getMarketMakingShortPrice(const std::string code, const double currentMidPrice, const int offsetTicks);
	
	bool __isBeyondUpperlimitOrLowerlimit(const string code, const int longOrderOffsetTicks, const int shortOrderOffsetTicks);
	
	TQZOrderType __tqz_getOrderType(const uint32_t orderId);

	bool __isBelongToHft(const uint32_t orderId);

	bool __midPriceIsChange(int offsetTicks);


	/// session part.
	void __initSessionStatus(TQZSessionStatus sessionStatus);

	int __tqz_getCurrentHourMinute();
	
	rapidjson::Document __tqz_loadJsonDocument(const std::string jsonPath);
	
	std::string __tqz_getSession(const std::string symbolCode);
	
	bool __isTradingTime(const std::string sessionString);

	bool __isEntrustable(std::string code);
	
	bool __isClosePositionsTime(const std::string sessionString);

	void __tqz_receiveCodeOnlyCloseCode(const uint32_t localid, const std::string code, const double lots);

	int __tqz_resetToHourMinute(int toHourMinute, const int offsetCloseMinutes);

	/// log part.
	std::string __tqz_getLogFileName(const TQZLogFileType logfileType);

	std::string __tqz_getCurrentTime();

	std::string __tqz_getCurrentMarketTime();

	std::string __tqz_getTimeString(const uint64_t hours, const uint64_t minutes, const uint64_t seconds, const uint64_t milliseconds);

	void __tqz_writeStrategyTradeLog(const uint32_t orderid, const double receiveTradePrice, const double lots);

	void __tqz_writeCancelOrderCountsLog(const std::string code);

	TQZLogMessage __tqz_getNewLogMessage(std::string code, uint32_t orderid, double currentPrice, double orderPrice, char* orderType, std::string currentMarketTime, std::string sendOrderTime, std::string orderComment);


	/// re connect part.
	void tqz_doReconnect();

	bool __tqz_queryReconnectResult();


	/// scan interval part.
	uint64_t __getCurrentTimestamp();

	bool __isNewScanInterval(const uint32_t scanInterval);


	/// tqz send order part. (can't use under market making condition)
	void tqz_buy(const std::string code, const double lots, const int offsetTicks, const std::string orderComment);
	void tqz_sell(const std::string code, const double lots, const int offsetTicks, const std::string orderComment);
	void tqz_short(const std::string code, const double lots, const int offsetTicks, const std::string orderComment);
	void tqz_cover(const std::string code, const double lots, const int offsetTicks, const std::string orderComment);

	void tqz_marketMaking(const int codeLongOffsetTicks, const int codeShortOffsetTicks);

	void tqz_closePositions(const std::string code, uint32_t closePositionsOffsetTicks);


	/// lock & unlock order part
	void __lockBuy();
	void __lockSell();
	void __lockShort();
	void __lockCover();

	void __unlockBuy();
	void __unlockSell();
	void __unlockShort();
	void __unlockCover();

	void __unlockAllOrders();

	bool __closeCodeIsLock(bool reSendLockOrder);

	bool __marketMakingIsLock(bool cancelLockOrder);

	bool __isMarketMakingAble();


	/// clear session cache.
	void __tqz_clearPreviousSessionCache();

	
	/// hft alpha part.
	int32_t totalAlphasValue(TicksUPtr& ticksUPtr);

	uint32_t getLongOffsetValue();
	uint32_t getShortOffsetValue();

	bool updateTicks(WTSTickData tickData);
};

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
高频量化交易系统(High-Frequency Trading System,简称HFT系统)是一种利用计算机算法和快速执行进行交易的技术。它通过快速获取市场数据、分析市场走势并进行交易,以在极短时间内获得利润。CPP是C++语言的缩写,是一种常用的编程语言。 高频交易系统CPP主要包括以下几个方面: 1. 数据获取和处理:高频交易系统需要快速获取市场数据,并进行实时的数据处理和分析。CPP语言提供了高效的数据处理能力,可以快速读取和处理海量的市场数据。 2. 算法开发:高频交易系统主要利用算法进行交易决策和执行。CPP语言支持复杂的算法设计和实现,具有高效的计算能力和低延迟的执行效果,能够满足高频交易系统对速度和性能的要求。 3. 执行交易和风险控制:高频交易系统需要实时执行交易指令,并进行风险控制,以避免不必要的损失。CPP语言可以实现快速的交易执行和灵活的风控策略,确保高频交易系统的安全和稳定运行。 4. 系统优化和监控:高频交易系统需要进行系统优化和监控,以提高交易执行效率和减少风险。CPP语言支持性能优化和系统监控的功能,可以对高频交易系统进行优化和监测,提升系统的稳定性和可靠性。 总之,高频量化交易系统CPP是一种利用C++语言开发的高效、快速和可靠的交易系统,能够满足高频交易对速度和性能的要求,提高交易决策的准确性和执行效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值