go.Scatter
で2つの線で囲われた領域を塗りつぶす方法についてのメモです。!pip install --upgrade pandas
!pip install --upgrade pandas-datareader
import pandas_datareader.data as web
nikkei225 = web.DataReader('^N225', 'yahoo', "2022-01-01", "2022-05-31")
nikkei225
High | Low | Open | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2022-01-04 | 29323.789062 | 28954.560547 | 29098.410156 | 29301.789062 | 66000000 | 29301.789062 |
2022-01-05 | 29388.160156 | 29204.449219 | 29288.800781 | 29332.160156 | 78600000 | 29332.160156 |
2022-01-06 | 29158.949219 | 28487.869141 | 29136.750000 | 28487.869141 | 71600000 | 28487.869141 |
2022-01-07 | 28813.089844 | 28293.699219 | 28711.529297 | 28478.560547 | 75200000 | 28478.560547 |
2022-01-11 | 28473.470703 | 28089.490234 | 28380.900391 | 28222.480469 | 73700000 | 28222.480469 |
... | ... | ... | ... | ... | ... | ... |
2022-05-26 | 26898.750000 | 26597.970703 | 26685.019531 | 26604.839844 | 64500000 | 26604.839844 |
2022-05-27 | 26996.699219 | 26731.599609 | 26947.800781 | 26781.679688 | 69300000 | 26781.679688 |
2022-05-30 | 27401.240234 | 27057.199219 | 27092.820312 | 27369.429688 | 97000000 | 27369.429688 |
2022-05-31 | 27463.330078 | 27250.699219 | 27318.089844 | 27279.800781 | 141800000 | 27279.800781 |
2022-06-01 | 27482.310547 | 27295.419922 | 27295.630859 | 27430.480469 | 0 | 27430.480469 |
99 rows × 6 columns
!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
import talib as ta
nikkei225["SMA5"] = ta.SMA(nikkei225.Close, timeperiod=5)
nikkei225["SMA20"] = ta.SMA(nikkei225.Close, timeperiod=20)
nikkei225
High | Low | Open | Close | Volume | Adj Close | SMA5 | SMA20 | |
---|---|---|---|---|---|---|---|---|
Date | ||||||||
2022-01-04 | 29323.789062 | 28954.560547 | 29098.410156 | 29301.789062 | 66000000 | 29301.789062 | NaN | NaN |
2022-01-05 | 29388.160156 | 29204.449219 | 29288.800781 | 29332.160156 | 78600000 | 29332.160156 | NaN | NaN |
2022-01-06 | 29158.949219 | 28487.869141 | 29136.750000 | 28487.869141 | 71600000 | 28487.869141 | NaN | NaN |
2022-01-07 | 28813.089844 | 28293.699219 | 28711.529297 | 28478.560547 | 75200000 | 28478.560547 | NaN | NaN |
2022-01-11 | 28473.470703 | 28089.490234 | 28380.900391 | 28222.480469 | 73700000 | 28222.480469 | 28764.571875 | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... |
2022-05-26 | 26898.750000 | 26597.970703 | 26685.019531 | 26604.839844 | 64500000 | 26604.839844 | 26754.266016 | 26575.806543 |
2022-05-27 | 26996.699219 | 26731.599609 | 26947.800781 | 26781.679688 | 69300000 | 26781.679688 | 26762.796094 | 26585.351562 |
2022-05-30 | 27401.240234 | 27057.199219 | 27092.820312 | 27369.429688 | 97000000 | 27369.429688 | 26836.378125 | 26618.817578 |
2022-05-31 | 27463.330078 | 27250.699219 | 27318.089844 | 27279.800781 | 141800000 | 27279.800781 | 26942.710156 | 26663.476074 |
2022-06-01 | 27482.310547 | 27295.419922 | 27295.630859 | 27430.480469 | 0 | 27430.480469 | 27093.246094 | 26692.605078 |
99 rows × 8 columns
import plotly
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
print(plotly.__version__)
fig = go.Figure(go.Candlestick(x=nikkei225.index,
open=nikkei225.Open, high=nikkei225.High,
low=nikkei225.Low, close=nikkei225.Close, name= 'OHLC')
)
#add sma
fig.add_trace(
go.Scatter(x=nikkei225.index ,y=nikkei225.SMA5, name= 'SMA5',
line=dict(color='limegreen' ,width=1)),
)
fig.add_trace(
go.Scatter(x=nikkei225.index ,y=nikkei225.SMA20, name= 'SMA20',
line=dict(color='navy' ,width=1))
)
iplot(fig)
cross
カラムを以下の様に追加します。import numpy as np
nikkei225['cross'] = np.nan
for index, row in nikkei225.iterrows():
if row['SMA5'] < row['SMA20']:
nikkei225.at[index,'cross'] = row['SMA5']
elif row['SMA5'] > row['SMA5']:
nikkei225.at[index,'cross'] = row['SMA20']
elif row['SMA5'] == row['SMA5']:
nikkei225.at[index,'cross'] = row['SMA20']
nikkei225
High | Low | Open | Close | Volume | Adj Close | SMA5 | SMA20 | cross | |
---|---|---|---|---|---|---|---|---|---|
Date | |||||||||
2022-01-04 | 29323.789062 | 28954.560547 | 29098.410156 | 29301.789062 | 66000000 | 29301.789062 | NaN | NaN | NaN |
2022-01-05 | 29388.160156 | 29204.449219 | 29288.800781 | 29332.160156 | 78600000 | 29332.160156 | NaN | NaN | NaN |
2022-01-06 | 29158.949219 | 28487.869141 | 29136.750000 | 28487.869141 | 71600000 | 28487.869141 | NaN | NaN | NaN |
2022-01-07 | 28813.089844 | 28293.699219 | 28711.529297 | 28478.560547 | 75200000 | 28478.560547 | NaN | NaN | NaN |
2022-01-11 | 28473.470703 | 28089.490234 | 28380.900391 | 28222.480469 | 73700000 | 28222.480469 | 28764.571875 | NaN | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2022-05-26 | 26898.750000 | 26597.970703 | 26685.019531 | 26604.839844 | 64500000 | 26604.839844 | 26754.266016 | 26575.806543 | 26575.806543 |
2022-05-27 | 26996.699219 | 26731.599609 | 26947.800781 | 26781.679688 | 69300000 | 26781.679688 | 26762.796094 | 26585.351562 | 26585.351562 |
2022-05-30 | 27401.240234 | 27057.199219 | 27092.820312 | 27369.429688 | 97000000 | 27369.429688 | 26836.378125 | 26618.817578 | 26618.817578 |
2022-05-31 | 27463.330078 | 27250.699219 | 27318.089844 | 27279.800781 | 141800000 | 27279.800781 | 26942.710156 | 26663.476074 | 26663.476074 |
2022-06-01 | 27482.310547 | 27295.419922 | 27295.630859 | 27430.480469 | 0 | 27430.480469 | 27093.246094 | 26692.605078 | 26692.605078 |
99 rows × 9 columns
cross
を使い、 SMA5
と SMA20
のそれぞれで塗りつぶし有りの line で描画します。fig = go.Figure(go.Candlestick(x=nikkei225.index,
open=nikkei225.Open, high=nikkei225.High,
low=nikkei225.Low, close=nikkei225.Close, name= 'OHLC')
)
fig.add_trace(
go.Scatter(
x = nikkei225.index,
y = nikkei225.cross,
name = "------",
line = dict(color = ('rgba(255, 255, 255, 0)'))
))
fig.add_trace(
go.Scatter(x=nikkei225.index ,y=nikkei225.SMA5, name= 'SMA5',
fill = 'tonexty',
fillcolor = 'rgba(0, 255, 0, 0.3)',
line=dict(color='limegreen' ,width=1)
)
)
fig.add_trace(
go.Scatter(
x = nikkei225.index,
y = nikkei225.cross,
name = "------",
line = dict(color = ('rgba(255, 255, 255, 0)'))
))
fig.add_trace(
go.Scatter(x=nikkei225.index ,y=nikkei225.SMA20, name= 'SMA20',
fill = 'tonexty',
fillcolor = 'rgba(255, 0, 0, 0.3)',
opacity=0.1,
line=dict(color='navy' ,width=1)
)
)
iplot(fig)
fill='tonexty'
で OK です。fig = go.Figure(go.Candlestick(x=nikkei225.index,
open=nikkei225.Open, high=nikkei225.High,
low=nikkei225.Low, close=nikkei225.Close, name= 'OHLC')
)
#add sma
fig.add_trace(
go.Scatter(x=nikkei225.index ,y=nikkei225.SMA5, name= 'SMA5',
line=dict(color='limegreen' ,width=1)),
)
fig.add_trace(
go.Scatter(x=nikkei225.index ,y=nikkei225.SMA20, name= 'SMA20',
fill='tonexty',
line=dict(color='navy' ,width=1))
)
iplot(fig)