mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-04 12:57:45 +08:00
Merge branch 'master' of https://github.com/fasiondog/hikyuu into feature/docs
This commit is contained in:
commit
18af4b77da
@ -100,16 +100,6 @@ AFPtr AllocateFundsBase::clone() {
|
||||
return p;
|
||||
}
|
||||
|
||||
void AllocateFundsBase::_check_weight(const SystemWeightList& sw_list) {
|
||||
price_t sum_weight = 0.0;
|
||||
for (const auto& sw : sw_list) {
|
||||
HKU_CHECK(!std::isnan(sw.weight) && sw.weight >= 0.0 && sw.weight <= 1.0,
|
||||
"Invalid weight ({}, {})", sw.sys->name(), sw.weight);
|
||||
sum_weight += sw.weight;
|
||||
}
|
||||
HKU_CHECK(sum_weight <= 1.001, "The cumulative weight exceeds 1! sum_weight: {}", sum_weight);
|
||||
}
|
||||
|
||||
SystemWeightList AllocateFundsBase::adjustFunds(const Datetime& date,
|
||||
const SystemWeightList& se_list,
|
||||
const std::unordered_set<SYSPtr>& running_list) {
|
||||
@ -211,6 +201,10 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date,
|
||||
break;
|
||||
}
|
||||
|
||||
if (!iter->sys) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果是运行中系统,不使用计算的权重,更新累积权重和
|
||||
if (running_set.find(iter->sys) != running_set.cend()) {
|
||||
FundsRecord sub_funds = m_tm->getFunds(date, m_query.kType());
|
||||
@ -274,6 +268,7 @@ SystemWeightList AllocateFundsBase::_adjust_with_running(
|
||||
// 回收所有运行中系统剩余资金,用于重新分配
|
||||
//-----------------------------------------------------------------
|
||||
for (const auto& sys : running_set) {
|
||||
if (sys) {
|
||||
auto sub_tm = sys->getTM();
|
||||
auto sub_cash = sub_tm->currentCash();
|
||||
if (sub_cash > 0.0 && sub_tm->checkout(date, sub_cash)) {
|
||||
@ -281,6 +276,7 @@ SystemWeightList AllocateFundsBase::_adjust_with_running(
|
||||
HKU_INFO_IF(trace, "[AF] Recycle cash: {:<.2f} from {}", sub_cash, sys->name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取计划分配的资产权重
|
||||
SystemWeightList sw_list = _allocateWeight(date, se_list);
|
||||
@ -336,6 +332,10 @@ SystemWeightList AllocateFundsBase::_adjust_with_running(
|
||||
|
||||
std::unordered_set<SYSPtr> reduced_running_set; // 缓存已执行过减仓的运行中系统
|
||||
for (auto iter = sw_list.begin(), end_iter = sw_list.end(); iter != end_iter; ++iter) {
|
||||
if (!iter->sys) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果当前系统是运行中的系统
|
||||
if (running_set.find(iter->sys) != running_set.cend()) {
|
||||
TMPtr sub_tm = iter->sys->getTM();
|
||||
@ -418,6 +418,10 @@ SystemWeightList AllocateFundsBase::_adjust_with_running(
|
||||
break;
|
||||
}
|
||||
|
||||
if (!iter->sys) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 系统期望分配的资产额
|
||||
price_t will_funds = roundUp(total_funds * iter->weight, precision);
|
||||
|
||||
|
@ -112,9 +112,6 @@ private:
|
||||
void _adjust_without_running(const Datetime& date, const SystemWeightList& se_list,
|
||||
const std::unordered_set<SYSPtr>& running_list);
|
||||
|
||||
/* 检查分配的权重是否在 0 和 1 之间,如果存在错误,抛出异常,仅在 trace 时生效*/
|
||||
void _check_weight(const SystemWeightList&);
|
||||
|
||||
private:
|
||||
string m_name; // 组件名称
|
||||
KQuery m_query; // 查询条件
|
||||
|
@ -143,6 +143,7 @@ void Portfolio::_readyForRun() {
|
||||
m_real_sys_list.reserve(total);
|
||||
for (size_t i = 0; i < total; i++) {
|
||||
SystemPtr& pro_sys = pro_sys_list[i];
|
||||
if (pro_sys) {
|
||||
SystemPtr sys = pro_sys->clone();
|
||||
m_se->bindRealToProto(sys, pro_sys);
|
||||
m_real_sys_list.emplace_back(sys);
|
||||
@ -158,6 +159,7 @@ void Portfolio::_readyForRun() {
|
||||
KData k = sys->getStock().getKData(m_query);
|
||||
sys->setTO(k);
|
||||
}
|
||||
}
|
||||
|
||||
// 告知 se 当前实际运行的系统列表
|
||||
m_se->calculate(m_real_sys_list, m_query);
|
||||
@ -325,14 +327,17 @@ void Portfolio::_runMoment(const Datetime& date, const Datetime& nextCycle, bool
|
||||
|
||||
// 如果选中的系统不在已有列表中, 则先清除其延迟买入操作,防止在调仓日出现未来信号
|
||||
for (auto& sys : m_tmp_selected_list) {
|
||||
if (sys.sys) {
|
||||
if (m_running_sys_set.find(sys.sys) == m_running_sys_set.end()) {
|
||||
sys.sys->clearDelayBuyRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (trace && !m_tmp_selected_list.empty()) {
|
||||
for (auto& sys : m_tmp_selected_list) {
|
||||
HKU_INFO("[PF] select: {}, score: {:<.4f}", sys.sys->name(), sys.weight);
|
||||
HKU_INFO_IF(sys.sys, "[PF] select: {}, score: {:<.4f}", sys.sys->name(),
|
||||
sys.weight);
|
||||
}
|
||||
}
|
||||
|
||||
@ -350,12 +355,14 @@ void Portfolio::_runMoment(const Datetime& date, const Datetime& nextCycle, bool
|
||||
|
||||
// 如果选中的系统不在已有列表中,且账户已经被分配了资金,则将其加入运行系统列表
|
||||
for (auto& sys : m_tmp_selected_list) {
|
||||
if (sys.sys) {
|
||||
if (m_running_sys_set.find(sys.sys) == m_running_sys_set.end()) {
|
||||
if (sys.sys->getTM()->cash(date, m_query.kType()) > 0.0) {
|
||||
m_running_sys_set.insert(sys.sys);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 从已运行系统列表中立即移除已没有持仓且没有资金的系统
|
||||
m_tmp_will_remove_sys.clear();
|
||||
|
Loading…
Reference in New Issue
Block a user