1

ichimoku data is calculated with pandas_ta and i want to add them to ETH chart with mplfinance.

import pandas as pd
import pandas_ta as ta
import mplfinance as mpl
import matplotlib.pyplot as plt

df=pd.read_csv('.../ETH-USD.csv',index_col=0,parse_dates=True)
df_org=df.copy()

new_names = {
    'ISA_9': 'tenkan_sen', 
    'ISB_26': 'kijun_sen', 
    'ITS_9': 'senkou_span_a', 
    'IKS_26': 'senkou_span_b', 
    'ICS_26': 'chikou_span'
}

ich= ta.ichimoku(df['High'], df['Low'], df['Close'],senkou=True)[0].rename(columns=new_names)
df=pd.concat([df,ich]).drop(columns=['senkou_span_a','senkou_span_b'])

The problem is that when i add the ichimoku to the chart with mpl.plot method, the result would't be satisfied because the (tenkan_sen,kijun_sen and chikou_span) data is shifted to the right.

fig=mpl.figure(figsize=(12,5))
ax = fig.add_subplot(1,1,1)
apds =[mpl.make_addplot(df.tenkan_sen,color='blue',ax=ax),
       mpl.make_addplot(df.kijun_sen,color='r',ax=ax),
       mpl.make_addplot(df.chikou_span,color='green',ax=ax),]

mpl.plot(df,type='candle',style='yahoo',addplot=apds,ax=ax)

image_result (mplfinance chart)

i think the problem is with the Null value with the shifted ichimoku data. i would appreciate for anyone who helps me to fix this issue....

I used matplotlib and it can handle thenull value in plotting, But the goal is to plot the data into mplfinance.....

  • I think it is due to a missing data frame at the data frame join. The correct graph can be obtained by modifying the following code. `df = df.merge(ich, left_on=df.index, right_on=ich.index).drop(columns=['senkou_span_a','senkou_span_b']);df.rename(columns={'key_0': 'Date'}, inplace=True);df['Date'] = pd.to_datetime(df['Date']);df.set_index('Date', inplace=True)` – r-beginners Mar 22 '23 at 11:32

2 Answers2

0

use below function to plot ichimoku (chikou_span,kijun_sen) on OHLC data ponit.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def plt_candle(path_data):

  try:
    data=pd.read_csv(path_data,index_col=0,parse_dates=True)
    print('Data is loaded')

    fig=plt.figure(figsize=(20,10))
    ax=fig.add_subplot(1,1,1)

    # Ichimoku-> chikou and kijen_sen
    period26_high = data.High.rolling(window=26).max()
    period26_low = data.Low.rolling(window=26).min()
    kijun_sen = pd.Series((period26_high + period26_low) /2).rename('kijun_sen')

    chikou_span = data.Close.shift(-25).rename('chikou_span')

    df=pd.concat([data,chikou_span,kijun_sen ],axis=1,join='inner')

    # Increase and decrease value of Data
    inc= df.Close > df.Open
    dec= df.Close < df.Open

    # OHLC value
    open=np.array(df.Open)
    close=np.array(df.Close)
    low=np.array(df.Low)
    high=np.array(df.High)
    date=np.array(df.index)

    bar_width=3

    plt.vlines(x=date, ymin=low, ymax=high, color='k', linestyle='-',
            linewidth=1)

    plt.vlines(date[inc],open[inc],close[inc],color='green',linewidth=bar_width)
    plt.vlines(date[dec],open[dec],close[dec],color='red',linewidth=bar_width)

    plt.plot(df.chikou_span,c='g', linewidth=1)
    plt.plot(df.kijun_sen,c='red',linewidth=1)

    plt.show()
  except:

     print('data path is incorect, try Again!')


 plt_candle('path_data...')
Desmond
  • 1
  • 1
0

The problem is in your pd.concat(). You have to add axis=1

df=pd.concat([df,ich],axis=1).drop(columns=['senkou_span_a','senkou_span_b'])

That should do the trick.


That said, note that you do not have to create subplots and access the Axes objects. Mplfinance will do that for you. As a general rule, avoid contact with the Axes objects when using mplfinance, unless there is no other way to accomplish what you are trying to do. This will keep your code simpler, and give you the full benefits of mplfinance:

import pandas as pd
import pandas_ta as ta
import mplfinance as mpl

df=pd.read_csv('ETH-USD.csv',index_col=0,parse_dates=True)
#df_org=df.copy()

new_names = {
    'ISA_9': 'tenkan_sen', 
    'ISB_26': 'kijun_sen', 
    'ITS_9': 'senkou_span_a', 
    'IKS_26': 'senkou_span_b', 
    'ICS_26': 'chikou_span'
}

ich= ta.ichimoku(df['High'], df['Low'], df['Close'],senkou=True)[0].rename(columns=new_names)
df=pd.concat([df,ich],axis=1).drop(columns=['senkou_span_a','senkou_span_b'])

apds =[mpl.make_addplot(df.tenkan_sen,color='blue'),
       mpl.make_addplot(df.kijun_sen,color='r'),
       mpl.make_addplot(df.chikou_span,color='green')]

mpl.plot(df,type='candle',style='yahoo',addplot=apds)


The result:

enter image description here

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61