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

Leverage API question/suggestion



  • I set leverage to 100 and make this call in my main class:

    cerebro.broker.setcommission(commission=args.commission, leverage=args.leverage)
    

    In my strategy init() I need to get the leverage for sizing. I make these calls:

    print("commission leverage: %.1f" % self.broker.getcommissioninfo(self.data).get_leverage())
    print("broker leverage: %.1f" % self.broker.get_leverage())
    

    It says "commission" leverage is 100.0, and broker leverage is 1.0. Nevermind my terminology in the print statements, it's just to distinguish A vs. B. According to this thread ([https://community.backtrader.com/topic/582/get_leverage-calls](link url)), we should use broker.get_leverage() instead of getcommissioninfo() unless we're creating a custom commission class. I would but I don't understand why it returns 1.0?

    Also, I would like to request Backtrader to consider adding an API perhaps called broker.get_buyingpower(). It would allow the strategy to see the leveraged buying power at sim time, when creating an order, because the broker knows the leverage as well as margin in use with current trades and orders.