TradeManager 去除不指定日期共有的 getFunds 方法,容易误用

This commit is contained in:
fasiondog 2022-11-22 00:51:58 +08:00
parent 83f4d42031
commit 9256578b3b
5 changed files with 28 additions and 48 deletions

View File

@ -22,7 +22,7 @@ string TradeManager::str() const {
os << std::fixed;
os.precision(2);
FundsRecord funds = getFunds();
FundsRecord funds = _getFunds();
string strip(",\n");
os << "TradeManager {\n"
<< " params: " << getParameter() << strip << " name: " << name() << strip
@ -192,14 +192,14 @@ TradeRecordList TradeManager::getTradeList(const Datetime& start_date,
TradeRecord temp_record;
temp_record.datetime = start_date;
auto low =
lower_bound(m_trade_list.begin(), m_trade_list.end(), temp_record,
auto low = lower_bound(
m_trade_list.begin(), m_trade_list.end(), temp_record,
std::bind(std::less<Datetime>(), std::bind(&TradeRecord::datetime, std::placeholders::_1),
std::bind(&TradeRecord::datetime, std::placeholders::_2)));
temp_record.datetime = end_date;
auto high =
lower_bound(m_trade_list.begin(), m_trade_list.end(), temp_record,
auto high = lower_bound(
m_trade_list.begin(), m_trade_list.end(), temp_record,
std::bind(std::less<Datetime>(), std::bind(&TradeRecord::datetime, std::placeholders::_1),
std::bind(&TradeRecord::datetime, std::placeholders::_2)));
@ -496,7 +496,7 @@ price_t TradeManager::cash(const Datetime& datetime, KQuery::KType ktype) {
return funds.cash;
}
FundsRecord TradeManager::getFunds(KQuery::KType ktype) const {
FundsRecord TradeManager::_getFunds(KQuery::KType ktype) const {
price_t value = 0.0; //当前市值
position_map_type::const_iterator iter = m_position.begin();
if (!getParam<bool>("use_contract")) {
@ -520,7 +520,7 @@ FundsRecord TradeManager::getFunds(KQuery::KType ktype) const {
}
FundsRecord TradeManager::getFunds(const Datetime& indatetime, KQuery::KType ktype) {
HKU_IF_RETURN(indatetime == Null<Datetime>(), getFunds(ktype));
HKU_IF_RETURN(indatetime == Null<Datetime>(), _getFunds(ktype));
HKU_IF_RETURN(indatetime < initDatetime(), FundsRecord());
Datetime datetime(indatetime.year(), indatetime.month(), indatetime.day(), 23, 59);
@ -528,7 +528,7 @@ FundsRecord TradeManager::getFunds(const Datetime& indatetime, KQuery::KType kty
if (datetime > lastDatetime()) {
//根据权息数据调整持仓
updateWithWeight(datetime);
return getFunds(ktype);
return _getFunds(ktype);
}
return _getFundsByContract(indatetime, ktype);
@ -663,7 +663,7 @@ FundsRecord TradeManager::_getFundsByContract(const Datetime& datetime, KQuery::
// tr.planPrice, tr.from);
// }
}
return tm->getFunds(ktype);
return tm->_getFunds(ktype);
}
PriceList TradeManager::getFundsCurve(const DatetimeList& dates, KQuery::KType ktype) {
@ -764,7 +764,8 @@ void TradeManager::_updateWithWeight(const Datetime& datetime) {
} /* for weight */
} /* for position */
std::sort(new_trade_buffer.begin(), new_trade_buffer.end(),
std::sort(
new_trade_buffer.begin(), new_trade_buffer.end(),
std::bind(std::less<Datetime>(), std::bind(&TradeRecord::datetime, std::placeholders::_1),
std::bind(&TradeRecord::datetime, std::placeholders::_2)));

View File

@ -184,17 +184,9 @@ public:
price_t goalPrice = 0.0, price_t planPrice = 0.0,
SystemPart from = PART_INVALID) override;
/**
*
* @note
* @param ktype
* @return
*/
virtual FundsRecord getFunds(KQuery::KType ktype = KQuery::DAY) const override;
/**
*
* @param datetime Null<Datetime>()getFunds(KType)
* @param datetime Null<Datetime>()
* @param ktype
* @return
*/
@ -246,6 +238,9 @@ private:
// 合约类交易结算,更新当前时刻前一结算日权益
void _updateSettleByDay(const Datetime& datetime);
// 获取账户最后交易时刻的资产详情
FundsRecord _getFunds(KQuery::KType ktype = KQuery::DAY) const;
FundsRecord _getFundsByContract(const Datetime& datetime, KQuery::KType ktype);
bool _add_init_tr(const TradeRecord&);

View File

@ -340,22 +340,12 @@ public:
return TradeRecord();
}
/**
*
* @param ktype
* @return
*/
virtual FundsRecord getFunds(KQuery::KType ktype = KQuery::DAY) const {
HKU_WARN("The subclass does not implement this method");
return FundsRecord();
}
/**
*
* @param datetime
* @param ktype
* @return
* @note datetime等于Null<Datetime>()getFunds(KType)
* @note datetime等于Null<Datetime>()
*/
virtual FundsRecord getFunds(const Datetime& datetime, KQuery::KType ktype = KQuery::DAY) {
HKU_WARN("The subclass does not implement this method");

View File

@ -340,8 +340,7 @@ TEST_CASE("test_TradeManager_normal_buy_and_sell_no_margin_by_day") {
CHECK_EQ(history_position_list.size(), 0);
auto position = tm->getPosition(Datetime(199305020000LL), stk);
CHECK_EQ(position, Null<PositionRecord>());
CHECK_EQ(tm->getFunds(KQuery::DAY), FundsRecord(100000., 0., 100000.0, 0.));
CHECK_EQ(tm->getFunds(Null<Datetime>(), KQuery::DAY), tm->getFunds(KQuery::DAY));
CHECK_EQ(tm->getFunds(Null<Datetime>(), KQuery::DAY), tm->getFunds(tm->lastDatetime()));
CHECK_EQ(tm->getFunds(Datetime(199304300000LL), KQuery::DAY), FundsRecord(0., 0., 0., 0.));
CHECK_EQ(tm->getFunds(Datetime(199305010900LL), KQuery::DAY),
FundsRecord(100000., 0., 100000.0, 0.));
@ -401,22 +400,22 @@ TEST_CASE("test_TradeManager_normal_buy_and_sell_no_margin_by_day") {
HKU_INFO("{}", tr);
HKU_INFO("{}", stk.getKRecord(Datetime(199305260000LL)));
HKU_INFO("{}", tm);
HKU_INFO("{}", tm->getFunds(KQuery::DAY));
// HKU_INFO("{}", tm->getFunds(KQuery::DAY));
auto current_num = tm->getHoldNumber(Datetime(199305260000LL), stk);
CHECK_EQ(current_num, 185); // 24日送转85股, 红利30元
HKU_INFO("{}", tr);
HKU_INFO("{}", stk.getKRecord(Datetime(199305260000LL)));
HKU_INFO("{}", tm);
HKU_INFO("{}", tm->getFunds(KQuery::DAY));
// HKU_INFO("{}", tm->getFunds(KQuery::DAY));
tr = tm->sell(Datetime(199305260000LL), stk, 28.1, MAX_DOUBLE);
CHECK_EQ(tm->currentCash(), 99458.50);
CHECK_EQ(tr, TradeRecord(stk, Datetime(199305260000LL), BUSINESS_SELL, 0., 28.1, 0., 185., cost,
0., 99458.5, 1., PART_INVALID));
CHECK(tm->getPositionList().empty());
CHECK_EQ(tm->getFunds(KQuery::DAY), FundsRecord(99458.5, 0., 100000.0, 0.));
CHECK_EQ(tm->getFunds(Null<Datetime>(), KQuery::DAY), tm->getFunds(KQuery::DAY));
// CHECK_EQ(tm->getFunds(KQuery::DAY), FundsRecord(99458.5, 0., 100000.0, 0.));
// CHECK_EQ(tm->getFunds(Null<Datetime>(), KQuery::DAY), tm->getFunds(KQuery::DAY));
// CHECK_EQ(tm->getFunds(KQuery::DAY), FundsRecord(100000., 0., 100000.0, 0.));
// CHECK_EQ(tm->getFunds(Null<Datetime>(), KQuery::DAY), tm->getFunds(KQuery::DAY));

View File

@ -13,10 +13,6 @@
using namespace boost::python;
using namespace hku;
FundsRecord (TradeManagerBase::*getFunds_1)(KQuery::KType) const = &TradeManagerBase::getFunds;
FundsRecord (TradeManagerBase::*getFunds_2)(const Datetime&,
KQuery::KType) = &TradeManagerBase::getFunds;
TradeCostPtr (TradeManagerBase::*get_costFunc)() const = &TradeManagerBase::costFunc;
void (TradeManagerBase::*set_costFunc)(const TradeCostPtr&) = &TradeManagerBase::costFunc;
@ -176,8 +172,7 @@ void export_TradeManager() {
:param ktype: K线类型
:rtype: float)")
.def("get_funds", getFunds_1, (arg("ktype")))
.def("get_funds", getFunds_2, (arg("datetime"), arg("ktype") = KQuery::DAY),
.def("get_funds", &TradeManager::getFunds, (arg("datetime"), arg("ktype") = KQuery::DAY),
R"(get_funds(self, [datetime, ktype = Query.DAY])