mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-04 12:57:45 +08:00
TradeManager 去除不指定日期共有的 getFunds 方法,容易误用
This commit is contained in:
parent
83f4d42031
commit
9256578b3b
@ -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,16 +192,16 @@ 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,
|
||||
std::bind(std::less<Datetime>(), std::bind(&TradeRecord::datetime, std::placeholders::_1),
|
||||
std::bind(&TradeRecord::datetime, std::placeholders::_2)));
|
||||
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,
|
||||
std::bind(std::less<Datetime>(), std::bind(&TradeRecord::datetime, std::placeholders::_1),
|
||||
std::bind(&TradeRecord::datetime, std::placeholders::_2)));
|
||||
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)));
|
||||
|
||||
result.insert(result.end(), low, high);
|
||||
|
||||
@ -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,9 +764,10 @@ void TradeManager::_updateWithWeight(const Datetime& datetime) {
|
||||
} /* for weight */
|
||||
} /* for position */
|
||||
|
||||
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)));
|
||||
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)));
|
||||
|
||||
size_t total = new_trade_buffer.size();
|
||||
for (size_t i = 0; i < total; ++i) {
|
||||
|
@ -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&);
|
||||
|
@ -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");
|
||||
|
@ -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));
|
||||
|
@ -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])
|
||||
|
||||
获取指定时刻的资产市值详情
|
||||
|
Loading…
Reference in New Issue
Block a user