Python/Pandas calculate Ichimoku chart components

前端 未结 6 995
闹比i
闹比i 2021-02-03 15:02

I have Pandas DataFrame object with Date, Open, Close, Low and High daily stock data. I want to calculate components of Ichimoku chart. I can get my data using the following cod

6条回答
  •  情书的邮戳
    2021-02-03 16:03

    EdChum's answer was very close in calculating the components for the Ichimoku Cloud.

    The methodologies are correct but it missed to accommodate for the future dates for both leading_spans . When we are shifting the leading spans by 26 , pandas just shifts till the last date or last index and the extra(or future) 26 values are ignored.

    Here's an implementation that accommodates for the future dates or future cloud formation

    from datetime import timedelta
    
    high_9 = df['High'].rolling(window= 9).max()
    low_9 = df['Low'].rolling(window= 9).min()
    df['tenkan_sen'] = (high_9 + low_9) /2
    
    high_26 = df['High'].rolling(window= 26).max()
    low_26 = df['Low'].rolling(window= 26).min()
    df['kijun_sen'] = (high_26 + low_26) /2
    
    # this is to extend the 'df' in future for 26 days
    # the 'df' here is numerical indexed df
    last_index = df.iloc[-1:].index[0]
    last_date = df['Date'].iloc[-1].date()
    for i in range(26):
        df.loc[last_index+1 +i, 'Date'] = last_date + timedelta(days=i)
    
    df['senkou_span_a'] = ((df['tenkan_sen'] + df['kijun_sen']) / 2).shift(26)
    
    high_52 = df['High'].rolling(window= 52).max()
    low_52 = df['Low'].rolling(window= 52).min()
    df['senkou_span_b'] = ((high_52 + low_52) /2).shift(26)
    
    # most charting softwares dont plot this line
    df['chikou_span'] = df['Close'].shift(-22) #sometimes -26 
    
    tmp = df[['Close','senkou_span_a','senkou_span_b','kijun_sen','tenkan_sen']].tail(300)
    a1 = tmp.plot(figsize=(15,10))
    a1.fill_between(tmp.index, tmp.senkou_span_a, tmp.senkou_span_b)
    

提交回复
热议问题