2015-01-07 01:26:14 +08:00
|
|
|
|
/*
|
|
|
|
|
* _Slippage.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 2013-3-21
|
|
|
|
|
* Author: fasiondog
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <hikyuu/trade_sys/slippage/SlippageBase.h>
|
2016-05-07 02:21:07 +08:00
|
|
|
|
#include <hikyuu/trade_sys/slippage/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 PySlippageBase : public SlippageBase {
|
|
|
|
|
PY_CLONE(PySlippageBase, SlippageBase)
|
2016-05-07 02:21:07 +08:00
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
public:
|
|
|
|
|
using SlippageBase::SlippageBase;
|
2024-03-29 01:10:05 +08:00
|
|
|
|
PySlippageBase(const SlippageBase& base) : SlippageBase(base) {}
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
void _calculate() override {
|
|
|
|
|
PYBIND11_OVERLOAD_PURE(void, SlippageBase, _calculate, );
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
void _reset() override {
|
|
|
|
|
PYBIND11_OVERLOAD(void, SlippageBase, _reset, );
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
price_t getRealBuyPrice(const Datetime& datetime, price_t planPrice) override {
|
|
|
|
|
PYBIND11_OVERLOAD_PURE_NAME(price_t, SlippageBase, "get_real_buy_price", getRealBuyPrice,
|
|
|
|
|
datetime, planPrice);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
price_t getRealSellPrice(const Datetime& datetime, price_t planPrice) override {
|
|
|
|
|
PYBIND11_OVERLOAD_PURE_NAME(price_t, SlippageBase, "get_real_sell_price", getRealSellPrice,
|
|
|
|
|
datetime, planPrice);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
void export_Slippage(py::module& m) {
|
2024-05-13 05:23:37 +08:00
|
|
|
|
py::class_<SlippageBase, SPPtr, PySlippageBase>(m, "SlippageBase", py::dynamic_attr(),
|
|
|
|
|
R"(移滑价差算法基类
|
2020-07-15 00:23:48 +08:00
|
|
|
|
|
|
|
|
|
自定义移滑价差接口:
|
|
|
|
|
|
|
|
|
|
- getRealBuyPrice : 【必须】计算实际买入价格
|
|
|
|
|
- getRealSellPrice : 【必须】计算实际卖出价格
|
|
|
|
|
- _calculate : 【必须】子类计算接口
|
|
|
|
|
- _clone : 【必须】克隆接口
|
2023-12-27 18:13:05 +08:00
|
|
|
|
- _reset : 【可选】重载私有变量)")
|
|
|
|
|
|
|
|
|
|
.def(py::init<>())
|
2024-03-29 01:10:05 +08:00
|
|
|
|
.def(py::init<const SlippageBase&>())
|
2023-12-27 18:13:05 +08:00
|
|
|
|
.def(py::init<const string&>(), R"(初始化构造函数
|
|
|
|
|
|
|
|
|
|
:param str name: 名称)")
|
|
|
|
|
|
|
|
|
|
.def("__str__", to_py_str<SlippageBase>)
|
|
|
|
|
.def("__repr__", to_py_str<SlippageBase>)
|
2020-07-15 00:23:48 +08:00
|
|
|
|
|
2023-12-31 19:18:49 +08:00
|
|
|
|
.def_property("name", py::overload_cast<>(&SlippageBase::name, py::const_),
|
2023-12-27 18:13:05 +08:00
|
|
|
|
py::overload_cast<const string&>(&SlippageBase::name),
|
|
|
|
|
py::return_value_policy::copy, "名称")
|
|
|
|
|
.def_property("to", &SlippageBase::getTO, &SlippageBase::setTO, "关联交易对象")
|
2020-07-15 00:23:48 +08:00
|
|
|
|
|
|
|
|
|
.def("get_param", &SlippageBase::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", &SlippageBase::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", &SlippageBase::haveParam, "是否存在指定参数")
|
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
.def("get_real_buy_price", &SlippageBase::getRealBuyPrice,
|
2020-07-15 00:23:48 +08:00
|
|
|
|
R"(get_real_buy_price(self, datetime, price)
|
|
|
|
|
|
|
|
|
|
【重载接口】计算实际买入价格
|
|
|
|
|
|
|
|
|
|
:param Datetime datetime: 买入时间
|
|
|
|
|
:param float price: 计划买入价格
|
|
|
|
|
:return: 实际买入价格
|
|
|
|
|
:rtype: float)")
|
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
.def("get_real_sell_price", &SlippageBase::getRealSellPrice,
|
2020-07-15 00:23:48 +08:00
|
|
|
|
R"(get_real_sell_price(self, datetime, price)
|
|
|
|
|
|
|
|
|
|
【重载接口】计算实际卖出价格
|
|
|
|
|
|
|
|
|
|
:param Datetime datetime: 卖出时间
|
|
|
|
|
:param float price: 计划卖出价格
|
|
|
|
|
:return: 实际卖出价格
|
|
|
|
|
:rtype: float)")
|
|
|
|
|
|
|
|
|
|
.def("reset", &SlippageBase::reset, "复位操作")
|
|
|
|
|
.def("clone", &SlippageBase::clone, "克隆操作")
|
2023-12-27 18:13:05 +08:00
|
|
|
|
.def("_calculate", &SlippageBase::_calculate, "【重载接口】子类计算接口")
|
|
|
|
|
.def("_reset", &SlippageBase::_reset, "【重载接口】子类复位接口,复位内部私有变量")
|
2020-07-15 00:23:48 +08:00
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
DEF_PICKLE(SPPtr);
|
2016-05-07 02:21:07 +08:00
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
m.def("SP_FixedPercent", SP_FixedPercent, py::arg("p") = 0.001,
|
|
|
|
|
R"(SP_FixedPercent([p=0.001])
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2020-07-15 00:23:48 +08:00
|
|
|
|
固定百分比移滑价差算法,买入实际价格 = 计划买入价格 * (1 + p),卖出实际价格 = 计划卖出价格 * (1 - p)
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2020-07-15 00:23:48 +08:00
|
|
|
|
:param float p: 偏移的固定百分比
|
|
|
|
|
:return: 移滑价差算法实例)");
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2023-12-27 18:13:05 +08:00
|
|
|
|
m.def("SP_FixedValue", SP_FixedValue, py::arg("value") = 0.01, R"(SP_FixedValuet([p=0.001])
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2020-07-15 00:23:48 +08:00
|
|
|
|
固定价格移滑价差算法,买入实际价格 = 计划买入价格 + 偏移价格,卖出实际价格 = 计划卖出价格 - 偏移价格
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
2020-07-15 00:23:48 +08:00
|
|
|
|
:param float p: 偏移价格
|
|
|
|
|
:return: 移滑价差算法实例)");
|
|
|
|
|
}
|