Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need help with my 2x leverage backtest code

Hello,
I created a backtest that rebalances every early January. It consists of 30% TLT and 70% SPY. This backtest works.
I then turn it into a 2x leverage portfolio (60% TLT, 140% SPY), but the backtest gave results that makes no sense.
Can someone please help?
Below the code.

Thank you in advanced.

def initialize (context):
context.TLT = sid(23921)
context.SPY = sid(8554)

schedule_function(rebalance, date_rules.month_start(), time_rules.market_open())

def rebalance(context, data):
today = get_datetime('US/Eastern')
if today.month == 1:
if data.can_trade(context.TLT):
order_target_percent(context.TLT, 0.60)
if data.can_trade(context.SPY):
order_target_percent(context.SPY, 1.40)

4 responses

but the backtest gave results that makes no sense.
What means "makes no sense"? Or you mean no difference?

Every year, the portfolio will be rebalanced, meaning some TLT arebought, and some SPY are sold, or vice versa.
However, my 2x leverage backtest showed that more TLT and SPY are bought every year, which doesn't make sense.

Try running the code in both 1x and 2x leverage and check the "Transaction details" and you will see what I mean.

Leon Teh,

You should also take into consideration that both SPY and TLT pay dividends which are reinvested on re-balance day.

I see. Any idea if it is possible to backtest without dividend?