Back to Community
Buying power (Interactive Brokers)

Hi there, I am having trouble controlling leverage in the backtest and live trading since
context.account.buying_power (IB: BuyingPower) for IB Margin Account = Available Funds * 4. But the backtest value buying_power is set to infinity.
I have been trying something like this:

    net_liquid=context.portfolio.portfolio_value  
    buying_power=context.account.available_funds*4  
    leverage= (buying_power/(net_liquid))  
    order_target_percent(sec, leverage)

But the result is different. I am wondering if anyone has any suggestions?

Clone Algorithm
6
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import pandas as pd

# The initialize function is the place to set your tradable universe and define any parameters. 
def initialize(context):
    context.algo3 = [symbol('SPXL')]
    set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.00))
    set_slippage(slippage.FixedSlippage(0))

    schedule_function(go_long, date_rules.every_day(), time_rules.market_close(minutes=30))
    schedule_function(go_short, date_rules.every_day(), time_rules.market_close(minutes=15))
    schedule_function(close_all, date_rules.every_day(), time_rules.market_close(minutes=1))
    schedule_function(my_record_close, date_rules.every_day(), time_rules.market_close()) 

def get_time():
    return pd.Timestamp(get_datetime()).tz_convert('US/Eastern')

def close_all(context, data):
    log.debug("%s Closing" % get_time().strftime('%H:%M'))
    for sec in context.algo3:
        order_target(sec, 0)
        

def go_long(context, data):
    log.debug("%s Going long" % get_time().strftime('%H:%M'))
    for sec in context.algo3:
        net_liquid=context.portfolio.portfolio_value
        buying_power=context.account.available_funds*4
        leverage= (buying_power/(3*net_liquid))
        order_target_percent(sec, leverage)

def go_short(context, data):
    log.debug("%s Going long" % get_time().strftime('%H:%M'))
    for sec in context.algo3:
        net_liquid=context.portfolio.portfolio_value
        buying_power=context.account.available_funds*4
        leverage= (buying_power/(4*net_liquid))
        order_target_percent(sec, -leverage)
    

def handle_data(context,data):
    pass


def my_record_close(context, data):
    record(leverage_close=context.account.leverage) 
    
There was a runtime error.
1 response

I think this is firstly due to what type of margin account you have. Do you have a portfolio margin account or a regular T margin?
Secondly, IB usually have different standards for different account types, for instance they will treat a proprietary group differently from and individual investor.
Third and last. I think you are required to exceed a certain set amount in net liquidity before you are eligible for using their full margin perks. If I remember correctly they set out something like a 100,000.00$ but I'm not entirely sure.
I hope something in this post is of value to you.

Best,
Carl