hikyuu2/hikyuu_pywrap/trade_sys/_Selector.cpp
2024-06-13 02:11:29 +08:00

235 lines
9.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* _Selector.cpp
*
* Created on: 2016年3月28日
* Author: fasiondog
*/
#include <hikyuu/trade_sys/selector/build_in.h>
#include "../pybind_utils.h"
namespace py = pybind11;
using namespace hku;
class PySelectorBase : public SelectorBase {
PY_CLONE(PySelectorBase, SelectorBase)
public:
using SelectorBase::SelectorBase;
PySelectorBase(const SelectorBase& base) : SelectorBase(base) {}
void _reset() override {
PYBIND11_OVERLOAD(void, SelectorBase, _reset, );
}
void _calculate() override {
PYBIND11_OVERLOAD_PURE(void, SelectorBase, _calculate, );
}
void _addSystem(const SystemPtr& sys) override {
PYBIND11_OVERLOAD(void, SelectorBase, _addSystem, sys);
}
void _removeAll() override {
PYBIND11_OVERLOAD(void, SelectorBase, _removeAll, );
}
SystemWeightList getSelected(Datetime date) override {
// PYBIND11_OVERLOAD_PURE_NAME(SystemWeightList, SelectorBase, "get_selected", getSelected,
// date);
auto self = py::cast(this);
py::sequence py_ret = self.attr("get_selected")(date);
auto c_ret = python_list_to_vector<SystemWeight>(py_ret);
return c_ret;
}
bool isMatchAF(const AFPtr& af) override {
PYBIND11_OVERLOAD_PURE_NAME(bool, SelectorBase, "is_match_af", isMatchAF, af);
}
};
void export_Selector(py::module& m) {
py::class_<SystemWeight>(m, "SystemWeight", py::dynamic_attr(),
"系统权重系数结构,在资产分配时,指定对应系统的资产占比系数")
.def(py::init<>())
.def(py::init<const SystemPtr&, price_t>())
.def("__str__", to_py_str<SystemWeight>)
.def("__repr__", to_py_str<SystemWeight>)
.def_readwrite("sys", &SystemWeight::sys, "对应的 System 实例")
.def_readwrite("weight", &SystemWeight::weight)
DEF_PICKLE(SystemWeight);
py::class_<SelectorBase, SEPtr, PySelectorBase>(
m, "SelectorBase",
R"(选择器策略基类,实现标的、系统策略的评估和选取算法,自定义选择器策略子类接口:
- get_selected -
- _calculate -
- _reset -
- _clone - )")
.def(py::init<>())
.def(py::init<const SelectorBase&>())
.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<>(&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)
:param str name:
:return:
:raises out_of_range: )")
.def("set_param", &SelectorBase::setParam<boost::any>, R"(set_param(self, name, value)
:param str name:
:param value:
:raises logic_error: Unsupported type! )")
.def("have_param", &SelectorBase::haveParam, "是否存在指定参数")
.def("reset", &SelectorBase::reset, "复位操作")
.def("clone", &SelectorBase::clone, "克隆操作")
.def("remove_all", &SelectorBase::removeAll, "清除所有已加入的原型系统")
.def("add_stock", &SelectorBase::addStock, py::arg("stock"), py::arg("sys"),
R"(add_stock(self, stock, sys)
:param Stock stock:
:param System sys: )")
.def(
"add_stock_list",
[](SelectorBase& self, py::sequence stk_list, const SYSPtr& sys) {
self.addStockList(python_list_to_vector<Stock>(stk_list), sys);
},
py::arg("stk_list"), py::arg("sys"),
R"(add_stock_list(self, stk_list, sys)
:param StockList stk_list:
:param System sys: )")
.def("get_proto_sys_list", &SelectorBase::getProtoSystemList, py::return_value_policy::copy)
.def("get_real_sys_list", &SelectorBase::getRealSystemList, py::return_value_policy::copy)
.def("calculate", &SelectorBase::calculate)
.def("_reset", &SelectorBase::_reset, "子类复位操作实现")
.def("_calculate", &SelectorBase::_calculate, "【重载接口】子类计算接口")
.def("is_match_af", &SelectorBase::isMatchAF, R"(is_match_af(self)
AF
:param AllocateFundsBase af: )")
.def("get_selected", &SelectorBase::getSelected,
R"(get_selected(self, datetime)
:param Datetime datetime:
:return:
:rtype: SystemList)")
.def("__add__",
[](const SelectorPtr& self, const SelectorPtr& other) { return self + other; })
.def("__add__", [](const SelectorPtr& self, double other) { return self + other; })
.def("__radd__", [](const SelectorPtr& self, double other) { return self + other; })
.def("__sub__",
[](const SelectorPtr& self, const SelectorPtr& other) { return self - other; })
.def("__sub__", [](const SelectorPtr& self, double other) { return self - other; })
.def("__rsub__", [](const SelectorPtr& self, double other) { return other - self; })
.def("__mul__",
[](const SelectorPtr& self, const SelectorPtr& other) { return self * other; })
.def("__mul__", [](const SelectorPtr& self, double other) { return self * other; })
.def("__rmul__", [](const SelectorPtr& self, double other) { return self * other; })
.def("__truediv__",
[](const SelectorPtr& self, const SelectorPtr& other) { return self / other; })
.def("__truediv__", [](const SelectorPtr& self, double other) { return self / other; })
.def("__rtruediv__", [](const SelectorPtr& self, double other) { return other / self; })
.def("__and__",
[](const SelectorPtr& self, const SelectorPtr& other) { return self & other; })
.def("__or__", [](const SelectorPtr& self, const SelectorPtr& other) { return self | other; })
DEF_PICKLE(SEPtr);
m.def("SE_Fixed", [](double weight) { return SE_Fixed(weight); }, py::arg("weight") = 1.0);
m.def(
"SE_Fixed",
[](const py::sequence& pystks, const SystemPtr& sys, double weight) {
StockList stks = python_list_to_vector<Stock>(pystks);
return SE_Fixed(stks, sys, weight);
},
py::arg("stk_list"), py::arg("sys"), py::arg("weight") = 1.0,
R"(SE_Fixed([stk_list, sys])
:param list stk_list:
:param System sys:
:param float weight:
:return: SE选择器实例)");
m.def("SE_Signal", py::overload_cast<>(SE_Signal));
m.def("SE_Signal", py::overload_cast<const StockList&, const SystemPtr&>(SE_Signal),
R"(SE_Signal([stk_list, sys])
:param list stk_list:
:param System sys:
:return: SE选择器实例)");
m.def("SE_MultiFactor", py::overload_cast<const MFPtr&, int>(SE_MultiFactor), py::arg("mf"),
py::arg("topn") = 10);
m.def(
"SE_MultiFactor",
[](const py::sequence& inds, int topn, int ic_n, int ic_rolling_n, const py::object& ref_stk,
const string& mode) {
IndicatorList c_inds = python_list_to_vector<Indicator>(inds);
Stock c_ref_stk = ref_stk.is_none() ? getStock("sh000300") : ref_stk.cast<Stock>();
return SE_MultiFactor(c_inds, topn, ic_n, ic_rolling_n, c_ref_stk, mode);
},
py::arg("inds"), py::arg("topn") = 10, py::arg("ic_n") = 5, py::arg("ic_rolling_n") = 120,
py::arg("ref_stk") = py::none(), py::arg("mode") = "MF_ICIRWeight",
R"(SE_MultiFactor
- MF:
:param MultiFactorBase mf:
:param int topn: topn
- :
:param sequense(Indicator) inds:
:param int topn: topn 0
:param int ic_n: IC N
:param int ic_rolling_n: IC
:param Stock ref_stk: ( sh000300 300)
:param str mode: "MF_ICIRWeight" | "MF_ICWeight" | "MF_EqualWeight" )");
}