hikyuu2/hikyuu/trade_sys/system.py

315 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.

#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
#
# The MIT License (MIT)
#
# Copyright (c) 2010-2017 fasiondog
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from ._trade_sys import (System,
SystemList,
getSystemPartName,
getSystemPartEnum,
TradeRequest)
from ._trade_sys import SYS_Simple as cpp_SYS_Simple
from hikyuu.util.unicode import (unicodeFunc, reprFunc)
from hikyuu.util.slice import list_getitem
System.__unicode__ = unicodeFunc
System.__repr__ = reprFunc
System.ENVIRONMENT = System.Part.ENVIRONMENT
System.CONDITION = System.Part.CONDITION
System.SIGNAL = System.Part.SIGNAL
System.STOPLOSS = System.Part.STOPLOSS
System.TAKEPROFIT = System.Part.TAKEPROFIT
System.MONEYMANAGER = System.Part.MONEYMANAGER
System.PROFITGOAL = System.Part.PROFITGOAL
System.SLIPPAGE = System.Part.SLIPPAGE
System.INVALID = System.Part.INVALID
SystemList.__getitem__ = list_getitem
#SystemPart = System.Part
def 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实例
"""
sys_ins = cpp_SYS_Simple()
if tm:
sys_ins.tm = tm
if mm:
sys_ins.mm = mm
if ev:
sys_ins.ev = ev
if cn:
sys_ins.cn = cn
if sg:
sys_ins.sg = sg
if st:
sys_ins.st = st
if tp:
sys_ins.tp = tp
if pg:
sys_ins.pg = pg
if sp:
sys_ins.sp = sp
return sys_ins
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
TradeRequest.__doc__ = """
delaybar开盘时进行交易Bar将要
System.getBuyTradeRequest() System.getSellTradeRequest()
BAR是否需要买入/Bar需要进行操作
"""
TradeRequest.valid.__doc__ = """该交易请求记录是否有效True | False"""
TradeRequest.business.__doc__ = """
:py:class:`hikyuu.trade_manage.BUSINESS`
"""
TradeRequest.datetime.__doc__ = """发出交易请求的时刻"""
TradeRequest.stoploss.__doc__ = """发出交易请求时刻的止损价"""
TradeRequest.part.__doc__ = """发出交易请求的来源,参见::py:class:`System.Part`"""
TradeRequest.count.__doc__ = """因操作失败,连续延迟的次数"""
System.Part.__doc__ = """
/
:py:class:`TradeRecord`
使使 System.ENVIRONMENT System.Part.ENVIRONMENT
- System.Part.ENVIRONMENT -
- System.Part.CONDITION -
- System.Part.SIGNAL -
- System.Part.STOPLOSS -
- System.Part.TAKEPROFIT -
- System.Part.MONEYMANAGER -
- System.Part.PROFITGOAL -
- System.Part.SLIPPAGE -
- System.Part.INVALID -
"""
System.__doc__ = """
delay=True (bool) : bar开盘时进行交易
delay_use_current_price=True (bool) : 使bar
//
使
max_delay_count=3 (int) :
tp_monotonic=True (bool) :
tp_delay_n=3 (int) :
ignore_sell_sg=False (bool) : 使/
support_borrow_cash=False (bool) :
support_borrow_stock=False (bool) :
"""
System.name.__doc__ = """系统名称"""
System.tm.__doc__ = """交易管理实例"""
System.mm.__doc__ = """资金管理策略"""
System.ev.__doc__ = """市场环境判断策略"""
System.cn.__doc__ = """系统有效条件"""
System.sg.__doc__ = """信号指示器"""
System.st.__doc__ = """止损策略"""
System.tp.__doc__ = """止盈策略"""
System.pg.__doc__ = """盈利目标策略"""
System.sp.__doc__ = """移滑价差算法"""
System.getParam.__doc__ = """
getParam(self, name)
:param str name:
:return:
:raises out_of_range:
"""
System.setParam.__doc__ = """
setParam(self, name, value)
:param str name:
:param value:
:type value: int | bool | float | string
:raises logic_error: Unsupported type!
"""
System.getTO.__doc__ = """
getTO(self)
:py:meth:`System.run` stock + query KData
:rtype: KData
"""
System.setTO.__doc__ = """
setTO(self, kdata)
:py:meth:`System.run` stock + query KData
:param KData kdata: K线数据
"""
System.getStock.__doc__ = """
getStock(self)
:rtype: Stock
"""
System.getTradeRecordList.__doc__ = """
getTradeRecordList(self)
:rtype: TradeRecordList
"""
System.getBuyTradeRequest.__doc__ = """
getBuyTradeRequest(self)
delay
:rtype: TradeRequest
"""
System.getSellTradeRequest.__doc__ = """
getSellTradeRequest(self)
delay
:rtype: TradeRequest
"""
System.run.__doc__ = """
run(self, stock, query[, reset=True])
:param Stock stock:
:param Query query: K线数据查询条件
:param bool reset: tm实例
"""
System.reset.__doc__ = """
reset(self, with_tm, with_ev)
TMEV是和具体系统无关的策略组件
TM时需要注意
:param bool with_tm: TM组件
:param bool with_ev: EV组件
"""
System.clone.__doc__ = """
clone(self, with_tm, with_ev)
T
MEV是和具体系统无关的策略组件clone将生成新的
TM是否共享的情况
:param bool with_tm: clone还是共享
:param bool with_ev: clone还是共享
"""
#------------------------------------------------------------------
# add doc-string for build_in func
#------------------------------------------------------------------
getSystemPartName.__doc__ = """
getSystemPartName(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
"""
getSystemPartEnum.__doc__ = """
getSystemPartEnum(part_name)
:param str part_name: :py:func:`getSystemPartName`
:rtype: System.Part
"""