Taking into account of periodic payments



  • 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.


  • administrators

    Yes, there is a way.

    You would override the _get_credit_interest(self, size, price, days, dt0, dt1) method of a CommissionInfo (or 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.

    dt0 is a standard datetime.datetime instance 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.



  • Thanks a lot for the detailed answer. I'm already using a customized version of the CommissionInfo class, 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 getoperationcost method, but it is messy and requires changes in parts of the code I would rather not mess with (such as the 'bbroker' class).


  • administrators

    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 int2pnl is True



  • Thanks again, those two lines did not exist in the version 1.9.6.99, which I was using. An update to 1.9.20.105 resolved the problem.


  • administrators

    Indeed, the original implementation of the interest didn't add the value to the pnl (via commission)


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.