Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Taking into account of periodic payments

    General Code/Help
    3
    8
    1979
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      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.

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by backtrader

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

        1 Reply Last reply Reply Quote 0
        • H
          Hippasus last edited by Hippasus

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

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by backtrader

            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

            1 Reply Last reply Reply Quote 0
            • H
              Hippasus last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators last edited by

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

                cmal 1 Reply Last reply Reply Quote 0
                • cmal
                  cmal last edited by

                  But periodic payments(dividends) assets also have a commission. How to make dividends and commission work at the same time?

                  1 Reply Last reply Reply Quote 0
                  • cmal
                    cmal @backtrader last edited by

                    @backtrader But periodic payments(dividends) assets (almost always) also have a commission. How to make dividends and commission work at the same time?

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                    $(document).ready(function () { app.coldLoad(); }); }