hikyuu2/hikyuu_pywrap/trade_sys/_MoneyManager.cpp

235 lines
9.7 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.

/*
* _MoneyManager.cpp
*
* Created on: 2013-3-13
* Author: fasiondog
*/
#include <hikyuu/trade_sys/moneymanager/build_in.h>
#include "../pybind_utils.h"
namespace py = pybind11;
using namespace hku;
class PyMoneyManagerBase : public MoneyManagerBase {
PY_CLONE(PyMoneyManagerBase, MoneyManagerBase)
public:
using MoneyManagerBase::MoneyManagerBase;
void _reset() override {
PYBIND11_OVERLOAD(void, MoneyManagerBase, _reset, );
}
void buyNotify(const TradeRecord& tr) override {
PYBIND11_OVERLOAD_NAME(void, MoneyManagerBase, "buy_notify", buyNotify, tr);
}
void sellNotify(const TradeRecord& tr) override {
PYBIND11_OVERLOAD_NAME(void, MoneyManagerBase, "sell_notify", sellNotify, tr);
}
double _getBuyNumber(const Datetime& datetime, const Stock& stock, price_t price, price_t risk,
SystemPart from) override {
PYBIND11_OVERLOAD_PURE_NAME(double, MoneyManagerBase, "_get_buy_num", _getBuyNumber,
datetime, stock, price, risk, from);
}
double _getSellNumber(const Datetime& datetime, const Stock& stock, price_t price, price_t risk,
SystemPart from) override {
PYBIND11_OVERLOAD_NAME(double, MoneyManagerBase, "_get_sell_num", _getSellNumber, datetime,
stock, price, risk, from);
}
double _getSellShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
price_t risk, SystemPart from) override {
PYBIND11_OVERLOAD_NAME(double, MoneyManagerBase, "_get_sell_short_num", _getSellShortNumber,
datetime, stock, price, risk, from);
}
double _getBuyShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
price_t risk, SystemPart from) override {
PYBIND11_OVERLOAD_NAME(double, MoneyManagerBase, "_get_buy_short_num", _getBuyShortNumber,
datetime, stock, price, risk, from);
}
};
void export_MoneyManager(py::module& m) {
py::class_<MoneyManagerBase, MMPtr, PyMoneyManagerBase>(m, "MoneyManagerBase",
R"(资金管理策略基类
- auto-checkin=False (bool) : checkin
- max-stock=20000 (int) :
- disable_ev_force_clean_position=False (bool) :
- disable_cn_force_clean_position=False (bool) :
- buyNotify :
- sellNotify :
- _getBuyNumber :
- _getSellNumber :
- _reset :
- _clone : )")
.def(py::init<>())
.def(py::init<const string&>(), R"(初始化构造函数
:param str name: )")
.def("__str__", to_py_str<MoneyManagerBase>)
.def("__repr__", to_py_str<MoneyManagerBase>)
.def_property("name", py::overload_cast<>(&MoneyManagerBase::name, py::const_),
py::overload_cast<const string&>(&MoneyManagerBase::name),
py::return_value_policy::copy, "名称")
.def_property("tm", &MoneyManagerBase::getTM, &MoneyManagerBase::setTM,
"设置或获取交易管理对象")
.def_property("query", &MoneyManagerBase::getQuery, &MoneyManagerBase::setQuery,
py::return_value_policy::copy, "设置或获取查询条件")
.def("get_param", &MoneyManagerBase::getParam<boost::any>, R"(get_param(self, name)
:param str name:
:return:
:raises out_of_range: )")
.def("set_param", &MoneyManagerBase::setParam<boost::any>, R"(set_param(self, name, value)
:param str name:
:param value:
:raises logic_error: Unsupported type! )")
.def("have_param", &MoneyManagerBase::haveParam, "是否存在指定参数")
.def("reset", &MoneyManagerBase::reset, "复位操作")
.def("clone", &MoneyManagerBase::clone, "克隆操作")
.def("buy_notify", &MoneyManagerBase::buyNotify,
R"(buy_notify(self, trade_record)
:param TradeRecord trade_record: )")
.def("sell_notify", &MoneyManagerBase::sellNotify,
R"(sell_notify(self, trade_record)
:param TradeRecord trade_record: )")
.def("get_buy_num", &MoneyManagerBase::getBuyNumber,
R"(get_buy_num(self, datetime, stock, price, risk, part_from)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:param System.Part part_from:
:return:
:rtype: float)")
.def("get_sell_num", &MoneyManagerBase::getSellNumber,
R"(get_sell_num(self, datetime, stock, price, risk, part_from)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:param System.Part part_from:
:return:
:rtype: float)")
.def("_get_buy_num", &MoneyManagerBase::_getBuyNumber,
R"(_get_buy_num(self, datetime, stock, price, risk, part_from)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:param System.Part part_from:
:return:
:rtype: float)")
.def("_get_sell_num", &MoneyManagerBase::_getSellNumber,
R"(_get_sell_num(self, datetime, stock, price, risk, part_from)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:param System.Part part_from:
:return:
:rtype: float)")
.def("get_sell_short_num", &MoneyManagerBase::getSellShortNumber)
.def("get_buy_short_num", &MoneyManagerBase::getBuyShortNumber)
.def("_get_sell_short_num", &MoneyManagerBase::_getSellShortNumber)
.def("_get_buy_short_num", &MoneyManagerBase::_getBuyShortNumber)
.def("_reset", &MoneyManagerBase::_reset, R"(【重载接口】子类复位接口,复位内部私有变量)")
DEF_PICKLE(MMPtr);
m.def("MM_Nothing", MM_Nothing, R"(MM_Nothing()
)");
m.def("MM_FixedRisk", MM_FixedRisk, py::arg("risk") = 1000.00,
R"(MM_FixedRisk([risk = 1000.00])
1000 = /
:param float risk:
:return: )");
m.def("MM_FixedCapital", MM_FixedCapital, py::arg("capital") = 10000.00,
R"(MM_FixedCapital([capital = 10000.0])
:param float capital:
:return: )");
m.def("MM_FixedCount", MM_FixedCount, py::arg("n") = 100, R"(MM_FixedCount([n = 100])
:param float n:
:return: )");
m.def("MM_FixedPercent", MM_FixedPercent, py::arg("p") = 0.03, R"(MM_FixedPercent([p = 0.03])
P * / R[BOOK3]_, [BOOK4]_ .
:param float p:
:return: )");
m.def("MM_FixedUnits", MM_FixedUnits, py::arg("n") = 33, R"(MM_FixedUnits([n = 33])
:param int n: n个资金单位
:return: )");
m.def("MM_WilliamsFixedRisk", MM_WilliamsFixedRisk, py::arg("p") = 0.1,
py::arg("max_loss") = 1000.0,
R"( MM_WilliamsFixedRisk([p=0.1, max_loss=1000.0])
)");
}