I received a gift from Tsinghua University Press not long ago 《 Explain profound theories in simple language Python Quantitative trading practice 》 A Book , Also promised the publishing house to write some reading notes , I'll hand in my homework today .
According to the contents of the book , I have also made some improvements myself —— use Python Plot the price of the stock 5 The daily average and 20 ma . as everyone knows ,5 The daily average is the life and death line of short-term trading , and 20 The daily moving average is the watershed of the medium and long-term trend . therefore , Based on these two moving averages , You can design some simple trading strategies .
Here is the code I practiced :
import pandas as pd import numpy as np from pandas_datareader import data import datetime import matplotlib.pyplot as plt
Import part of the library , No explanation. , Pull data below :
end_date = datetime.date.today() start_date = end_date - datetime.timedelta(days = 100) price = data.DataReader('601127.ss','yahoo', start_date, end_date) price.head()
Here I choose from yahoo PULL 601127 This stock used to 100 Days of market data . Can see the earliest data to 2021 Year of 10 month 8 Japan :
Then I started adding 5 Day and 20 ma
price['ma5'] = price['Adj Close'].rolling(5).mean() price['ma20'] = price['Adj Close'].rolling(20).mean() price.tail()
You can see in the data :
For the convenience of observation , I drew a picture with code :
fig = plt.figure(figsize=(16,9)) ax1 = fig.add_subplot(111, ylabel='Price') price['Adj Close'].plot(ax=ax1, color='g', lw=2., legend=True) price.ma5.plot(ax=ax1, color='r', lw=2., legend=True) price.ma20.plot(ax=ax1, color='b', lw=2., legend=True) plt.grid() plt.show()
So you can see the image directly :
In this way, we can design the moving average strategy according to the moving average of different periods .
If you are interested in similar content , You might as well read this book 《 Explain profound theories in simple language Python Quantitative trading practice 》. I personally feel like following the code to knock , Do it yourself to improve , It's still fun .