It will be the 3rd time. This time, I will do something that seems to be the Nikkei Stock Average. First, read the csv data as usual.
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
plt.style.use('ggplot') #Magic
font = {'family' : 'meiryo'}
nikkei = pd.read_csv("nikkei.csv", parse_dates=['Data date']) #Read csv data
nikkei.head() #Take a look at the overview
First, let's calculate the date and time earnings fluctuation rate and show it in the figure.
nikkei['Day. Change P'] = (nikkei['closing price'] - nikkei['Open price']) / nikkei['Open price'] #Calculate the date and time earnings fluctuation rate
plt.figure(figsize=(22, 8))
plt.hist(nikkei['Day. Change P'], bins = 100)
I feel like this. It's almost normal distribution. It seems that there are many days when the Nikkei average does not fluctuate much. Please use your own judgment for analysis. I mean, I was able to do it like this ...
Next is how to write a candlestick. It was unexpectedly easy. There is something that is just right.
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
#Create OHLC (open-high-low-close) charts
trace = go.Ohlc(x = nikkei['Data date'],
open = nikkei['Open price'],
high = nikkei['High price'],
low = nikkei['Low price'],
close = nikkei['closing price'])
data = [trace]
iplot(data)
Finally, let's calculate and plot the moving average.
nikkei['5dayma'] = nikkei['closing price'].rolling(window = 5).mean()
nikkei['25dayma'] = nikkei['closing price'].rolling(window = 25).mean()
nikkei['50dayma'] = nikkei['closing price'].rolling(window = 50).mean()
nikkei.tail()
Let's calculate as above and add it to the data frame. Here, with .head (), it is difficult to check whether it is possible to calculate properly such that '5dayma' is calculated only on the 5th day by displaying NaN from the 1st to the 4th day, so .tail () I am using.
trace_close = go.Scatter(x = nikkei['Data date'][-200:],
y = nikkei['closing price'][-200:],
name = "close",
line = dict(color = '#000000'), #Make it a black line
opacity = 0.8)
trace_5day = go.Scatter(x = nikkei['Data date'][-200:],
y = nikkei['5dayma'][-200:],
name = "5day",
opacity = 0.8)
trace_25day = go.Scatter(x = nikkei['Data date'][-200:],
y = nikkei['25dayma'][-200:],
name = "25day",
opacity = 0.8)
trace_50day = go.Scatter(x = nikkei['Data date'][-200:],
y = nikkei['50dayma'][-200:],
name = "50day",
opacity = 0.8)
data = [trace_close, trace_5day, trace_25day, trace_50day]
layout = dict(title = "Moving averages:5, 25, 50 for 200days", )
fig = dict(data = data, layout=layout)
iplot(fig)
It turned out to be something like this. As a note, when setting each axis, slice it as "[-200:]" and display 200 days. You don't have to do it because it just emphasizes the appearance. In that case, it will be the whole period.
Compared to the previous heatmap, this time we have a decent one. I haven't done much yet, but I hope I can play with this ** Nikkei 225 ** theme a little more.
Recommended Posts