hikyuu2/hikyuu_pywrap/trade_sys/_MoneyManager.cpp

289 lines
12 KiB
C++
Raw Normal View History

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() {}
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);
}
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")) {
return _getSellNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
return MoneyManagerBase::_getSellNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
double default_getSellNumber(const Datetime& datetime, const Stock& stock, price_t price,
price_t risk, SystemPart from) {
return MoneyManagerBase::_getSellNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
double _getBuyNumber(const Datetime& datetime, const Stock& stock, price_t price, price_t risk,
SystemPart from) {
return this->get_override("_getBuyNumber")(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +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")) {
return _getSellShortNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
return MoneyManagerBase::_getSellShortNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
double default_getSellShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
price_t risk, SystemPart from) {
return MoneyManagerBase::_getSellShortNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +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")) {
return _getBuyShortNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
return MoneyManagerBase::_getBuyShortNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
double default_getBuyShortNumber(const Datetime& datetime, const Stock& stock, price_t price,
price_t risk, SystemPart from) {
return MoneyManagerBase::_getBuyShortNumber(datetime, stock, price, risk, from);
2015-01-07 01:26:14 +08:00
}
void _reset() {
if (override func = get_override("_reset")) {
func();
} else {
MoneyManagerBase::_reset();
}
}
void default_reset() {
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<>())
.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, "名称")
2020-07-15 00:23:48 +08:00
.add_property("tm", &MoneyManagerBase::getTM, &MoneyManagerBase::setTM,
"设置或获取交易管理对象")
.add_property("query", &MoneyManagerBase::getQuery, &MoneyManagerBase::setQuery,
"设置或获取查询条件")
2020-07-13 23:52:35 +08:00
2020-07-15 00:23:48 +08:00
.def("get_param", &MoneyManagerBase::getParam<boost::any>, R"(get_param(self, name)
2020-07-13 23:52:35 +08:00
:param str name:
:return:
:raises out_of_range: )")
2020-07-15 00:23:48 +08:00
.def("set_param", &MoneyManagerBase::setParam<object>, R"(set_param(self, name, value)
2020-07-13 23:52:35 +08:00
:param str name:
:param value:
:raises logic_error: Unsupported type! )")
.def("haveParam", &MoneyManagerBase::haveParam, "是否存在指定参数")
2020-07-15 00:23:48 +08:00
.def("reset", &MoneyManagerBase::reset, "复位操作")
.def("clone", &MoneyManagerBase::clone, "克隆操作")
.def("buy_notify", &MoneyManagerBase::buyNotify, &MoneyManagerWrap::default_buyNotify,
R"(buy_notify(self, trade_record)
:param TradeRecord trade_record: )")
.def("sell_notify", &MoneyManagerBase::sellNotify, &MoneyManagerWrap::default_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)")
2020-07-15 00:23:48 +08:00
.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", pure_virtual(&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,
&MoneyManagerWrap::default_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("getSellShortNumber", &MoneyManagerBase::getSellShortNumber)
.def("getBuyShortNumber", &MoneyManagerBase::getBuyShortNumber)
.def("_getSellShortNumber", &MoneyManagerBase::_getSellShortNumber,
&MoneyManagerWrap::default_getSellShortNumber)
.def("_getBuyShortNumber", &MoneyManagerBase::_getBuyShortNumber,
2020-07-15 00:23:48 +08:00
&MoneyManagerWrap::default_getBuyShortNumber)*/
.def("_reset", &MoneyManagerBase::_reset, &MoneyManagerWrap::default_reset,
R"(【重载接口】子类复位接口,复位内部私有变量)")
.def("_clone", pure_virtual(&MoneyManagerBase::_clone), "【重载接口】子类克隆接口")
2015-01-07 01:26:14 +08:00
#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(name_init_pickle_suite<MoneyManagerBase>())
2015-01-07 01:26:14 +08:00
#endif
;
2015-01-07 01:26:14 +08:00
register_ptr_to_python<MoneyManagerPtr>();
2020-07-15 00:23:48 +08:00
def("MM_Nothing", MM_Nothing, R"(MM_Nothing()
)");
def("MM_FixedRisk", MM_FixedRisk, (arg("risk") = 1000.00), R"(MM_FixedRisk([risk = 1000.00])
1000 = /
:param float risk:
:return: )");
def("MM_FixedCapital", MM_FixedCapital, (arg("capital") = 10000.00),
R"(MM_FixedCapital([capital = 10000.0])
:param float capital:
:return: )");
def("MM_FixedCount", MM_FixedCount, (arg("n") = 100), R"(MM_FixedCount([n = 100])
:param float n:
:return: )");
def("MM_FixedPercent", MM_FixedPercent, (arg("p") = 0.03), R"(MM_FixedPercent([p = 0.03])
P * / R[BOOK3]_, [BOOK4]_ .
:param float p:
:return: )");
def("MM_FixedUnits", MM_FixedUnits, (arg("n") = 33), R"(MM_FixedUnits([n = 33])
:param int n: n个资金单位
:return: )");
def("MM_WilliamsFixedRisk", MM_WilliamsFixedRisk, (arg("p") = 0.1, arg("max_loss") = 1000.0),
R"( MM_WilliamsFixedRisk([p=0.1, max_loss=1000.0])
)");
2015-01-07 01:26:14 +08:00
}