规整 python 接口

This commit is contained in:
fasiondog 2020-07-15 22:50:22 +08:00
parent 320605423e
commit 5049b9cffa
16 changed files with 392 additions and 820 deletions

View File

@ -57,6 +57,7 @@ from .extend import *
from .deprecated import *
from .indicator import *
from .trade_manage import *
from .trade_sys import *
__version__ = get_version()

View File

@ -30,9 +30,8 @@
#===============================================================================
#TradeManager.buy等方法默认参数使用了Part这里必须先导入否则报初始化错误
from hikyuu import Datetime, Query, QueryByDate, StockManager, System
from hikyuu import Datetime, Query, QueryByDate, StockManager, System, getSystemPartName
from hikyuu.util.slice import list_getitem
from hikyuu.trade_sys.system import getSystemPartName
BUSINESS.INIT = BUSINESS.BUSINESS_INIT
BUSINESS.BUY = BUSINESS.BUSINESS_BUY

View File

@ -24,8 +24,4 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
__all__ = ['system', 'environment', 'condition', 'moneymanager', 'signal',
'stoploss', 'profitgoal', 'slippage', 'selector', 'portfolio',
'allocatefunds']
from .trade_sys import *

View File

@ -1,38 +0,0 @@
#!/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 hikyuu.cpp.core import (AllocateFundsBase, SystemWeight, SystemWeightList, AF_EqualWeight)
from hikyuu.util.slice import list_getitem
SystemWeightList.__getitem__ = list_getitem
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
#------------------------------------------------------------------
# add doc-string for build_in func
#------------------------------------------------------------------

View File

@ -1,63 +0,0 @@
#!/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 hikyuu.cpp.core import ConditionBase, CN_OPLine
def cn_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtCN(func, params={}, name='crtCN'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (ConditionBase, ), {'__init__': cn_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
ConditionBase.__init__.__doc__ = """
__init__(self[, name="ConditionBase"])
:param str name:
"""

View File

@ -1,63 +0,0 @@
#!/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 hikyuu.cpp.core import EnvironmentBase, EV_TwoLine
def ev_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtEV(func, params={}, name='crtEV'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (EnvironmentBase, ), {'__init__': ev_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
EnvironmentBase.__init__.__doc__ = """
__init__(self[, name='EnvironmentBase'])
:param str name:
"""

View File

@ -1,66 +0,0 @@
#!/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 hikyuu.cpp.core import (
MoneyManagerBase, MM_Nothing, MM_FixedRisk, MM_FixedCapital, MM_FixedCount, MM_FixedPercent,
MM_FixedUnits, MM_WilliamsFixedRisk
)
def mm_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtMM(func, params={}, name='crtMM'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (MoneyManagerBase, ), {'__init__': mm_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
MoneyManagerBase.__init__.__doc__ = """
__init__(self[, name="MoneyManagerBase])
:param str name:
"""

View File

@ -1,61 +0,0 @@
#!/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 hikyuu.cpp.core import (Portfolio, PF_Simple)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
Portfolio.__doc__ = """实现多标的、多策略的投资组合"""
Portfolio.name.__doc__ = """名称"""
Portfolio.tm.__doc__ = """关联的交易管理实例"""
Portfolio.se.__doc__ = """选择器策略"""
Portfolio.run.__doc__ = """
run(self, query)
:param Query query:
"""
#------------------------------------------------------------------
# add doc-string for build_in func
#------------------------------------------------------------------
PF_Simple.__doc__ = """
PF_Simple([tm, sys, se])
:param TradeManager tm:
:param System sys:
:param SelectorBase se:
"""

View File

@ -1,63 +0,0 @@
#!/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 hikyuu.cpp.core import (ProfitGoalBase, PG_NoGoal, PG_FixedPercent, PG_FixedHoldDays)
def pg_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtPG(func, params={}, name='crtPG'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (ProfitGoalBase, ), {'__init__': pg_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
ProfitGoalBase.__init__.__doc__ = """
__init__(self[, name="ProfitGoalBase"])
:param str name:
"""

View File

@ -1,136 +0,0 @@
#!/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 hikyuu.cpp.core import (SelectorBase, SE_Fixed)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
SelectorBase.__doc__ = """
freq (int | 1) Bar为单位
SelectorBase.getSelectedSystemList -
SelectorBase._reset -
SelectorBase._clone -
"""
SelectorBase.name.__doc__ = """名称"""
SelectorBase.__init__.__doc__ = """
__init__(self[, name="SelectorBase])
:param str name:
"""
SelectorBase.getParam.__doc__ = """
getParam(self, name)
:param str name:
:return:
:raises out_of_range:
"""
SelectorBase.setParam.__doc__ = """
setParam(self, name, value)
:param str name:
:param value:
:type value: int | bool | float | string
:raises logic_error: Unsupported type!
"""
SelectorBase.addStock.__doc__ = """
addStock(self, stock, sys)
:param Stock stock:
:param System sys:
"""
SelectorBase.addStockList.__doc__ = """
addStockList(self, stk_list, sys)
:param StockList stk_list:
:param System sys:
"""
SelectorBase.clear.__doc__ = """
clear(self)
"""
SelectorBase.getSelectedSystemList.__doc__ = """
getSelectedSystemList(self, datetime)
:param Datetime datetime:
:return:
:rtype: SystemList
"""
SelectorBase._reset.__doc__ = """
_reset(self)
"""
SelectorBase._clone.__doc__ = """
_clone(self)
"""
#------------------------------------------------------------------
# add doc-string for build_in func
#------------------------------------------------------------------
SE_Fixed.__doc__ = """
SE_Fixed([stocklist, sys])
:param StockList stocklist:
:param System sys:
:return: SE选择器实例
"""

View File

@ -1,63 +0,0 @@
#!/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 hikyuu.cpp.core import (
SignalBase, SG_Bool, SG_Single, SG_Single2, SG_Cross, SG_CrossGold, SG_Flex
)
def sig_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtSG(func, params={}, name='crtSG'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (SignalBase, ), {'__init__': sig_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
SignalBase.__init__.__doc__ = """
__init__(self[, name="SignalBase"])
:param str name:
"""

View File

@ -1,63 +0,0 @@
#!/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 hikyuu.cpp.core import (SlippageBase, SL_FixedPercent, SL_FixedValue)
def sl_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtSL(func, params={}, name='crtSL'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (SlippageBase, ), {'__init__': sl_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
SlippageBase.__init__.__doc__ = """
__init__(self[, name="SlippageBase"])
:param str name:
"""

View File

@ -1,67 +0,0 @@
#!/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.
import hikyuu.cpp.core as csys
StoplossBase = csys.StoplossBase
ST_FixedPercent = csys.ST_FixedPercent
ST_Indicator = csys.ST_Indicator
ST_Saftyloss = csys.ST_Saftyloss
def st_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtST(func, params={}, name='crtST'):
"""
/
:param func: /
:param {} params:
:param str name:
:return: /
"""
meta_x = type(name, (StoplossBase, ), {'__init__': st_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# add doc-string
#------------------------------------------------------------------
StoplossBase.__init__.__doc__ = """
__init__(self[, name="StoplossBase"])
:param str name:
"""

View File

@ -1,122 +0,0 @@
#!/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 hikyuu.cpp.core import (System, SystemList, getSystemPartName, getSystemPartEnum, TradeRequest)
from hikyuu.cpp.core import SYS_Simple as cpp_SYS_Simple
from hikyuu.util.slice import list_getitem
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
SystemList.__str__ = lambda self: str(list(self))
SystemList.__repr__ = lambda self: repr(list(self))
#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
#------------------------------------------------------------------
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 -
"""

View File

@ -0,0 +1,366 @@
# -*- coding: utf8 -*-
from hikyuu.util.slice import list_getitem
from hikyuu.cpp.core import SYS_Simple as cpp_SYS_Simple
from hikyuu.cpp.core import (
System, SystemList, SystemWeight, SystemWeightList, ConditionBase, EnvironmentBase,
MoneyManagerBase, ProfitGoalBase, SelectorBase, SignalBase, SlippageBase, StoplossBase
)
#------------------------------------------------------------------
# System
#------------------------------------------------------------------
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.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
SystemList.__str__ = lambda self: str(list(self))
SystemList.__repr__ = lambda self: repr(list(self))
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
#------------------------------------------------------------------
# allocatefunds
#------------------------------------------------------------------
SystemWeightList.__getitem__ = list_getitem
SystemWeightList.__str__ = lambda self: str(list(self))
SystemWeightList.__repr__ = lambda self: repr(list(self))
#------------------------------------------------------------------
# condition
#------------------------------------------------------------------
ConditionBase.__init__.__doc__ = """
__init__(self[, name="ConditionBase"])
:param str name:
"""
def cn_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtCN(func, params={}, name='crtCN'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (ConditionBase, ), {'__init__': cn_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# environment
#------------------------------------------------------------------
EnvironmentBase.__init__.__doc__ = """
__init__(self[, name='EnvironmentBase'])
:param str name:
"""
def ev_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtEV(func, params={}, name='crtEV'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (EnvironmentBase, ), {'__init__': ev_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# moneymanager
#------------------------------------------------------------------
MoneyManagerBase.__init__.__doc__ = """
__init__(self[, name="MoneyManagerBase])
:param str name:
"""
def mm_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtMM(func, params={}, name='crtMM'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (MoneyManagerBase, ), {'__init__': mm_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# profitgoal
#------------------------------------------------------------------
ProfitGoalBase.__init__.__doc__ = """
__init__(self[, name="ProfitGoalBase"])
:param str name:
"""
def pg_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtPG(func, params={}, name='crtPG'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (ProfitGoalBase, ), {'__init__': pg_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# selector
#------------------------------------------------------------------
SelectorBase.__init__.__doc__ = """
__init__(self[, name="SelectorBase])
:param str name:
"""
#------------------------------------------------------------------
# signal
#------------------------------------------------------------------
SignalBase.__init__.__doc__ = """
__init__(self[, name="SignalBase"])
:param str name:
"""
def sig_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtSG(func, params={}, name='crtSG'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (SignalBase, ), {'__init__': sig_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# slippage
#------------------------------------------------------------------
SlippageBase.__init__.__doc__ = """
__init__(self[, name="SlippageBase"])
:param str name:
"""
def sl_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtSL(func, params={}, name='crtSL'):
"""
:param func:
:param {} params:
:param str name:
:return:
"""
meta_x = type(name, (SlippageBase, ), {'__init__': sl_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)
#------------------------------------------------------------------
# stoploss
#------------------------------------------------------------------
StoplossBase.__init__.__doc__ = """
__init__(self[, name="StoplossBase"])
:param str name:
"""
def st_init(self, name, params):
super(self.__class__, self).__init__(name)
self._name = name
self._params = params
for k, v in params.items():
self.setParam(k, v)
def crtST(func, params={}, name='crtST'):
"""
/
:param func: /
:param {} params:
:param str name:
:return: /
"""
meta_x = type(name, (StoplossBase, ), {'__init__': st_init})
meta_x._clone = lambda self: meta_x(self._name, self._params)
meta_x._calculate = func
return meta_x(name, params)

View File

@ -7,21 +7,22 @@
#include <boost/python.hpp>
#include <hikyuu/trade_sys/portfolio/build_in.h>
#include <hikyuu/trade_sys/selector/crt/SE_Fixed.h>
#include <hikyuu/trade_sys/allocatefunds/crt/AF_EqualWeight.h>
#include "../_Parameter.h"
#include "../pickle_support.h"
using namespace boost::python;
using namespace hku;
BOOST_PYTHON_FUNCTION_OVERLOADS(PF_Simple_overload, PF_Simple, 0, 3);
void (Portfolio::*pf_set_name)(const string&) = &Portfolio::name;
const string& (Portfolio::*pf_get_name)() const = &Portfolio::name;
void export_Portfolio() {
class_<Portfolio>("Portfolio", init<>())
class_<Portfolio>("Portfolio", R"(实现多标的、多策略的投资组合)", init<>())
.def(init<const string&>())
.def(init<const TradeManagerPtr&, const SelectorPtr&, const AFPtr&>())
.def(self_ns::str(self))
.def(self_ns::repr(self))
@ -30,10 +31,16 @@ void export_Portfolio() {
.def("have_param", &Portfolio::haveParam)
.add_property("name", make_function(pf_get_name, return_value_policy<copy_const_reference>()),
pf_set_name)
.add_property("tm", &Portfolio::getTM, &Portfolio::setTM)
.add_property("se", &Portfolio::getSE, &Portfolio::setSE)
.def("run", &Portfolio::run)
pf_set_name, "名称")
.add_property("tm", &Portfolio::getTM, &Portfolio::setTM, "设置或获取交易管理对象")
.add_property("se", &Portfolio::getSE, &Portfolio::setSE, "设置或获取交易对象选择算法")
.def("run", &Portfolio::run, R"(run(self, query)
:param Query query: )")
#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(name_init_pickle_suite<Portfolio>())
#endif
@ -41,5 +48,13 @@ void export_Portfolio() {
register_ptr_to_python<PortfolioPtr>();
def("PF_Simple", PF_Simple, PF_Simple_overload());
def("PF_Simple", PF_Simple,
(arg("tm") = TradeManagerPtr(), arg("se") = SE_Fixed(), arg("af") = AF_EqualWeight()),
R"(PF_Simple([tm, sys, se])
:param TradeManager tm:
:param System sys:
:param SelectorBase se: )");
}