[Introduction to Python] Let's use pandas

[Introduction to Python] Let's use pandas

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

Date and time earnings volatility and histogram

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)

2020-02-06 (3).png

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 ...

Draw a candlestick

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)

2020-02-06 (5).png

Draw a moving average

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)

2020-02-06 (6).png 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.

in conclusion

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

[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use foreach with Python
[Python] How to use Pandas Series
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
[Python] Use pandas to extract △△ that maximizes ○○
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introduction to Python Let's prepare the development environment
[Introduction to Udemy Python3 + Application] 23. How to use tuples
Let's use python janome easily
Introduction to Python Django (2) Win
python3: How to use bottle (2)
[Python] Convert list to Pandas [Pandas]
How to use Python argparse
Introduction to serial communication [Python]
How to use Pandas Rolling
Python: How to use pydub
[Python] How to use checkio
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
[Python] How to use input ()
[Introduction] How to use open3d
How to use Python lambda
[Python] How to use virtualenv
Introduction to Python For, While
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Introduction to Python] How to use while statements (repetitive processing)
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
I tried to summarize how to use pandas in python
[Introduction to Udemy Python3 + Application] 30. How to use the set
Introduction to Python numpy pandas matplotlib (~ towards B3 ~ part2)
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Introduction to Python Hands On Part 1
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
Easy to use Jupyter notebook (Python3.5)