Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
When does this algorithm sell?

Hi,

I am trying to understand this simple ETF rebalancing and diversification algorithm so I can work up from it.
It makes pretty good returns, and I see it is using order_target_percent.

'''
A simple algorithm showing how diversification and rebalancing  
can make dramatic improvements to volatility and returns.  
Note that this trades in 3X leveraged ETFs to get increased returns.  
The diversification however, keeps the volatility in check.  
'''
def initialize(context):  
    """  
    Called once at the start of the algorithm.  
    """  
    # Here are any algorithm 'constants' we'll be using  
    context.target_leverage = 1.0  
    # Here are the ETFs we want to trade along with the weights  
    # Ensure they add to 1.00  
    context.etfs = {  
        symbol('TYD'): 0.1, # Daily 7-10 Year Treasury Bull 3X Shares  
        symbol('TMF'): 0.2, # Daily 20+ Year Treasury Bull 3X Shares  
        symbol('EDZ'): 0.2, # Daily MSCI Emerging Markets Bear 3X Shares  
        symbol('SPXL'): 0.5, # Daily S&P 500 Bull 3X Shares  
    }  
    # Set commision model for Robinhood  
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))  
    # Rebalance our portfolio to maintain target weights  
    schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(minutes = 35))  

def rebalance(context, data):  
    for stock, weight in context.etfs.items():  
        order_target_percent(stock, weight*context.target_leverage)  

When does it actually sell?

2 responses

@Nikhil

Rebalancing is the selling of part of the winners and the buying of part of the losers in order to get the target allocation.

When does it actually sell?

As you scheduled.

schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(minutes = 35)  

To see the action of the algorithm:

Set initial capital to 100000 -> Run Full Backtest -> Activity -> Transactions -> Load Transactions.

Happy backtesting.

Okay, I see. Thanks Vladimir.