Skip to content

Commit f6c23a8

Browse files
committed
feat(context): 增加预加载数量参数
1 parent f9214b4 commit f6c23a8

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

hikyuu/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def load_hikyuu(**kwargs):
208208
options = {
209209
"stock_list": ["sh000001"],
210210
"ktype_list": ["day"],
211+
"preload_num: {"day_max": 100000}
211212
"load_history_finance": False,
212213
"load_weight": False,
213214
"start_spot": False,
@@ -223,6 +224,7 @@ def load_hikyuu(**kwargs):
223224
支持的K线类型有:
224225
'day', 'week', 'month', 'quarter', 'halfyear', 'year', 'min', 'min5',
225226
'min15', 'min30', 'min60', 'hour2'
227+
preload_num (dict): {'day_max': 100000, 'week_max': 100000, 'month_max': 100000, ...}
226228
load_history_finance (boolean): 预加载历史财务数至内存,默认为 True
227229
load_weight (boolean): 加载权息数据,默认为 True
228230
@@ -295,8 +297,10 @@ def load_hikyuu(**kwargs):
295297
# 优先使用传入参数作为加载上下文
296298
if 'stock_list' in kwargs:
297299
context.stock_list = kwargs['stock_list']
298-
if 'ktype_list' in os.environ:
300+
if 'ktype_list' in kwargs:
299301
context.ktype_list = kwargs['ktype_list']
302+
if 'preload_num' in kwargs:
303+
context.preload_num = kwargs['preload_num']
300304
if 'load_history_finance' in kwargs:
301305
hku_param.set("load_history_finance", kwargs['load_history_finance'])
302306
if 'load_weight' in kwargs:

hikyuu_cpp/hikyuu/StockManager.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,35 @@ void StockManager::loadData() {
127127

128128
void StockManager::loadAllKData() {
129129
// 按 K 线类型控制加载顺序
130-
vector<KQuery::KType> default_ktypes{
131-
KQuery::DAY, KQuery::MIN, KQuery::WEEK, KQuery::MONTH, KQuery::QUARTER, KQuery::HALFYEAR,
132-
KQuery::YEAR, KQuery::MIN5, KQuery::MIN15, KQuery::MIN30, KQuery::MIN60, KQuery::MIN3,
133-
KQuery::HOUR2, KQuery::HOUR4, KQuery::HOUR6, KQuery::HOUR12};
134-
135130
vector<KQuery::KType> ktypes;
136131
vector<string> low_ktypes;
137132

138133
// 如果上下文指定了 ktype list,则按上下文指定的 ktype 顺序加载,否则按默认顺序加载
139134
const auto& context_ktypes = m_context.getKTypeList();
140135
if (context_ktypes.empty()) {
141-
ktypes = std::move(default_ktypes);
142-
HKU_ASSERT(ktypes.size() == KQuery::getAllKType().size());
136+
ktypes = KQuery::getAllKType();
143137

144138
} else {
139+
// 使用上下文预加载参数覆盖全局预加载参数
145140
ktypes = context_ktypes;
141+
for (const auto& ktype : ktypes) {
142+
m_preloadParam.set<bool>(ktype, true);
143+
}
146144
}
147145

146+
const auto& context_preload_num = m_context.getPreloadNum();
148147
low_ktypes.reserve(ktypes.size());
149148
for (const auto& ktype : ktypes) {
150149
auto& back = low_ktypes.emplace_back(ktype);
151150
to_lower(back);
151+
152+
// 判断上下文是否指定了预加载数量,如果指定了,则覆盖默认值
153+
string preload_key = fmt::format("{}_max", back);
154+
auto context_iter = context_preload_num.find(preload_key);
155+
if (context_iter != context_preload_num.end()) {
156+
m_preloadParam.set<int>(preload_key, context_iter->second);
157+
}
158+
152159
HKU_INFO_IF(m_preloadParam.tryGet<bool>(back, false), "Preloading all {} kdata to buffer !",
153160
back);
154161
}

hikyuu_cpp/hikyuu/StrategyContext.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,25 @@ StrategyContext::StrategyContext(const vector<string>& stockCodeList) {
4949
}
5050

5151
StrategyContext::StrategyContext(const vector<string>& stockCodeList,
52-
const vector<KQuery::KType>& ktypeList) {
52+
const vector<KQuery::KType>& ktypeList,
53+
const unordered_map<string, int>& preloadNum) {
5354
_removeDuplicateCode(stockCodeList);
5455
_checkAndRemoveDuplicateKType(ktypeList);
56+
setPreloadNum(preloadNum);
57+
}
58+
59+
void StrategyContext::setPreloadNum(const unordered_map<string, int>& preloadNum) {
60+
m_preloadNum.clear();
61+
m_preloadNum.reserve(preloadNum.size());
62+
for (auto it = preloadNum.cbegin(); it != preloadNum.cend(); ++it) {
63+
string key = it->first;
64+
to_lower(key);
65+
if (it->second > 0) {
66+
m_preloadNum[key] = it->second;
67+
} else {
68+
HKU_WARN("Invalid preload number {}: {}", key, it->second);
69+
}
70+
}
5571
}
5672

5773
void StrategyContext::_removeDuplicateCode(const vector<string>& stockCodeList) {

hikyuu_cpp/hikyuu/StrategyContext.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ class HKU_API StrategyContext {
2424

2525
/**
2626
* 构造函数
27-
* @note 未指定 ktypelist 时,默认按预加载参数加载全部
2827
* @param stockCodeList 指定的证券代码列表,如:如:{"sz000001", "sz000002"}
2928
*/
3029
explicit StrategyContext(const vector<string>& stockCodeList);
3130

3231
/**
3332
* 构造函数
3433
* @note 证券列表中如果包含 ("ALL") 则表示全部证券;
35-
* 指定K线类型列表同时影响着K线数据的优先加载顺序,靠前的将优先加载。同时受系统配置中预加载参数影响!
34+
* 1. 指定K线类型列表同时影响着K线数据的优先加载顺序,靠前的将优先加载。
35+
* 2. 未指定 ktypelist 或 preloadNum 时,将使用全局配置文件参数
3636
* @param stockCodeList 指定的证券代码列表,如:{"sh000001", "sz000001"}
3737
* @param ktypeList 指定的 K线数据列表,如:{"day", "min"}
38+
* @param preloadNum 指定的预加载数量,如:{{"min_max", 100}, {"day_max", 200}}
3839
*/
39-
StrategyContext(const vector<string>& stockCodeList, const vector<KQuery::KType>& ktypeList);
40+
StrategyContext(const vector<string>& stockCodeList, const vector<KQuery::KType>& ktypeList,
41+
const unordered_map<string, int>& preloadNum = {});
4042

4143
// 自定义移动构造与赋值会引起 python 中无法正常退出
4244
// StrategyContext(const StrategyContext&) = default;
@@ -75,6 +77,12 @@ class HKU_API StrategyContext {
7577
return m_ktypeList;
7678
}
7779

80+
void setPreloadNum(const unordered_map<string, int>& preloadNum);
81+
82+
const unordered_map<string, int>& getPreloadNum() const noexcept {
83+
return m_preloadNum;
84+
}
85+
7886
/**
7987
* 隐含的默认必须被加载的证券列表
8088
* @note 影响交易日历判断、和某些常被作为默认比较基准的证券,通常被作为某些函数的默认值
@@ -100,6 +108,7 @@ class HKU_API StrategyContext {
100108
vector<string> m_mustLoad{"sh000001", "sh000300"}; // 默认必须加载的 stock
101109
vector<string> m_stockCodeList;
102110
vector<KQuery::KType> m_ktypeList;
111+
unordered_map<string, int> m_preloadNum;
103112
};
104113

105114
HKU_API std::ostream& operator<<(std::ostream& os, const StrategyContext& context);

hikyuu_pywrap/_StrategyContext.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ void export_StrategeContext(py::module& m) {
1515
py::class_<StrategyContext>(m, "StrategyContext", "策略上下文")
1616
.def(py::init<>())
1717
.def(py::init<const vector<string>&>())
18-
.def(py::init<const vector<string>&, const vector<KQuery::KType>&>(), py::arg("stock_list"),
19-
py::arg("ktype_list"))
18+
.def(py::init<const vector<string>&, const vector<KQuery::KType>&,
19+
const unordered_map<string, int>&>(),
20+
py::arg("stock_list"), py::arg("ktype_list"),
21+
py::arg("preload_num") = unordered_map<string, int>(),
22+
R"(__init__(self, stock_list, ktype_list, [preload_num={}])
23+
24+
创建策略上下文
25+
26+
:param stock_list: 需要加载的证券代码列表,如:["sz000001", "sz000002"], 如包含 'ALL', 表示加载全部
27+
:param ktype_list: 需要加载的K线类型列表, 如:["day", "min"], 未指定时取全局配置文件中配置的默认值
28+
:param preload_num: 预加载数量,默认为空,如:{"min_max": 100, "day_max": 200}. 未指定时取全局配置文件中配置的默认值
29+
:return: 策略上下文对象)")
2030

2131
.def("__str__", &StrategyContext::str)
2232
.def("__repr__", &StrategyContext::str)
@@ -29,6 +39,8 @@ void export_StrategeContext(py::module& m) {
2939
&StrategyContext::setStockCodeList, py::return_value_policy::copy, "股票代码列表")
3040
.def_property("ktype_list", py::overload_cast<>(&StrategyContext::getKTypeList, py::const_),
3141
&StrategyContext::setKTypeList, py::return_value_policy::copy, "需要的K线类型")
42+
.def_property("preload_num", py::overload_cast<>(&StrategyContext::getPreloadNum, py::const_),
43+
&StrategyContext::setPreloadNum, py::return_value_policy::copy, "预加载数量")
3244

3345
.def("empty", &StrategyContext::empty, "上下文证券代码列表是否为空");
3446
}

0 commit comments

Comments
 (0)