2015-01-07 01:26:14 +08:00
|
|
|
|
/*
|
|
|
|
|
* StockManager.h
|
|
|
|
|
*
|
|
|
|
|
* Created on: 2011-11-9
|
|
|
|
|
* Author: fasiondog
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef STOCKMANAGER_H_
|
|
|
|
|
#define STOCKMANAGER_H_
|
|
|
|
|
|
2021-01-25 00:31:47 +08:00
|
|
|
|
#include <mutex>
|
2021-03-25 00:55:39 +08:00
|
|
|
|
#include <thread>
|
2017-10-09 02:25:02 +08:00
|
|
|
|
#include "utilities/Parameter.h"
|
2019-04-07 22:55:47 +08:00
|
|
|
|
#include "data_driver/DataDriverFactory.h"
|
2016-04-03 00:08:31 +08:00
|
|
|
|
#include "Block.h"
|
2015-01-07 01:26:14 +08:00
|
|
|
|
#include "MarketInfo.h"
|
|
|
|
|
#include "StockTypeInfo.h"
|
2021-02-11 18:06:18 +08:00
|
|
|
|
#include "StrategyContext.h"
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
namespace hku {
|
|
|
|
|
|
|
|
|
|
typedef vector<string> MarketList;
|
|
|
|
|
|
2017-10-13 01:50:11 +08:00
|
|
|
|
Parameter default_preload_param();
|
2017-10-14 03:08:49 +08:00
|
|
|
|
Parameter default_other_param();
|
2017-10-13 01:50:11 +08:00
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 证券信息统一管理类
|
|
|
|
|
* @ingroup StockManage
|
|
|
|
|
*/
|
|
|
|
|
class HKU_API StockManager {
|
|
|
|
|
public:
|
|
|
|
|
/** 获取StockManager单例实例 */
|
|
|
|
|
static StockManager& instance();
|
|
|
|
|
virtual ~StockManager();
|
|
|
|
|
|
2024-02-26 17:34:02 +08:00
|
|
|
|
StockManager(const StockManager&) = delete;
|
|
|
|
|
StockManager& operator=(const StockManager&) = delete;
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 初始化函数,必须在程序入口调用
|
2021-02-11 18:06:18 +08:00
|
|
|
|
* @param baseInfoParam 基础信息驱动参数
|
|
|
|
|
* @param blockParam 板块驱动参数
|
|
|
|
|
* @param kdataParam K线驱动参数
|
|
|
|
|
* @param preloadParam 预加载参数
|
|
|
|
|
* @param hikyuuParam 其他参数
|
|
|
|
|
* @param context 策略上下文
|
2015-01-07 01:26:14 +08:00
|
|
|
|
*/
|
2020-04-20 23:59:47 +08:00
|
|
|
|
void init(const Parameter& baseInfoParam, const Parameter& blockParam,
|
|
|
|
|
const Parameter& kdataParam, const Parameter& preloadParam = default_preload_param(),
|
2021-02-11 18:06:18 +08:00
|
|
|
|
const Parameter& hikyuuParam = default_other_param(),
|
2021-02-17 19:43:59 +08:00
|
|
|
|
const StrategyContext& context = StrategyContext({"all"}));
|
2017-10-13 01:50:11 +08:00
|
|
|
|
|
2021-01-29 00:18:01 +08:00
|
|
|
|
/** 重新加载 */
|
|
|
|
|
void reload();
|
|
|
|
|
|
2020-03-01 01:24:55 +08:00
|
|
|
|
/** 主动退出并释放资源 */
|
|
|
|
|
static void quit();
|
|
|
|
|
|
2020-10-29 00:03:25 +08:00
|
|
|
|
/** 获取基础信息驱动参数 */
|
2020-12-19 23:57:24 +08:00
|
|
|
|
const Parameter& getBaseInfoDriverParameter() const;
|
2020-10-29 00:03:25 +08:00
|
|
|
|
|
|
|
|
|
/** 获取板块驱动参数 */
|
2020-12-19 23:57:24 +08:00
|
|
|
|
const Parameter& getBlockDriverParameter() const;
|
2020-10-29 00:03:25 +08:00
|
|
|
|
|
|
|
|
|
/** 获取 K 线数据驱动参数 */
|
2020-12-19 23:57:24 +08:00
|
|
|
|
const Parameter& getKDataDriverParameter() const;
|
2020-10-29 00:03:25 +08:00
|
|
|
|
|
|
|
|
|
/** 获取预加载参数 */
|
2020-12-19 23:57:24 +08:00
|
|
|
|
const Parameter& getPreloadParameter() const;
|
2020-10-29 00:03:25 +08:00
|
|
|
|
|
|
|
|
|
/** 获取其他参数 */
|
2020-12-19 23:57:24 +08:00
|
|
|
|
const Parameter& getHikyuuParameter() const;
|
2017-10-26 02:02:32 +08:00
|
|
|
|
|
2021-02-11 18:06:18 +08:00
|
|
|
|
/** 获取策略上下文 */
|
|
|
|
|
const StrategyContext& getStrategyContext() const;
|
|
|
|
|
|
2020-10-29 00:03:25 +08:00
|
|
|
|
/** 获取基础信息驱动 */
|
2019-04-07 22:55:47 +08:00
|
|
|
|
BaseInfoDriverPtr getBaseInfoDriver() const;
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取用于保存零时变量等的临时目录,如为配置则为当前目录
|
|
|
|
|
* 由m_config中的“tmpdir”指定
|
|
|
|
|
*/
|
|
|
|
|
string tmpdir() const;
|
|
|
|
|
|
2019-04-03 22:31:55 +08:00
|
|
|
|
/** 获取数据目录 */
|
|
|
|
|
string datadir() const;
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/** 获取证券数量 */
|
|
|
|
|
size_t size() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据"市场简称证券代码"获取对应的证券实例
|
|
|
|
|
* @param querystr 格式:“市场简称证券代码”,如"sh000001"
|
|
|
|
|
* @return 对应的证券实例,如果实例不存在,则Null<Stock>(),不抛出异常
|
|
|
|
|
*/
|
2019-08-03 15:14:39 +08:00
|
|
|
|
Stock getStock(const string& querystr) const;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/** 同 getStock @see getStock */
|
|
|
|
|
Stock operator[](const string&) const;
|
|
|
|
|
|
2024-03-29 17:57:06 +08:00
|
|
|
|
StockList getStockList(
|
|
|
|
|
std::function<bool(const Stock&)>&& filter = std::function<bool(const Stock&)>()) const;
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取相应的市场信息
|
|
|
|
|
* @param market 指定的市场标识
|
|
|
|
|
* @return 相应的市场信息,如果相应的市场信息不存在,则返回Null<MarketInfo>()
|
|
|
|
|
*/
|
|
|
|
|
MarketInfo getMarketInfo(const string& market) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取相应的证券类型详细信息
|
|
|
|
|
* @param type 证券类型
|
|
|
|
|
* @return 对应的证券类型信息,如果不存在,则返回Null<StockTypeInf>()
|
|
|
|
|
*/
|
2020-10-01 22:52:50 +08:00
|
|
|
|
StockTypeInfo getStockTypeInfo(uint32_t type) const;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
/** 获取市场简称列表 */
|
|
|
|
|
MarketList getAllMarket() const;
|
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取预定义的板块
|
|
|
|
|
* @param category 板块分类
|
|
|
|
|
* @param name 板块名称
|
|
|
|
|
* @return 板块,如找不到返回空
|
|
|
|
|
*/
|
|
|
|
|
Block getBlock(const string& category, const string& name);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定分类的板块列表
|
|
|
|
|
* @param category 板块分类
|
|
|
|
|
* @return 板块列表
|
|
|
|
|
*/
|
2019-08-03 15:14:39 +08:00
|
|
|
|
BlockList getBlockList(const string& category);
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有板块
|
|
|
|
|
* @return 板块列表
|
|
|
|
|
*/
|
|
|
|
|
BlockList getBlockList();
|
|
|
|
|
|
2024-01-04 15:41:19 +08:00
|
|
|
|
// 目前支持"SH"
|
2024-03-01 17:41:51 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取交易日历,目前支持"SH"
|
|
|
|
|
* @param query
|
|
|
|
|
* @param market
|
|
|
|
|
* @return DatetimeList
|
|
|
|
|
*/
|
2019-11-10 19:45:57 +08:00
|
|
|
|
DatetimeList getTradingCalendar(const KQuery& query, const string& market = "SH");
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2024-03-01 17:41:51 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取10年期中国国债收益率
|
|
|
|
|
*/
|
2024-03-02 03:42:14 +08:00
|
|
|
|
const ZhBond10List& getZhBond10() const;
|
2024-03-01 17:41:51 +08:00
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
/**
|
2021-02-07 00:56:04 +08:00
|
|
|
|
* 判断指定日期是否为节假日
|
|
|
|
|
* @param d 指定日期
|
2015-01-07 01:26:14 +08:00
|
|
|
|
*/
|
2021-02-07 00:56:04 +08:00
|
|
|
|
bool isHoliday(const Datetime& d) const;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2024-04-13 17:16:18 +08:00
|
|
|
|
const string& getHistoryFinanceFieldName(size_t ix) const;
|
|
|
|
|
size_t getHistoryFinanceFieldIndex(const string& name) const;
|
2024-04-14 03:39:45 +08:00
|
|
|
|
vector<std::pair<size_t, string>> getHistoryFinanceAllFields() const;
|
2024-04-13 17:16:18 +08:00
|
|
|
|
|
2024-04-13 18:05:25 +08:00
|
|
|
|
vector<HistoryFinanceInfo> getHistoryFinance(const Stock& stk, Datetime start, Datetime end);
|
2024-04-13 17:16:18 +08:00
|
|
|
|
|
2017-10-09 02:25:02 +08:00
|
|
|
|
/**
|
2021-02-07 00:56:04 +08:00
|
|
|
|
* 添加Stock,仅供临时增加的特殊Stock使用
|
|
|
|
|
* @param stock
|
|
|
|
|
* @return true 成功 | false 失败
|
2017-10-09 02:25:02 +08:00
|
|
|
|
*/
|
2021-02-07 00:56:04 +08:00
|
|
|
|
bool addStock(const Stock& stock);
|
2017-10-09 02:25:02 +08:00
|
|
|
|
|
2017-09-20 02:29:53 +08:00
|
|
|
|
/**
|
|
|
|
|
* 从CSV文件(K线数据)增加临时的Stock,可用于只有CSV格式的K线数据时,进行临时测试
|
|
|
|
|
* @details 增加的临时Stock,其market为“TMP”
|
|
|
|
|
* @param code 自行编号的证券代码,不能和已有的Stock相同,否则将返回Null<Stock>
|
|
|
|
|
* @param day_filename 日线CSV文件名
|
|
|
|
|
* @param min_filename 分钟线CSV文件名
|
|
|
|
|
* @param tick 最小跳动量,默认0.01
|
|
|
|
|
* @param tickValue 最小跳动量价值,默认0.01
|
|
|
|
|
* @param precision 价格精度,默认2
|
|
|
|
|
* @param minTradeNumber 单笔最小交易量,默认1
|
|
|
|
|
* @param maxTradeNumber 单笔最大交易量,默认1000000
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2020-04-20 23:59:47 +08:00
|
|
|
|
Stock addTempCsvStock(const string& code, const string& day_filename,
|
|
|
|
|
const string& min_filename, price_t tick = 0.01, price_t tickValue = 0.01,
|
|
|
|
|
int precision = 2, size_t minTradeNumber = 1,
|
2019-11-10 19:45:57 +08:00
|
|
|
|
size_t maxTradeNumber = 1000000);
|
2017-07-31 02:26:10 +08:00
|
|
|
|
|
2017-09-20 02:29:53 +08:00
|
|
|
|
/**
|
|
|
|
|
* 移除增加的临时Stock
|
|
|
|
|
* @param code
|
|
|
|
|
*/
|
2017-07-31 02:26:10 +08:00
|
|
|
|
void removeTempCsvStock(const string& code);
|
|
|
|
|
|
2021-03-25 00:55:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取当前执行线程id,主要用于判断 Strategy 是以独立进程还是线程方式运行
|
|
|
|
|
*/
|
|
|
|
|
std::thread::id thread_id() const {
|
|
|
|
|
return m_thread_id;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
public:
|
2016-04-03 00:08:31 +08:00
|
|
|
|
typedef StockMapIterator const_iterator;
|
2019-11-10 19:45:57 +08:00
|
|
|
|
const_iterator begin() const {
|
|
|
|
|
return m_stockDict.begin();
|
|
|
|
|
}
|
|
|
|
|
const_iterator end() const {
|
|
|
|
|
return m_stockDict.end();
|
|
|
|
|
}
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2020-10-29 00:03:25 +08:00
|
|
|
|
private:
|
|
|
|
|
/* 设置K线驱动 */
|
2021-01-24 20:47:38 +08:00
|
|
|
|
void setKDataDriver(const KDataDriverConnectPoolPtr&);
|
2020-10-29 00:03:25 +08:00
|
|
|
|
|
2024-01-04 15:41:19 +08:00
|
|
|
|
/* 加载 K线数据至缓存 */
|
|
|
|
|
void loadAllKData();
|
|
|
|
|
|
2021-02-07 00:56:04 +08:00
|
|
|
|
/* 加载节假日信息 */
|
|
|
|
|
void loadAllHolidays();
|
|
|
|
|
|
|
|
|
|
/* 初始化时,添加市场信息 */
|
|
|
|
|
void loadAllMarketInfos();
|
|
|
|
|
|
|
|
|
|
/* 初始化时,添加证券类型信息 */
|
|
|
|
|
void loadAllStockTypeInfo();
|
|
|
|
|
|
|
|
|
|
/* 加载所有证券 */
|
|
|
|
|
void loadAllStocks();
|
|
|
|
|
|
|
|
|
|
/* 加载所有权息数据 */
|
|
|
|
|
void loadAllStockWeights();
|
|
|
|
|
|
2024-03-01 17:41:51 +08:00
|
|
|
|
/** 加载10年期中国国债收益率数据 */
|
|
|
|
|
void loadAllZhBond10();
|
|
|
|
|
|
2024-04-13 17:16:18 +08:00
|
|
|
|
/** 加载历史财经字段索引 */
|
|
|
|
|
void loadHistoryFinanceField();
|
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
private:
|
2020-04-20 23:59:47 +08:00
|
|
|
|
StockManager();
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2020-04-03 02:05:27 +08:00
|
|
|
|
static StockManager* m_sm;
|
2024-03-11 19:40:40 +08:00
|
|
|
|
std::atomic_bool m_initializing;
|
2021-03-25 00:55:39 +08:00
|
|
|
|
std::thread::id m_thread_id; // 记录线程id,用于判断Stratege是以独立进程方式还是线程方式运行
|
2017-10-13 01:50:11 +08:00
|
|
|
|
string m_tmpdir;
|
2019-04-03 22:31:55 +08:00
|
|
|
|
string m_datadir;
|
2020-10-29 00:03:25 +08:00
|
|
|
|
BaseInfoDriverPtr m_baseInfoDriver;
|
2017-10-14 03:08:49 +08:00
|
|
|
|
BlockInfoDriverPtr m_blockDriver;
|
2017-10-09 02:25:02 +08:00
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
|
StockMapIterator::stock_map_t m_stockDict; // SH000001 -> stock
|
2021-01-26 22:58:19 +08:00
|
|
|
|
std::mutex* m_stockDict_mutex;
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
|
|
typedef unordered_map<string, MarketInfo> MarketInfoMap;
|
2021-01-25 00:31:47 +08:00
|
|
|
|
mutable MarketInfoMap m_marketInfoDict;
|
|
|
|
|
std::mutex* m_marketInfoDict_mutex;
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
2020-10-01 22:52:50 +08:00
|
|
|
|
typedef unordered_map<uint32_t, StockTypeInfo> StockTypeInfoMap;
|
2021-01-25 00:31:47 +08:00
|
|
|
|
mutable StockTypeInfoMap m_stockTypeInfo;
|
|
|
|
|
std::mutex* m_stockTypeInfo_mutex;
|
2017-10-26 02:02:32 +08:00
|
|
|
|
|
2021-02-07 00:56:04 +08:00
|
|
|
|
std::unordered_set<Datetime> m_holidays; // 节假日
|
|
|
|
|
std::mutex* m_holidays_mutex;
|
|
|
|
|
|
2024-03-01 17:41:51 +08:00
|
|
|
|
ZhBond10List m_zh_bond10; // 10年期中国国债收益率数据
|
|
|
|
|
|
2024-04-13 17:16:18 +08:00
|
|
|
|
unordered_map<string, size_t> m_field_name_to_ix; // 财经字段名称到字段索引映射
|
|
|
|
|
unordered_map<size_t, string> m_field_ix_to_name; // 财经字段索引到字段名称映射
|
|
|
|
|
|
2017-10-26 02:02:32 +08:00
|
|
|
|
Parameter m_baseInfoDriverParam;
|
|
|
|
|
Parameter m_blockDriverParam;
|
|
|
|
|
Parameter m_kdataDriverParam;
|
|
|
|
|
Parameter m_preloadParam;
|
|
|
|
|
Parameter m_hikyuuParam;
|
2021-02-11 18:06:18 +08:00
|
|
|
|
StrategyContext m_context;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline size_t StockManager::size() const {
|
|
|
|
|
return m_stockDict.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Stock StockManager::operator[](const string& query) const {
|
|
|
|
|
return getStock(query);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 00:56:04 +08:00
|
|
|
|
inline bool StockManager::isHoliday(const Datetime& d) const {
|
|
|
|
|
return m_holidays.count(d);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 23:57:24 +08:00
|
|
|
|
inline const Parameter& StockManager::getBaseInfoDriverParameter() const {
|
2017-10-26 02:02:32 +08:00
|
|
|
|
return m_baseInfoDriverParam;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 23:57:24 +08:00
|
|
|
|
inline const Parameter& StockManager::getBlockDriverParameter() const {
|
2017-10-26 02:02:32 +08:00
|
|
|
|
return m_blockDriverParam;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 23:57:24 +08:00
|
|
|
|
inline const Parameter& StockManager::getKDataDriverParameter() const {
|
2017-10-26 02:02:32 +08:00
|
|
|
|
return m_kdataDriverParam;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 23:57:24 +08:00
|
|
|
|
inline const Parameter& StockManager::getPreloadParameter() const {
|
2017-10-26 02:02:32 +08:00
|
|
|
|
return m_preloadParam;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-19 23:57:24 +08:00
|
|
|
|
inline const Parameter& StockManager::getHikyuuParameter() const {
|
2017-10-26 02:02:32 +08:00
|
|
|
|
return m_hikyuuParam;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 18:06:18 +08:00
|
|
|
|
inline const StrategyContext& StockManager::getStrategyContext() const {
|
|
|
|
|
return m_context;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 22:55:47 +08:00
|
|
|
|
inline BaseInfoDriverPtr StockManager::getBaseInfoDriver() const {
|
2020-10-29 00:03:25 +08:00
|
|
|
|
return m_baseInfoDriver;
|
2019-04-07 22:55:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 17:16:18 +08:00
|
|
|
|
inline const string& StockManager::getHistoryFinanceFieldName(size_t ix) const {
|
|
|
|
|
return m_field_ix_to_name.at(ix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline size_t StockManager::getHistoryFinanceFieldIndex(const string& name) const {
|
|
|
|
|
return m_field_name_to_ix.at(name);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 18:05:25 +08:00
|
|
|
|
inline vector<HistoryFinanceInfo> StockManager::getHistoryFinance(const Stock& stk, Datetime start,
|
|
|
|
|
Datetime end) {
|
2024-04-13 17:16:18 +08:00
|
|
|
|
return m_baseInfoDriver->getHistoryFinance(stk.market(), stk.code(), start, end);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 19:45:57 +08:00
|
|
|
|
} // namespace hku
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
#endif /* STOCKMANAGER_H_ */
|