From 3da11e708bf084af46c31d1866007a1760431f2f Mon Sep 17 00:00:00 2001 From: fasiondog Date: Wed, 27 Dec 2023 18:13:05 +0800 Subject: [PATCH] trade_sys finish --- .../hikyuu/trade_manage/OrderBrokerBase.h | 4 +- .../hikyuu/trade_sys/signal/SignalBase.h | 4 +- .../hikyuu/trade_sys/slippage/SlippageBase.h | 4 +- .../hikyuu/trade_sys/stoploss/StoplossBase.h | 4 +- hikyuu_pywrap/trade_manage/_OrderBroker.cpp | 3 +- hikyuu_pywrap/trade_manage/_TradeManager.cpp | 10 +- hikyuu_pywrap/trade_sys/_Selector.cpp | 136 ++++++---------- hikyuu_pywrap/trade_sys/_Signal.cpp | 102 +++++------- hikyuu_pywrap/trade_sys/_Slippage.cpp | 90 +++++------ hikyuu_pywrap/trade_sys/_Stoploss.cpp | 108 +++++-------- hikyuu_pywrap/trade_sys/_System.cpp | 150 ++++++++---------- hikyuu_pywrap/trade_sys/trade_sys_main.cpp | 20 +-- hikyuu_pywrap/xmake.lua | 10 +- 13 files changed, 268 insertions(+), 377 deletions(-) diff --git a/hikyuu_cpp/hikyuu/trade_manage/OrderBrokerBase.h b/hikyuu_cpp/hikyuu/trade_manage/OrderBrokerBase.h index 0cace9b5..2c2055b6 100644 --- a/hikyuu_cpp/hikyuu/trade_manage/OrderBrokerBase.h +++ b/hikyuu_cpp/hikyuu/trade_manage/OrderBrokerBase.h @@ -37,7 +37,7 @@ public: virtual ~OrderBrokerBase(); /** 获取名称 */ - string name() const; + const string& name() const; /** 设置名称 */ void name(const string& name); @@ -106,7 +106,7 @@ HKU_API std::ostream& operator<<(std::ostream& os, const OrderBrokerBase&); /** @ingroup OrderBroker */ HKU_API std::ostream& operator<<(std::ostream& os, const OrderBrokerPtr&); -inline string OrderBrokerBase::name() const { +inline const string& OrderBrokerBase::name() const { return m_name; } diff --git a/hikyuu_cpp/hikyuu/trade_sys/signal/SignalBase.h b/hikyuu_cpp/hikyuu/trade_sys/signal/SignalBase.h index ff781781..f1c74670 100644 --- a/hikyuu_cpp/hikyuu/trade_sys/signal/SignalBase.h +++ b/hikyuu_cpp/hikyuu/trade_sys/signal/SignalBase.h @@ -98,7 +98,7 @@ public: SignalPtr clone(); /** 获取名称 */ - string name() const; + const string& name() const; /** 设置名称 */ void name(const string& name); @@ -208,7 +208,7 @@ inline KData SignalBase::getTO() const { return m_kdata; } -inline string SignalBase::name() const { +inline const string& SignalBase::name() const { return m_name; } diff --git a/hikyuu_cpp/hikyuu/trade_sys/slippage/SlippageBase.h b/hikyuu_cpp/hikyuu/trade_sys/slippage/SlippageBase.h index 2a3089e4..2963fb72 100644 --- a/hikyuu_cpp/hikyuu/trade_sys/slippage/SlippageBase.h +++ b/hikyuu_cpp/hikyuu/trade_sys/slippage/SlippageBase.h @@ -39,7 +39,7 @@ public: KData getTO() const; /** 获取名称 */ - string name() const; + const string& name() const; /** 设置名称 */ void name(const string& name); @@ -150,7 +150,7 @@ typedef shared_ptr SPPtr; HKU_API std::ostream& operator<<(std::ostream&, const SlippageBase&); HKU_API std::ostream& operator<<(std::ostream&, const SlippagePtr&); -inline string SlippageBase::name() const { +inline const string& SlippageBase::name() const { return m_name; } diff --git a/hikyuu_cpp/hikyuu/trade_sys/stoploss/StoplossBase.h b/hikyuu_cpp/hikyuu/trade_sys/stoploss/StoplossBase.h index d721bc01..064dd797 100644 --- a/hikyuu_cpp/hikyuu/trade_sys/stoploss/StoplossBase.h +++ b/hikyuu_cpp/hikyuu/trade_sys/stoploss/StoplossBase.h @@ -35,7 +35,7 @@ public: virtual ~StoplossBase(); /** 获取名称 */ - string name() const; + const string& name() const; /** 设置名称 */ void name(const string& name); @@ -171,7 +171,7 @@ typedef shared_ptr TPPtr; HKU_API std::ostream& operator<<(std::ostream& os, const StoplossBase&); HKU_API std::ostream& operator<<(std::ostream& os, const StoplossPtr&); -inline string StoplossBase::name() const { +inline const string& StoplossBase::name() const { return m_name; } diff --git a/hikyuu_pywrap/trade_manage/_OrderBroker.cpp b/hikyuu_pywrap/trade_manage/_OrderBroker.cpp index 5c54e975..e8dc994b 100644 --- a/hikyuu_pywrap/trade_manage/_OrderBroker.cpp +++ b/hikyuu_pywrap/trade_manage/_OrderBroker.cpp @@ -42,7 +42,8 @@ void export_OrderBroker(py::module& m) { .def("__repr__", to_py_str) .def_property("name", py::overload_cast(&OrderBrokerBase::name, py::const_), - py::overload_cast(&OrderBrokerBase::name), "名称(可读写)") + py::overload_cast(&OrderBrokerBase::name), + py::return_value_policy::copy, "名称(可读写)") .def("buy", &OrderBrokerBase::buy, R"(buy(self, datetime, market, code, price, num) diff --git a/hikyuu_pywrap/trade_manage/_TradeManager.cpp b/hikyuu_pywrap/trade_manage/_TradeManager.cpp index dbd7258a..1923028d 100644 --- a/hikyuu_pywrap/trade_manage/_TradeManager.cpp +++ b/hikyuu_pywrap/trade_manage/_TradeManager.cpp @@ -242,9 +242,6 @@ FundsRecord (TradeManagerBase::*getFunds_2)(const Datetime&, TradeCostPtr (TradeManagerBase::*get_costFunc)() const = &TradeManagerBase::costFunc; void (TradeManagerBase::*set_costFunc)(const TradeCostPtr&) = &TradeManagerBase::costFunc; -const string& (TradeManagerBase::*tm_get_name)() const = &TradeManagerBase::name; -void (TradeManagerBase::*tm_set_name)(const string&) = &TradeManagerBase::name; - TradeRecordList (TradeManagerBase::*_getTradeList_1)() const = &TradeManagerBase::getTradeList; TradeRecordList (TradeManagerBase::*_getTradeList_2)(const Datetime&, const Datetime&) const = &TradeManagerBase::getTradeList; @@ -269,9 +266,10 @@ void export_TradeManager(py::module& m) { .def("__str__", &TradeManagerBase::str) .def("__repr__", &TradeManagerBase::str) - // .def_property("name", make_function(tm_get_name, - // return_value_policy()), - // tm_set_name, "名称") + .def_property("name", py::overload_cast<>(&TradeManagerBase::name, py::const_), + py::overload_cast(&TradeManagerBase::name), + py::return_value_policy::copy, "名称") + .def_property_readonly("init_cash", &TradeManagerBase::initCash, "(只读)初始资金") .def_property_readonly("current_cash", &TradeManagerBase::currentCash, "(只读)当前资金") .def_property_readonly("init_datetime", &TradeManagerBase::initDatetime, diff --git a/hikyuu_pywrap/trade_sys/_Selector.cpp b/hikyuu_pywrap/trade_sys/_Selector.cpp index 003a11d7..163d9efc 100644 --- a/hikyuu_pywrap/trade_sys/_Selector.cpp +++ b/hikyuu_pywrap/trade_sys/_Selector.cpp @@ -5,95 +5,67 @@ * Author: fasiondog */ -#include #include -#include "../_Parameter.h" #include "../pybind_utils.h" -#include "../pickle_support.h" -using namespace boost::python; +namespace py = pybind11; using namespace hku; -class SelectorWrap : public SelectorBase, public wrapper { +class PySelectorBase : public SelectorBase { + PY_CLONE(PySelectorBase, SelectorBase) + public: - SelectorWrap() : SelectorBase() {} - SelectorWrap(const string& name) : SelectorBase(name) {} - virtual ~SelectorWrap() {} + using SelectorBase::SelectorBase; - void _reset() { - if (override func = this->get_override("_reset")) { - func(); - } else { - SelectorBase::_reset(); - } + void _reset() override { + PYBIND11_OVERLOAD(void, SelectorBase, _reset, ); } - void default_reset() { - this->SelectorBase::_reset(); + void _calculate() override { + PYBIND11_OVERLOAD_PURE(void, SelectorBase, _calculate, ); } - bool isMatchAF(const AFPtr& af) { - return this->get_override("is_match_af")(af); + SystemList getSelectedOnOpen(Datetime date) override { + PYBIND11_OVERLOAD_PURE_NAME(SystemList, SelectorBase, "get_selected_on_open", + getSelectedOnOpen, date); } - SystemList getSelectedOnOpen(Datetime date) { - return this->get_override("get_selected_on_open")(date); + SystemList getSelectedOnClose(Datetime date) override { + PYBIND11_OVERLOAD_PURE_NAME(SystemList, SelectorBase, "get_selected_on_close", + getSelectedOnClose, date); } - SystemList getSelectedOnClose(Datetime date) { - return this->get_override("get_selected_on_close")(date); - } - - SelectorPtr _clone() { - return this->get_override("_clone")(); - } - - void _calculate() { - this->get_override("_calculate")(); + bool isMatchAF(const AFPtr& af) override { + PYBIND11_OVERLOAD_PURE_NAME(bool, SelectorBase, "is_match_af", isMatchAF, af); } }; -const string& (SelectorBase::*se_get_name)() const = &SelectorBase::name; -void (SelectorBase::*se_set_name)(const string&) = &SelectorBase::name; - -SelectorPtr (*SE_Fixed_1)() = SE_Fixed; -SelectorPtr py_SE_Fixed(py::list stock_list, const SystemPtr& sys) { - return SE_Fixed(python_list_to_vector(stock_list), sys); -} - -SEPtr (*SE_Signal_1)() = SE_Signal; -SelectorPtr py_SE_Signal(py::list stock_list, const SystemPtr& sys) { - return SE_Signal(python_list_to_vector(stock_list), sys); -} - -bool py_se_add_stock_list(SelectorBase& se, py::list stock_list, const SystemPtr& protoSys) { - return se.addStockList(python_list_to_vector(stock_list), protoSys); -} - -void export_Selector() { - class_( - "SelectorBase", +void export_Selector(py::module& m) { + py::class_( + m, "SelectorBase", R"(选择器策略基类,实现标的、系统策略的评估和选取算法,自定义选择器策略子类接口: - get_selected_on_open - 【必须】获取指定时刻开盘时选择的系统实例列表 - get_selected_on_close - 【必须】获取指定时刻收盘时选择的系统实例列表 - _calculate - 【必须】计算接口 - _reset - 【可选】重置私有属性 - - _clone - 【必须】克隆接口)", - init<>()) - .def(init()) - .def(self_ns::str(self)) - .def(self_ns::repr(self)) - .add_property("name", make_function(se_get_name, return_value_policy()), - se_set_name, "算法名称") - .add_property("proto_sys_list", - make_function(&SelectorBase::getProtoSystemList, - return_value_policy()), - "原型系统列表") - .add_property("real_sys_list", - make_function(&SelectorBase::getRealSystemList, - return_value_policy()), - "由 PF 运行时设定的实际运行系统列表") + - _clone - 【必须】克隆接口)") + + .def(py::init<>()) + .def(py::init(), R"(初始化构造函数 + + :param str name: 名称)") + + .def("__str__", to_py_str) + .def("__repr__", to_py_str) + + .def_property("name", py::overload_cast(&SelectorBase::name, py::const_), + py::overload_cast(&SelectorBase::name), + py::return_value_policy::copy, "算法名称") + .def_property_readonly("proto_sys_list", &SelectorBase::getProtoSystemList, + py::return_value_policy::copy, "原型系统列表") + .def_property_readonly("real_sys_list", &SelectorBase::getRealSystemList, + py::return_value_policy::copy, "由 PF 运行时设定的实际运行系统列表") .def("get_param", &SelectorBase::getParam, R"(get_param(self, name) @@ -103,7 +75,7 @@ void export_Selector() { :return: 参数值 :raises out_of_range: 无此参数)") - .def("set_param", &SelectorBase::setParam, R"(set_param(self, name, value) + .def("set_param", &SelectorBase::setParam, R"(set_param(self, name, value) 设置参数 @@ -117,7 +89,7 @@ void export_Selector() { .def("clone", &SelectorBase::clone, "克隆操作") .def("remove_all", &SelectorBase::removeAll, "清除所有已加入的原型系统") - .def("add_stock", &SelectorBase::addStock, (arg("stock"), arg("sys")), + .def("add_stock", &SelectorBase::addStock, py::arg("stock"), py::arg("sys"), R"(add_stock(self, stock, sys) 加入初始标的及其对应的系统策略原型 @@ -125,7 +97,7 @@ void export_Selector() { :param Stock stock: 加入的初始标的 :param System sys: 系统策略原型)") - .def("add_stock_list", py_se_add_stock_list, (arg("stk_list"), arg("sys")), + .def("add_stock_list", &SelectorBase::addStockList, py::arg("stk_list"), py::arg("sys"), R"(add_stock_list(self, stk_list, sys) 加入初始标的列表及其系统策略原型 @@ -133,17 +105,16 @@ void export_Selector() { :param StockList stk_list: 加入的初始标的列表 :param System sys: 系统策略原型)") - .def("_reset", &SelectorBase::_reset, &SelectorWrap::default_reset, "子类复位操作实现") - .def("_clone", pure_virtual(&SelectorBase::_clone), "子类克隆操作实现接口") - .def("_calculate", pure_virtual(&SelectorBase::_calculate), "【重载接口】子类计算接口") + .def("_reset", &SelectorBase::_reset, "子类复位操作实现") + .def("_calculate", &SelectorBase::_calculate, "【重载接口】子类计算接口") - .def("is_match_af", pure_virtual(&SelectorBase::isMatchAF), R"(is_match_af(self) + .def("is_match_af", &SelectorBase::isMatchAF, R"(is_match_af(self) 【重载接口】判断是否和 AF 匹配 :param AllocateFundsBase af: 资产分配算法)") - .def("get_selected_on_open", pure_virtual(&SelectorBase::getSelectedOnOpen), + .def("get_selected_on_open", &SelectorBase::getSelectedOnOpen, R"(get_selected_on_open(self, datetime) 【重载接口】获取指定时刻开盘时选取的系统实例 @@ -152,7 +123,7 @@ void export_Selector() { :return: 选取的系统实例列表 :rtype: SystemList)") - .def("get_selected_on_close", pure_virtual(&SelectorBase::getSelectedOnClose), + .def("get_selected_on_close", &SelectorBase::getSelectedOnClose, R"(get_selected_on_close(self, datetime) 【重载接口】获取指定时刻收盘时选取的系统实例 @@ -161,15 +132,11 @@ void export_Selector() { :return: 选取的系统实例列表 :rtype: SystemList)") -#if HKU_PYTHON_SUPPORT_PICKLE - .def_pickle(name_init_pickle_suite()) -#endif - ; + DEF_PICKLE(SEPtr); - register_ptr_to_python(); - - def("SE_Fixed", SE_Fixed_1); - def("SE_Fixed", py_SE_Fixed, R"(SE_Fixed([stk_list, sys]) + m.def("SE_Fixed", py::overload_cast(SE_Fixed)); + m.def("SE_Fixed", py::overload_cast(SE_Fixed), + R"(SE_Fixed([stk_list, sys]) 固定选择器,即始终选择初始划定的标的及其系统策略原型 @@ -177,8 +144,9 @@ void export_Selector() { :param System sys: 系统策略原型 :return: SE选择器实例)"); - def("SE_Signal", SE_Signal_1); - def("SE_Signal", py_SE_Signal, R"(SE_Signal([stk_list, sys]) + m.def("SE_Signal", py::overload_cast(SE_Signal)); + m.def("SE_Signal", py::overload_cast(SE_Signal), + R"(SE_Signal([stk_list, sys]) 信号选择器,仅依靠系统买入信号进行选中 diff --git a/hikyuu_pywrap/trade_sys/_Signal.cpp b/hikyuu_pywrap/trade_sys/_Signal.cpp index 8574193b..c6b0b526 100644 --- a/hikyuu_pywrap/trade_sys/_Signal.cpp +++ b/hikyuu_pywrap/trade_sys/_Signal.cpp @@ -5,45 +5,29 @@ * Author: fasiondog */ -#include #include -#include "../_Parameter.h" -#include "../pickle_support.h" +#include "../pybind_utils.h" -using namespace boost::python; +namespace py = pybind11; using namespace hku; -class SignalWrap : public SignalBase, public wrapper { +class PySignalBase : public SignalBase { + PY_CLONE(PySignalBase, SignalBase) + public: - SignalWrap() : SignalBase() {} - SignalWrap(const string& name) : SignalBase(name) {} + using SignalBase::SignalBase; - void _reset() { - if (override func = this->get_override("_reset")) { - func(); - } else { - SignalBase::_reset(); - } + void _calculate() override { + PYBIND11_OVERLOAD_PURE(void, SignalBase, _calculate, ); } - void default_reset() { - this->SignalBase::_reset(); - } - - SignalPtr _clone() { - return this->get_override("_clone")(); - } - - void _calculate() { - this->get_override("_calculate")(); + void _reset() override { + PYBIND11_OVERLOAD(void, SignalBase, _reset, ); } }; -string (SignalBase::*sg_get_name)() const = &SignalBase::name; -void (SignalBase::*sg_set_name)(const string&) = &SignalBase::name; - -void export_Signal() { - class_("SignalBase", R"(信号指示器基类 +void export_Signal(py::module& m) { + py::class_(m, "SignalBase", R"(信号指示器基类 信号指示器负责产生买入、卖出信号。 公共参数: @@ -54,15 +38,18 @@ void export_Signal() { - _calculate : 【必须】子类计算接口 - _clone : 【必须】克隆接口 - - _reset : 【可选】重载私有变量)", - init<>()) - .def(init()) + - _reset : 【可选】重载私有变量)") - .def(self_ns::str(self)) - .def(self_ns::repr(self)) + .def(py::init<>()) + .def(py::init()) - .add_property("name", sg_get_name, sg_set_name, "名称") - .add_property("to", &SignalBase::getTO, &SignalBase::setTO, "设置或获取交易对象") + .def("__str__", to_py_str) + .def("__repr__", to_py_str) + + .def_property("name", py::overload_cast(&SignalBase::name, py::const_), + py::overload_cast(&SignalBase::name), + py::return_value_policy::copy, "名称") + .def_property("to", &SignalBase::getTO, &SignalBase::setTO, "设置或获取交易对象") .def("get_param", &SignalBase::getParam, R"(get_param(self, name) @@ -72,7 +59,7 @@ void export_Signal() { :return: 参数值 :raises out_of_range: 无此参数)") - .def("set_param", &SignalBase::setParam, R"(set_param(self, name, value) + .def("set_param", &SignalBase::setParam, R"(set_param(self, name, value) 设置参数 @@ -131,20 +118,13 @@ void export_Signal() { .def("reset", &SignalBase::reset, "复位操作") .def("clone", &SignalBase::clone, "克隆操作") - .def("_calculate", pure_virtual(&SignalBase::_calculate), "【重载接口】子类计算接口") - .def("_reset", &SignalBase::_reset, &SignalWrap::default_reset, - "【重载接口】子类复位接口,复位内部私有变量") - .def("_clone", pure_virtual(&SignalBase::_clone), "【重载接口】子类克隆接口") + .def("_calculate", &SignalBase::_calculate, "【重载接口】子类计算接口") + .def("_reset", &SignalBase::_reset, "【重载接口】子类复位接口,复位内部私有变量") -#if HKU_PYTHON_SUPPORT_PICKLE - .def_pickle(name_init_pickle_suite()) -#endif - ; + DEF_PICKLE(SGPtr); - register_ptr_to_python(); - - def("SG_Bool", SG_Bool, (arg("buy"), arg("sell")), - R"(SG_Bool(buy, sell) + m.def("SG_Bool", SG_Bool, py::arg("buy"), py::arg("sell"), + R"(SG_Bool(buy, sell) 布尔信号指示器,使用运算结果为类似bool数组的Indicator分别作为买入、卖出指示。 @@ -152,8 +132,9 @@ void export_Signal() { :param Indicator sell: 卖出指示(结果Indicator中相应位置>0则代表卖出) :return: 信号指示器)"); - def("SG_Single", SG_Single, (arg("ind"), arg("filter_n") = 10, arg("filter_p") = 0.1), - R"(SG_Single(ind[, filter_n = 10, filter_p = 0.1]) + m.def("SG_Single", SG_Single, py::arg("ind"), py::arg("filter_n") = 10, + py::arg("filter_p") = 0.1, + R"(SG_Single(ind[, filter_n = 10, filter_p = 0.1]) 生成单线拐点信号指示器。使用《精明交易者》 [BOOK1]_ 中给出的曲线拐点算法判断曲线趋势,公式见下:: @@ -168,8 +149,9 @@ void export_Signal() { :param float filter_p: 过滤器百分比 :return: 信号指示器)"); - def("SG_Single2", SG_Single2, (arg("ind"), arg("filter_n") = 10, arg("filter_p") = 0.1), - R"(SG_Single2(ind[, filter_n = 10, filter_p = 0.1]) + m.def("SG_Single2", SG_Single2, py::arg("ind"), py::arg("filter_n") = 10, + py::arg("filter_p") = 0.1, + R"(SG_Single2(ind[, filter_n = 10, filter_p = 0.1]) 生成单线拐点信号指示器2 [BOOK1]_:: @@ -183,8 +165,8 @@ void export_Signal() { :param float filter_p: 过滤器百分比 :return: 信号指示器)"); - def("SG_Cross", SG_Cross, (arg("fast"), arg("slow")), - R"(SG_Cross(fast, slow) + m.def("SG_Cross", SG_Cross, py::arg("fast"), py::arg("slow"), + R"(SG_Cross(fast, slow) 双线交叉指示器,当快线从下向上穿越慢线时,买入;当快线从上向下穿越慢线时,卖出。如:5日MA上穿10日MA时买入,5日MA线下穿MA10日线时卖出:: @@ -194,8 +176,8 @@ void export_Signal() { :param Indicator slow: 慢线 :return: 信号指示器)"); - def("SG_CrossGold", SG_CrossGold, (arg("fast"), arg("slow")), - R"(SG_CrossGold(fast, slow) + m.def("SG_CrossGold", SG_CrossGold, py::arg("fast"), py::arg("slow"), + R"(SG_CrossGold(fast, slow) 金叉指示器,当快线从下向上穿越慢线且快线和慢线的方向都是向上时为金叉,买入; 当快线从上向下穿越慢线且快线和慢线的方向都是向下时死叉,卖出。:: @@ -206,8 +188,8 @@ void export_Signal() { :param Indicator slow: 慢线 :return: 信号指示器)"); - def("SG_Flex", SG_Flex, (arg("op"), arg("slow_n")), - R"(SG_Flex(ind, slow_n) + m.def("SG_Flex", SG_Flex, py::arg("op"), py::arg("slow_n"), + R"(SG_Flex(ind, slow_n) 使用自身的EMA(slow_n)作为慢线,自身作为快线,快线向上穿越慢线买入,快线向下穿越慢线卖出。 @@ -215,8 +197,8 @@ void export_Signal() { :param int slow_n: 慢线EMA周期 :return: 信号指示器)"); - def("SG_Band", SG_Band, (arg("ind"), arg("lower"), arg("upper")), - R"(SG_Band(ind, lower, upper) + m.def("SG_Band", SG_Band, py::arg("ind"), py::arg("lower"), py::arg("upper"), + R"(SG_Band(ind, lower, upper) 指标区间指示器, 当指标超过上轨时,买入; 当指标低于下轨时,卖出。:: diff --git a/hikyuu_pywrap/trade_sys/_Slippage.cpp b/hikyuu_pywrap/trade_sys/_Slippage.cpp index 3472bedc..c2d1db4d 100644 --- a/hikyuu_pywrap/trade_sys/_Slippage.cpp +++ b/hikyuu_pywrap/trade_sys/_Slippage.cpp @@ -5,55 +5,40 @@ * Author: fasiondog */ -#include #include #include -#include "../_Parameter.h" -#include "../pickle_support.h" +#include "../pybind_utils.h" -using namespace boost::python; +namespace py = pybind11; using namespace hku; -class SlippageWrap : public SlippageBase, public wrapper { +class PySlippageBase : public SlippageBase { + PY_CLONE(PySlippageBase, SlippageBase) + public: - SlippageWrap() : SlippageBase() {} - SlippageWrap(const string& name) : SlippageBase(name) {} - virtual ~SlippageWrap() {} + using SlippageBase::SlippageBase; - void _reset() { - if (override func = get_override("_reset")) { - func(); - } else { - SlippageBase::_reset(); - } + void _calculate() override { + PYBIND11_OVERLOAD_PURE(void, SlippageBase, _calculate, ); } - void default_reset() { - this->SlippageBase::_reset(); + void _reset() override { + PYBIND11_OVERLOAD(void, SlippageBase, _reset, ); } - SlippagePtr _clone() { - return this->get_override("_clone")(); + price_t getRealBuyPrice(const Datetime& datetime, price_t planPrice) override { + PYBIND11_OVERLOAD_PURE_NAME(price_t, SlippageBase, "get_real_buy_price", getRealBuyPrice, + datetime, planPrice); } - void _calculate() { - this->get_override("_calculate"); - } - - price_t getRealBuyPrice(const Datetime& datetime, price_t price) { - return this->get_override("get_real_buy_price")(datetime, price); - } - - price_t getRealSellPrice(const Datetime& datetime, price_t price) { - return this->get_override("get_real_sell_price")(datetime, price); + price_t getRealSellPrice(const Datetime& datetime, price_t planPrice) override { + PYBIND11_OVERLOAD_PURE_NAME(price_t, SlippageBase, "get_real_sell_price", getRealSellPrice, + datetime, planPrice); } }; -string (SlippageBase::*sp_get_name)() const = &SlippageBase::name; -void (SlippageBase::*sp_set_name)(const string&) = &SlippageBase::name; - -void export_Slippage() { - class_("SlippageBase", R"(移滑价差算法基类 +void export_Slippage(py::module& m) { + py::class_(m, "SlippageBase", R"(移滑价差算法基类 自定义移滑价差接口: @@ -61,14 +46,20 @@ void export_Slippage() { - getRealSellPrice : 【必须】计算实际卖出价格 - _calculate : 【必须】子类计算接口 - _clone : 【必须】克隆接口 - - _reset : 【可选】重载私有变量)", - init<>()) - .def(init()) - .def(self_ns::str(self)) - .def(self_ns::repr(self)) + - _reset : 【可选】重载私有变量)") - .add_property("name", sp_get_name, sp_set_name, "名称") - .add_property("to", &SlippageBase::getTO, &SlippageBase::setTO, "设置或获取交易对象") + .def(py::init<>()) + .def(py::init(), R"(初始化构造函数 + + :param str name: 名称)") + + .def("__str__", to_py_str) + .def("__repr__", to_py_str) + + .def_property("name", py::overload_cast(&SlippageBase::name, py::const_), + py::overload_cast(&SlippageBase::name), + py::return_value_policy::copy, "名称") + .def_property("to", &SlippageBase::getTO, &SlippageBase::setTO, "关联交易对象") .def("get_param", &SlippageBase::getParam, R"(get_param(self, name) @@ -78,7 +69,7 @@ void export_Slippage() { :return: 参数值 :raises out_of_range: 无此参数)") - .def("set_param", &SlippageBase::setParam, R"(set_param(self, name, value) + .def("set_param", &SlippageBase::setParam, R"(set_param(self, name, value) 设置参数 @@ -88,7 +79,7 @@ void export_Slippage() { .def("have_param", &SlippageBase::haveParam, "是否存在指定参数") - .def("get_real_buy_price", pure_virtual(&SlippageBase::getRealBuyPrice), + .def("get_real_buy_price", &SlippageBase::getRealBuyPrice, R"(get_real_buy_price(self, datetime, price) 【重载接口】计算实际买入价格 @@ -98,7 +89,7 @@ void export_Slippage() { :return: 实际买入价格 :rtype: float)") - .def("get_real_sell_price", pure_virtual(&SlippageBase::getRealSellPrice), + .def("get_real_sell_price", &SlippageBase::getRealSellPrice, R"(get_real_sell_price(self, datetime, price) 【重载接口】计算实际卖出价格 @@ -110,21 +101,20 @@ void export_Slippage() { .def("reset", &SlippageBase::reset, "复位操作") .def("clone", &SlippageBase::clone, "克隆操作") - .def("_calculate", pure_virtual(&SlippageBase::_calculate), "【重载接口】子类计算接口") - .def("_reset", &SlippageBase::_reset, &SlippageWrap::default_reset, - "【重载接口】子类复位接口,复位内部私有变量") - .def("_clone", pure_virtual(&SlippageBase::_clone), "【重载接口】子类克隆接口"); + .def("_calculate", &SlippageBase::_calculate, "【重载接口】子类计算接口") + .def("_reset", &SlippageBase::_reset, "【重载接口】子类复位接口,复位内部私有变量") - register_ptr_to_python(); + DEF_PICKLE(SPPtr); - def("SP_FixedPercent", SP_FixedPercent, (arg("p") = 0.001), R"(SP_FixedPercent([p=0.001]) + m.def("SP_FixedPercent", SP_FixedPercent, py::arg("p") = 0.001, + R"(SP_FixedPercent([p=0.001]) 固定百分比移滑价差算法,买入实际价格 = 计划买入价格 * (1 + p),卖出实际价格 = 计划卖出价格 * (1 - p) :param float p: 偏移的固定百分比 :return: 移滑价差算法实例)"); - def("SP_FixedValue", SP_FixedValue, (arg("value") = 0.01), R"(SP_FixedValuet([p=0.001]) + m.def("SP_FixedValue", SP_FixedValue, py::arg("value") = 0.01, R"(SP_FixedValuet([p=0.001]) 固定价格移滑价差算法,买入实际价格 = 计划买入价格 + 偏移价格,卖出实际价格 = 计划卖出价格 - 偏移价格 diff --git a/hikyuu_pywrap/trade_sys/_Stoploss.cpp b/hikyuu_pywrap/trade_sys/_Stoploss.cpp index ab62ac87..ce3cb9a4 100644 --- a/hikyuu_pywrap/trade_sys/_Stoploss.cpp +++ b/hikyuu_pywrap/trade_sys/_Stoploss.cpp @@ -5,74 +5,57 @@ * Author: fasiondog */ -#include #include -#include "../_Parameter.h" -#include "../pickle_support.h" +#include "../pybind_utils.h" -using namespace boost::python; +namespace py = pybind11; using namespace hku; -class StoplossWrap : public StoplossBase, public wrapper { +class PyStoplossBase : public StoplossBase { + PY_CLONE(PyStoplossBase, StoplossBase) + public: - StoplossWrap() : StoplossBase() {} - StoplossWrap(const string& name) : StoplossBase(name) {} - virtual ~StoplossWrap() {} + using StoplossBase::StoplossBase; - void _reset() { - if (override func = this->get_override("_reset")) { - func(); - } else { - StoplossBase::_reset(); - } + void _calculate() override { + PYBIND11_OVERLOAD_PURE(void, StoplossBase, _calculate, ); } - void default_reset() { - this->StoplossBase::_reset(); + void _reset() override { + PYBIND11_OVERLOAD(void, StoplossBase, _reset, ); } - StoplossPtr _clone() { - return this->get_override("_clone")(); + price_t getPrice(const Datetime& datetime, price_t price) override { + PYBIND11_OVERLOAD_PURE_NAME(price_t, StoplossBase, "get_price", getPrice, datetime, price); } - void _calculate() { - this->get_override("_calculate")(); - } - - price_t getPrice(const Datetime& datetime, price_t price) { - return this->get_override("get_price")(datetime, price); - } - - price_t getShortPrice(const Datetime& datetime, price_t price) { - if (override getShortPrice = get_override("get_short_price")) { - return getShortPrice(datetime, price); - } - return StoplossBase::getShortPrice(datetime, price); - } - - price_t default_getShortPrice(const Datetime& datetime, price_t price) { - return this->StoplossBase::getShortPrice(datetime, price); + price_t getShortPrice(const Datetime& datetime, price_t price) override { + PYBIND11_OVERLOAD_NAME(price_t, StoplossBase, "get_short_price", getShortPrice, datetime, + price); } }; -string (StoplossBase::*st_get_name)() const = &StoplossBase::name; -void (StoplossBase::*st_set_name)(const string&) = &StoplossBase::name; - -void export_Stoploss() { - class_("StoplossBase", R"(止损/止赢算法基类 +void export_Stoploss(py::module& m) { + py::class_(m, "StoplossBase", + R"(止损/止赢算法基类 自定义止损/止赢策略接口: - _calculate : 【必须】子类计算接口 - _clone : 【必须】克隆接口 - - _reset : 【可选】重载私有变量)", - init<>()) - .def(init()) - .def(self_ns::str(self)) - .def(self_ns::repr(self)) + - _reset : 【可选】重载私有变量)") + .def(py::init<>()) + .def(py::init(), R"(初始化构造函数 + + :param str name: 名称)") - .add_property("name", st_get_name, st_set_name, "名称") - .add_property("to", &StoplossBase::getTO, &StoplossBase::setTO, "设置或获取交易对象") - .add_property("tm", &StoplossBase::getTM, &StoplossBase::setTM, "设置或获取交易管理实例") + .def("__str__", to_py_str) + .def("__repr__", to_py_str) + + .def_property("name", py::overload_cast(&StoplossBase::name, py::const_), + py::overload_cast(&StoplossBase::name), + py::return_value_policy::copy, "名称") + .def_property("tm", &StoplossBase::getTM, &StoplossBase::setTM, "关联交易管理实例") + .def_property("to", &StoplossBase::getTO, &StoplossBase::setTO, "关联交易对象") .def("get_param", &StoplossBase::getParam, R"(get_param(self, name) @@ -82,7 +65,7 @@ void export_Stoploss() { :return: 参数值 :raises out_of_range: 无此参数)") - .def("set_param", &StoplossBase::setParam, R"(set_param(self, name, value) + .def("set_param", &StoplossBase::setParam, R"(set_param(self, name, value) 设置参数 @@ -92,7 +75,7 @@ void export_Stoploss() { .def("have_param", &StoplossBase::haveParam, "是否存在指定参数") - .def("get_price", pure_virtual(&StoplossBase::getPrice), R"(get_price(self, datetime, price) + .def("get_price", &StoplossBase::getPrice, R"(get_price(self, datetime, price) 【重载接口】获取本次预期交易(买入)时的计划止损价格,如果不存在止损价,则返回0。用于系统在交易执行前向止损策略模块查询本次交易的计划止损价。 @@ -104,31 +87,24 @@ void export_Stoploss() { :return: 止损价格 :rtype: float)") - //.def("getShortPrice", &StoplossBase::getShortPrice, &StoplossWrap::default_getShortPrice) + .def("get_short_price", &StoplossBase::getShortPrice) .def("reset", &StoplossBase::reset, "复位操作") .def("clone", &StoplossBase::clone, "克隆操作") - .def("_calculate", pure_virtual(&StoplossBase::_calculate), "【重载接口】子类计算接口") - .def("_reset", &StoplossBase::_reset, &StoplossWrap::default_reset, - "【重载接口】子类复位接口,复位内部私有变量") - .def("_clone", pure_virtual(&StoplossBase::_clone), "【重载接口】子类克隆接口") + .def("_calculate", &StoplossBase::_calculate, "【重载接口】子类计算接口") + .def("_reset", &StoplossBase::_reset, "【重载接口】子类复位接口,复位内部私有变量") -#if HKU_PYTHON_SUPPORT_PICKLE - .def_pickle(name_init_pickle_suite()) -#endif - ; + DEF_PICKLE(StoplossPtr); - register_ptr_to_python(); - - def("ST_FixedPercent", ST_FixedPercent, (arg("p") = 0.03), R"(ST_FixedPercent([p=0.03]) + m.def("ST_FixedPercent", ST_FixedPercent, py::arg("p") = 0.03, R"(ST_FixedPercent([p=0.03]) 固定百分比止损策略,即当价格低于买入价格的某一百分比时止损 :param float p: 百分比(0,1] :return: 止损/止赢策略实例)"); - def("ST_Indicator", ST_Indicator, (arg("op"), arg("kpart") = "CLOSE"), - R"(ST_Indicator(op[, kpart="CLOSE"]) + m.def("ST_Indicator", ST_Indicator, py::arg("op"), py::arg("kpart") = "CLOSE", + R"(ST_Indicator(op[, kpart="CLOSE"]) 使用技术指标作为止损价。如使用10日EMA作为止损::: @@ -138,8 +114,8 @@ void export_Stoploss() { :param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL :return: 止损/止赢策略实例)"); - def("ST_Saftyloss", ST_Saftyloss, (arg("n1") = 10, arg("n2") = 3, arg("p") = 2.0), - R"(ST_Saftyloss([n1=10, n2=3, p=2.0]) + m.def("ST_Saftyloss", ST_Saftyloss, py::arg("n1") = 10, py::arg("n2") = 3, py::arg("p") = 2.0, + R"(ST_Saftyloss([n1=10, n2=3, p=2.0]) 参见《走进我的交易室》(2007年 地震出版社) 亚历山大.艾尔德(Alexander Elder) P202 计算说明:在回溯周期内(一般为10到20天),将所有向下穿越的长度相加除以向下穿越的次数, diff --git a/hikyuu_pywrap/trade_sys/_System.cpp b/hikyuu_pywrap/trade_sys/_System.cpp index 14cec452..c4f9d71c 100644 --- a/hikyuu_pywrap/trade_sys/_System.cpp +++ b/hikyuu_pywrap/trade_sys/_System.cpp @@ -5,36 +5,28 @@ * Author: fasiondog */ -#include #include -#include -#include "../_Parameter.h" -#include "../pickle_support.h" +#include "../pybind_utils.h" -using namespace boost::python; +namespace py = pybind11; using namespace hku; #if defined(_MSC_VER) #pragma warning(disable : 4267) #endif -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(run_overload, run, 2, 3); - -const string& (System::*sys_get_name)() const = &System::name; -void (System::*sys_set_name)(const string&) = &System::name; - void (System::*run_1)(const KQuery&, bool) = &System::run; void (System::*run_2)(const KData&, bool) = &System::run; void (System::*run_3)(const Stock&, const KQuery&, bool reset) = &System::run; TradeRecord (System::*runMoment_1)(const Datetime&) = &System::runMoment; -void export_System() { - def( - "SYS_Simple", SYS_Simple, - (arg("tm") = TradeManagerPtr(), arg("mm") = MoneyManagerPtr(), arg("ev") = EnvironmentPtr(), - arg("cn") = ConditionPtr(), arg("sg") = SignalPtr(), arg("st") = StoplossPtr(), - arg("tp") = StoplossPtr(), arg("pg") = ProfitGoalPtr(), arg("sp") = SlippagePtr()), +void export_System(py::module& m) { + m.def( + "SYS_Simple", SYS_Simple, py::arg("tm") = TradeManagerPtr(), + py::arg("mm") = MoneyManagerPtr(), py::arg("ev") = EnvironmentPtr(), + py::arg("cn") = ConditionPtr(), py::arg("sg") = SignalPtr(), py::arg("st") = StoplossPtr(), + py::arg("tp") = StoplossPtr(), py::arg("pg") = ProfitGoalPtr(), py::arg("sp") = SlippagePtr(), R"(SYS_Simple([tm=None, mm=None, ev=None, cn=None, sg=None, st=None, tp=None, pg=None, sp=None]) 创建简单系统实例(每次交易不进行多次加仓或减仓,即每次买入后在卖出时全部卖出), 系统实例在运行时(调用run方法),至少需要一个配套的交易管理实例、一个资金管理策略 @@ -65,7 +57,7 @@ void export_System() { :param SlippageBase sp: 移滑价差算法 :return: system实例)"); - def("get_system_part_name", getSystemPartName, R"(get_system_part_name(part) + m.def("get_system_part_name", getSystemPartName, R"(get_system_part_name(part) 获取部件的字符串名称 @@ -82,21 +74,22 @@ void export_System() { :param int part: System.Part 枚举值 :rtype: str)"); - def("get_system_part_enum", getSystemPartEnum, R"(get_system_part_enum(part_name) + m.def("get_system_part_enum", getSystemPartEnum, R"(get_system_part_enum(part_name) 根据系统部件的字符串名称获取相应的枚举值 :param str part_name: 系统部件的字符串名称,参见::py:func:`getSystemPartName` :rtype: System.Part)"); - class_( - "TradeRequest", - R"(交易请求记录。系统内部在实现延迟操作时登记的交易请求信息。暴露该结构的主要目的是用于在“delay”模式(延迟到下一个bar开盘时进行交易)的情况下,系统实际已知下一个Bar将要进行交易,此时可通过 :py:meth:`System.getBuyTradeRequest` 、 :py:meth:`System.getSellTradeRequest` 来获知下一个BAR是否需要买入/卖出。主要用于提醒或打印下一个Bar需要进行操作。对于系统本身的运行没有影响。)", - init<>()) - - //.def(self_ns::str(self)) - //.def(self_ns::repr(self)) + py::class_( + m, "TradeRequest", + R"(交易请求记录。系统内部在实现延迟操作时登记的交易请求信息。暴露该结构的主要目的是用于 +在“delay”模式(延迟到下一个bar开盘时进行交易)的情况下,系统实际已知下一个Bar将要 +进行交易,此时可通过 System.getBuyTradeRequest() 、 System.getSellTradeRequest() +来获知下一个BAR是否需要买入/卖出。主要用于提醒或打印下一个Bar需要进行操作。对于系统 +本身的运行没有影响。)") + .def(py::init<>()) .def_readwrite("valid", &TradeRequest::valid, "该交易请求记录是否有效(True | False)") .def_readwrite("business", &TradeRequest::business, "交易业务类型,参见::py:class:`hikyuu.trade_manage.BUSINESS`") @@ -105,17 +98,11 @@ void export_System() { .def_readwrite("part", &TradeRequest::from, "发出交易请求的来源,参见::py:class:`System.Part`") .def_readwrite("count", &TradeRequest::count, "因操作失败,连续延迟的次数") -#if HKU_PYTHON_SUPPORT_PICKLE - .def_pickle(normal_pickle_suite()) -#endif - ; + DEF_PICKLE(TradeRequest); - class_("SystemList").def(vector_indexing_suite()); - - scope in_System = - class_( - "System", - R"(系统基类。需要扩展或实现更复杂的系统交易行为,可从此类继承。 + py::class_( + m, "System", + R"(系统基类。需要扩展或实现更复杂的系统交易行为,可从此类继承。 系统是指针对单个交易对象的完整策略,包括环境判断、系统有效条件、资金管理、止损、止盈、盈利目标、移滑价差的完整策略,用于模拟回测。 @@ -128,29 +115,30 @@ void export_System() { - tp_delay_n=3 (int) : 止盈延迟开始的天数,即止盈策略判断从实际交易几天后开始生效 - ignore_sell_sg=False (bool) : 忽略卖出信号,只使用止损/止赢等其他方式卖出 - ev_open_position=False (bool): 是否使用市场环境判定进行初始建仓 - - cn_open_position=False (bool): 是否使用系统有效性条件进行初始建仓)", - init()) - .def(init()) - .def(self_ns::str(self)) - .def(self_ns::repr(self)) + - cn_open_position=False (bool): 是否使用系统有效性条件进行初始建仓)") - .add_property("name", - make_function(sys_get_name, return_value_policy()), - sys_set_name, "系统名称") - .add_property("tm", &System::getTM, &System::setTM, "关联的交易管理实例") - .add_property("to", &System::getTO, &System::setTO, "交易对象 KData") - .add_property("mm", &System::getMM, &System::setMM, "资金管理策略") - .add_property("ev", &System::getEV, &System::setEV, "市场环境判断策略") - .add_property("cn", &System::getCN, &System::setCN, "系统有效条件") - .add_property("sg", &System::getSG, &System::setSG, "信号指示器") - .add_property("st", &System::getST, &System::setST, "止损策略") - .add_property("tp", &System::getTP, &System::setTP, "止盈策略") - .add_property("pg", &System::getPG, &System::setPG, "盈利目标策略") - .add_property("sp", &System::getSP, &System::setSP, "移滑价差算法") + .def(py::init()) + .def(py::init()) + .def("__str__", to_py_str) + .def("__repr__", to_py_str) - .def("get_param", &System::getParam, R"(get_param(self, name) + .def_property("name", py::overload_cast(&System::name, py::const_), + py::overload_cast(&System::name), py::return_value_policy::copy, + "系统名称") + .def_property("tm", &System::getTM, &System::setTM, "关联的交易管理实例") + .def_property("to", &System::getTO, &System::setTO, "交易对象 KData") + .def_property("mm", &System::getMM, &System::setMM, "资金管理策略") + .def_property("ev", &System::getEV, &System::setEV, "市场环境判断策略") + .def_property("cn", &System::getCN, &System::setCN, "系统有效条件") + .def_property("sg", &System::getSG, &System::setSG, "信号指示器") + .def_property("st", &System::getST, &System::setST, "止损策略") + .def_property("tp", &System::getTP, &System::setTP, "止盈策略") + .def_property("pg", &System::getPG, &System::setPG, "盈利目标策略") + .def_property("sp", &System::getSP, &System::setSP, "移滑价差算法") + + .def("get_param", &System::getParam, R"(get_param(self, name) 获取指定的参数 @@ -158,7 +146,7 @@ void export_System() { :return: 参数值 :raises out_of_range: 无此参数)") - .def("set_param", &System::setParam, R"(set_param(self, name, value) + .def("set_param", &System::setParam, R"(set_param(self, name, value) 设置参数 @@ -166,56 +154,55 @@ void export_System() { :param value: 参数值 :raises logic_error: Unsupported type! 不支持的参数类型)") - .def("have_param", &System::haveParam, "是否存在指定参数") + .def("have_param", &System::haveParam, "是否存在指定参数") - .def("get_stock", &System::getStock, R"(get_stock(self) + .def("get_stock", &System::getStock, R"(get_stock(self) 获取关联的证券 :rtype: Stock)") - .def("get_trade_record_list", &System::getTradeRecordList, - return_value_policy(), R"(get_trade_record_list(self) + .def("get_trade_record_list", &System::getTradeRecordList, py::return_value_policy::copy, + R"(get_trade_record_list(self) 获取实际执行的交易记录,和 TM 的区别是不包含权息调整带来的交易记录 :rtype: TradeRecordList)") - .def("get_buy_trade_request", &System::getBuyTradeRequest, - return_value_policy(), R"(get_buy_trade_request(self) + .def("get_buy_trade_request", &System::getBuyTradeRequest, py::return_value_policy::copy, + R"(get_buy_trade_request(self) 获取买入请求,“delay”模式下查看下一时刻是否存在买入操作 :rtype: TradeRequest)") - .def("get_sell_trade_request", &System::getSellTradeRequest, - return_value_policy(), R"(get_sell_trade_request(self) + .def("get_sell_trade_request", &System::getSellTradeRequest, py::return_value_policy::copy, + R"(get_sell_trade_request(self) 获取卖出请求,“delay”模式下查看下一时刻是否存在卖出操作 :rtype: TradeRequest)") - /*.def("getSellShortTradeRequest", &System::getSellShortTradeRequest, - return_value_policy()) - .def("getBuyShortTradeRequest", &System::getBuyShortTradeRequest, - return_value_policy())*/ + .def("get_sell_short_trade_request", &System::getSellShortTradeRequest, + py::return_value_policy::copy) + .def("get_buy_short_trade_request", &System::getBuyShortTradeRequest, + py::return_value_policy::copy) - .def("reset", &System::reset, R"(reset(self, with_tm, with_ev) + .def("reset", &System::reset, R"(reset(self, with_tm, with_ev) 复位操作。TM、EV是和具体系统无关的策略组件,可以在不同的系统中进行共享,复位将引起系统运行时被重新清空并计算。尤其是在共享TM时需要注意! :param bool with_tm: 是否复位TM组件 :param bool with_ev: 是否复位EV组件)") - .def("clone", &System::clone, R"(clone(self) + .def("clone", &System::clone, R"(clone(self) 克隆操作。)") - //.def("run", &System::run, run_overload(args("stock", "query", "reset"))) - .def("run", run_1, (arg("query"), arg("reset") = true)) - .def("run", run_2, (arg("kdata"), arg("reset") = true)) - .def("run", run_3, (arg("stock"), arg("query"), arg("reset") = true), - R"(run(self, stock, query[, reset=True]) + .def("run", run_1, py::arg("query"), py::arg("reset") = true) + .def("run", run_2, py::arg("kdata"), py::arg("reset") = true) + .def("run", run_3, py::arg("stock"), py::arg("query"), py::arg("reset") = true, + R"(run(self, stock, query[, reset=True]) 运行系统,执行回测 @@ -223,14 +210,11 @@ void export_System() { :param Query query: K线数据查询条件 :param bool reset: 是否同时复位所有组件,尤其是tm实例)") - .def("ready", &System::readyForRun) + .def("ready", &System::readyForRun) -#if HKU_PYTHON_SUPPORT_PICKLE - .def_pickle(name_init_pickle_suite()) -#endif - ; + DEF_PICKLE(System); - enum_("Part") + py::enum_(m, "Part") .value("ENVIRONMENT", PART_ENVIRONMENT) .value("CONDITION", PART_CONDITION) .value("SIGNAL", PART_SIGNAL) @@ -241,6 +225,4 @@ void export_System() { .value("SLIPPAGE", PART_SLIPPAGE) .value("ALLOCATEFUNDS", PART_ALLOCATEFUNDS) .value("INVALID", PART_INVALID); - - register_ptr_to_python(); } diff --git a/hikyuu_pywrap/trade_sys/trade_sys_main.cpp b/hikyuu_pywrap/trade_sys/trade_sys_main.cpp index 28d8cd55..f58c86de 100644 --- a/hikyuu_pywrap/trade_sys/trade_sys_main.cpp +++ b/hikyuu_pywrap/trade_sys/trade_sys_main.cpp @@ -12,12 +12,12 @@ namespace py = pybind11; void export_Environment(py::module& m); void export_Condition(py::module& m); void export_MoneyManager(py::module& m); -// void export_Signal(py::module& m); -// void export_Stoploss(py::module& m); +void export_Signal(py::module& m); +void export_Stoploss(py::module& m); void export_ProfitGoal(py::module& m); -// void export_Slippage(py::module& m); -// void export_System(py::module& m); -// void export_Selector(py::module& m); +void export_Slippage(py::module& m); +void export_System(py::module& m); +void export_Selector(py::module& m); void export_Portfolio(py::module& m); void export_AllocateFunds(py::module& m); @@ -25,12 +25,12 @@ void export_trade_sys_main(py::module& m) { export_Environment(m); export_Condition(m); export_MoneyManager(m); - // export_Signal(m); - // export_Stoploss(m); + export_Signal(m); + export_Stoploss(m); export_ProfitGoal(m); - // export_Slippage(m); - // export_System(m); - // export_Selector(m); + export_Slippage(m); + export_System(m); + export_Selector(m); export_AllocateFunds(m); export_Portfolio(m); } diff --git a/hikyuu_pywrap/xmake.lua b/hikyuu_pywrap/xmake.lua index 813eca20..6219ccd8 100644 --- a/hikyuu_pywrap/xmake.lua +++ b/hikyuu_pywrap/xmake.lua @@ -44,14 +44,8 @@ target("core") add_files("./global/*.cpp") add_files("./trade_instance/*.cpp") add_files("./trade_manage/*.cpp") - add_files("./trade_sys/trade_sys_main.cpp") - add_files("./trade_sys/_AllocateFunds.cpp") - add_files("./trade_sys/_Condition.cpp") - add_files("./trade_sys/_Environment.cpp") - add_files("./trade_sys/_MoneyManager.cpp") - add_files("./trade_sys/_Portfolio.cpp") - add_files("./trade_sys/_ProfitGoal.cpp") - -- add_files("./trade_sys/_ProfitGoal.cpp") + add_files("./trade_sys/*.cpp") + add_rpathdirs("$ORIGIN", "$ORIGIN/lib", "$ORIGIN/../lib")