hikyuu2/hikyuu_pywrap/trade_sys/_System.cpp

260 lines
11 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 <boost/python.hpp>
#include <hikyuu/trade_sys/system/build_in.h>
#include "../_Parameter.h"
#include "../pickle_support.h"
using namespace boost::python;
using namespace hku;
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(run_overload, run, 2, 3);
string (System::*sys_get_name)() const = &System::name;
void (System::*sys_set_name)(const string&) = &System::name;
void (System::*run_1)(const KQuery&, bool) = &System::run;
void (System::*run_2)(const Stock&, const KQuery&, bool reset) = &System::run;
void export_System() {
def(
"SYS_Simple", SYS_Simple,
(arg("tm") = TradeManagerPtr(), arg("mm") = MoneyManagerPtr(), arg("ev") = EnvironmentPtr(),
arg("cn") = ConditionPtr(), arg("sg") = SignalPtr(), arg("st") = StoplossPtr(),
arg("tp") = StoplossPtr(), arg("pg") = ProfitGoalPtr(), arg("sp") = SlippagePtr()),
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(initCash = 300000)
#创建信号指示器以5日EMA为快线5日EMA自身的10日EMA最为慢线快线向上穿越慢线时买入反之卖出
my_sg = SG_Flex(OP(EMA(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实例)");
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)");
def("get_system_part_enum", getSystemPartEnum, R"(get_system_part_enum(part_name)
:param str part_name: :py:func:`getSystemPartName`
:rtype: System.Part)");
class_<TradeRequest>(
"TradeRequest",
R"(交易请求记录。系统内部在实现延迟操作时登记的交易请求信息。暴露该结构的主要目的是用于在“delay”模式延迟到下一个bar开盘时进行交易的情况下系统实际已知下一个Bar将要进行交易此时可通过 :py:meth:`System.getBuyTradeRequest` 、 :py:meth:`System.getSellTradeRequest` 来获知下一个BAR是否需要买入/卖出。主要用于提醒或打印下一个Bar需要进行操作。对于系统本身的运行没有影响。)",
init<>())
//.def(self_ns::str(self))
//.def(self_ns::repr(self))
.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, "因操作失败,连续延迟的次数")
#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(normal_pickle_suite<TradeRequest>())
#endif
;
SystemList::const_reference (SystemList::*SystemList_at)(SystemList::size_type) const =
&SystemList::at;
void (SystemList::*append)(const SystemPtr&) = &SystemList::push_back;
class_<SystemList>("SystemList")
.def("__iter__", iterator<SystemList>())
.def("size", &SystemList::size)
.def("__len__", &SystemList::size)
.def("get", SystemList_at, return_value_policy<copy_const_reference>())
.def("append", append);
scope in_System =
class_<System>(
"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): 使)",
init<const string&>())
.def(init<const TradeManagerPtr&, const MoneyManagerPtr&, const EnvironmentPtr&,
const ConditionPtr&, const SignalPtr&, const StoplossPtr&, const StoplossPtr&,
const ProfitGoalPtr&, const SlippagePtr&, const string&>())
.def(self_ns::str(self))
.def(self_ns::repr(self))
.add_property("name", sys_get_name, sys_set_name, "系统名称")
.add_property("tm", &System::getTM, &System::setTM, "关联的交易管理实例")
.add_property("to", &System::getTO, &System::setTO, "交易对象 KData")
.add_property("mm", &System::getMM, &System::setMM, "资金管理策略")
.add_property("ev", &System::getEV, &System::setEV, "市场环境判断策略")
.add_property("cn", &System::getCN, &System::setCN, "系统有效条件")
.add_property("sg", &System::getSG, &System::setSG, "信号指示器")
.add_property("st", &System::getST, &System::setST, "止损策略")
.add_property("tp", &System::getTP, &System::setTP, "止盈策略")
.add_property("pg", &System::getPG, &System::setPG, "盈利目标策略")
.add_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<object>, 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,
return_value_policy<copy_const_reference>(), R"(get_trade_record_list(self)
:rtype: TradeRecordList)")
.def("get_buy_trade_request", &System::getBuyTradeRequest,
return_value_policy<copy_const_reference>(), R"(get_buy_trade_request(self)
delay
:rtype: TradeRequest)")
.def("get_sell_trade_request", &System::getSellTradeRequest,
return_value_policy<copy_const_reference>(), R"(get_sell_trade_request(self)
delay
:rtype: TradeRequest)")
/*.def("getSellShortTradeRequest", &System::getSellShortTradeRequest,
return_value_policy<copy_const_reference>())
.def("getBuyShortTradeRequest", &System::getBuyShortTradeRequest,
return_value_policy<copy_const_reference>())*/
.def("reset", &System::reset, R"(reset(self, with_tm, with_ev)
TMEV是和具体系统无关的策略组件TM时需要注意
:param bool with_tm: TM组件
:param bool with_ev: EV组件)")
.def("clone", &System::clone, R"(clone(self)
)")
//.def("run", &System::run, run_overload(args("stock", "query", "reset")))
.def("run", run_1, (arg("query"), arg("reset") = true))
.def("run", run_2, (arg("stock"), arg("query"), arg("reset") = true),
R"(run(self, stock, query[, reset=True])
:param Stock stock:
:param Query query: K线数据查询条件
:param bool reset: tm实例)")
/*.def("readyForRun", &System::readyForRun)
.def("runMoment", run_monent_1)
.def("runMoment", run_monent_2)
.def("_runMoment", &System::_runMoment)
.def("_environmentIsValid", &System::_environmentIsValid)
.def("_conditionIsValid", &System::_conditionIsValid)
.def("_buyNotifyAll", &System::_buyNotifyAll)
.def("_sellNotifyAll", &System::_sellNotifyAll)
.def("_buy", &System::_buy)
.def("_sell", &System::_sell)*/
//.def("_haveBuyRequest", &System::_haveBuyRequest)
//.def("_haveSellRequest", &System::_haveSellRequest)
//.def("_submitBuyRequest", &System::_submitBuyRequest)
//.def("_submitSellRequest", &System::_submitSellRequest)
//.def("_clearBuyRequest", &System::_clearBuyRequest)
//.def("_clearSellRequest", &System::_clearSellRequest)
#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(name_init_pickle_suite<System>())
#endif
;
enum_<SystemPart>("Part")
.value("ENVIRONMENT", PART_ENVIRONMENT)
.value("CONDITION", PART_CONDITION)
.value("SIGNAL", PART_SIGNAL)
.value("STOPLOSS", PART_STOPLOSS)
.value("TAKEPROFIT", PART_TAKEPROFIT)
.value("MONEYMANAGER", PART_MONEYMANAGER)
.value("PROFITGOAL", PART_PROFITGOAL)
.value("SLIPPAGE", PART_SLIPPAGE)
.value("ALLOCATEFUNDS", PART_ALLOCATEFUNDS)
.value("INVALID", PART_INVALID);
register_ptr_to_python<SystemPtr>();
}