Skip to content

Commit

Permalink
fix(PositionRecord): 修复未平仓记录的总盈利计算问题, 只对已清仓记录有效,未清仓将返回0
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Feb 28, 2025
1 parent 7e70716 commit 73a60d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions hikyuu_cpp/hikyuu/trade_manage/PositionRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ string PositionRecord::str() const {
return os.str();
}

price_t PositionRecord::totalProfit() const {
if (cleanDatetime == Null<Datetime>()) {
HKU_ERROR("The profit cannot be calculated without cleaned records!");
return 0.0;
}
return buyMoney - totalCost - sellMoney;
}

bool HKU_API operator==(const PositionRecord& d1, const PositionRecord& d2) {
return d1.stock == d2.stock && d1.takeDatetime == d2.takeDatetime &&
d1.cleanDatetime == d2.cleanDatetime && fabs(d1.number - d2.number) < 0.00001 &&
Expand Down
9 changes: 5 additions & 4 deletions hikyuu_cpp/hikyuu/trade_manage/PositionRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class HKU_API PositionRecord {
/** 仅用于python的__str__ */
string str() const;

/** 盈利 = 买入资金 - 累计交易总成本 - 卖出资金 */
price_t totalProfit() const {
return buyMoney - totalCost - sellMoney;
}
/**
* @brief 盈亏 = 买入资金 - 累计交易总成本 - 卖出资金
* @note 只对已清仓的记录有效,未清仓将返回0.0
*/
price_t totalProfit() const;

Stock stock; ///< 交易对象
Datetime takeDatetime; ///< 初次建仓日期
Expand Down
6 changes: 5 additions & 1 deletion hikyuu_pywrap/trade_manage/_PositionRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ void export_PositionRecord(py::module& m) {
"累计交易风险 = 各次 (买入价格-止损)*买入数量, 不包含交易成本")
.def_readwrite("sell_money", &PositionRecord::sellMoney, "累计卖出资金(float)")
.def_property_readonly("total_profit", &PositionRecord::totalProfit,
"累计盈利 = 累计卖出资金 - 累计买入资金 - 累计交易成本")
R"(total_profit(self):
累计盈利 = 累计卖出资金 - 累计买入资金 - 累计交易成本
注意: 只对已清仓的记录有效, 未清仓的记录返回0 )")

DEF_PICKLE(PositionRecord);
}

0 comments on commit 73a60d3

Please sign in to comment.