You are here:  Home » 量化交易与机器学习 » backtrader » 佣金:backtrader中文教程

佣金:Credit

In某些情况下,在真正的经纪人现金量可能会下降,因为资产操作包括利率。例如:

  • stocks
  • ETF的长期和short

The负责去直接针对在经纪商账户的现金余额。但它仍然可以看作是佣金计划的一部分。正因为如此它一直仿照backtrader.

CommInfoBase类(它也是CommissionInfo主接口对象)已扩展:

  • 两(2)新的参数,允许设定利率和确定是否应该只应用于与短边或长的和short

Parameters

  • interest(DEF:0.0)如果这是不为零,这是收取拿着每年的利息卖空位置。这主要是指股票短期selling

    The默认公式适用于:days * price * size * (interest / 365)

    必须以绝对术语指定:0.05-> 5%

    可以通过重写方法来更改行为: get_credit_interest

  • interest_long(DEF:False)有些产品,如交易所买卖基金获得收取的短期和长期利益位置。如果部份是Trueinterest不为零的利率上都将进行directions

The formula

The默认实现充电将使用以下公式:

days * abs(size) * price * (interest / 365)

在哪里:

  • days:距今位置的天数打开或最后贷款利息计算拿了place

Overriding的formula

In为​​了改变CommissionInfo需要的公式子类。该被覆盖的方法是:

def _get_credit_interest(self, size, price, days, dt0, dt1):
    """
    This method returns  the cost in terms of credit interest charged by
    the broker.

    In the case of ``size > 0`` this method will only be called if the
    parameter to the class ``interest_long`` is ``True``

    The formulat for the calculation of the credit interest rate is:

      The formula: ``days * price * abs(size) * (interest / 365)``


    Params:
      - ``data``: data feed for which interest is charged

      - ``size``: current position size. > 0 for long positions and < 0 for
        short positions (this parameter will not be ``0``)

      - ``price``: current position price

      - ``days``: number of days elapsed since last credit calculation
        (this is (dt0 - dt1).days)

      - ``dt0``: (datetime.datetime) current datetime

      - ``dt1``: (datetime.datetime) datetime of previous calculation

    ``dt0`` and ``dt1`` are not used in the default implementation and are
    provided as extra input for overridden methods
    """

这可能是在broker不考虑周末或节假日时计算利率。在这种情况下,该子类会做trick

import backtrader as bt

class MyCommissionInfo(bt.CommInfo):

   def _get_credit_interest(self, size, price, days, dt0, dt1):
       return 1.0 * abs(size) * price * (self.p.interest / 365.0)

In这种情况下,在公式中:

  • days已经换成1.0

因为如果周末/节假日不计,该下次计算将总是发生1交易DA以前calculation