hikyuu2/hikyuu_pywrap/trade_sys/_Signal.cpp

210 lines
7.2 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* _Signal.cpp
*
* Created on: 2013-3-18
* Author: fasiondog
*/
2016-04-11 01:27:04 +08:00
#include <hikyuu/trade_sys/signal/build_in.h>
2023-12-27 18:13:05 +08:00
#include "../pybind_utils.h"
2015-01-07 01:26:14 +08:00
2023-12-27 18:13:05 +08:00
namespace py = pybind11;
2015-01-07 01:26:14 +08:00
using namespace hku;
2023-12-27 18:13:05 +08:00
class PySignalBase : public SignalBase {
PY_CLONE(PySignalBase, SignalBase)
2016-04-03 00:08:31 +08:00
2023-12-27 18:13:05 +08:00
public:
using SignalBase::SignalBase;
PySignalBase(const SignalBase& base) : SignalBase(base) {}
2015-01-07 01:26:14 +08:00
2023-12-27 18:13:05 +08:00
void _calculate() override {
PYBIND11_OVERLOAD_PURE(void, SignalBase, _calculate, );
2015-01-07 01:26:14 +08:00
}
2023-12-27 18:13:05 +08:00
void _reset() override {
PYBIND11_OVERLOAD(void, SignalBase, _reset, );
2015-01-07 01:26:14 +08:00
}
};
2023-12-27 18:13:05 +08:00
void export_Signal(py::module& m) {
py::class_<SignalBase, SGPtr, PySignalBase>(m, "SignalBase", R"(信号指示器基类
2020-07-15 00:23:48 +08:00
- alternate (bool|True) 线线
- _calculate :
- _clone :
2023-12-27 18:13:05 +08:00
- _reset : )")
.def(py::init<>())
.def(py::init<const string&>())
.def(py::init<const SignalBase&>())
2020-07-15 00:23:48 +08:00
2023-12-27 18:13:05 +08:00
.def("__str__", to_py_str<SignalBase>)
.def("__repr__", to_py_str<SignalBase>)
2020-07-15 00:23:48 +08:00
.def_property("name", py::overload_cast<>(&SignalBase::name, py::const_),
2023-12-27 18:13:05 +08:00
py::overload_cast<const string&>(&SignalBase::name),
py::return_value_policy::copy, "名称")
.def_property("to", &SignalBase::getTO, &SignalBase::setTO, "设置或获取交易对象")
2020-07-15 00:23:48 +08:00
.def("get_param", &SignalBase::getParam<boost::any>, R"(get_param(self, name)
:param str name:
:return:
:raises out_of_range: )")
2023-12-27 18:13:05 +08:00
.def("set_param", &SignalBase::setParam<boost::any>, R"(set_param(self, name, value)
2020-07-15 00:23:48 +08:00
:param str name:
:param value:
:raises logic_error: Unsupported type! )")
.def("have_param", &SignalBase::haveParam, "是否存在指定参数")
.def("should_buy", &SignalBase::shouldBuy, R"(should_buy(self, datetime)
:param Datetime datetime:
:rtype: bool)")
.def("should_sell", &SignalBase::shouldSell, R"(should_sell(self, datetime)
:param Datetime datetime:
:rtype: bool)")
.def("next_time_should_buy", &SignalBase::nextTimeShouldBuy,
R"(next_time_should_byu(self)
)")
.def("next_time_should_sell", &SignalBase::nextTimeShouldSell, R"(next_time_should_sell(self)
)")
2020-07-15 00:23:48 +08:00
.def("get_buy_signal", &SignalBase::getBuySignal, R"(get_buy_signal(self)
:rtype: DatetimeList)")
.def("get_sell_signal", &SignalBase::getSellSignal, R"(get_sell_signal(self)
:rtype: DatetimeList)")
.def("_add_buy_signal", &SignalBase::_addBuySignal, R"(_add_buy_signal(self, datetime)
_calculate中调用
:param Datetime datetime: )")
.def("_add_sell_signal", &SignalBase::_addSellSignal, R"(_add_sell_signal(self, datetime)
_calculate中调用
:param Datetime datetime: )")
.def("reset", &SignalBase::reset, "复位操作")
.def("clone", &SignalBase::clone, "克隆操作")
2023-12-27 18:13:05 +08:00
.def("_calculate", &SignalBase::_calculate, "【重载接口】子类计算接口")
.def("_reset", &SignalBase::_reset, "【重载接口】子类复位接口,复位内部私有变量")
2015-01-07 01:26:14 +08:00
2023-12-27 18:13:05 +08:00
DEF_PICKLE(SGPtr);
2015-01-07 01:26:14 +08:00
2023-12-27 18:13:05 +08:00
m.def("SG_Bool", SG_Bool, py::arg("buy"), py::arg("sell"),
R"(SG_Bool(buy, sell)
2020-07-15 00:23:48 +08:00
使bool数组的Indicator分别作为买入
:param Indicator buy: Indicator中相应位置>0
:param Indicator sell: Indicator中相应位置>0
:return: )");
2023-12-27 18:13:05 +08:00
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])
2020-07-15 00:23:48 +08:00
线使 [BOOK1]_ 线线::
filter = percentage * STDEV((AMA-AMA[1], N)
Buy When AMA - AMA[1] > filter
or Buy When AMA - AMA[2] > filter
or Buy When AMA - AMA[3] > filter
:param Indicator ind:
:param int filer_n: N日周期
:param float filter_p:
:return: )");
2015-01-07 01:26:14 +08:00
2023-12-27 18:13:05 +08:00
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])
2020-07-15 00:23:48 +08:00
线2 [BOOK1]_::
2015-01-07 01:26:14 +08:00
2020-07-15 00:23:48 +08:00
filter = percentage * STDEV((AMA-AMA[1], N)
Buy When AMA - @lowest(AMA,n) > filter
Sell When @highest(AMA, n) - AMA > filter
:param Indicator ind:
:param int filer_n: N日周期
:param float filter_p:
:return: )");
2023-12-27 18:13:05 +08:00
m.def("SG_Cross", SG_Cross, py::arg("fast"), py::arg("slow"),
R"(SG_Cross(fast, slow)
2020-07-15 00:23:48 +08:00
线线穿线线穿线5MA上穿10日MA时买入5MA线下穿MA10日线时卖出::
SG_Cross(MA(C, n=10), MA(C, n=30))
2020-07-15 00:23:48 +08:00
:param Indicator fast: 线
:param Indicator slow: 线
:return: )");
2023-12-27 18:13:05 +08:00
m.def("SG_CrossGold", SG_CrossGold, py::arg("fast"), py::arg("slow"),
R"(SG_CrossGold(fast, slow)
2020-07-15 00:23:48 +08:00
线穿线线线
线穿线线线::
SG_CrossGold(MA(C, n=10), MA(C, n=30))
2020-07-15 00:23:48 +08:00
:param Indicator fast: 线
:param Indicator slow: 线
:return: )");
2023-12-27 18:13:05 +08:00
m.def("SG_Flex", SG_Flex, py::arg("op"), py::arg("slow_n"),
R"(SG_Flex(ind, slow_n)
2020-07-15 00:23:48 +08:00
使EMA(slow_n)线线线穿线线穿线
:param Indicator ind:
:param int slow_n: 线EMA周期
:return: )");
2023-12-27 18:13:05 +08:00
m.def("SG_Band", SG_Band, py::arg("ind"), py::arg("lower"), py::arg("upper"),
R"(SG_Band(ind, lower, upper)
,
::
SG_Band(MA(C, n=10), 100, 200)
)");
2020-07-15 00:23:48 +08:00
}