优化 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 * @return Indicator
*/ */
Indicator HKU_API BLOCKSETNUM(const Block& blk, const KQuery& query); Indicator HKU_API BLOCKSETNUM(const Block& blk, const KQuery& query);
Indicator HKU_API BLOCKSETNUM(const Block& blk);
/**
*
* @param category
* @param name
* @param query
* @return Indicator
*/
Indicator HKU_API BLOCKSETNUM(const string& category, const string& name, const KQuery& query);
} // namespace hku } // namespace hku

View File

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

View File

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

View File

@ -20,7 +20,7 @@ BOOST_CLASS_EXPORT(hku::IInSum)
namespace hku { namespace hku {
IInSum::IInSum() : IndicatorImp("INSUM", 1) { IInSum::IInSum() : IndicatorImp("INSUM", 1) {
setParam<KQuery>("query", KQueryByIndex(-100)); setParam<KQuery>("query", KQuery(0, 0));
setParam<Block>("block", Block()); setParam<Block>("block", Block());
setParam<int>("mode", 0); setParam<int>("mode", 0);
setParam<string>("market", "SH"); 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) { static void insum_min(const IndicatorList& inds, Indicator::value_t* dst, size_t len) {
for (const auto& value : inds) { 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(); const auto* data = value.data();
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
if (!std::isnan(data[i])) { if (!std::isnan(data[i])) {
@ -163,7 +170,9 @@ void IInSum::_calculate(const Indicator& ind) {
dates = k.getDatetimeList(); dates = k.getDatetimeList();
} else { } else {
q = getParam<KQuery>("query"); 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(); 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<KQuery>("query", query);
p->setParam<Block>("block", block); p->setParam<Block>("block", block);
p->setParam<int>("mode", mode); p->setParam<int>("mode", mode);
if (query == Null<KQuery>()) { p->setParam<bool>("ignore_context", false);
p->setParam<bool>("ignore_context", true);
}
return Indicator(p)(ind); return Indicator(p)(ind);
} }
Indicator HKU_API INSUM(const string& category, const string& name, const KQuery& query, Indicator HKU_API INSUM(const Block& block, const Indicator& ind, int mode) {
const Indicator& ind, int mode) { return INSUM(block, KQuery(0, 0), ind, mode);
Block block = StockManager::instance().getBlock(category, name);
return INSUM(block, query, ind, mode);
} }
} /* namespace hku */ } /* namespace hku */

View File

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