From 60927dda8b076af23d3baefe9a14cb1f162efe26 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Fri, 9 Feb 2018 01:05:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=B5=84=E4=BA=A7=E5=88=86?= =?UTF-8?q?=E9=85=8D=E7=AE=97=E6=B3=95=E5=8F=8A=E8=B5=84=E4=BA=A7=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=A1=86=E6=9E=B6=EF=BC=88=E7=BB=A7=E7=BB=AD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../allocatefunds/AllocateFundsBase.cpp | 86 ++++++++++++++++- .../allocatefunds/AllocateFundsBase.h | 85 ++++++++++++++++- .../trade_sys/allocatefunds/SystemWeight.cpp | 22 ++++- .../trade_sys/allocatefunds/SystemWeight.h | 31 ++----- .../hikyuu/trade_sys/allocatefunds/build_in.h | 2 +- .../allocatefunds/crt/AF_EqualWeight.h | 21 +++++ .../imp/EqualWeightAllocateFunds.cpp | 53 +++++++++++ .../imp/EqualWeightAllocateFunds.h | 25 +++++ libs/hikyuu/trade_sys/portfolio/Portfolio.cpp | 29 +++--- libs/hikyuu/trade_sys/portfolio/Portfolio.h | 3 +- .../trade_sys/portfolio/crt/PF_Simple.h | 8 +- .../trade_sys/portfolio/imp/PF_Simple.cpp | 9 +- .../trade_sys/selector/imp/FixedSelector.h | 2 +- .../trade_sys/_AllocateFunds.cpp | 92 +++++++++++++++++++ libs/hikyuu_python/trade_sys/_Portfolio.cpp | 5 +- .../trade_sys/trade_sys_main.cpp | 2 + tools/hikyuu/interactive/interactive.py | 1 + tools/hikyuu/trade_sys/__init__.py | 3 +- tools/hikyuu/trade_sys/allocatefunds.py | 43 +++++++++ 19 files changed, 462 insertions(+), 60 deletions(-) create mode 100644 libs/hikyuu/trade_sys/allocatefunds/crt/AF_EqualWeight.h create mode 100644 libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.cpp create mode 100644 libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.h create mode 100644 libs/hikyuu_python/trade_sys/_AllocateFunds.cpp create mode 100644 tools/hikyuu/trade_sys/allocatefunds.py diff --git a/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp b/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp index 0b2188f0..f3f3a7bc 100644 --- a/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp +++ b/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp @@ -5,17 +5,35 @@ * Author: fasiondog */ +#include #include "AllocateFundsBase.h" namespace hku { -AllocateFundsBase::AllocateFundsBase(): m_name("AllocateMoneyBase") { +HKU_API std::ostream & operator<<(std::ostream& os, const AllocateFundsBase& af) { + os << "AllocateFunds(" << af.name() << ", " << af.getParameter() << ")"; + return os; +} +HKU_API std::ostream & operator<<(std::ostream& os, const AFPtr& af) { + if (af) { + os << *af; + } else { + os << "AllocateFunds(NULL)"; + } + + return os; +} + +AllocateFundsBase::AllocateFundsBase(): m_name("AllocateMoneyBase") { + setParam("adjust_hold_sys", false); //是否调整之前已经持仓策略的持仓 + setParam("max_sys_num", 10); //最大系统实例数 } AllocateFundsBase::AllocateFundsBase(const string& name) : m_name("AllocateMoneyBase") { - + setParam("adjust_hold_sys", false); + setParam("max_sys_num", 10); //最大系统实例数 } AllocateFundsBase::~AllocateFundsBase() { @@ -43,9 +61,71 @@ AFPtr AllocateFundsBase::clone() { } SystemList AllocateFundsBase -::getAllocateWeight(const SystemList& se_list, const SystemList& hold_list) { +::getAllocateSystem(const Datetime& date, + const SystemList& se_list, const SystemList& hold_list) { SystemList result; + + SystemWeightList sw_list = allocateWeight(se_list, hold_list); + + /*sort(sw_list.begin(), sw_list.end(), + boost::bind(std::less(), + boost::bind(&SystemWeight::weight, _1), + boost::bind(&SystemWeight::weight, _2))); + */ + price_t total_weight = 0.0; + + auto sw_iter = sw_list.begin(); + for (; sw_iter != sw_list.end(); ++sw_iter) { + total_weight += sw_iter->weight; + } + + if (total_weight == 0.0) { + return result; + } + + int precision = m_tm->getParam("precision"); + price_t per_cash = roundDown(m_tm->currentCash() / total_weight, precision); + + sw_iter = sw_list.begin(); + for (; sw_iter != sw_list.end(); ++sw_iter) { + price_t will_cash = roundDown(per_cash * sw_iter->weight, precision); + if (will_cash == 0.0) { + continue; + } + + TMPtr tm = sw_iter->sys->getTM(); + precision = tm->getParam("precision"); + will_cash = roundDown(will_cash, precision); + if (tm->currentCash() < will_cash) { + price_t cash = will_cash - tm->currentCash(); + tm->checkin(date, cash); + m_tm->checkout(date, cash); + } + + result.push_back(sw_iter->sys); + } + return result; } +SystemWeightList AllocateFundsBase +::allocateWeight(const SystemList& se_list, const SystemList& hold_list) { + SystemWeightList result; + + result = _allocateWeight(se_list, hold_list); + + size_t total = result.size(); + if (total <= getParam("max_sys_num")) { + return result; + } + + auto iter = result.begin() + total; + SystemWeightList sw_list; + copy(result.begin(), iter, sw_list.begin()); + swap(result, sw_list); + + return result; +} + + } /* namespace hku */ diff --git a/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h b/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h index a4e36238..e429d4db 100644 --- a/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h +++ b/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.h @@ -27,6 +27,31 @@ public: AllocateFundsBase(const string& name); virtual ~AllocateFundsBase(); + string name() const; + void name(const string& name); + + /** + * Portfolio获取实际获得资产分配的系统策略实例 + * @param date 指定日期 + * @param se_list 系统实例选择器选出的系统实例 + * @param hold_list 当前持仓的系统实例 + * @return + */ + SystemList getAllocateSystem(const Datetime& date, + const SystemList& se_list, + const SystemList& hold_list); + + /** + * 获取实际分配资产的系统实例及其权重 + * @details 实际调用子类接口 _allocateWeight,并根据允许的最大持仓系统数参数对子类返回的 + * 系统实例及权重列表进行了截断处理 + * @param se_list + * @param hold_list + * @return + */ + SystemWeightList allocateWeight(const SystemList& se_list, + const SystemList& hold_list); + /** 获取交易账户 */ TMPtr getTM(); @@ -44,9 +69,16 @@ public: virtual void _reset() {} /** 子类克隆私有变量接口 */ - virtual AFPtr _clone() {return AFPtr(); } + virtual AFPtr _clone() = 0; - SystemList getAllocateWeight(const SystemList&, const SystemList&); + /** + * 子类分配权重接口 + * @param se_list 选择器选出的系统 实例列表 + * @param hold_list 当前持仓的系统实例列表 + * @return + */ + virtual SystemWeightList _allocateWeight(const SystemList& se_list, + const SystemList& hold_list) = 0; private: string m_name; @@ -77,9 +109,58 @@ private: #endif /* HKU_SUPPORT_SERIALIZATION */ }; +#if HKU_SUPPORT_SERIALIZATION +BOOST_SERIALIZATION_ASSUME_ABSTRACT(SelectorBase) +#endif + +#if HKU_SUPPORT_SERIALIZATION +/** + * 对于没有私有变量的继承子类,可直接使用该宏定义序列化 + * @code + * class Drived: public AllocateFundsBase { + * ALLOCATEFUNDS_NO_PRIVATE_MEMBER_SERIALIZATION + * + * public: + * Drived(); + * ... + * }; + * @endcode + * @ingroup Selector + */ +#define ALLOCATEFUNDS_NO_PRIVATE_MEMBER_SERIALIZATION private:\ + friend class boost::serialization::access; \ + template \ + void serialize(Archive & ar, const unsigned int version) { \ + ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(AllocateFundsBase); \ + } +#else +#define ALLOCATEFUNDS_NO_PRIVATE_MEMBER_SERIALIZATION +#endif + +#define ALLOCATEFUNDS_IMP(classname) public:\ + virtual AFPtr _clone() {\ + return AFPtr(new classname());\ + }\ + virtual SystemWeightList _allocateWeight(const SystemList&, const SystemList&); + + typedef shared_ptr AllocateFundsPtr; typedef shared_ptr AFPtr; + +HKU_API std::ostream & operator<<(std::ostream&, const AllocateFundsBase&); +HKU_API std::ostream & operator<<(std::ostream&, const AFPtr&); + + +inline string AllocateFundsBase::name() const { + return m_name; +} + +inline void AllocateFundsBase::name(const string& name) { + m_name = name; +} + + inline TMPtr AllocateFundsBase::getTM() { return m_tm; } diff --git a/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.cpp b/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.cpp index f0a84a67..4d414a20 100644 --- a/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.cpp +++ b/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.cpp @@ -9,12 +9,30 @@ namespace hku { -SystemWeight::SystemWeight(): m_weight(100) { +HKU_API std::ostream & operator<<(std::ostream & os, const SystemWeight& sw) { + os << std::fixed; + os.precision(4); + + string name("NULL"); + if (sw.sys) { + name = sw.sys->name(); + } + + os << "SystemWeight(sys: " << name + << ", weight: " << sw.weight + << ")"<< std::endl; + + os.unsetf(std::ostream::floatfield); + os.precision(); + return os; +} + +SystemWeight::SystemWeight(): weight(100) { } SystemWeight::SystemWeight(const SystemPtr& sys, price_t weight) -: m_sys(sys), m_weight(weight) { +: sys(sys), weight(weight) { } diff --git a/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.h b/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.h index c0dcca6d..929d0bba 100644 --- a/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.h +++ b/libs/hikyuu/trade_sys/allocatefunds/SystemWeight.h @@ -24,9 +24,11 @@ public: void setWeight(price_t weight); price_t getWeight() const; +public: + SystemPtr sys; + price_t weight; + private: - SystemPtr m_sys; - price_t m_weight; //============================================ // 序列化支持 @@ -36,14 +38,14 @@ private: friend class boost::serialization::access; template void save(Archive & ar, const unsigned int version) const { - ar & BOOST_SERIALIZATION_NVP(m_sys); - ar & BOOST_SERIALIZATION_NVP(m_weight); + ar & BOOST_SERIALIZATION_NVP(sys); + ar & BOOST_SERIALIZATION_NVP(weight); } template void load(Archive & ar, const unsigned int version) { - ar & BOOST_SERIALIZATION_NVP(m_sys); - ar & BOOST_SERIALIZATION_NVP(m_weight); + ar & BOOST_SERIALIZATION_NVP(sys); + ar & BOOST_SERIALIZATION_NVP(weight); } BOOST_SERIALIZATION_SPLIT_MEMBER() @@ -52,22 +54,7 @@ private: typedef vector SystemWeightList; -inline void SystemWeight::setSYS(const SystemPtr& sys) { - m_sys = sys; -} - -inline SystemPtr SystemWeight::getSYS() const { - return m_sys; -} - -inline void SystemWeight::setWeight(price_t weight) { - m_weight = weight; -} - -inline price_t SystemWeight::getWeight() const { - return m_weight; -} - +HKU_API std::ostream & operator<<(std::ostream &, const SystemWeight&); } /* namespace hku */ diff --git a/libs/hikyuu/trade_sys/allocatefunds/build_in.h b/libs/hikyuu/trade_sys/allocatefunds/build_in.h index a91ac843..2bc45267 100644 --- a/libs/hikyuu/trade_sys/allocatefunds/build_in.h +++ b/libs/hikyuu/trade_sys/allocatefunds/build_in.h @@ -8,7 +8,7 @@ #ifndef TRADE_SYS_ALLOCATEFUNDS_BUILD_IN_H_ #define TRADE_SYS_ALLOCATEFUNDS_BUILD_IN_H_ - +#include "crt/AF_EqualWeight.h" #endif /* TRADE_SYS_ALLOCATEFUNDS_BUILD_IN_H_ */ diff --git a/libs/hikyuu/trade_sys/allocatefunds/crt/AF_EqualWeight.h b/libs/hikyuu/trade_sys/allocatefunds/crt/AF_EqualWeight.h new file mode 100644 index 00000000..0c19c32f --- /dev/null +++ b/libs/hikyuu/trade_sys/allocatefunds/crt/AF_EqualWeight.h @@ -0,0 +1,21 @@ +/* + * AF_EqualWeight.h + * + * Created on: 2018年2月8日 + * Author: fasiondog + */ + +#ifndef TRADE_SYS_ALLOCATEFUNDS_CRT_AF_EQUALWEIGHT_H_ +#define TRADE_SYS_ALLOCATEFUNDS_CRT_AF_EQUALWEIGHT_H_ + +#include "../AllocateFundsBase.h" + +namespace hku { + +AFPtr HKU_API AF_EqualWeight(); + +} /* namespace hku */ + + + +#endif /* TRADE_SYS_ALLOCATEFUNDS_CRT_AF_EQUALWEIGHT_H_ */ diff --git a/libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.cpp b/libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.cpp new file mode 100644 index 00000000..2d6d4ff2 --- /dev/null +++ b/libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.cpp @@ -0,0 +1,53 @@ +/* + * EqualWeightAllocateFunds.cpp + * + * Created on: 2018年2月8日 + * Author: fasiondog + */ + +#include "EqualWeightAllocateFunds.h" + +namespace hku { + +EqualWeightAllocateFunds::EqualWeightAllocateFunds() +:AllocateFundsBase("AF_EqualWeight") { + +} + +EqualWeightAllocateFunds::~EqualWeightAllocateFunds() { + +} + +SystemWeightList EqualWeightAllocateFunds +::_allocateWeight(const SystemList& se_list, const SystemList& hold_list) { + SystemWeightList result; + + SystemList temp_list; + if (getParam("adjust_hold_sys")) { + copy(se_list.begin(), se_list.end(), temp_list.begin()); + copy(hold_list.begin(), hold_list.end(), temp_list.end()); + } else { + copy(se_list.begin(), se_list.end(), temp_list.begin()); + } + + size_t total = temp_list.size(); + if (total == 0) { + return result; + } + + price_t weight = 100.0 / total; + for (auto iter = temp_list.begin(); iter != temp_list.end(); ++iter) { + SystemWeight sw; + sw.sys = *iter; + sw.weight = weight; + result.push_back(sw); + } + + return result; +} + +AFPtr HKU_API AF_EqualWeight() { + return make_shared(); +} + +} /* namespace hku */ diff --git a/libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.h b/libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.h new file mode 100644 index 00000000..58d98c6d --- /dev/null +++ b/libs/hikyuu/trade_sys/allocatefunds/imp/EqualWeightAllocateFunds.h @@ -0,0 +1,25 @@ +/* + * EqualWeightAllocateFunds.h + * + * Created on: 2018年2月8日 + * Author: fasiondog + */ + +#ifndef TRADE_SYS_ALLOCATEFUNDS_IMP_EQUALWEIGHTALLOCATEFUNDS_H_ +#define TRADE_SYS_ALLOCATEFUNDS_IMP_EQUALWEIGHTALLOCATEFUNDS_H_ + +#include "../AllocateFundsBase.h" + +namespace hku { + +class EqualWeightAllocateFunds: public AllocateFundsBase { + ALLOCATEFUNDS_IMP(EqualWeightAllocateFunds) + +public: + EqualWeightAllocateFunds(); + virtual ~EqualWeightAllocateFunds(); +}; + +} /* namespace hku */ + +#endif /* TRADE_SYS_ALLOCATEFUNDS_IMP_EQUALWEIGHTALLOCATEFUNDS_H_ */ diff --git a/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp b/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp index 0702c607..e4da770e 100644 --- a/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp +++ b/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp @@ -37,8 +37,9 @@ Portfolio::Portfolio(const string& name) : m_name(name) { Portfolio::Portfolio(const TradeManagerPtr& tm, const SystemPtr& sys, - const SelectorPtr& se) -: m_se(se), m_tm(tm), m_name("Portfolio") { + const SelectorPtr& se, + const AFPtr& af) +: m_name("Portfolio"), m_se(se), m_tm(tm), m_af(af) { initParam(); } @@ -330,6 +331,13 @@ void Portfolio::run(const KQuery& query) { } else { cur_hold_sys_sets.erase(*sys_iter); } + + //同步交易记录 + TradeRecordList tr_list = (*sys_iter)->getTM()->getTradeList(*date_iter, Null()); + auto tr_iter = tr_list.begin(); + for (; tr_iter != tr_list.end(); ++tr_iter) { + m_tm->addTradeRecord(*tr_iter); + } } SystemList cur_hold_sys_list; @@ -340,22 +348,7 @@ void Portfolio::run(const KQuery& query) { //计算当前时刻选择的系统实例 SystemList selected_list = m_se->getSelectedSystemList(*date_iter); - SystemList sw_list = m_af->getAllocateWeight(selected_list, cur_hold_sys_list); - - //资金分配都由AF自动处理好? 需要调仓卖出的,都由AF处理?这里只处理调仓完的系统列表? - /*auto sw_iter = sw_map.begin(); - for (; sw_iter != sw_map.end(); ++sw_iter) { - if (sw_iter->second == 0.0) { - SYSPtr sys = sw_iter->first; - TMPtr tm = sys->getTM(); - PositionRecordList pos_list = tm->getPositionList(); - auto pos_iter = pos_list.begin(); - for (; pos_iter != pos_list.end(); ++pos_iter) { - KRecord kr = pos_iter->stock.getKRecordByDate(*date_iter, query.kType()); - sys->_sell(kr, PART_ALLOCATEFUNDS); - } - } - }*/ + SystemList sw_list = m_af->getAllocateSystem(*date_iter, selected_list, cur_hold_sys_list); auto sw_iter = sw_list.begin(); for (; sw_iter != sw_list.end(); ++sw_iter) { diff --git a/libs/hikyuu/trade_sys/portfolio/Portfolio.h b/libs/hikyuu/trade_sys/portfolio/Portfolio.h index 6a1789a8..3974d87e 100644 --- a/libs/hikyuu/trade_sys/portfolio/Portfolio.h +++ b/libs/hikyuu/trade_sys/portfolio/Portfolio.h @@ -41,7 +41,8 @@ public: Portfolio(const string& name); Portfolio(const TradeManagerPtr& tm, const SystemPtr& sys, - const SelectorPtr& st); + const SelectorPtr& st, + const AFPtr& af); virtual ~Portfolio(); string name() const { return m_name; } diff --git a/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h b/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h index a108877e..19d6a336 100644 --- a/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h +++ b/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h @@ -11,13 +11,15 @@ #include "../Portfolio.h" #include "../../system/crt/SYS_Simple.h" #include "../../selector/crt/SE_Fixed.h" +#include "../../allocatefunds/crt/AF_EqualWeight.h" namespace hku { PortfolioPtr HKU_API PF_Simple( - const TradeManagerPtr& tm = TradeManagerPtr(), - const SystemPtr& sys = SYS_Simple(), - const SelectorPtr& st = SE_Fixed()); + const TMPtr& tm = TradeManagerPtr(), + const SYSPtr& sys = SYS_Simple(), + const SEPtr& st = SE_Fixed(), + const AFPtr& af = AF_EqualWeight()); } /* namespace hku */ diff --git a/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp b/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp index 3fe428fc..71867c6e 100644 --- a/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp +++ b/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp @@ -10,10 +10,11 @@ namespace hku { PortfolioPtr HKU_API PF_Simple( - const TradeManagerPtr& tm, - const SystemPtr& sys, - const SelectorPtr& st) { - return make_shared(tm, sys, st); + const TMPtr& tm, + const SYSPtr& sys, + const SEPtr& st, + const AFPtr& af) { + return make_shared(tm, sys, st, af); } } /* namespace hku */ diff --git a/libs/hikyuu/trade_sys/selector/imp/FixedSelector.h b/libs/hikyuu/trade_sys/selector/imp/FixedSelector.h index a1c33dc0..9dbc6034 100644 --- a/libs/hikyuu/trade_sys/selector/imp/FixedSelector.h +++ b/libs/hikyuu/trade_sys/selector/imp/FixedSelector.h @@ -13,7 +13,7 @@ namespace hku { class FixedSelector: public SelectorBase { - SELECTOR_IMP(FixedSelector); + SELECTOR_IMP(FixedSelector) SELECTOR_NO_PRIVATE_MEMBER_SERIALIZATION public: diff --git a/libs/hikyuu_python/trade_sys/_AllocateFunds.cpp b/libs/hikyuu_python/trade_sys/_AllocateFunds.cpp new file mode 100644 index 00000000..f6d5cea0 --- /dev/null +++ b/libs/hikyuu_python/trade_sys/_AllocateFunds.cpp @@ -0,0 +1,92 @@ +/* + * _Selector.cpp + * + * Created on: 2016年3月28日 + * Author: fasiondog + */ + +#include +#include +#include "../_Parameter.h" +#include "../pickle_support.h" + +using namespace boost::python; +using namespace hku; + +class AllocateFundsBaseWrap : public AllocateFundsBase, public wrapper { +public: + AllocateFundsBaseWrap(): AllocateFundsBase() {} + AllocateFundsBaseWrap(const string& name): AllocateFundsBase(name) {} + virtual ~AllocateFundsBaseWrap() {} + + void _reset() { + if (override func = this->get_override("_reset")) { + func(); + } else { + AllocateFundsBase::_reset(); + } + } + + void default_reset() { + this->AllocateFundsBase::_reset(); + } + + SystemWeightList _allocateWeight(const SystemList& se_list, + const SystemList& hold_list) { + return this->get_override("_allocateWeight")(se_list, hold_list); + } + + AFPtr _clone() { + return this->get_override("_clone")(); + } +}; + +string (AllocateFundsBase::*af_get_name)() const = &AllocateFundsBase::name; +void (AllocateFundsBase::*af_set_name)(const string&) = &AllocateFundsBase::name; + +void export_AllocateFunds() { + class_("SystemWeight", init<>()) + .def(init()) + .def(self_ns::str(self)) + .def_readwrite("sys", &SystemWeight::sys) + .def_readwrite("weight", &SystemWeight::weight) +#if HKU_PYTHON_SUPPORT_PICKLE + .def_pickle(normal_pickle_suite()) +#endif + ; + + SystemWeightList::const_reference (SystemWeightList::*SystemWeightList_at)(SystemWeightList::size_type) const = &SystemWeightList::at; + void (SystemWeightList::*append)(const SystemWeight&) = &SystemWeightList::push_back; + class_("SystemWeightList") + .def("__iter__", iterator()) + .def("size", &SystemWeightList::size) + .def("__len__", &SystemWeightList::size) + .def("__getitem__", SystemWeightList_at, return_value_policy()) + .def("append", append) + ; + + + class_("AllocateFundsBase", init<>()) + .def(init()) + .def(self_ns::str(self)) + .add_property("name", af_get_name, af_set_name) + .def("getParam", &AllocateFundsBase::getParam) + .def("setParam", &AllocateFundsBase::setParam) + + .def("reset", &AllocateFundsBase::reset) + .def("clone", &AllocateFundsBase::clone) + .def("_reset", &AllocateFundsBase::_reset, &AllocateFundsBaseWrap::default_reset) + .def("_clone", pure_virtual(&AllocateFundsBase::_clone)) + .def("_allocateWeight", pure_virtual(&AllocateFundsBase::_allocateWeight)) +#if HKU_PYTHON_SUPPORT_PICKLE + .def_pickle(name_init_pickle_suite()) +#endif + ; + + register_ptr_to_python(); + + def("AF_EqualWeight", AF_EqualWeight); + +} + + diff --git a/libs/hikyuu_python/trade_sys/_Portfolio.cpp b/libs/hikyuu_python/trade_sys/_Portfolio.cpp index 399cab1b..42746c69 100644 --- a/libs/hikyuu_python/trade_sys/_Portfolio.cpp +++ b/libs/hikyuu_python/trade_sys/_Portfolio.cpp @@ -13,7 +13,7 @@ using namespace boost::python; using namespace hku; -BOOST_PYTHON_FUNCTION_OVERLOADS(PF_Simple_overload, PF_Simple, 0, 3); +BOOST_PYTHON_FUNCTION_OVERLOADS(PF_Simple_overload, PF_Simple, 0, 4); void (Portfolio::*pf_set_name)(const string&) = &Portfolio::name; string (Portfolio::*pf_get_name)() const= &Portfolio::name; @@ -24,7 +24,8 @@ void export_Portfolio() { .def(init()) .def(init()) + const SelectorPtr&, + const AFPtr&>()) .def(self_ns::str(self)) .def("getParam", &Portfolio::getParam) .def("setParam", &Portfolio::setParam) diff --git a/libs/hikyuu_python/trade_sys/trade_sys_main.cpp b/libs/hikyuu_python/trade_sys/trade_sys_main.cpp index 033c7390..01685ae8 100644 --- a/libs/hikyuu_python/trade_sys/trade_sys_main.cpp +++ b/libs/hikyuu_python/trade_sys/trade_sys_main.cpp @@ -19,6 +19,7 @@ void export_Slippage(); void export_System(); void export_Selector(); void export_Portfolio(); +void export_AllocateFunds(); BOOST_PYTHON_MODULE(_trade_sys) { docstring_options doc_options(false); @@ -31,6 +32,7 @@ BOOST_PYTHON_MODULE(_trade_sys) { export_Slippage(); export_System(); export_Selector(); + export_AllocateFunds(); export_Portfolio(); } diff --git a/tools/hikyuu/interactive/interactive.py b/tools/hikyuu/interactive/interactive.py index 2026e4ab..dd4b391b 100644 --- a/tools/hikyuu/interactive/interactive.py +++ b/tools/hikyuu/interactive/interactive.py @@ -47,6 +47,7 @@ from hikyuu.trade_sys.stoploss import * from hikyuu.trade_sys.profitgoal import * from hikyuu.trade_sys.slippage import * from hikyuu.trade_sys.selector import * +from hikyuu.trade_sys.allocatefunds import * from hikyuu.trade_sys.portfolio import * from hikyuu.interactive import * diff --git a/tools/hikyuu/trade_sys/__init__.py b/tools/hikyuu/trade_sys/__init__.py index 9dde8385..fd64cee6 100644 --- a/tools/hikyuu/trade_sys/__init__.py +++ b/tools/hikyuu/trade_sys/__init__.py @@ -26,5 +26,6 @@ __all__ = ['system', 'environment', 'condition', 'moneymanager', 'signal', - 'stoploss', 'profitgoal', 'slippage', 'selector', 'portfolio'] + 'stoploss', 'profitgoal', 'slippage', 'selector', 'portfolio', + 'allocatefunds'] diff --git a/tools/hikyuu/trade_sys/allocatefunds.py b/tools/hikyuu/trade_sys/allocatefunds.py new file mode 100644 index 00000000..9b1fb8a8 --- /dev/null +++ b/tools/hikyuu/trade_sys/allocatefunds.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# -*- coding: utf8 -*- +# cp936 +# +# The MIT License (MIT) +# +# Copyright (c) 2010-2017 fasiondog +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +from ._trade_sys import (AllocateFundsBase, AF_EqualWeight) +from hikyuu.util.unicode import (unicodeFunc, reprFunc) + +AllocateFundsBase.__unicode__ = unicodeFunc +AllocateFundsBase.__repr__ = reprFunc + + +#------------------------------------------------------------------ +# add doc-string +#------------------------------------------------------------------ + + + +#------------------------------------------------------------------ +# add doc-string for build_in func +#------------------------------------------------------------------