Display candlestick chart in Python (matplotlib edition) It is a continuation of.
Plotly is not a Python-only package, but also supports R, Matlab, JavaScript, etc. In online mode, you need to create an account, but if you want to use it offline, you don't need to create an account. The following is an offline explanation.
Installation of Plotly's Python package can be found on this page (https://plot.ly/python/getting-started/)
$ pip install plotly
Is OK.
Then, like previous article, create fictitious 4-value data. However, this time, the data is for one year.
import numpy as np
import pandas as pd
idx = pd.date_range('2015/01/01', '2015/12/31 23:59', freq='T')
dn = np.random.randint(2, size=len(idx))*2-1
rnd_walk = np.cumprod(np.exp(dn*0.0002))*100
df = pd.Series(rnd_walk, index=idx).resample('B').ohlc()
The reference page is here. Candlestick Charts in Python
First, display the candlestick chart with the standard settings.
from plotly.offline import init_notebook_mode, iplot
from plotly.tools import FigureFactory as FF
init_notebook_mode(connected=True) #Settings for Jupyter notebook
fig = FF.create_candlestick(df.open, df.high, df.low, df.close, dates=df.index)
iplot(fig)
Just pass Open data, High data, Low data, Close data, date and time data to the argument of create_candlestick
.
The chart looks like this.
Actually, you can zoom and pan the chart freely, so you can enlarge it as shown below.
Plotly charts are very useful as interactive charts, but after all, there is space on Saturdays and Sundays, and they are not continuous on business days.
Therefore, we will work on the x-axis data so that only business days are continuous. The procedure is the same as for matplotlib. If you omit the dates
argument of create_candlestick
, the x-axis data will be an integer index. Therefore, the date and time data corresponding to the index is displayed as the x-axis scale.
fig = FF.create_candlestick(df.open, df.high, df.low, df.close)
xtick0 = (5-df.index[0].weekday())%5 #First monday index
fig['layout'].update({
'xaxis':{
'showgrid': True,
'ticktext': [x.strftime('%Y-%m-%d') for x in df.index][xtick0::5],
'tickvals': np.arange(xtick0,len(df),5)
}
})
iplot(fig)
The displayed chart is as follows.
The x-axis scale is displayed at weekly intervals, so it's a little fine, but it's easier to see if you zoom in.
You can see that this is a candlestick chart for business days only.
Recommended Posts