Back to Community
Need help un-deprecating an algo

Looking for a hand on removing the deprecated functions from this code

Clone Algorithm
15
Loading...
Backtest from to with initial capital
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
 
def initialize(context):

    context.nocash = True #True = Spread evenly among ETFs over MA
    
    #set_symbol_lookup_date('2016-01-01')
    #list of asset classes
    context.assetclasses = symbols(
                                   # US Equity 
                                   #'VTI', # US Broad Stocks
                                   'IJH', # US Mid Cap
                                   'SPY', # US Large Cap
                                   'IWM', # US Small Cap
                                   'IWC', # US Microcap
                                   'RSP', # US Equal Weight
                                   'QQQ', # NASDAQ
        
                                   # Foreign Equity     
                                   'EFA', # Foreign Developed Stocks
                                   #'DWM', # Developed Total
                                   #'ADRD', # Developed Large Cap
                                   #'DIM', # Developed Mid Cap
                                   #'DLS', # Developed Small Cap
                                   'EEM', # Emerging Stocks
                                   #'VWO', # Emerging Total
                                   #'ADRE', # Emerging Large Cap
                                   #'DGS', # Emerging Small Cap
                                   #'FM', # Frontier Stocks
                                   #'FXI', # China
                                   #'EWJ', # Japan
                                   #'IOO', # Global Large Cap
                                   #'VT', # Total World
        
                                   # Real Estate
                                   'IYR', # US Real Estate
                                   'RWX', # Foreign Real Estate
        
                                   # Commodities 
                                   'DBC', # Commodities
                                   #'SLV', # Silver
                                   'GLD', # Gold
                                   #'GDX', # Gold Miners
                                   #'KXI', # Global Consumer Staples
        
                                   # Fixed Income
                                   'TLT', # US 20 Year Treasury
                                   #'ZROZ', # US Zero Coupon
                                   #'EDV', # US Extended Duration
                                   #'IEF', # US 10 Year Treasury
                                   #'IEI', # US 7 Year Treasury
                                   #'SHY', # US 1-3 Year Treasury
                                   'TIP', # US TIPS
                                   'LQD',  # US Corporate Bonds
                                   #'IGOV', # Foreign Treasury
                                   #'IBND', # Foreign Corporate Bonds
                                   'MUB', # US Municipal Bonds
                                   #'HYD', # US Municipal Bond High Yield
                                   #'MBB', # US Mortgage Backed Securities
                                   #'AGG', # US Total Bond Market
                                   'BNDX', # Foreign Total Bond Market
                                   'HYG', # US Corporate High Yield
        
                                   # Currency
                                   #'UDN', # Foreign Currency
                                   #'UUP', # US Dollar
        
                                   # Alternative
                                   #'XIV', # Volatility
                                   
                                  )
    schedule_function(rebalance,
                      date_rule=date_rules.month_start(),
                      time_rule=time_rules.market_open())
    

def rebalance(context, data):
    buylist = []
    
    #find which asset classes are above their moving average
    for s in context.assetclasses:
        if s in data and data[s].price > data[s].mavg(100):
            buylist.append(s)
    
    #sell all positions that are not above moving average
    for s in context.portfolio.positions:
        if s not in buylist:
            order_target_percent(s, 0)
    #If nothing is worth buying, don't open a black hole by dividing by zero
    if len(buylist) == 0:
        return
    
    #Do you allocate all your portfolio to the best performing asset classes or do you allocate only 1/n?
    if context.nocash:
        weight = 1.0/len(buylist)
    else:
        weight = 1.0/len(context.assetclasses)

    
    for s in buylist:
        order_target_percent(s, weight)

def handle_data(context, data):
    pass
There was a runtime error.
3 responses

Hey William,

Looks like I got it to work by changing the following lines in the rebalance section, note that for this you will need to "import talib" at the top of your code.

    for s in context.assetclasses:  
        history = data.history(s, 'price',150,'1d')  
        MA = talib.SMA(history,timeperiod=100)  
        if data.current(s, 'price') > MA[-1]:  
            buylist.append(s)  

Hope this helps and good luck!

That worked great, thanks!

William,

There is anoter more native solution without talib:

    for s in context.assetclasses:  
        if data.current(s, 'price') > data.history(s, 'price',100,'1d').mean():  
            buylist.append(s)