在某些情況下,真實經紀人的現金金額可能會減少,因為資產操作包括利率。例子:
-
賣空股票
-
交易所買賣基金包括多頭和空頭
該費用直接與經紀人帳戶中的現金餘額挂鉤。但它仍然可以被視為傭金計劃的一部分。因此,它已被建模為 backtrader。
該CommInfoBase 類(以及隨之而來的主 CommissionInfo 介面物件)已擴展為:
- 兩(2)個新參數,允許設置利率並確定是否應僅應用於空頭或同時應用於多頭和空頭
參數
-
interest(防守:0.0)如果此值為非零,則為持有賣空頭寸所收取的年利息。這主要用於股票賣空
套用預設公式:
days * price * size * (interest / 365)必須以絕對值指定:0.05 -> 5%
注意
可以通過重寫該方法來更改行為:
get_credit_interest -
interest_long(防守:False)一些產品,如ETF,需要對空頭和多頭頭寸收取利息。如果 ths 為
True非interest零,則將在兩個方向上收取利息
公式
預設實現將使用以下公式:
days * abs(size) * price * (interest / 365)
哪裡:
days:自開倉或信用利息計算 last 天數
覆蓋公式
為了改變公式,需要子類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
'''
可能是經紀人在計算利率時不考慮週末或銀行假日。在這種情況下,這個子類可以解決問題。
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)
在本例中,在公式中:
days已被替換為1.0
因為如果週末/銀行假日不計算在內,下一次計算將始終發生在1 上一次計算之後的交易da