CommInfoBase 通过拓展 interest 和 interest_long 这两个参数, 使得为空单或者多空单的设置融资成本成为可能。
Parameters
- interest (def: 0.0)
If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling
The default formula applied: days * price * size * (interest / 365)
It must be specified in absolute terms: 0.05 -> 5%
- interest_long (def: False)
Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions
Overriding the formula
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
'''
Example :
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 this case, in the formula:
- days has been replaced by 1.0
Because if weekends/bank holidays do not count, the next calculation will always happen 1 trading da after the previous calculation
网友评论