Merge branch 'master' of https://github.com/fasiondog/hikyuu into feature/docs

This commit is contained in:
fasiondog 2024-09-25 15:53:06 +08:00
commit 652a147926
4 changed files with 33 additions and 17 deletions

View File

@ -129,15 +129,10 @@ void TradeManager::_reset() {
TradeManagerPtr TradeManager::_clone() {
auto p = make_shared<TradeManager>(m_init_datetime, m_init_cash, m_costfunc, m_name);
p->m_params = m_params;
p->m_name = m_name;
p->m_init_datetime = m_init_datetime;
p->m_init_cash = m_init_cash;
p->m_last_update_datetime = m_last_update_datetime;
// costfunc是一个公共的函数对象是共享实现无须deepcopy
p->m_costfunc = m_costfunc;
p->m_cash = m_cash;
p->m_checkin_cash = m_checkin_cash;
p->m_checkout_cash = m_checkout_cash;
@ -149,11 +144,7 @@ TradeManagerPtr TradeManager::_clone() {
p->m_trade_list = m_trade_list;
p->m_position = m_position;
p->m_position_history = m_position_history;
p->m_broker_list = m_broker_list;
p->m_broker_last_datetime = m_broker_last_datetime;
p->m_actions = m_actions;
return p;
}

View File

@ -168,6 +168,7 @@ public:
p->m_name = m_name;
p->m_broker_last_datetime = m_broker_last_datetime;
p->m_costfunc = m_costfunc;
p->m_broker_list = m_broker_list;
return p;
}

View File

@ -19,9 +19,9 @@ MultiFactorSelector::MultiFactorSelector() : SelectorBase("SE_MultiFactor") {
setParam<bool>("only_should_buy", false);
setParam<bool>("ignore_null", true); // 是否忽略 MF 中 score 值为 nan 的证券
setParam<int>("topn", 10);
setParam<bool>("reverse", false); // 逆序,此时 topn 代表最末尾的几个,相当于按最低值排序
setParam<int>("ic_n", 5);
setParam<int>("ic_rolling_n", 120);
setParam<KQuery>("query", KQuery());
setParam<Stock>("ref_stk", Stock());
setParam<string>("mode", "MF_ICIRWeight");
}
@ -32,12 +32,14 @@ MultiFactorSelector::MultiFactorSelector(const MFPtr& mf, int topn)
setParam<bool>("only_should_buy", false);
setParam<bool>("ignore_null", true);
setParam<int>("topn", topn);
setParam<bool>("reverse", false);
setParam<int>("ic_n", mf->getParam<int>("ic_n"));
setParam<KQuery>("query", mf->getQuery());
setParam<Stock>("ref_stk", mf->getRefStock());
if (mf->haveParam("ic_rolling_n")) {
setParam<int>("ic_rolling_n", mf->getParam<int>("ic_rolling_n"));
} else {
setParam<int>("ic_rolling_n", 120);
}
setParam<string>("mode", mf->name());
}
@ -84,11 +86,34 @@ SystemWeightList MultiFactorSelector::getSelected(Datetime date) {
}
ScoreRecordList scores;
if (getParam<bool>("ignore_null")) {
scores = m_mf->getScores(date, 0, topn,
[](const ScoreRecord& sc) { return !std::isnan(sc.value); });
if (!getParam<bool>("reverse")) {
if (getParam<bool>("ignore_null")) {
scores = m_mf->getScores(date, 0, topn,
[](const ScoreRecord& sc) { return !std::isnan(sc.value); });
} else {
scores = m_mf->getScores(date, 0, topn);
}
} else {
scores = m_mf->getScores(date, 0, topn);
ScoreRecordList raw_scores = m_mf->getScores(date);
auto iter = raw_scores.rbegin();
for (size_t count = 0; count < topn && iter != raw_scores.rend(); ++iter) {
if (!std::isnan(iter->value)) {
scores.emplace_back(*iter);
count++;
}
}
if (scores.size() < topn && !getParam<bool>("ignore_null")) {
size_t lack = topn - scores.size();
auto iter = raw_scores.rbegin();
for (size_t count = 0; count < lack && iter != raw_scores.rend(); ++iter) {
if (std::isnan(iter->value)) {
scores.emplace_back(*iter);
count++;
} else {
break;
}
}
}
}
if (getParam<bool>("only_should_buy")) {
@ -118,7 +143,7 @@ void MultiFactorSelector::_calculate() {
stks.emplace_back(sys->getStock());
}
auto query = getParam<KQuery>("query");
const auto& query = m_query;
auto ic_n = getParam<int>("ic_n");
auto ic_rolling_n = getParam<int>("ic_rolling_n");
auto mode = getParam<string>("mode");

View File

@ -254,7 +254,6 @@ void export_TradeManager(py::module& m) {
- reinvest=False (bool) :
- precision=2 (int) :
- support_borrow_cash=False (bool) :
- support_borrow_stock=False (bool) :