Merge pull request #186 from fasiondog/feature/analysis

fixed AllocateFundsBase,调整未执行系统时,判断是否有实际的信号产生
This commit is contained in:
fasiondog 2024-03-04 15:36:13 +08:00 committed by GitHub
commit fbc1a1d545
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -419,16 +419,16 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date, const Syst
std::bind(std::less<double>(), std::bind(&SystemWeight::m_weight, std::placeholders::_1), std::bind(std::less<double>(), std::bind(&SystemWeight::m_weight, std::placeholders::_1),
std::bind(&SystemWeight::m_weight, std::placeholders::_2))); std::bind(&SystemWeight::m_weight, std::placeholders::_2)));
//倒序遍历在遇到权重为0或等于运行的最大运行时系统数时结束遍历 // 检测是否有信号发生,过滤掉没有发生信号的系统 以及 权重为 0 的系统
size_t count = 0; SystemWeightList new_sw_list;
auto sw_iter = sw_list.rbegin(); auto sw_iter = sw_list.rbegin();
for (; sw_iter != sw_list.rend(); ++sw_iter) { for (; sw_iter != sw_list.rend(); ++sw_iter) {
if (sw_iter->getWeight() <= 0.0 || count >= max_num) if (sw_iter->getWeight() <= 0.0)
break; break;
count++; if (sw_iter->getSYS()->getSG()->shouldBuy(date)) {
new_sw_list.emplace_back(*sw_iter);
}
} }
auto end_iter = sw_iter; // 记录结束位置
// 总账号资金精度 // 总账号资金精度
int precision = m_shadow_tm->getParam<int>("precision"); int precision = m_shadow_tm->getParam<int>("precision");
@ -455,8 +455,9 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date, const Syst
// 再次遍历选中子系统列表,并将剩余现金按权重比例转入子账户 // 再次遍历选中子系统列表,并将剩余现金按权重比例转入子账户
double weight_unit = getParam<double>("weight_unit"); double weight_unit = getParam<double>("weight_unit");
price_t per_cash = total_funds * weight_unit; // 每单位权重资金 price_t per_cash = total_funds * weight_unit; // 每单位权重资金
sw_iter = sw_list.rbegin(); size_t can_run_count = 0;
for (; sw_iter != end_iter; ++sw_iter) { for (auto sw_iter = new_sw_list.begin(), end_iter = new_sw_list.end(); sw_iter != end_iter;
++sw_iter) {
// 该系统期望分配的资金 // 该系统期望分配的资金
price_t will_cash = roundUp(per_cash * (sw_iter->getWeight() / weight_unit), precision); price_t will_cash = roundUp(per_cash * (sw_iter->getWeight() / weight_unit), precision);
if (will_cash <= 0.0) { if (will_cash <= 0.0) {
@ -466,12 +467,24 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date, const Syst
// 计算实际可分配的资金 // 计算实际可分配的资金
price_t need_cash = will_cash <= can_allocate_cash ? will_cash : can_allocate_cash; price_t need_cash = will_cash <= can_allocate_cash ? will_cash : can_allocate_cash;
// 检测是否可以发生交易,不能的话,忽略
auto krecord = sw_iter->getSYS()->getTO().getKRecord(date);
if (krecord.closePrice < need_cash) {
continue;
}
// 尝试从总账户中取出资金存入子账户 // 尝试从总账户中取出资金存入子账户
TMPtr sub_tm = sw_iter->getSYS()->getTM(); TMPtr sub_tm = sw_iter->getSYS()->getTM();
if (m_shadow_tm->checkout(date, need_cash)) { if (m_shadow_tm->checkout(date, need_cash)) {
sub_tm->checkin(date, need_cash); sub_tm->checkin(date, need_cash);
can_allocate_cash = roundDown(can_allocate_cash - need_cash, precision); can_allocate_cash = roundDown(can_allocate_cash - need_cash, precision);
if (can_allocate_cash <= 0.0) { if (can_allocate_cash <= 0.0) {
continue;
}
// 如果超出允许运行的最大系统数,跳出循环
can_run_count++;
if (can_run_count > max_num) {
break; break;
} }
} }