hikyuu2/hikyuu_pywrap/trade_sys/_Signal.cpp

234 lines
8.1 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* _Signal.cpp
*
* Created on: 2013-3-18
* Author: fasiondog
*/
#include <boost/python.hpp>
2016-04-11 01:27:04 +08:00
#include <hikyuu/trade_sys/signal/build_in.h>
2016-04-03 00:08:31 +08:00
#include "../_Parameter.h"
2015-01-07 01:26:14 +08:00
#include "../pickle_support.h"
using namespace boost::python;
using namespace hku;
class SignalWrap : public SignalBase, public wrapper<SignalBase> {
public:
2020-07-15 00:23:48 +08:00
SignalWrap() : SignalBase() {}
2016-04-03 00:08:31 +08:00
SignalWrap(const string& name) : SignalBase(name) {}
2015-01-07 01:26:14 +08:00
void _reset() {
2016-04-03 00:08:31 +08:00
if (override func = this->get_override("_reset")) {
func();
} else {
SignalBase::_reset();
}
}
void default_reset() {
this->SignalBase::_reset();
2015-01-07 01:26:14 +08:00
}
SignalPtr _clone() {
return this->get_override("_clone")();
}
void _calculate() {
2016-04-03 00:08:31 +08:00
this->get_override("_calculate")();
2015-01-07 01:26:14 +08:00
}
};
2017-06-17 16:59:15 +08:00
string (SignalBase::*sg_get_name)() const = &SignalBase::name;
void (SignalBase::*sg_set_name)(const string&) = &SignalBase::name;
2015-01-07 01:26:14 +08:00
void export_Signal() {
2020-07-15 00:23:48 +08:00
class_<SignalWrap, boost::noncopyable>("SignalBase", R"(信号指示器基类
- alternate (bool|True) 线线
- _calculate :
- _clone :
- _reset : )",
init<>())
.def(init<const string&>())
.def(self_ns::str(self))
.def(self_ns::repr(self))
.add_property("name", sg_get_name, sg_set_name, "名称")
.add_property("to", &SignalBase::getTO, &SignalBase::setTO, "设置或获取交易对象")
.def("get_param", &SignalBase::getParam<boost::any>, R"(get_param(self, name)
:param str name:
:return:
:raises out_of_range: )")
.def("set_param", &SignalBase::setParam<object>, R"(set_param(self, name, value)
: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, "克隆操作")
.def("_calculate", pure_virtual(&SignalBase::_calculate), "【重载接口】子类计算接口")
.def("_reset", &SignalBase::_reset, &SignalWrap::default_reset,
"【重载接口】子类复位接口,复位内部私有变量")
.def("_clone", pure_virtual(&SignalBase::_clone), "【重载接口】子类克隆接口")
2015-01-07 01:26:14 +08:00
#if HKU_PYTHON_SUPPORT_PICKLE
2020-07-15 00:23:48 +08:00
.def_pickle(name_init_pickle_suite<SignalBase>())
2015-01-07 01:26:14 +08:00
#endif
2020-07-15 00:23:48 +08:00
;
2015-01-07 01:26:14 +08:00
register_ptr_to_python<SignalPtr>();
2020-07-15 00:23:48 +08:00
def("SG_Bool", SG_Bool, (arg("buy"), arg("sell"), arg("kpart") = "CLOSE"),
R"(SG_Bool(buy, sell[, kpart='CLOSE'])
使bool数组的Indicator分别作为买入
:param Indicator buy: Indicator中相应位置>0
:param Indicator sell: Indicator中相应位置>0
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return: )");
def("SG_Single", SG_Single,
(arg("ind"), arg("filter_n") = 10, arg("filter_p") = 0.1, arg("kpart") = "CLOSE"),
R"(SG_Single(ind[, filter_n = 10, filter_p = 0.1, kpart='CLOSE'])
线使 [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:
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return: )");
2015-01-07 01:26:14 +08:00
2020-07-15 00:23:48 +08:00
def("SG_Single2", SG_Single2,
(arg("ind"), arg("filter_n") = 10, arg("filter_p") = 0.1, arg("kpart") = "CLOSE"),
R"(SG_Single2(ind[, filter_n = 10, filter_p = 0.1, kpart='CLOSE'])
线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:
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return: )");
def("SG_Cross", SG_Cross, (arg("fast"), arg("slow"), arg("kpart") = "CLOSE"),
R"(SG_Cross(fast, slow[, kpart = "CLOSE"])
线线穿线线穿线5MA上穿10日MA时买入5MA线下穿MA10日线时卖出::
SG_Cross(OP(MA(n=10)), OP(MA(n=30)))
:param Indicator fast: 线
:param Indicator slow: 线
:param string kpart: OPEN|HIGH|LOW|CLOSE|AMO|VOL|KDATA
:return: )");
def("SG_CrossGold", SG_CrossGold, (arg("fast"), arg("slow"), arg("kpart") = "CLOSE"),
R"(SG_CrossGold(fast, slow[, kpart = "CLOSE"])
线穿线线线
线穿线线线::
SG_CrossGold(OP(MA(n=10)), OP(MA(n=30)))
:param Indicator fast: 线
:param Indicator slow: 线
:param string kpart: OPEN|HIGH|LOW|CLOSE|AMO|VOL|KDATA
:return: )");
def("SG_Flex", SG_Flex, (arg("op"), arg("slow_n"), arg("kpart") = "CLOSE"),
R"(SG_Flex(ind, slow_n[, kpart = 'CLOSE'])
使EMA(slow_n)线线线穿线线穿线
:param Indicator ind:
:param int slow_n: 线EMA周期
:param string kpart: KDATA|OPEN|HIGH|LOW|CLOSE|AMO|VOL
:return: )");
def ("SG_Band", SG_Band, (arg("ind"), arg("lower"), arg("upper"), arg("kpart") = "CLOSE"),
R"(SG_Band(ind, lower, upper[, kpart = 'CLOSE'])
,
::
SG_Band(OP(MA(n=10)), 100, 200)
)");
2020-07-15 00:23:48 +08:00
}