Back to Community
Average Directional Index - Would appreciate some ideas on how I can improve returns

Haven't tinkered with this one extensively, but would appreciate any ideas on how I can improve returns.

Clone Algorithm
3
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 numpy as np

talibADX = ta.ADX(timeperiod=14)
talibMDI = ta.MINUS_DI(timeperiod=14)
talibPDI = ta.PLUS_DI(timeperiod=14)
topN           = 15
rotationPeriod = 30   

def initialize(context):
    set_universe(universe.DollarVolumeUniverse(floor_percentile=95.00, ceiling_percentile=100.0))
    context.dayCount = 0
    
    set_do_not_order_list(security_lists.leveraged_etf_list)
    
def handle_data(context, data):
    trendingSecurities  = []
    trendingValues      = []
    trendingDirections  = []
    adx_data  = talibADX(data)
    mdi_data  = talibMDI(data)
    pdi_data  = talibPDI(data)
    
    if context.dayCount == 0:
        #Close open position
        for stock in context.portfolio.positions:
            if context.portfolio.positions[stock]['amount'] <> 0:
                order(stock, -context.portfolio.positions[stock]['amount'])
        
        for stock in data:
            # Check if a security is still trading
            if data[stock].datetime < get_datetime():
                continue
            # Check for a pending order
            if get_open_orders(stock):
                #print get_open_orders(stock)
                continue
            # Set a limit on the price of a security
            if data[stock].close_price > 500.00:
                continue
            ADX = adx_data[stock]
            mDI = mdi_data[stock]
            pDI = pdi_data[stock]
            if not np.isnan(ADX):
                if pDI > mDI and ADX > 45:
                    trendingSecurities.append(stock)
                    trendingValues.append(ADX)
                    trendingDirections.append('Up') 
                elif pDI < mDI and ADX > 35:
                    trendingSecurities.append(stock)
                    trendingValues.append(ADX) 
                    trendingDirections.append('Down')
        symbols = []  
        
        for i in range (0, min(topN, len(trendingValues))):
            security = trendingSecurities[np.argsort(trendingValues)[i]]
            position = 1200
            symbols.append(str(security.symbol))
            
            # Check if the security is eligible in the Quantopian Open 
            if security in security_lists.leveraged_etf_list:
                log.info("%s is an over-leveraged ETF, not purchasing" % (security.symbol))
                
            elif trendingDirections[np.argsort(trendingValues)[i]] == 'Up':
                order(security, position)
            else:    
                order(security, -position)
        print sorted(symbols)   
    context.dayCount += 1
    if context.dayCount == rotationPeriod:
        context.dayCount = 0
        
    record(Cash=context.portfolio.cash)
There was a runtime error.