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/

    Maintenance Margin

    General Discussion
    2
    4
    1836
    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.
    • T
      ThatBlokeDave last edited by

      Hello,
      Are there any plans to support maintenance margin, margin calls / automatic closing of positions in Backtrader?

      I see from the docs, the margin is set as a fixed monetary value. I.e it is a deposit required to open the position. However, in some markets, we have the initial margin required to enter the position (usually a percentage of the value - set by the amount of leverage you are using), followed by a maintenance margin that is needed to keep the position open.

      https://www.backtrader.com/docu/commission-schemes/commission-schemes.html#backtrader.CommInfoBase

      If not I plan to implement it by making a check for the account balance and open position(s) PnL and then seeing if it would result in a margin close out. If so, I will close the position manually and mark it with an appropriate log. However, if there are plans to support it or I have missed something in the docs once again, it would be good to know!

      Note

      I see there is an automargin feature that can be used to automatically calculate the initial margin required. However, I am a bit confused by this line:

      Use param automargin * price if automargin > 0

      I thought automargin was a boolean value so not sure how it is used in a calculation. Perhaps I am having a dumbo moment :).

      Cheers

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

        Automargin is explained in the reference for CommInfoBase, for example here Docs - Commissions: Stocks vs Futures

        automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy
        
          - Use param margin if param automargin evaluates to False
          - Use param mult * price if automargin < 0
          - Use param automargin * price if automargin > 0
        

        At the moment there are no plans for the support of those. It requires some changes to the entire architecture.

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

          @backtrader

          Ok thanks for the info. I can implement a work around to support those for now.

          My confusion for auto-margin was exactly in the place you have referenced. I thought it should be set as True or False since the default is False. Having re-read it, I guess this means I should set it as a float or int.

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

            It tries to support many combinations:

            • Evaluates to False (it can be False, None, 0 ...)

            Else it is assumed to be a numeric value

            • if > 0 then use it with price
            • if < 0 then use mult * price
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors