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.
-
One is telling you the leverage that will be applied to an asset when acquiring it.
The other is telling you the actual leverage of your account, which is obviously 1 if nothing has been bought.
It's in the topic you link to:
@backtrader said in get_leverage() calls:
@ThatBlokeDave said in get_leverage() calls:
However, self.broker.get_leverage() appears to be doing a different calculation
It's not a different calculation. It's different altogether. Invoking that method, which is intended for user consumption unlike the one above, reports the actual leverage level in the market.