mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-02 20:08:26 +08:00
continue for Portfolio
This commit is contained in:
parent
1e2ec115c3
commit
7e2302fce7
@ -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 &&
|
||||||
|
@ -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:
|
||||||
|
@ -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 */
|
||||||
|
@ -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;
|
||||||
|
@ -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)
|
||||||
|
|
||||||
运行投资组合策略
|
运行投资组合策略
|
||||||
|
Loading…
Reference in New Issue
Block a user