trade_sys finish

This commit is contained in:
fasiondog 2023-12-27 18:13:05 +08:00
parent 5e3d64ca2e
commit 3da11e708b
13 changed files with 268 additions and 377 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<SlippageBase> 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;
}

View File

@ -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<StoplossBase> 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;
}

View File

@ -42,7 +42,8 @@ void export_OrderBroker(py::module& m) {
.def("__repr__", to_py_str<OrderBrokerBase>)
.def_property("name", py::overload_cast<void>(&OrderBrokerBase::name, py::const_),
py::overload_cast<const string&>(&OrderBrokerBase::name), "名称(可读写)")
py::overload_cast<const string&>(&OrderBrokerBase::name),
py::return_value_policy::copy, "名称(可读写)")
.def("buy", &OrderBrokerBase::buy, R"(buy(self, datetime, market, code, price, num)

View File

@ -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<copy_const_reference>()),
// tm_set_name, "名称")
.def_property("name", py::overload_cast<>(&TradeManagerBase::name, py::const_),
py::overload_cast<const string&>(&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,

View File

@ -5,95 +5,67 @@
* Author: fasiondog
*/
#include <boost/python.hpp>
#include <hikyuu/trade_sys/selector/build_in.h>
#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<SelectorBase> {
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<StockList>(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<StockList>(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<StockList>(stock_list), protoSys);
}
void export_Selector() {
class_<SelectorWrap, boost::noncopyable>(
"SelectorBase",
void export_Selector(py::module& m) {
py::class_<SelectorBase, SEPtr, PySelectorBase>(
m, "SelectorBase",
R"(选择器策略基类,实现标的、系统策略的评估和选取算法,自定义选择器策略子类接口:
- get_selected_on_open -
- get_selected_on_close -
- _calculate -
- _reset -
- _clone - )",
init<>())
.def(init<const string&>())
.def(self_ns::str(self))
.def(self_ns::repr(self))
.add_property("name", make_function(se_get_name, return_value_policy<copy_const_reference>()),
se_set_name, "算法名称")
.add_property("proto_sys_list",
make_function(&SelectorBase::getProtoSystemList,
return_value_policy<copy_const_reference>()),
"原型系统列表")
.add_property("real_sys_list",
make_function(&SelectorBase::getRealSystemList,
return_value_policy<copy_const_reference>()),
"由 PF 运行时设定的实际运行系统列表")
- _clone - )")
.def(py::init<>())
.def(py::init<const string&>(), R"(初始化构造函数
:param str name: )")
.def("__str__", to_py_str<SelectorBase>)
.def("__repr__", to_py_str<SelectorBase>)
.def_property("name", py::overload_cast<void>(&SelectorBase::name, py::const_),
py::overload_cast<const string&>(&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<boost::any>, R"(get_param(self, name)
@ -103,7 +75,7 @@ void export_Selector() {
:return:
:raises out_of_range: )")
.def("set_param", &SelectorBase::setParam<object>, R"(set_param(self, name, value)
.def("set_param", &SelectorBase::setParam<boost::any>, 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<SelectorBase>())
#endif
;
DEF_PICKLE(SEPtr);
register_ptr_to_python<SelectorPtr>();
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<void>(SE_Fixed));
m.def("SE_Fixed", py::overload_cast<const StockList&, const SystemPtr&>(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<void>(SE_Signal));
m.def("SE_Signal", py::overload_cast<const StockList&, const SystemPtr&>(SE_Signal),
R"(SE_Signal([stk_list, sys])

View File

@ -5,45 +5,29 @@
* Author: fasiondog
*/
#include <boost/python.hpp>
#include <hikyuu/trade_sys/signal/build_in.h>
#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<SignalBase> {
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_<SignalWrap, boost::noncopyable>("SignalBase", R"(信号指示器基类
void export_Signal(py::module& m) {
py::class_<SignalBase, SGPtr, PySignalBase>(m, "SignalBase", R"(信号指示器基类
@ -54,15 +38,18 @@ void export_Signal() {
- _calculate :
- _clone :
- _reset : )",
init<>())
.def(init<const string&>())
- _reset : )")
.def(self_ns::str(self))
.def(self_ns::repr(self))
.def(py::init<>())
.def(py::init<const string&>())
.add_property("name", sg_get_name, sg_set_name, "名称")
.add_property("to", &SignalBase::getTO, &SignalBase::setTO, "设置或获取交易对象")
.def("__str__", to_py_str<SignalBase>)
.def("__repr__", to_py_str<SignalBase>)
.def_property("name", py::overload_cast<void>(&SignalBase::name, py::const_),
py::overload_cast<const string&>(&SignalBase::name),
py::return_value_policy::copy, "名称")
.def_property("to", &SignalBase::getTO, &SignalBase::setTO, "设置或获取交易对象")
.def("get_param", &SignalBase::getParam<boost::any>, R"(get_param(self, name)
@ -72,7 +59,7 @@ void export_Signal() {
:return:
:raises out_of_range: )")
.def("set_param", &SignalBase::setParam<object>, R"(set_param(self, name, value)
.def("set_param", &SignalBase::setParam<boost::any>, 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<SignalBase>())
#endif
;
DEF_PICKLE(SGPtr);
register_ptr_to_python<SignalPtr>();
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)
线线穿线线穿线5MA上穿10日MA时买入5MA线下穿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)
,
::

View File

@ -5,55 +5,40 @@
* Author: fasiondog
*/
#include <boost/python.hpp>
#include <hikyuu/trade_sys/slippage/SlippageBase.h>
#include <hikyuu/trade_sys/slippage/build_in.h>
#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<SlippageBase> {
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_<SlippageWrap, boost::noncopyable>("SlippageBase", R"(移滑价差算法基类
void export_Slippage(py::module& m) {
py::class_<SlippageBase, SPPtr, PySlippageBase>(m, "SlippageBase", R"(移滑价差算法基类
@ -61,14 +46,20 @@ void export_Slippage() {
- getRealSellPrice :
- _calculate :
- _clone :
- _reset : )",
init<>())
.def(init<const string&>())
.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<const string&>(), R"(初始化构造函数
:param str name: )")
.def("__str__", to_py_str<SlippageBase>)
.def("__repr__", to_py_str<SlippageBase>)
.def_property("name", py::overload_cast<void>(&SlippageBase::name, py::const_),
py::overload_cast<const string&>(&SlippageBase::name),
py::return_value_policy::copy, "名称")
.def_property("to", &SlippageBase::getTO, &SlippageBase::setTO, "关联交易对象")
.def("get_param", &SlippageBase::getParam<boost::any>, R"(get_param(self, name)
@ -78,7 +69,7 @@ void export_Slippage() {
:return:
:raises out_of_range: )")
.def("set_param", &SlippageBase::setParam<object>, R"(set_param(self, name, value)
.def("set_param", &SlippageBase::setParam<boost::any>, 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<SlippagePtr>();
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])
= + = -

View File

@ -5,74 +5,57 @@
* Author: fasiondog
*/
#include <boost/python.hpp>
#include <hikyuu/trade_sys/stoploss/build_in.h>
#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<StoplossBase> {
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_<StoplossWrap, boost::noncopyable>("StoplossBase", R"(止损/止赢算法基类
void export_Stoploss(py::module& m) {
py::class_<StoplossBase, StoplossPtr, PyStoplossBase>(m, "StoplossBase",
R"(止损/止赢算法基类
/
- _calculate :
- _clone :
- _reset : )",
init<>())
.def(init<const string&>())
.def(self_ns::str(self))
.def(self_ns::repr(self))
- _reset : )")
.def(py::init<>())
.def(py::init<const string&>(), 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<StoplossBase>)
.def("__repr__", to_py_str<StoplossBase>)
.def_property("name", py::overload_cast<void>(&StoplossBase::name, py::const_),
py::overload_cast<const string&>(&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<boost::any>, R"(get_param(self, name)
@ -82,7 +65,7 @@ void export_Stoploss() {
:return:
:raises out_of_range: )")
.def("set_param", &StoplossBase::setParam<object>, R"(set_param(self, name, value)
.def("set_param", &StoplossBase::setParam<boost::any>, 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<StoplossBase>())
#endif
;
DEF_PICKLE(StoplossPtr);
register_ptr_to_python<StoplossPtr>();
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"])
使使10EMA作为止损::
@ -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
1020穿穿

View File

@ -5,36 +5,28 @@
* Author: fasiondog
*/
#include <boost/python.hpp>
#include <hikyuu/trade_sys/system/build_in.h>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#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>(
"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_<TradeRequest>(
m, "TradeRequest",
R"(交易请求记录。系统内部在实现延迟操作时登记的交易请求信息。暴露该结构的主要目的是用于
delaybar开盘时进行交易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<TradeRequest>())
#endif
;
DEF_PICKLE(TradeRequest);
class_<SystemList>("SystemList").def(vector_indexing_suite<SystemList>());
scope in_System =
class_<System>(
"System",
R"(系统基类。需要扩展或实现更复杂的系统交易行为,可从此类继承。
py::class_<System, SystemPtr>(
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<const string&>())
.def(init<const TradeManagerPtr&, const MoneyManagerPtr&, const EnvironmentPtr&,
const ConditionPtr&, const SignalPtr&, const StoplossPtr&, const StoplossPtr&,
const ProfitGoalPtr&, const SlippagePtr&, const string&>())
.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<copy_const_reference>()),
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<const string&>())
.def(py::init<const TradeManagerPtr&, const MoneyManagerPtr&, const EnvironmentPtr&,
const ConditionPtr&, const SignalPtr&, const StoplossPtr&, const StoplossPtr&,
const ProfitGoalPtr&, const SlippagePtr&, const string&>())
.def("__str__", to_py_str<System>)
.def("__repr__", to_py_str<System>)
.def("get_param", &System::getParam<boost::any>, R"(get_param(self, name)
.def_property("name", py::overload_cast<void>(&System::name, py::const_),
py::overload_cast<const string&>(&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<boost::any>, R"(get_param(self, name)
@ -158,7 +146,7 @@ void export_System() {
:return:
:raises out_of_range: )")
.def("set_param", &System::setParam<object>, R"(set_param(self, name, value)
.def("set_param", &System::setParam<boost::any>, 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<copy_const_reference>(), 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<copy_const_reference>(), 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<copy_const_reference>(), 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<copy_const_reference>())
.def("getBuyShortTradeRequest", &System::getBuyShortTradeRequest,
return_value_policy<copy_const_reference>())*/
.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)
TMEV是和具体系统无关的策略组件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<System>())
#endif
;
DEF_PICKLE(System);
enum_<SystemPart>("Part")
py::enum_<SystemPart>(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<SystemPtr>();
}

View File

@ -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);
}

View File

@ -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")