Python/Pandas calculate Ichimoku chart components

前端 未结 6 971
闹比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:06

    import mplfinance as mpf
    import pandas as pd
    
    #Import the data into a "df", with headers, with the name of the stock like "stk = 'AAPL'"
    #MPLFinance does not fill-in-between,hence there is no cloud.
    
    #Tenkan Sen
    tenkan_max = df['High'].rolling(window = 9, min_periods = 0).max()
    tenkan_min = df['Low'].rolling(window = 9, min_periods = 0).min()
    df['tenkan_avg'] = (tenkan_max + tenkan_min) / 2
    
    #Kijun Sen
    kijun_max = df['High'].rolling(window = 26, min_periods = 0).max()
    kijun_min = df['Low'].rolling(window = 26, min_periods = 0).min()
    df['kijun_avg'] = (kijun_max + kijun_min) / 2
    
    #Senkou Span A
    #(Kijun + Tenkan) / 2 Shifted ahead by 26 periods
    df['senkou_a'] = ((df['kijun_avg'] + df['tenkan_avg']) / 2).shift(26)
    
    #Senkou Span B
    #52 period High + Low / 2
    senkou_b_max = df['High'].rolling(window = 52, min_periods = 0).max()
    senkou_b_min = df['Low'].rolling(window = 52, min_periods = 0).min()
    df['senkou_b'] = ((senkou_b_max + senkou_b_min) / 2).shift(52)
    
    #Chikou Span
    #Current close shifted -26
    df['chikou'] = (df['Close']).shift(-26)
    
    
    #Plotting Ichimoku
    
    #m_plots = ['kijun_avg', 'tenkan_avg',df[df.columns[5:]][-250:] ]
    
    add_plots= [
                mpf.make_addplot(df['kijun_avg'][-250:]),
                mpf.make_addplot(df['tenkan_avg'][-250:]),
                mpf.make_addplot(df['chikou'][-250:]),
                mpf.make_addplot(df['senkou_a'][-250:]),
                mpf.make_addplot(df['senkou_b'][-250:])
               ]
    
    mpf.plot(df[-250:], type = 'candle', mav= 200, volume = True, ylabel = "Price", ylabel_lower  = 'Volume', style = 'nightclouds', figratio=(15,10), figscale = 1.5,  addplot = add_plots,  title = '%s' %stk)
    

提交回复
热议问题