优化 INSUM, BLOCKSETNUM 可直接输入 stock list, 可以忽略 query 参数

This commit is contained in:
fasiondog 2024-06-16 02:53:00 +08:00
parent c00f897ceb
commit bba5f4c5a7
5 changed files with 61 additions and 43 deletions

View File

@ -18,14 +18,6 @@ namespace hku {
* @return Indicator
*/
Indicator HKU_API BLOCKSETNUM(const Block& blk, const KQuery& query);
/**
*
* @param category
* @param name
* @param query
* @return Indicator
*/
Indicator HKU_API BLOCKSETNUM(const string& category, const string& name, const KQuery& query);
Indicator HKU_API BLOCKSETNUM(const Block& blk);
} // namespace hku

View File

@ -23,14 +23,11 @@ Indicator HKU_API INSUM(const Block& block, const KQuery& query, const Indicator
/**
* .:0-,1-,2-,3-.
* @param category
* @param category
* @param query
* @param block
* @param ind
* @param mode :0-,1-,2-,3-.
* @return Indicator
*/
Indicator HKU_API INSUM(const string& category, const string& name, const KQuery& query,
const Indicator& ind, int mode);
Indicator HKU_API INSUM(const Block& block, const Indicator& ind, int mode);
} // namespace hku

View File

@ -43,7 +43,9 @@ void IBlockSetNum::_calculate(const Indicator& ind) {
dates = k.getDatetimeList();
} else {
KQuery q = getParam<KQuery>("query");
dates = StockManager::instance().getTradingCalendar(q, getParam<string>("market"));
if (q != KQuery(0, 0)) {
dates = StockManager::instance().getTradingCalendar(q, getParam<string>("market"));
}
}
size_t total = dates.size();
@ -72,17 +74,13 @@ Indicator HKU_API BLOCKSETNUM(const Block& block, const KQuery& query) {
IndicatorImpPtr p = make_shared<IBlockSetNum>();
p->setParam<KQuery>("query", query);
p->setParam<Block>("block", block);
if (query == Null<KQuery>()) {
p->setParam<bool>("ignore_context", true);
} else {
p->calculate();
}
p->setParam<bool>("ignore_context", false);
p->calculate();
return Indicator(p);
}
Indicator HKU_API BLOCKSETNUM(const string& category, const string& name, const KQuery& query) {
Block block = StockManager::instance().getBlock(category, name);
return BLOCKSETNUM(block, query);
Indicator HKU_API BLOCKSETNUM(const Block& block) {
return BLOCKSETNUM(block, KQuery(0, 0));
}
} /* namespace hku */

View File

@ -20,7 +20,7 @@ BOOST_CLASS_EXPORT(hku::IInSum)
namespace hku {
IInSum::IInSum() : IndicatorImp("INSUM", 1) {
setParam<KQuery>("query", KQueryByIndex(-100));
setParam<KQuery>("query", KQuery(0, 0));
setParam<Block>("block", Block());
setParam<int>("mode", 0);
setParam<string>("market", "SH");
@ -138,7 +138,14 @@ static void insum_max(const IndicatorList& inds, Indicator::value_t* dst, size_t
static void insum_min(const IndicatorList& inds, Indicator::value_t* dst, size_t len) {
for (const auto& value : inds) {
HKU_ASSERT(value.size() == len);
if (value.empty()) {
continue;
}
if (value.size() != len) {
HKU_WARN("Ignore stock: {}, value len: {}, dst len: {}, stk: {}",
value.getContext().getStock().name(), value.size(), len);
continue;
}
const auto* data = value.data();
for (size_t i = 0; i < len; i++) {
if (!std::isnan(data[i])) {
@ -163,7 +170,9 @@ void IInSum::_calculate(const Indicator& ind) {
dates = k.getDatetimeList();
} else {
q = getParam<KQuery>("query");
dates = StockManager::instance().getTradingCalendar(q, getParam<string>("market"));
if (q != KQuery(0, 0)) {
dates = StockManager::instance().getTradingCalendar(q, getParam<string>("market"));
}
}
size_t total = dates.size();
@ -193,16 +202,12 @@ Indicator HKU_API INSUM(const Block& block, const KQuery& query, const Indicator
p->setParam<KQuery>("query", query);
p->setParam<Block>("block", block);
p->setParam<int>("mode", mode);
if (query == Null<KQuery>()) {
p->setParam<bool>("ignore_context", true);
}
p->setParam<bool>("ignore_context", false);
return Indicator(p)(ind);
}
Indicator HKU_API INSUM(const string& category, const string& name, const KQuery& query,
const Indicator& ind, int mode) {
Block block = StockManager::instance().getBlock(category, name);
return INSUM(block, query, ind, mode);
Indicator HKU_API INSUM(const Block& block, const Indicator& ind, int mode) {
return INSUM(block, KQuery(0, 0), ind, mode);
}
} /* namespace hku */

View File

@ -1849,6 +1849,7 @@ void export_Indicator_build_in(py::module& m) {
:param int ix:
:param int name: )");
m.def("BLOCKSETNUM", py::overload_cast<const Block&>(BLOCKSETNUM), py::arg("block"));
m.def("BLOCKSETNUM", py::overload_cast<const Block&, const KQuery&>(BLOCKSETNUM),
py::arg("block"), py::arg("query"), R"(BLOCKSETNUM(block, query)
@ -1858,15 +1859,29 @@ void export_Indicator_build_in(py::module& m) {
:param Query query: )");
m.def(
"BLOCKSETNUM", py::overload_cast<const string&, const string&, const KQuery&>(BLOCKSETNUM),
py::arg("category"), py::arg("name"), py::arg("query"), R"(BLOCKSETNUM(category, name, query)
"BLOCKSETNUM",
[](const py::sequence& stks) {
Block blk;
blk.add(python_list_to_vector<Stock>(stks));
return BLOCKSETNUM(blk);
},
py::arg("stks"));
m.def(
"BLOCKSETNUM",
[](const py::sequence& stks, const KQuery& query) {
Block blk;
blk.add(python_list_to_vector<Stock>(stks));
return BLOCKSETNUM(blk, query);
},
py::arg("stks"), py::arg("query"), R"(BLOCKSETNUM(block, query)
:param str category:
:param str name:
:param query )");
:param Sequence stks: stock list
:param Query query: )");
m.def("INSUM", py::overload_cast<const Block&, const Indicator&, int>(INSUM), py::arg("block"),
py::arg("ind"), py::arg("mode"));
m.def("INSUM", py::overload_cast<const Block&, const KQuery&, const Indicator&, int>(INSUM),
py::arg("block"), py::arg("query"), py::arg("ind"), py::arg("mode"),
R"(INSUM(block, query, ind, mode)
@ -1881,14 +1896,25 @@ void export_Indicator_build_in(py::module& m) {
m.def(
"INSUM",
py::overload_cast<const string&, const string&, const KQuery&, const Indicator&, int>(INSUM),
py::arg("category"), py::arg("name"), py::arg("query"), py::arg("ind"), py::arg("mode"),
R"(INSUM(category, name, ind, mode)
[](const py::sequence stks, const Indicator& ind, int mode) {
Block blk;
blk.add(python_list_to_vector<Stock>(stks));
return INSUM(blk, ind, mode);
},
py::arg("stks"), py::arg("ind"), py::arg("mode"));
m.def(
"INSUM",
[](const py::sequence stks, const KQuery& query, const Indicator& ind, int mode) {
Block blk;
blk.add(python_list_to_vector<Stock>(stks));
return INSUM(blk, query, ind, mode);
},
py::arg("stks"), py::arg("query"), py::arg("ind"), py::arg("mode"),
R"(INSUM(stks, query, ind, mode)
.:0-,1-,2-,3-.
:param str category:
:param str name:
:param Sequence stks: stock list
:param Query query:
:param Indicator ind:
:param int mode: :0-,1-,2-,3-.