Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
CCI

CCI

1 response

This can help

# CCI_strategy  
import talib  
# ------------------------------------------------------  
STOCK = symbol('SPY'); PERIOD = 20; UB = 100; LB = -100;  
# ------------------------------------------------------  
def initialize(context):  
    schedule_function(CCI_strategy, date_rules.every_day(), time_rules.market_open(minutes = 65))

def CCI_strategy(context, data):  
    H = data.history(STOCK, 'high' , PERIOD + 2, '1d')  
    L = data.history(STOCK, 'low'  , PERIOD + 2, '1d')  
    C = data.history(STOCK, 'price', PERIOD + 2, '1d')  
    cci = talib.CCI(H, L, C, PERIOD)  
    curr_position = context.portfolio.positions[STOCK].amount  

    if (curr_position <= 0) and (cci[-1] < LB) and (cci[-2]> LB):  
        order_target_percent(STOCK, 1.0)  
        print('Buying')  

    elif (curr_position >= 0) and (cci[-1] > UB) and (cci[-2] < UB):  
        order_target_percent(STOCK, -1.0)  
        print('Shorting')  

    record(leverage = context.account.leverage, CCI = cci[-1], UB = UB, LB = LB)