2015-01-07 01:26:14 +08:00
|
|
|
|
/*
|
|
|
|
|
* _MoneyManager.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 2013-3-13
|
|
|
|
|
* Author: fasiondog
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <boost/python.hpp>
|
|
|
|
|
#include <hikyuu/trade_sys/moneymanager/build_in.h>
|
2016-04-28 02:39:49 +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 MoneyManagerWrap : public MoneyManagerBase, public wrapper<MoneyManagerBase> {
|
|
|
|
|
public:
|
2016-04-28 02:39:49 +08:00
|
|
|
|
MoneyManagerWrap() : MoneyManagerBase() {}
|
2020-05-12 00:26:29 +08:00
|
|
|
|
MoneyManagerWrap(const string& name) : MoneyManagerBase(name) {}
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
void buyNotify(const TradeRecord& record) {
|
|
|
|
|
if (override buyNotify = this->get_override("buyNotify")) {
|
|
|
|
|
buyNotify(record);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
MoneyManagerBase::buyNotify(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void default_buyNotify(const TradeRecord& record) {
|
|
|
|
|
MoneyManagerBase::buyNotify(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sellNotify(const TradeRecord& record) {
|
|
|
|
|
if (override sellNotify = this->get_override("sellNotify")) {
|
|
|
|
|
sellNotify(record);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
MoneyManagerBase::sellNotify(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void default_sellNotify(const TradeRecord& record) {
|
|
|
|
|
MoneyManagerBase::sellNotify(record);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
double _getSellNumber(const Datetime& datetime, const Stock& stock, price_t price, price_t risk,
|
|
|
|
|
SystemPart from) {
|
2016-04-03 00:08:31 +08:00
|
|
|
|
if (override _getSellNumber = this->get_override("_getSellNumber")) {
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return _getSellNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return MoneyManagerBase::_getSellNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
double default_getSellNumber(const Datetime& datetime, const Stock& stock, price_t price,
|
|
|
|
|
price_t risk, SystemPart from) {
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return MoneyManagerBase::_getSellNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
double _getBuyNumber(const Datetime& datetime, const Stock& stock, price_t price, price_t risk,
|
|
|
|
|
SystemPart from) {
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return this->get_override("_getBuyNumber")(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
double _getSellShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
|
|
|
|
|
price_t risk, SystemPart from) {
|
2016-04-03 00:08:31 +08:00
|
|
|
|
if (override _getSellShortNumber = this->get_override("_getSellShortNumber")) {
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return _getSellShortNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return MoneyManagerBase::_getSellShortNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
double default_getSellShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
|
|
|
|
|
price_t risk, SystemPart from) {
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return MoneyManagerBase::_getSellShortNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
double getBuyShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
|
|
|
|
|
price_t risk, SystemPart from) {
|
2016-04-03 00:08:31 +08:00
|
|
|
|
if (override _getBuyShortNumber = this->get_override("_getBuyShortNumber")) {
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return _getBuyShortNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return MoneyManagerBase::_getBuyShortNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
double default_getBuyShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
|
|
|
|
|
price_t risk, SystemPart from) {
|
2018-01-11 02:54:36 +08:00
|
|
|
|
return MoneyManagerBase::_getBuyShortNumber(datetime, stock, price, risk, from);
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _reset() {
|
2018-04-25 02:43:55 +08:00
|
|
|
|
if (override func = get_override("_reset")) {
|
|
|
|
|
func();
|
|
|
|
|
} else {
|
|
|
|
|
MoneyManagerBase::_reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
void default_reset() {
|
2018-04-25 02:43:55 +08:00
|
|
|
|
this->MoneyManagerBase::_reset();
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MoneyManagerPtr _clone() {
|
|
|
|
|
return this->get_override("_clone")();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-17 16:59:15 +08:00
|
|
|
|
string (MoneyManagerBase::*mm_get_name)() const = &MoneyManagerBase::name;
|
|
|
|
|
void (MoneyManagerBase::*mm_set_name)(const string&) = &MoneyManagerBase::name;
|
2016-04-28 02:39:49 +08:00
|
|
|
|
|
2015-01-07 01:26:14 +08:00
|
|
|
|
void export_MoneyManager() {
|
2020-07-13 23:52:35 +08:00
|
|
|
|
class_<MoneyManagerWrap, boost::noncopyable>("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 : 【必须】克隆接口)",
|
|
|
|
|
init<>())
|
2020-05-12 00:26:29 +08:00
|
|
|
|
.def(init<const string&>())
|
|
|
|
|
.def(self_ns::str(self))
|
2020-07-13 23:52:35 +08:00
|
|
|
|
.def(self_ns::repr(self))
|
|
|
|
|
|
|
|
|
|
.add_property("name", mm_get_name, mm_set_name, "名称")
|
|
|
|
|
|
|
|
|
|
.def("getParam", &MoneyManagerBase::getParam<boost::any>, R"(get_param(self, name)
|
|
|
|
|
|
|
|
|
|
获取指定的参数
|
|
|
|
|
|
|
|
|
|
:param str name: 参数名称
|
|
|
|
|
:return: 参数值
|
|
|
|
|
:raises out_of_range: 无此参数)")
|
|
|
|
|
|
|
|
|
|
.def("setParam", &MoneyManagerBase::setParam<object>, R"(set_param(self, name, value)
|
|
|
|
|
|
|
|
|
|
设置参数
|
|
|
|
|
|
|
|
|
|
:param str name: 参数名称
|
|
|
|
|
:param value: 参数值
|
|
|
|
|
:raises logic_error: Unsupported type! 不支持的参数类型)")
|
|
|
|
|
|
|
|
|
|
.def("haveParam", &MoneyManagerBase::haveParam, "是否存在指定参数")
|
|
|
|
|
|
2020-05-12 00:26:29 +08:00
|
|
|
|
.def("setTM", &MoneyManagerBase::setTM)
|
|
|
|
|
.def("getTM", &MoneyManagerBase::getTM)
|
|
|
|
|
.def("setQuery", &MoneyManagerBase::setQuery)
|
|
|
|
|
.def("getQuery", &MoneyManagerBase::getQuery)
|
|
|
|
|
.def("reset", &MoneyManagerBase::reset)
|
|
|
|
|
.def("clone", &MoneyManagerBase::clone)
|
|
|
|
|
.def("buyNotify", &MoneyManagerBase::buyNotify, &MoneyManagerWrap::default_buyNotify)
|
|
|
|
|
.def("sellNotify", &MoneyManagerBase::sellNotify, &MoneyManagerWrap::default_sellNotify)
|
|
|
|
|
|
|
|
|
|
.def("getBuyNumber", &MoneyManagerBase::getBuyNumber)
|
|
|
|
|
.def("getSellNumber", &MoneyManagerBase::getSellNumber)
|
|
|
|
|
.def("getSellShortNumber", &MoneyManagerBase::getSellShortNumber)
|
|
|
|
|
.def("getBuyShortNumber", &MoneyManagerBase::getBuyShortNumber)
|
|
|
|
|
|
|
|
|
|
.def("_getBuyNumber", pure_virtual(&MoneyManagerBase::_getBuyNumber))
|
|
|
|
|
.def("_getSellNumber", &MoneyManagerBase::_getSellNumber,
|
|
|
|
|
&MoneyManagerWrap::default_getSellNumber)
|
|
|
|
|
.def("_getSellShortNumber", &MoneyManagerBase::_getSellShortNumber,
|
|
|
|
|
&MoneyManagerWrap::default_getSellShortNumber)
|
|
|
|
|
.def("_getBuyShortNumber", &MoneyManagerBase::_getBuyShortNumber,
|
|
|
|
|
&MoneyManagerWrap::default_getBuyShortNumber)
|
|
|
|
|
.def("_reset", &MoneyManagerBase::_reset, &MoneyManagerWrap::default_reset)
|
|
|
|
|
.def("_clone", pure_virtual(&MoneyManagerBase::_clone))
|
2015-01-07 01:26:14 +08:00
|
|
|
|
#if HKU_PYTHON_SUPPORT_PICKLE
|
2020-05-12 00:26:29 +08:00
|
|
|
|
.def_pickle(name_init_pickle_suite<MoneyManagerBase>())
|
2015-01-07 01:26:14 +08:00
|
|
|
|
#endif
|
2020-05-12 00:26:29 +08:00
|
|
|
|
;
|
2015-01-07 01:26:14 +08:00
|
|
|
|
|
|
|
|
|
register_ptr_to_python<MoneyManagerPtr>();
|
|
|
|
|
|
2017-05-22 01:39:55 +08:00
|
|
|
|
def("MM_Nothing", MM_Nothing);
|
2020-05-12 00:26:29 +08:00
|
|
|
|
def("MM_FixedRisk", MM_FixedRisk, (arg("risk") = 1000.00));
|
2016-05-03 02:44:03 +08:00
|
|
|
|
def("MM_FixedCapital", MM_FixedCapital, (arg("capital") = 10000.00));
|
2020-05-12 00:26:29 +08:00
|
|
|
|
def("MM_FixedCount", MM_FixedCount, (arg("n") = 100));
|
|
|
|
|
def("MM_FixedPercent", MM_FixedPercent, (arg("p") = 0.03));
|
2016-05-05 02:56:45 +08:00
|
|
|
|
def("MM_FixedUnits", MM_FixedUnits, (arg("n") = 33));
|
2020-05-12 00:26:29 +08:00
|
|
|
|
def("MM_WilliamsFixedRisk", MM_WilliamsFixedRisk, (arg("p") = 0.1, arg("max_loss") = 1000.0));
|
2015-01-07 01:26:14 +08:00
|
|
|
|
}
|