For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Maintenance Margin

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

    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!


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


  • administrators

    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.

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

  • administrators

    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

Log in to reply