Skip to content

Commit

Permalink
Merge pull request #191 from fasiondog/feature/performance
Browse files Browse the repository at this point in the history
Performance 优化
  • Loading branch information
fasiondog committed Mar 9, 2024
2 parents 5300233 + 8540924 commit d51bfa6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 62 deletions.
115 changes: 57 additions & 58 deletions hikyuu_cpp/hikyuu/trade_manage/Performance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,61 @@

namespace hku {

Performance::Performance()
: m_keys({"帐户初始金额",
"累计投入本金",
"累计投入资产",
"累计借入现金",
"累计借入资产",
"累计红利",
"现金余额",
"未平仓头寸净值",
"当前总资产",
"已平仓交易总成本",
"已平仓净利润总额",
"单笔交易最大占用现金比例%",
"交易平均占用现金比例%",
"已平仓帐户收益率%",
"帐户年复合收益率%",
"帐户平均年收益率%",
"赢利交易赢利总额",
"亏损交易亏损总额",
"已平仓交易总数",
"赢利交易数",
"亏损交易数",
"赢利交易比例%",
"赢利期望值",
"赢利交易平均赢利",
"亏损交易平均亏损",
"平均赢利/平均亏损比例",
"净赢利/亏损比例",
"最大单笔赢利",
"最大单笔盈利百分比%",
"最大单笔亏损",
"最大单笔亏损百分比%",
"赢利交易平均持仓时间",
"赢利交易最大持仓时间",
"亏损交易平均持仓时间",
"亏损交易最大持仓时间",
"空仓总时间",
"空仓时间/总时间%",
"平均空仓时间",
"最长空仓时间",
"最大连续赢利笔数",
"最大连续亏损笔数",
"最大连续赢利金额",
"最大连续亏损金额",
"R乘数期望值",
"交易机会频率/年",
"年度期望R乘数",
"赢利交易平均R乘数",
"亏损交易平均R乘数",
"最大单笔赢利R乘数",
"最大单笔亏损R乘数",
"最大连续赢利R乘数",
"最大连续亏损R乘数"}) {
for (const auto& key : m_keys) {
StringList Performance::ms_keys{"帐户初始金额",
"累计投入本金",
"累计投入资产",
"累计借入现金",
"累计借入资产",
"累计红利",
"现金余额",
"未平仓头寸净值",
"当前总资产",
"已平仓交易总成本",
"已平仓净利润总额",
"单笔交易最大占用现金比例%",
"交易平均占用现金比例%",
"已平仓帐户收益率%",
"帐户年复合收益率%",
"帐户平均年收益率%",
"赢利交易赢利总额",
"亏损交易亏损总额",
"已平仓交易总数",
"赢利交易数",
"亏损交易数",
"赢利交易比例%",
"赢利期望值",
"赢利交易平均赢利",
"亏损交易平均亏损",
"平均赢利/平均亏损比例",
"净赢利/亏损比例",
"最大单笔赢利",
"最大单笔盈利百分比%",
"最大单笔亏损",
"最大单笔亏损百分比%",
"赢利交易平均持仓时间",
"赢利交易最大持仓时间",
"亏损交易平均持仓时间",
"亏损交易最大持仓时间",
"空仓总时间",
"空仓时间/总时间%",
"平均空仓时间",
"最长空仓时间",
"最大连续赢利笔数",
"最大连续亏损笔数",
"最大连续赢利金额",
"最大连续亏损金额",
"R乘数期望值",
"交易机会频率/年",
"年度期望R乘数",
"赢利交易平均R乘数",
"亏损交易平均R乘数",
"最大单笔赢利R乘数",
"最大单笔亏损R乘数",
"最大连续赢利R乘数",
"最大连续亏损R乘数"};

Performance::Performance() {
for (const auto& key : ms_keys) {
m_result[key] = 0.0;
}
}
Expand All @@ -74,14 +75,12 @@ Performance::~Performance() {}
Performance& Performance::operator=(const Performance& other) {
HKU_IF_RETURN(this == &other, *this);
m_result = other.m_result;
m_keys = m_keys;
return *this;
}

Performance& Performance::operator=(Performance&& other) {
HKU_IF_RETURN(this == &other, *this);
m_result = std::move(other.m_result);
m_keys = std::move(other.m_keys);
return *this;
}

Expand All @@ -103,7 +102,7 @@ double Performance::get(const string& name) const {
PriceList Performance::values() const {
PriceList result(m_result.size());
size_t i = 0;
for (const auto& key : m_keys) {
for (const auto& key : ms_keys) {
result[i++] = m_result.at(key);
}
return result;
Expand All @@ -120,7 +119,7 @@ string Performance::report(const TradeManagerPtr& tm, const Datetime& datetime)

buf.setf(std::ios_base::fixed);
buf.precision(tm->precision());
for (const auto& key : m_keys) {
for (const auto& key : ms_keys) {
buf << key << ": " << m_result.at(key) << std::endl;
}

Expand Down
8 changes: 4 additions & 4 deletions hikyuu_cpp/hikyuu/trade_manage/Performance.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class HKU_API Performance {
Performance();
virtual ~Performance();

Performance(const Performance& other) : m_result(other.m_result), m_keys(other.m_keys) {}
Performance(Performance&& other) : m_result(std::move(other.m_result)), m_keys(other.m_keys) {}
Performance(const Performance& other) : m_result(other.m_result) {}
Performance(Performance&& other) : m_result(std::move(other.m_result)) {}

Performance& operator=(const Performance& other);
Performance& operator=(Performance&& other);
Expand Down Expand Up @@ -58,7 +58,7 @@ class HKU_API Performance {

/** 获取所有统计项名称,顺序与 values 相同 */
const StringList& names() const {
return m_keys;
return ms_keys;
}

/** 获取所有统计项值,顺序与 names 相同*/
Expand All @@ -70,7 +70,7 @@ class HKU_API Performance {

private:
map_type m_result;
StringList m_keys; // 保存统计项顺序, map/unordered_map都不能保持按插入顺序遍历
static StringList ms_keys; // 保存统计项顺序, map/unordered_map都不能保持按插入顺序遍历
};

} /* namespace hku */
Expand Down

0 comments on commit d51bfa6

Please sign in to comment.