hikyuu2/hikyuu_pywrap/trade_sys/_Stoploss.cpp

155 lines
5.8 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* _Stoploss.cpp
*
* Created on: 2013-3-21
* Author: fasiondog
*/
#include <boost/python.hpp>
#include <hikyuu/trade_sys/stoploss/build_in.h>
2016-04-18 19:54:42 +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 StoplossWrap : public StoplossBase, public wrapper<StoplossBase> {
public:
2020-07-15 00:23:48 +08:00
StoplossWrap() : StoplossBase() {}
StoplossWrap(const string& name) : StoplossBase(name) {}
2015-01-07 01:26:14 +08:00
virtual ~StoplossWrap() {}
void _reset() {
2016-04-03 00:08:31 +08:00
if (override func = this->get_override("_reset")) {
func();
} else {
StoplossBase::_reset();
}
}
void default_reset() {
this->StoplossBase::_reset();
2015-01-07 01:26:14 +08:00
}
StoplossPtr _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
}
price_t getPrice(const Datetime& datetime, price_t price) {
return this->get_override("getPrice")(datetime, price);
}
price_t getShortPrice(const Datetime& datetime, price_t price) {
if (override getShortPrice = get_override("getShortPrice")) {
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);
}
};
2017-06-17 16:59:15 +08:00
string (StoplossBase::*st_get_name)() const = &StoplossBase::name;
void (StoplossBase::*st_set_name)(const string&) = &StoplossBase::name;
2015-01-07 01:26:14 +08:00
void export_Stoploss() {
2020-07-15 00:23:48 +08:00
class_<StoplossWrap, boost::noncopyable>("StoplossBase", R"(止损/止赢算法基类
/
- _calculate :
- _clone :
- _reset : )",
init<>())
.def(init<const string&>())
.def(self_ns::str(self))
2020-08-26 00:25:45 +08:00
.def(self_ns::repr(self))
2020-07-15 00:23:48 +08:00
.add_property("name", st_get_name, st_set_name, "名称")
.add_property("to", &StoplossBase::getTO, &StoplossBase::setTO, "设置或获取交易对象")
.add_property("tm", &StoplossBase::getTM, &StoplossBase::setTM, "设置或获取交易管理实例")
.def("get_param", &StoplossBase::getParam<boost::any>, R"(get_param(self, name)
:param str name:
:return:
:raises out_of_range: )")
.def("set_param", &StoplossBase::setParam<object>, R"(set_param(self, name, value)
:param str name:
:param value:
:raises logic_error: Unsupported type! )")
.def("have_param", &StoplossBase::haveParam, "是否存在指定参数")
.def("get_price", pure_virtual(&StoplossBase::getPrice), R"(get_price(self, datetime, price)
0
.. note::
/getPrice可以传入计划交易的价格30%price参数price为0.0使使price参数使30%price参数
:param Datetime datetime:
:param float price:
:return:
:rtype: float)")
//.def("getShortPrice", &StoplossBase::getShortPrice, &StoplossWrap::default_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), "【重载接口】子类克隆接口")
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<StoplossBase>())
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<StoplossPtr>();
2020-07-15 00:23:48 +08:00
def("ST_FixedPercent", ST_FixedPercent, (arg("p") = 0.03), R"(ST_FixedPercent([p=0.03])
:param float p: (0,1]
:return: /)");
2015-01-07 01:26:14 +08:00
2020-07-15 00:23:48 +08:00
def("ST_Indicator", ST_Indicator, (arg("op"), arg("kpart") = "CLOSE"),
R"(ST_Indicator(op[, kpart="CLOSE"])
2015-01-07 01:26:14 +08:00
2020-07-15 00:23:48 +08:00
使使10EMA作为止损::
ST_Indicator(OP(EMA(n=10)))
:param Indicator op:
: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])
2007 .(Alexander Elder) P202
1020穿穿
线线
N日3
:param int n1: 10
:param int n2: 线n2日内的最高值3
:param double p: 2
:return: /)");
}