Although the world is tough in Corona, stocks are showing the appearance of the Corona monetary easing bubble as of January 2009. Right now, I'm crazy about this trend, but as the saying goes, "Setsubun ceiling, equinoctial week", I think Ikeike is just around the corner. After that, I feel that the range market will come for a while until the next trend comes. I would like to consider system trading using machine learning that can respond to range markets and trend markets.
As a library of python
, there is a library called TA-Lib
, but for studying, I will consider it without using it as much as possible.
As for this strategy,
There are various indicators, but it is a simple Bollinger Bands strategy.
After studying python
in Bollinger Bands, I think it would be good if there were other good indicators (MACD, RSI) and what would happen if the indicators were combined.
While making a system trade of Bollinger Bands first
I will confirm and make a system trade.
First, we will consider the target of 1306 Nomura Asset's TOPIX-linked exchange-traded fund. Scraping technology is postponed. Click to collect from this site. https://kabuoji3.com/ It's a little annoying, but you can get the annual csv file from 2001 with just a click. 1306_2001.csv ~ 1306_2021.csv
You can get files of these daily etf price movements.
First is the data combination. This was easily combined with pandas
.
I combined it without thinking deeply, but since "1306 TSE ETF TOPIX Exchange Traded Fund (ETF)" is included in the 0th line, data = pd.read_csv (file, encoding ='Shift-Jis'" , header = 1)
and header = 1
on the first line
Was read as the column
name.
Since the column
name is Japanese, I changed it to appropriate English for the time being.
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
#read_Combine all the data with csv.
#1306_2001 to 1306_2021
df = None
for i in range(2001,2021):
file = '1306_' + str(i) + '.csv'
print(file)
data = pd.read_csv(file,encoding='Shift-Jis',header = 1)
df = pd.concat( [df,data],ignore_index=True)
df.rename(columns={'date': 'day', 'Open price': 'open_price',
'High price':'high_price', 'Low price':'low_price',
'closing price': 'end_price', 'Volume':'volume',
'Closing price adjustment value':'end_price_ad_value'}, inplace=True)
print(df.head())
For the time being, the pandas
dataset has been created.
When you make a graph, it looks like this. It is the highest price in 10 years now. It's cool.
Next is how to draw a Bollinger band.
What is Bollinger Bands is quoted from the website of MONEX, Inc. https://info.monex.co.jp/technical-analysis/indicators/003.html "It is composed of a moving average line and standard deviation, and it is an index that adds a line showing the moving average and a line showing the width of price movement above and below it, and says that" most of the price falls within this band. " It is one of the technical indicators that apply statistics. "
That is, you need to calculate the moving average and standard deviation from the price of the stock. Later, in terms of "technical indicators that apply statistics," it may be compatible with machine learning. First, calculate the moving average.
Simple moving average is the average price of the closing price for a certain period. So we use the rolling
function of pandas
. For example, in the rolling
function,rolling (5) .mean ()
returns the average of five numbers. So, let's calculate with df ["end_price "] .rolling (5) .mean (). Round (1)
and draw a 5-day moving average.
#'day'To datetime type
df['day'] = pd.to_datetime(df['day'])
#'day'Replace with index.
df.set_index('day', inplace=True)
#Calculate the 5-day moving average
df["5day_ave"]=df["end_price"].rolling(5).mean().round(1)
#Take out the second half of 2019 from July to December
df_temp = df['2019-07':'2019-12']
#Draw on the graph
plt.plot(df_temp.index, df_temp["end_price"], label="daily")
plt.plot(df_temp.index, df_temp["5day_ave"], label="sma5")
plt.xlabel("day")
plt.ylabel("price")
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%b-%Y'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=2))
plt.gca().xaxis.set_minor_locator(mdates.MonthLocator(interval=1))
plt.gcf().autofmt_xdate()
plt.legend()
plt.show()
I was able to draw a moving average line. Next, I will draw the Bollinger Bands. It is in the next Investment quest: Make a system trade with pyhton (2).
Recommended Posts