hikyuu2/hikyuu_pywrap/trade_sys/_System.cpp
2024-04-01 04:06:10 +08:00

268 lines
12 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.

/*
* _System.cpp
*
* Created on: 2013-3-22
* Author: fasiondog
*/
#include <hikyuu/trade_sys/system/build_in.h>
#include "../pybind_utils.h"
namespace py = pybind11;
using namespace hku;
#if defined(_MSC_VER)
#pragma warning(disable : 4267)
#endif
void (System::*run_1)(const KQuery&, bool, bool) = &System::run;
void (System::*run_2)(const KData&, bool, bool) = &System::run;
void (System::*run_3)(const Stock&, const KQuery&, bool, bool) = &System::run;
TradeRecord (System::*runMoment_1)(const Datetime&) = &System::runMoment;
void export_System(py::module& m) {
m.def("get_system_part_name", getSystemPartName, R"(get_system_part_name(part)
- System.Part.ENVIRONMENT - "EV"
- System.Part.CONDITION - "CN"
- System.Part.SIGNAL - "SG"
- System.Part.STOPLOSS - "ST"
- System.Part.TAKEPROFIT - "TP"
- System.Part.MONEYMANAGER - "MM"
- System.Part.PROFITGOAL - "PG"
- System.Part.SLIPPAGE - "SP"
- System.Part.INVALID - "--"
:param int part: System.Part
:rtype: str)");
m.def("get_system_part_enum", getSystemPartEnum, R"(get_system_part_enum(part_name)
:param str part_name: :py:func:`getSystemPartName`
:rtype: System.Part)");
//--------------------------------------------------------------------------------------
py::class_<TradeRequest>(
m, "TradeRequest",
R"(交易请求记录。系统内部在实现延迟操作时登记的交易请求信息。暴露该结构的主要目的是用于
delaybar开盘时进行交易Bar将要
System.getBuyTradeRequest() System.getSellTradeRequest()
BAR是否需要买入/Bar需要进行操作
)")
.def(py::init<>())
.def_readwrite("valid", &TradeRequest::valid, "该交易请求记录是否有效True | False")
.def_readwrite("business", &TradeRequest::business,
"交易业务类型,参见::py:class:`hikyuu.trade_manage.BUSINESS`")
.def_readwrite("datetime", &TradeRequest::datetime, "发出交易请求的时刻")
.def_readwrite("stoploss", &TradeRequest::stoploss, "发出交易请求时刻的止损价")
.def_readwrite("part", &TradeRequest::from,
"发出交易请求的来源,参见::py:class:`System.Part`")
.def_readwrite("count", &TradeRequest::count, "因操作失败,连续延迟的次数")
DEF_PICKLE(TradeRequest);
//--------------------------------------------------------------------------------------
py::class_<System, SystemPtr>(
m, "System",
R"(系统基类。需要扩展或实现更复杂的系统交易行为,可从此类继承。
- delay=True (bool) : bar开盘时进行交易
- delay_use_current_price=True (bool) : 使bar的价格计算新的止损价//使
- max_delay_count=3 (int) : 001
- tp_monotonic=True (bool) :
- tp_delay_n=3 (int) :
- ignore_sell_sg=False (bool) : 使/
- ev_open_position=False (bool): 使
- cn_open_position=False (bool): 使)")
.def(py::init<const string&>())
.def(py::init<const TradeManagerPtr&, const MoneyManagerPtr&, const EnvironmentPtr&,
const ConditionPtr&, const SignalPtr&, const StoplossPtr&, const StoplossPtr&,
const ProfitGoalPtr&, const SlippagePtr&, const string&>())
.def("__str__", to_py_str<System>)
.def("__repr__", to_py_str<System>)
.def_property("name", py::overload_cast<>(&System::name, py::const_),
py::overload_cast<const string&>(&System::name), py::return_value_policy::copy,
"系统名称")
.def_property_readonly("query", &System::getQuery, py::return_value_policy::copy, "查询条件")
.def_property("tm", &System::getTM, &System::setTM, "关联的交易管理实例")
.def_property("to", &System::getTO, &System::setTO, "交易对象 KData")
.def_property("mm", &System::getMM, &System::setMM, "资金管理策略")
.def_property("ev", &System::getEV, &System::setEV, "市场环境判断策略")
.def_property("cn", &System::getCN, &System::setCN, "系统有效条件")
.def_property("sg", &System::getSG, &System::setSG, "信号指示器")
.def_property("st", &System::getST, &System::setST, "止损策略")
.def_property("tp", &System::getTP, &System::setTP, "止盈策略")
.def_property("pg", &System::getPG, &System::setPG, "盈利目标策略")
.def_property("sp", &System::getSP, &System::setSP, "移滑价差算法")
.def("get_param", &System::getParam<boost::any>, R"(get_param(self, name)
:param str name:
:return:
:raises out_of_range: )")
.def("set_param", &System::setParam<boost::any>, R"(set_param(self, name, value)
:param str name:
:param value:
:raises logic_error: Unsupported type! )")
.def("have_param", &System::haveParam, "是否存在指定参数")
.def("get_stock", &System::getStock, R"(get_stock(self)
:rtype: Stock)")
.def("get_trade_record_list", &System::getTradeRecordList, py::return_value_policy::copy,
R"(get_trade_record_list(self)
TM
:rtype: TradeRecordList)")
.def("get_buy_trade_request", &System::getBuyTradeRequest, py::return_value_policy::copy,
R"(get_buy_trade_request(self)
delay
:rtype: TradeRequest)")
.def("get_sell_trade_request", &System::getSellTradeRequest, py::return_value_policy::copy,
R"(get_sell_trade_request(self)
delay
:rtype: TradeRequest)")
.def("get_sell_short_trade_request", &System::getSellShortTradeRequest,
py::return_value_policy::copy)
.def("get_buy_short_trade_request", &System::getBuyShortTradeRequest,
py::return_value_policy::copy)
.def("reset", &System::reset,
R"(reset(self)
)")
.def("force_reset_all", &System::forceResetAll,
R"(force_reset_all(self)
)")
.def("clone", &System::clone,
R"(clone(self)
)")
.def("run", run_1, py::arg("query"), py::arg("reset") = true, py::arg("reset_all") = false)
.def("run", run_2, py::arg("kdata"), py::arg("reset") = true, py::arg("reset_all") = false)
.def("run", run_3, py::arg("stock"), py::arg("query"), py::arg("reset") = true,
py::arg("reset_all") = false,
R"(run(self, stock, query[, reset=True])
:param Stock stock:
:param Query query: K线数据查询条件
:param bool reset:
:param bool reset_all: )")
.def("ready", &System::readyForRun)
DEF_PICKLE(System);
//--------------------------------------------------------------------------------------
m.def(
"SYS_Simple",
[](py::object tm = py::none(), py::object mm = py::none(), py::object ev = py::none(),
py::object cn = py::none(), py::object sg = py::none(), py::object st = py::none(),
py::object tp = py::none(), py::object pg = py::none(), py::object sp = py::none()) {
TradeManagerPtr ctm;
if (!tm.is_none()) {
ctm = tm.cast<TradeManagerPtr>();
}
MoneyManagerPtr cmm;
if (!mm.is_none()) {
cmm = mm.cast<MoneyManagerPtr>();
}
EnvironmentPtr cev;
if (!ev.is_none()) {
cev = ev.cast<EnvironmentPtr>();
}
ConditionPtr ccn;
if (!cn.is_none()) {
ccn = cn.cast<ConditionPtr>();
}
SignalPtr csg;
if (!sg.is_none()) {
csg = sg.cast<SignalPtr>();
}
StoplossPtr cst;
if (!st.is_none()) {
cst = st.cast<StoplossPtr>();
}
StoplossPtr ctp;
if (!tp.is_none()) {
ctp = tp.cast<StoplossPtr>();
}
ProfitGoalPtr cpg;
if (!pg.is_none()) {
cpg = pg.cast<ProfitGoalPtr>();
}
SlippagePtr csp;
if (!sp.is_none()) {
csp = sp.cast<SlippagePtr>();
}
return SYS_Simple(ctm, cmm, cev, ccn, csg, cst, ctp, cpg, csp);
},
py::arg("tm") = py::none(), py::arg("mm") = py::none(), py::arg("ev") = py::none(),
py::arg("cn") = py::none(), py::arg("sg") = py::none(), py::arg("st") = py::none(),
py::arg("tp") = py::none(), py::arg("pg") = py::none(), py::arg("sp") = py::none(),
R"(SYS_Simple([tm=None, mm=None, ev=None, cn=None, sg=None, st=None, tp=None, pg=None, sp=None])
(run方法
run时没有任何输出
tmsgmm使 run ::
#创建模拟交易账户进行回测初始资金30万
my_tm = crtTM(init_cash = 300000)
#创建信号指示器以5日EMA为快线5日EMA自身的10日EMA作为慢线快线向上穿越慢线时买入反之卖出
my_sg = SG_Flex(EMA(C, n=5), slow_n=10)
#固定每次买入1000股
my_mm = MM_FixedCount(1000)
#创建交易系统并运行
sys = SYS_Simple(tm = my_tm, sg = my_sg, mm = my_mm)
sys.run(sm['sz000001'], Query(-150))
:param TradeManager tm:
:param MoneyManager mm:
:param EnvironmentBase ev:
:param ConditionBase cn:
:param SignalBase sg:
:param StoplossBase st:
:param StoplossBase tp:
:param ProfitGoalBase pg:
:param SlippageBase sp:
:return: system实例)");
}