hikyuu2/hikyuu/trade_sys/moneymanager.py

285 lines
8.9 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 (MoneyManagerBase,
MM_Nothing,
MM_FixedRisk,
MM_FixedCapital,
MM_FixedCount,
MM_FixedPercent,
MM_FixedUnits,
MM_WilliamsFixedRisk)
from hikyuu.util.unicode import (unicodeFunc, reprFunc)
MoneyManagerBase.__unicode__ = unicodeFunc
MoneyManagerBase.__repr__ = reprFunc
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.__doc__ = """
auto-checkin=False (bool) :
checkin
max-stock=200 (int) :
MoneyManagerBase.buyNotify() -
MoneyManagerBase.sellNotify() -
MoneyManagerBase._getBuyNumber() -
MoneyManagerBase._getSellNumber() -
MoneyManagerBase._reset() -
MoneyManagerBase._clone() -
"""
MoneyManagerBase.name.__doc__ = """名称"""
MoneyManagerBase.__init__.__doc__ = """
__init__(self[, name="MoneyManagerBase])
:param str name:
"""
MoneyManagerBase.getParam.__doc__ = """
getParam(self, name)
.. note::
auto-checkinbool类型False
checkin
:param str name:
:return:
:raises out_of_range:
"""
MoneyManagerBase.setParam.__doc__ = """
setParam(self, name, value)
:param str name:
:param value:
:type value: int | bool | float | string
:raises logic_error: Unsupported type!
"""
MoneyManagerBase.reset.__doc__ = """
reset(self)
"""
MoneyManagerBase.clone.__doc__ = """
clone(self)
"""
MoneyManagerBase.buyNotify.__doc__ = """
buyNotify(self, trade_record)
:param TradeRecord trade_record:
"""
MoneyManagerBase.sellNotify.__doc__ = """
sellNotify(self, trade_record)
:param TradeRecord trade_record:
"""
MoneyManagerBase.getBuyNumber.__doc__ = """
getBuyNumber(self, datetime, stock, price, risk)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:return:
:rtype: int
"""
MoneyManagerBase.getSellNumber.__doc__ = """
getSellNumber(self, datetime, stock, price, risk)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:return:
:rtype: int
"""
MoneyManagerBase._getBuyNumber.__doc__ = """
_getBuyNumber(self, datetime, stock, price, risk)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:return:
:rtype: int
"""
MoneyManagerBase._getSellNumber.__doc__ = """
_getSellNumber(self, datetime, stock, price, risk)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float risk: 00
:return:
:rtype: int
"""
MoneyManagerBase._reset.__doc__ = """
_reset(self)
"""
MoneyManagerBase._clone.__doc__ = """
_clone(self)
"""
#------------------------------------------------------------------
# add doc-string for build_in func
#------------------------------------------------------------------
MM_Nothing.__doc__ = """
MM_Nothing()
"""
MM_FixedRisk.__doc__ = """
MM_FixedRisk([risk = 1000.00])
1000 = /
:param float risk:
:return:
"""
MM_FixedCapital.__doc__ = """
MM_FixedCapital([capital = 10000.0])
:param float capital:
:return:
"""
MM_FixedCount.__doc__ = """
MM_FixedCount([n = 100])
:param int n:
:return:
"""
MM_FixedPercent.__doc__ = """
MM_FixedPercent([p = 0.03])
P * / R
:param float p:
:return:
"""
MM_FixedUnits.__doc__ = """
MM_FixedUnits([n = 33])
:param int n: n个资金单位
:return:
"""
MM_WilliamsFixedRisk.__doc__ = """
MM_WilliamsFixedRisk([p=0.1, max_loss=1000.0])
"""