From 202dc2a7c05073e2cfb85d3b38aeb891943e6799 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 10 Aug 2020 00:27:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B3=A8=E9=87=8A=E5=B8=AE?= =?UTF-8?q?=E5=8A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hikyuu_cpp/hikyuu/doc.h | 2 +- .../allocatefunds/AllocateFundsBase.cpp | 25 +++++++++---------- .../allocatefunds/AllocateFundsBase.h | 23 ++++++++++++++++- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/hikyuu_cpp/hikyuu/doc.h b/hikyuu_cpp/hikyuu/doc.h index 2d34ac79..7c474ce7 100644 --- a/hikyuu_cpp/hikyuu/doc.h +++ b/hikyuu_cpp/hikyuu/doc.h @@ -89,7 +89,7 @@ * @details 选择交易对象 * @ingroup Portfolio * - * @defgroup AllocateMoney AllocateMoney 资金分配算法模块 + * @defgroup AllocateFunds Allocate Funds 资产分配算法模块 * @details 进行资金分配 * @ingroup Portfolio * diff --git a/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp b/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp index d9c635ef..8cac1658 100644 --- a/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp +++ b/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp @@ -262,7 +262,7 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date, const Syst return; } - //如果持仓的系统数已大于等于最大持仓系统数,直接输出已持仓系统列表,并返回 + //如果运行中的系统数已大于等于允许的最大系统数,直接返回 int max_num = getParam("max_sys_num"); if (running_list.size() >= max_num) { return; @@ -281,7 +281,7 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date, const Syst } } - //获取权重 + //获取计划分配的资金权重 SystemWeightList sw_list = _allocateWeight(date, pure_se_list); if (sw_list.size() == 0) { return; @@ -292,26 +292,26 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date, const Syst boost::bind(std::less(), boost::bind(&SystemWeight::m_weight, _1), boost::bind(&SystemWeight::m_weight, _2))); - //倒序遍历,计算总权重,并在遇到权重为0或等于最大持仓时结束遍历 - size_t remain = max_num - running_list.size(); + //倒序遍历,计算总权重,并在遇到权重为0或等于运行的最大运行时系统数时结束遍历 price_t total_weight = 0.0; + size_t count = 0; auto sw_iter = sw_list.rbegin(); - for (size_t count = 0; sw_iter != sw_list.rend(); ++sw_iter, count++) { - if (sw_iter->getWeight() <= 0.0 || count >= remain) + for (; sw_iter != sw_list.rend(); ++sw_iter) { + if (sw_iter->getWeight() <= 0.0 || count >= max_num) break; total_weight += sw_iter->getWeight(); + count++; } if (total_weight <= 0.0) { return; } - auto end_iter = sw_iter; + auto end_iter = sw_iter; // 记录结束位置 - int precision = m_tm->getParam("precision"); - price_t per_cash = 0.0; - - per_cash = m_tm->currentCash() / total_weight; // 每单位权重资金 + // 再次遍历选中子系统列表,并将剩余现金按权重比例转入子账户 + int precision = m_tm->getParam("precision"); // 总账号资金精度 + price_t per_cash = m_tm->currentCash() / total_weight; // 每单位权重资金 sw_iter = sw_list.rbegin(); for (; sw_iter != end_iter; ++sw_iter) { // 该系统期望分配的资金 @@ -321,8 +321,7 @@ void AllocateFundsBase::_adjust_without_running(const Datetime& date, const Syst } // 尝试从总账户中取出资金存入子账户 - SYSPtr sub_sys = sw_iter->getSYS(); - TMPtr sub_tm = sub_sys->getTM(); + TMPtr sub_tm = sw_iter->getSYS()->getTM(); if (m_tm->checkout(date, will_cash)) { sub_tm->checkin(date, will_cash); } diff --git a/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h b/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h index 271e9955..2601895b 100644 --- a/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h +++ b/hikyuu_cpp/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h @@ -20,15 +20,30 @@ namespace hku { +/** + * 资产分配算法 + * @ingroup AllocateFunds + */ class HKU_API AllocateFundsBase : public enable_shared_from_this { PARAMETER_SUPPORT public: + /** 默认构造函数 */ AllocateFundsBase(); + + /** + * 构造函数 + * @param name 算法名称 + */ AllocateFundsBase(const string& name); + + /** 析构函数 */ virtual ~AllocateFundsBase(); + /** 获取算法名称 */ string name() const; + + /** 修改算法名称 */ void name(const string& name); /** @@ -47,7 +62,10 @@ public: /** 设定交易账户 */ void setTM(const TMPtr&); + /** 获取关联查询条件 */ KQuery getQuery(); + + /** 设置查询条件 */ void setQuery(KQuery query); /** 获取不参与资产分配的保留比例 */ @@ -80,11 +98,14 @@ public: * @param se_list 系统实例选择器选出的系统实例 * @return */ - virtual SystemWeightList _allocateWeight(const Datetime& date, const SystemList& se_list) = 0; +private: + /* 同时调整已运行中的子系统(已分配资金或已持仓) */ void _adjust_with_running(const Datetime& date, const SystemList& se_list, const std::list& running_list); + + /* 仅适用剩余资金在选中的子系统中分配资金 */ void _adjust_without_running(const Datetime& date, const SystemList& se_list, const std::list& running_list);