Skip to content

Commit

Permalink
调整 MF.getScores 命名及不存在时返回空列表,不抛出异常
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Apr 3, 2024
1 parent 8c12727 commit 0aa68bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
18 changes: 12 additions & 6 deletions hikyuu_cpp/hikyuu/trade_sys/multifactor/MultiFactorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,20 @@ const IndicatorList& MultiFactorBase::getAllFactors() {
return m_all_factors;
}

const ScoreRecordList& MultiFactorBase::getScore(const Datetime& d) {
ScoreRecordList MultiFactorBase::getScores(const Datetime& d) {
calculate();
ScoreRecordList ret;
const auto iter = m_date_index.find(d);
HKU_CHECK(iter != m_date_index.cend(), "Could not find this date: {}", d);
return m_stk_factor_by_date[iter->second];
HKU_IF_RETURN(iter == m_date_index.cend(), ret);
ret = m_stk_factor_by_date[iter->second];
return ret;
}

ScoreRecordList MultiFactorBase::getScores(const Datetime& date, size_t start, size_t end) {
ScoreRecordList ret;
HKU_IF_RETURN(start >= end, ret);

const auto& cross = getScore(date);
const auto& cross = getScores(date);
if (end == Null<size_t>() || end > cross.size()) {
end = cross.size();
}
Expand All @@ -211,7 +213,9 @@ ScoreRecordList MultiFactorBase::getScores(const Datetime& date, size_t start, s
ScoreRecordList ret;
HKU_IF_RETURN(start >= end, ret);

const auto& cross = getScore(date);
const auto& cross = getScores(date);
HKU_IF_RETURN(cross.empty(), ret);

if (end == Null<size_t>() || end > cross.size()) {
end = cross.size();
}
Expand All @@ -237,7 +241,9 @@ ScoreRecordList MultiFactorBase::getScores(
ScoreRecordList ret;
HKU_IF_RETURN(start >= end, ret);

const auto& cross = getScore(date);
const auto& cross = getScores(date);
HKU_IF_RETURN(cross.empty(), ret);

if (end == Null<size_t>() || end > cross.size()) {
end = cross.size();
}
Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/trade_sys/multifactor/MultiFactorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class HKU_API MultiFactorBase : public enable_shared_from_this<MultiFactorBase>
const IndicatorList& getAllFactors();

/** 获取指定日期截面的所有因子值,已经降序排列 */
const ScoreRecordList& getScore(const Datetime&);
ScoreRecordList getScores(const Datetime&);

ScoreRecordList getScores(const Datetime& date, size_t start, size_t end = Null<size_t>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ TEST_CASE("test_MF_EqualWeight") {
auto ic2 = IC(MA(CLOSE()), stks, query, ref_stk, 1);
CHECK_UNARY(ic1.equal(ic2));

CHECK_THROWS_AS(mf->getScore(Datetime(20111204)), std::exception);
auto cross = mf->getScore(Datetime(20111205));
CHECK_UNARY(mf->getScores(Datetime(20111204)).empty());
auto cross = mf->getScores(Datetime(20111205));
CHECK_EQ(cross.size(), 2);
CHECK_EQ(cross[0].stock, sm["sh600004"]);
CHECK_EQ(cross[0].value, doctest::Approx(6.85));
CHECK_EQ(cross[1].stock, sm["sh600005"]);
CHECK_EQ(cross[1].value, doctest::Approx(3.13));

cross = mf->getScore(Datetime(20111206));
cross = mf->getScores(Datetime(20111206));
CHECK_EQ(cross.size(), 2);
CHECK_EQ(cross[0].stock, sm["sh600004"]);
CHECK_EQ(cross[0].value, doctest::Approx(6.855));
Expand Down

0 comments on commit 0aa68bd

Please sign in to comment.