pip install TA-Lib
!curl -L http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz -O && tar xzvf ta-lib-0.4.0-src.tar.gz
!cd ta-lib && ./configure --prefix=/usr && make && make install && cd - && pip install ta-lib
Successfully built ta-lib
Installing collected packages: ta-lib
Successfully installed ta-lib-0.4.24
nikkei225
に格納されているところからまずは移動平均を計算したいと思います。nikkei225
の中身は以下ようなものです。
(今回は、2021年1月から2022年4月のデータを使います。)nikkei225
High | Low | Open | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2021-01-04 | 27602.109375 | 27042.320312 | 27575.570312 | 27258.380859 | 51500000 | 27258.380859 |
2021-01-05 | 27279.779297 | 27073.460938 | 27151.380859 | 27158.630859 | 55000000 | 27158.630859 |
2021-01-06 | 27196.400391 | 27002.179688 | 27102.849609 | 27055.939453 | 72700000 | 27055.939453 |
2021-01-07 | 27624.730469 | 27340.460938 | 27340.460938 | 27490.130859 | 98900000 | 27490.130859 |
2021-01-08 | 28139.029297 | 27667.750000 | 27720.140625 | 28139.029297 | 84900000 | 28139.029297 |
... | ... | ... | ... | ... | ... | ... |
2022-04-22 | 27205.830078 | 26904.380859 | 27197.800781 | 27105.259766 | 58500000 | 27105.259766 |
2022-04-25 | 26764.480469 | 26487.839844 | 26692.480469 | 26590.779297 | 62700000 | 26590.779297 |
2022-04-26 | 26808.990234 | 26592.990234 | 26743.210938 | 26700.109375 | 64300000 | 26700.109375 |
2022-04-27 | 26406.619141 | 26051.039062 | 26313.140625 | 26386.630859 | 97300000 | 26386.630859 |
2022-04-28 | 26876.949219 | 26348.359375 | 26430.279297 | 26847.900391 | 86700000 | 26847.900391 |
324 rows × 6 columns
import talib as ta
sma5 = ta.SMA(nikkei225.Close, timeperiod=5)
ema5 = ta.EMA(nikkei225.Close, timeperiod=5)
macd, macdsignal, macdhist = ta.MACD(nikkei225.Close, fastperiod=12, slowperiod=26, signalperiod=9)
# make layout
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=('OHLC', 'MACD'), )
#add candlestick chart
fig.add_trace(go.Candlestick(x=nikkei225.index,
open=nikkei225.Open, high=nikkei225.High,
low=nikkei225.Low, close=nikkei225.Close, name= 'nikkei225'),
row=1, col=1
)
#add sma
fig.add_trace(
go.Scatter(x=sma50.index ,y=sma50, name= 'sma50',
line=dict(color='limegreen' ,width=1)),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=sma200.index ,y=sma200, name= 'sma200',
line=dict(color='navy' ,width=1)),
row=1, col=1
)
#add macd
fig.add_trace(
go.Scatter(x=macd.index ,y=macd, name= 'macd',
line=dict(color='lime' ,width=1)),
row=2, col=1
)
fig.add_trace(
go.Scatter(x=macdsignal.index ,y=macdsignal, name= 'macdsignal',
line=dict(color='maroon' ,width=1)),
row=2, col=1
)
fig.add_trace(
go.Bar(x=macdhist.index ,y=macdhist, name= 'macdhist'),
row=2, col=1
)
plotly.offline.iplot(fig)