continue for Portfolio

This commit is contained in:
fasiondog 2020-08-13 00:35:19 +08:00
parent 1e2ec115c3
commit 7e2302fce7
5 changed files with 109 additions and 0 deletions

View File

@ -43,6 +43,29 @@ FundsRecord ::FundsRecord(price_t cash, price_t market_value, price_t short_mark
borrow_cash(borrow_cash), borrow_cash(borrow_cash),
borrow_asset(borrow_asset) {} borrow_asset(borrow_asset) {}
FundsRecord FundsRecord::operator+(const FundsRecord other) {
FundsRecord result;
result.cash = cash + other.cash;
result.market_value = market_value + other.market_value;
result.short_market_value = short_market_value + other.short_market_value;
result.base_cash = base_cash + other.base_cash;
result.base_asset = base_asset + other.base_asset;
result.borrow_cash = borrow_cash + other.borrow_cash;
result.borrow_asset = borrow_asset + other.borrow_asset;
return result;
}
FundsRecord& FundsRecord::operator+=(const FundsRecord other) {
cash += other.cash;
market_value += other.market_value;
short_market_value += other.short_market_value;
base_cash += other.base_cash;
base_asset += other.base_asset;
borrow_cash += other.borrow_cash;
borrow_asset += other.borrow_asset;
return *this;
}
bool HKU_API operator==(const FundsRecord& d1, const FundsRecord& d2) { bool HKU_API operator==(const FundsRecord& d1, const FundsRecord& d2) {
if (std::fabs(d1.cash - d2.cash) < 0.0001 && if (std::fabs(d1.cash - d2.cash) < 0.0001 &&
std::fabs(d1.market_value - d2.market_value) < 0.0001 && std::fabs(d1.market_value - d2.market_value) < 0.0001 &&

View File

@ -47,6 +47,10 @@ public:
//当前收益 = 当前净资产 - 当前投入本值资产 //当前收益 = 当前净资产 - 当前投入本值资产
// = cash + market_value - short_market_value - borrow_cash - base_cash - base_asset // = cash + market_value - short_market_value - borrow_cash - base_cash - base_asset
FundsRecord operator+(const FundsRecord other);
FundsRecord& operator+=(const FundsRecord other);
//序列化支持 //序列化支持
#if HKU_SUPPORT_SERIALIZATION #if HKU_SUPPORT_SERIALIZATION
private: private:

View File

@ -207,4 +207,48 @@ void Portfolio::run(const KQuery& query) {
} }
} }
FundsRecord Portfolio::getFunds(KQuery::KType ktype) const {
FundsRecord total_funds;
for (auto& sub_sys : m_running_sys_list) {
FundsRecord funds = sub_sys->getTM()->getFunds(ktype);
total_funds += funds;
}
total_funds.cash += m_tm->currentCash();
return total_funds;
}
FundsRecord Portfolio::getFunds(const Datetime& datetime, KQuery::KType ktype) {
FundsRecord total_funds;
for (auto& sub_sys : m_all_sys_set) {
FundsRecord funds = sub_sys->getTM()->getFunds(datetime, ktype);
total_funds += funds;
}
total_funds.cash += m_tm->cash(datetime, ktype);
return total_funds;
}
PriceList Portfolio::getFundsCurve(const DatetimeList& dates, KQuery::KType ktype) {
size_t total = dates.size();
PriceList result(total);
for (auto& sub_sys : m_all_sys_set) {
auto curve = sub_sys->getTM()->getFundsCurve(dates, ktype);
for (auto i = 0; i < total; i++) {
result[i] += curve[i];
}
}
return result;
}
PriceList Portfolio::getProfitCurve(const DatetimeList& dates, KQuery::KType ktype) {
size_t total = dates.size();
PriceList result(total);
for (auto& sub_sys : m_all_sys_set) {
auto curve = sub_sys->getTM()->getProfitCurve(dates, ktype);
for (auto i = 0; i < total; i++) {
result[i] += curve[i];
}
}
return result;
}
} /* namespace hku */ } /* namespace hku */

View File

@ -89,6 +89,38 @@ public:
typedef shared_ptr<Portfolio> PortfolioPtr; typedef shared_ptr<Portfolio> PortfolioPtr;
PortfolioPtr clone(); PortfolioPtr clone();
/**
*
* @param ktype
* @return
*/
FundsRecord getFunds(KQuery::KType ktype = KQuery::DAY) const;
/**
*
* @param datetime Null<Datetime>()
* @param ktype
* @return
* @note datetime等于Null<Datetime>()getFunds(KType)
*/
FundsRecord getFunds(const Datetime& datetime, KQuery::KType ktype = KQuery::DAY);
/**
* 线
* @param dates 线
* @param ktype K线类型KQuery::DAY
* @return
*/
PriceList getFundsCurve(const DatetimeList& dates, KQuery::KType ktype = KQuery::DAY);
/**
* 线线
* @param dates 线
* @param ktype K线类型KQuery::DAY
* @return 线
*/
PriceList getProfitCurve(const DatetimeList& dates, KQuery::KType ktype = KQuery::DAY);
protected: protected:
string m_name; string m_name;
TMPtr m_tm; TMPtr m_tm;

View File

@ -35,6 +35,12 @@ void export_Portfolio() {
.add_property("tm", &Portfolio::getTM, &Portfolio::setTM, "设置或获取交易管理对象") .add_property("tm", &Portfolio::getTM, &Portfolio::setTM, "设置或获取交易管理对象")
.add_property("se", &Portfolio::getSE, &Portfolio::setSE, "设置或获取交易对象选择算法") .add_property("se", &Portfolio::getSE, &Portfolio::setSE, "设置或获取交易对象选择算法")
.def("reset", &Portfolio::reset)
.def("clone", &Portfolio::clone)
.def("readyForRun", &Portfolio::readyForRun)
.def("runMoment", &Portfolio::runMoment)
.def("run", &Portfolio::run, R"(run(self, query) .def("run", &Portfolio::run, R"(run(self, query)