You are here:  Home » 量化交易与机器学习 » backtrader » 用户定义Commissions- backtrader中文教程

用户定义Commissions

The佣金制度的实施是返工就在不久以前。最重要的:返工部分涉及:

  • 保留原来的CommissionInfo类和behavior
  • Opening门轻松创建用户定义commissions
  • Making,格式为xx%为新的默认佣金计划,而不是0.XX(只是口味的问题),保持行为configurable

The基础知识在下面的扩展他Commissions.

Note

See的CommInfoBase文档字符串中概述为参数reference

Defining一个委员会Scheme

It涉及1或2 steps

  1. SubclassingCommInfoBase

    只需改变缺省参数可以是足够了。backtrader已经与目前的模块中的一些定义做这个backtrader.commissions。期货常规行业标准是一个每合同和每轮固定量。该定义是可以做到:

    class CommInfo_Futures_Fixed(CommInfoBase):
        params = (
            ("stocklike", False),
            ("commtype", CommInfoBase.COMM_FIXED),
        )
    

    对于股票和PERC明智佣金:

    class CommInfo_Stocks_Perc(CommInfoBase):
        params = (
            ("stocklike", True),
            ("commtype", CommInfoBase.COMM_PERC),
        )
    

    作为默认上述的百分比,这里的解释(作为参数传递commission)是认为:XX%。如若老/其他行为来希望0.xx,就可以轻松完成:

    class CommInfo_Stocks_PercAbs(CommInfoBase):
        params = (
            ("stocklike", True),
            ("commtype", CommInfoBase.COMM_PERC),
            ("percabs", True),
        )
    
  2. 重写(如果需要的话)的_getcommissionmethod

    定义为:

    def _getcommission(self, size, price, pseudoexec):
       """Calculates the commission of an operation at a given price
    
       pseudoexec: if True the operation has not yet been executed
       """
    

    在一个实际的例子below

How更多细节将其应用到platform

Once了CommInfoBase子类是放置诀窍是使用broker.addcommissioninfo而不是通常的broker.setcommission。该后者将在内部使用遗留CommissionInfoObject.

做得比所述更容易:

...

comminfo = CommInfo_Stocks_PercAbs(commission=0.005)  # 0.5%
cerebro.broker.addcommissioninfo(comminfo)

addcommissioninfo的定义方法如下:

def addcommissioninfo(self, comminfo, name=None):
    self.comminfo[name] = comminfo

设置name装置该comminfo对象将只适用于资产使用该名称。`的默认值为None意味着它适用于所有资产在system.

A实际example

票务#45问起一个它适用于期货事务监察委员会方案,是个明智的,并使用在合同的整个“虚拟”价值的提成比例。即:包括在佣金calculation.

It未来乘数应该很容易:

import backtrader as bt

class CommInfo_Fut_Perc_Mult(bt.CommInfoBase):
    params = (
      ("stocklike", False),  # Futures
      ("commtype", bt.CommInfoBase.COMM_PERC),  # Apply % Commission
    # ("percabs", False),  # pass perc as xx% which is the default
    )

    def _getcommission(self, size, price, pseudoexec):
        return size * price * self.p.commission * self.p.mult

付诸系统:

comminfo = CommInfo_Fut_Perc_Mult(
    commission=0.1,  # 0.1%
    mult=10,
    margin=2000  # Margin is needed for futures-like instruments
)

cerebro.broker.addcommission(comminfo)

如果格式0.XX是优选的作为默认值,只需设置PARAMpercabsTrue

class CommInfo_Fut_Perc_Mult(bt.CommInfoBase):
    params = (
      ("stocklike", False),  # Futures
      ("commtype", bt.CommInfoBase.COMM_PERC),  # Apply % Commission
      ("percabs", True),  # pass perc as 0.xx
    )

comminfo = CommInfo_Fut_Perc_Mult(
    commission=0.001,  # 0.1%
    mult=10,
    margin=2000  # Margin is needed for futures-like instruments
)

cerebro.broker.addcommissioninfo(comminfo)

这一切都应该做的trick.

Explainingpseudoexec

让我们回忆起_getcommission定义:

def _getcommission(self, size, price, pseudoexec):
    """Calculates the commission of an operation at a given price

    pseudoexec: if True the operation has not yet been executed
    """

的目的pseudoexecARG看似不起眼,但它提供一个平台purpose.

  • The可以调用这个方法做的可用现金的预先计算和一些其他tasks
  • This意味着该方法可(它实际上将)被调用多于一旦与同parameters

pseudoexec指示调用是否与实际执行订单。虽然乍一看,这似乎不是“有关部门”是,如果场景等考虑以下几点:

  • 经纪人提供期货往返佣金,一旦50%的折扣协商合同的量已经exceeeded 5000 units

    In这样的情况下,并且如果pseudoexec是不存在的多个非执行调用该方法会很快引发的假设折现在place.

Putting方案的工作:

import backtrader as bt

class CommInfo_Fut_Discount(bt.CommInfoBase):
    params = (
      ("stocklike", False),  # Futures
      ("commtype", bt.CommInfoBase.COMM_FIXED),  # Apply Commission

      # Custom params for the discount
      ("discount_volume", 5000),  # minimum contracts to achieve discount
      ("discount_perc", 50.0),  # 50.0% discount
    )

    negotiated_volume = 0  # attribute to keep track of the actual volume

    def _getcommission(self, size, price, pseudoexec):
        if self.negotiated_volume > self.p.discount_volume:
           actual_discount = self.p.discount_perc / 100.0
        else:
           actual_discount = 0.0

        commission = self.p.commission * (1.0 - actual_discount)
        commvalue = size * price * commission

        if not pseudoexec:
           # keep track of actual real executed size for future discounts
           self.negotiated_volume += size

        return commvalue

目的和存在的pseudoexec是希望明确now.

CommInfoBase文档字符串和params

Here是:

class CommInfoBase(with_metaclass(MetaParams)):
    """Base Class for the Commission Schemes.

    Params:

      - commission (def: 0.0): base commission value in percentage or monetary
        units

      - mult (def 1.0): multiplier applied to the asset for value/profit

      - margin (def: None): amount of monetary units needed to open/hold an
        operation. It only applies if the final ``_stocklike`` attribute in the
        class is set to False

      - commtype (def: None): Supported values are CommInfoBase.COMM_PERC
        (commission to be understood as %) and CommInfoBase.COMM_FIXED
        (commission to be understood as monetary units)

        The default value of ``None`` is a supported value to retain
        compatibility with the legacy ``CommissionInfo`` object. If
        ``commtype`` is set to None, then the following applies:

          - margin is None: Internal _commtype is set to COMM_PERC and
            _stocklike is set to True (Operating %-wise with Stocks)

          - margin is not None: _commtype set to COMM_FIXED and _stocklike set
            to False (Operating with fixed rount-trip commission with Futures)

        If this param is set to something else than None, then it will be
        passed to the internal ``_commtype`` attribute and the same will be
        done with the param ``stocklike`` and the internal attribute
        ``_stocklike``

      - stocklike (def: False):  Indicates if the instrument is Stock-like or
        Futures-like (see the ``commtype`` discussion above)

      - percabs (def: False): when ``commtype`` is set to COMM_PERC, whether
        the parameter ``commission`` has to be understood as XX% or 0.XX

        If this param is True: 0.XX
        If this param is False: XX%

    Attributes:

      - _stocklike: Final value to use for Stock-like/Futures-like behavior
      - _commtype: Final value to use for PERC vs FIXED commissions

      This two are used internally instead of the declared params to enable the
      compatibility check described above for the legacy ``CommissionInfo``
      object
    """

    COMM_PERC, COMM_FIXED = range(2)

    params = (
        ("commission", 0.0), ("mult", 1.0), ("margin", None),
        ("commtype", None),
        ("stocklike", False),
        ("percabs", False),
    )

评论被关闭。