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

Hello, I am new to algorithmic trading. In the notebook I coded the ichimoku cloud. In the notebook it runs perfectly. However, when I code the ichimoku cloud in the IDE, I have probelms with the shift command. Where it says 'Shift(26)' I receive an error that says ' 'int' object has no attribute 'shift''. Below is the code. I would appreciate if someone could tell me what am i doing wrong.

def initialize(context):
context.spy = sid(8554)

schedule_function(ichi,date_rules.every_day())  

def ichi(context,data):

cur_price = data.current(context.spy,'price')  


prices = data.history(context.spy,'price', 30 , '1d')  
max_prices = data.history(context.spy, 'high',30,'1d')  
close_prices = data.history(context.spy, 'close',30,'1d')  
min_prices = data.history(context.spy,'low',30,'1d')  

nine_period_high =  max_prices.rolling(window=9).max()  
nine_period_low = min_prices.rolling(window=9).min()  
tenkan_sen = (nine_period_high + nine_period_low) /2  

period26_high = max_prices.rolling(window=26).max()  
period26_low = min_prices.rolling(window=26).min()  
kijun_sen = (period26_high + period26_low) / 2  

senkou_span_a = (tenkan_sen + kijun_sen) / (2).shift(26)  

period52_high = max_prices.rolling(window=52).max()  
period52_low = min_prices.rolling(window=52).min()  
senkou_span_b = ((period52_high + period52_low) / 2).shift(26)  

if cur_price > senkou_span_a:  
    order_target_percent(context.spy, 1.0)  

elif cur_price < senkou_span_a:  
    order_target_percent(context.spy,-1.0)  

else:  
    pass  
5 responses

@Pedro,

Try this:

# Ichimoku Clouds by Vladimir  
#---------------------------------------------------  
equity = symbol('SPY'); n_f = 9; n_m = 26; n_s = 52;  
#---------------------------------------------------  
def initialize(context):  
    schedule_function(Ichimoku, date_rules.every_day(), time_rules.market_open(minutes = 65))

def Ichimoku(context, data):  
    bars = n_s*2  
    cur_price = data.current(equity,'price')  
    H = data.history(equity, 'high', bars, '1d')  
    L = data.history(equity,'low', bars, '1d')

    tenkan_sen = (H.rolling(n_f).max() + L.rolling(n_f).min())/2  
    kijun_sen = (H.rolling(n_m).max() + L.rolling(n_m).min())/2  
    senkou_span_a = ((tenkan_sen + kijun_sen) / 2).shift(n_m)  
    senkou_span_b = ((H.rolling(n_s).max() + L.rolling(n_s).min())/2).shift(n_m)

    record(senkou_span_a = senkou_span_a[-1], senkou_span_b = senkou_span_b[-1], price = cur_price)  

This works perfectly, whanks Vladimir

And do you know how to code to buy if the price is above span_a and below span_b. I tried using a very simple code (below), and I receive the following error: 'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'

f cur_price > senkou_span_a:
order_target_percent(context.spy, 1.0)

elif cur_price < senkou_span_a:
order_target_percent(context.spy,-1.0)

else:
pass

Try this way:

    if cur_price > senkou_span_a[-1]:  
        order_target_percent(context.spy, 1.0)  

    elif cur_price < senkou_span_a[-1]:  
        order_target_percent(context.spy, -1.0)  

Thank you so much, you rock