Back to Community
Ichimoku Order after n days

Hi, I am new to algo trading. I was building an ichimoku cloud indicator. I would like to buy when the close price has been above the cloud for 3 consecutive days, and short the stock if the close price has been below the cloud for 3 consecutive days. I am unable to find anything on internet. Below is the code I am using. Thanks in advance!

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())

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)  

if  (cur_price > senkou_span_a[-1]):  
    order_target_percent(equity, 1.0)  

elif  (cur_price < senkou_span_b[-1]):  
    order_target_percent(equity, -1.0)  

else:  
    pass