Taking into account of periodic payments
Hippasus last edited by
Is there any way which allows for periodic payments to be taken into account (such as dividends or coupons)?
If not, where would be the best part of the code to implement such an update? Commissions?
The payments should be considered when calculating the PnL of a specific trade.
Yes, there is a way.
- Documentation: https://www.backtrader.com/docu/commission-credit.html
- Blog: https://www.backtrader.com/blog/posts/2016-08-22-credit-interest/credit-interest.html?highlight=interest
You would override the
_get_credit_interest(self, size, price, days, dt0, dt1)method of a
CommInfoBase) subclass and decide when you have to return negative interest.
Notice that negative is remarked above, because if the returned value is positive it will be deducted from the actual cash of the platform.
dt0is a standard
datetime.datetimeinstance and should allow to decide if its time for dividend payout or not.
Only one drawback with this approach (it may not be a drawback but it's worth mentioning in any case)
- The returned amount is added back to the system as a commission
The rationale behind this is that if you have an open position an interest charge has to affect the final P&L numbers. Of course you may end up with a negative commission.
On the positive side, you know that the trade was positive, because the dividend helped.
Hippasus last edited by Hippasus
Thanks a lot for the detailed answer. I'm already using a customized version of the
CommissionInfoclass, specifically my own version of
_get_credit_interest(self, size, price, days, dt0, dt1)due to a credit interest which is variable over time. However it seems to me as the credit interest is directly deducted from the cash, as you mentioned, but does never show up in the PnL. Especially the commission is constantly 0, no matter how big the interest rate is.
For now I have resorted to calculations inside the
getoperationcostmethod, but it is messy and requires changes in parts of the code I would rather not mess with (such as the 'bbroker' class).
Yes, interest is directly deducted from the cash if positive and added to the cash if negative. Brokers charge interest directly against the cash in your account.
As mentioned above, interest will be assigned to the commission which will affect pnl. This assignment takes place against orders which reduce/close your position, because this makes it count for the pnl of a trade. The relevant code from the broker:
if closed and self.p.int2pnl: # Assign accumulated interest data closedcomm += self.d_credit.pop(order.data, 0.0)
Tbe default value for
Hippasus last edited by
Thanks again, those two lines did not exist in the version 184.108.40.206, which I was using. An update to 220.127.116.11 resolved the problem.
Indeed, the original implementation of the interest didn't add the value to the pnl (via commission)