From 9256578b3bcb4c7a5869291fcfd84f9155f8e16e Mon Sep 17 00:00:00 2001 From: fasiondog Date: Tue, 22 Nov 2022 00:51:58 +0800 Subject: [PATCH] =?UTF-8?q?TradeManager=20=E5=8E=BB=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=97=A5=E6=9C=9F=E5=85=B1=E6=9C=89=E7=9A=84?= =?UTF-8?q?=20getFunds=20=E6=96=B9=E6=B3=95=EF=BC=8C=E5=AE=B9=E6=98=93?= =?UTF-8?q?=E8=AF=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hikyuu/trade_manage/TradeManager.cpp | 33 ++++++++++--------- hikyuu_cpp/hikyuu/trade_manage/TradeManager.h | 13 +++----- .../hikyuu/trade_manage/TradeManagerBase.h | 12 +------ .../hikyuu/trade_manage/test_TradeManager.cpp | 11 +++---- hikyuu_pywrap/trade_manage/_TradeManager.cpp | 7 +--- 5 files changed, 28 insertions(+), 48 deletions(-) diff --git a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp index a37e69c5..1d9d9928 100644 --- a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp +++ b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp @@ -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(), 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(), 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(), 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(), 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("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(), getFunds(ktype)); + HKU_IF_RETURN(indatetime == Null(), _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(), 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(), 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) { diff --git a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h index ea72a4d7..4fd9bde4 100644 --- a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h +++ b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h @@ -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()时,与getFunds(KType)同 + * @param datetime 指定日期,等于Null()时,为最后交易时刻的市值 * @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&); diff --git a/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h b/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h index 8cefcd43..df6ba73a 100644 --- a/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h +++ b/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h @@ -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()时,与getFunds(KType)同 + * @note 当datetime等于Null()时,为最后交易时刻的市值 */ virtual FundsRecord getFunds(const Datetime& datetime, KQuery::KType ktype = KQuery::DAY) { HKU_WARN("The subclass does not implement this method"); diff --git a/hikyuu_cpp/unit_test/hikyuu/trade_manage/test_TradeManager.cpp b/hikyuu_cpp/unit_test/hikyuu/trade_manage/test_TradeManager.cpp index 62830788..90cce742 100644 --- a/hikyuu_cpp/unit_test/hikyuu/trade_manage/test_TradeManager.cpp +++ b/hikyuu_cpp/unit_test/hikyuu/trade_manage/test_TradeManager.cpp @@ -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()); - CHECK_EQ(tm->getFunds(KQuery::DAY), FundsRecord(100000., 0., 100000.0, 0.)); - CHECK_EQ(tm->getFunds(Null(), KQuery::DAY), tm->getFunds(KQuery::DAY)); + CHECK_EQ(tm->getFunds(Null(), 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(), KQuery::DAY), tm->getFunds(KQuery::DAY)); + // CHECK_EQ(tm->getFunds(KQuery::DAY), FundsRecord(99458.5, 0., 100000.0, 0.)); + // CHECK_EQ(tm->getFunds(Null(), KQuery::DAY), tm->getFunds(KQuery::DAY)); // CHECK_EQ(tm->getFunds(KQuery::DAY), FundsRecord(100000., 0., 100000.0, 0.)); // CHECK_EQ(tm->getFunds(Null(), KQuery::DAY), tm->getFunds(KQuery::DAY)); diff --git a/hikyuu_pywrap/trade_manage/_TradeManager.cpp b/hikyuu_pywrap/trade_manage/_TradeManager.cpp index 5fb4bdde..75712e70 100644 --- a/hikyuu_pywrap/trade_manage/_TradeManager.cpp +++ b/hikyuu_pywrap/trade_manage/_TradeManager.cpp @@ -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]) 获取指定时刻的资产市值详情