Python stock quantitative trading (1) ---K-line chart, moving average and trading volume drawing

Thinking far away from the ancestors' virtue, thinking about the sorrow of parents; thinking about the grace of repaying the country, thinking about the blessing of making a family; thinking about the urgency of helping people outside, thinking about the evil of leisure and self inside.

Preface

It is said that 2020 is the starting point of the bull market. Obviously, for data, programmers have an absolute advantage, because most programmers have been dealing with data all their lives, and programmers only need to master data processing and chart drawing. You can do a good job in the stock market.

However, the quantitative stock trading column is not an introduction to the basics, but only the knowledge related to stocks. Therefore, for matplotlib chart drawing and numpy, pandas library, we will not do too much introduction, we need to read this kind of blog, but also need these 3 basic knowledge. Below, we go directly to the topic.

Get stock data

For the python language, there are many libraries for obtaining stocks, such as akshare, baostock and tushare libraries. And these three stock data acquisition libraries are similar, so the subsequent chapters of the editor here are all based on the akshare library for stock data acquisition. Not much to say, let's take a look at how to obtain stock data for the past one month. The code is as follows:

import akshare as ak
def get_codeData(code="sz002241"):
    df = ak.stock_zh_a_daily(symbol=code, start_date="20201203", end_date="20210115",
                                                          adjust="qfq")
    df.to_excel("歌尔股份k.xlsx")

As shown in the code above, we use the stock_zh_a_daily method to obtain data for a period of time. The date here can be adjusted according to your needs. For example, set start_date=“20200101” and end_date=“20210101” to obtain stocks for a period of one year data.

The stock code is divided into sh and sz. sz stands for Shenzhen stocks. All Shenzhen stocks must be accompanied by sz. For example, the Goertek stock here is sz002241. Similarly, sh is a stock on the Shanghai Stock Exchange, and all stocks on the Shanghai Stock Exchange must carry sh in front of them.

Here, let’s take a look at the obtained stock data format, as shown in the figure below:
Insert picture description here
date: transaction date
open: represents the opening price
high: the highest price of the
day low: the lowest price of the day
close: the closing price of the
day volume: the trading volume of the day (yuan)
outstanding_share : Current equity (shares)
turnover: turnover rate

Now that we have got the data, let's draw a K-line chart.

Draw candlestick chart

In python, drawing K-line graph requires mpl_finance library, and this library provides 4 ways to draw K-line graph, here we introduce one of them, the code is as follows:

import mpl_finance as mpf
import matplotlib.pyplot as plt
import pandas as pd

#创建绘图的基本参数
fig=plt.figure(figsize=(12, 8))
ax=fig.add_subplot(111)

#获取刚才的股票数据
df = pd.read_excel("歌尔股份k.xlsx")
mpf.candlestick2_ochl(ax, df["open"], df["close"], df["high"], df["low"], width=0.6, colorup='r',colordown='green',alpha=1.0)
#显示出来
plt.show()

After running this piece of code, the following renderings will be displayed:
No time candlestick chart
There is a problem with this candlestick chart, that is, the X coordinate axis is not the displayed time, but the number. This is because the method of drawing the candlestick chart does not provide the X axis The parameter, then how to replace the following number with the time? Let's first look at a piece of code:

import matplotlib.ticker as ticker
#将股票时间转换为标准时间,不带时分秒的数据
def date_to_num(dates):
    num_time = []
    for date in dates:
        date_time = datetime.strptime(date, '%Y-%m-%d')
        num_date = date2num(date_time)
        num_time.append(num_date)
    return num_time

#创建绘图的基本参数
fig=plt.figure(figsize=(12, 8))
ax=fig.add_subplot(111)

#获取刚才的股票数据
df = pd.read_excel("歌尔股份k.xlsx")
mpf.candlestick2_ochl(ax, df["open"], df["close"], df["high"], df["low"], width=0.6, colorup='r',colordown='green',alpha=1.0)
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.strftime('%Y-%m-%d'))
def format_date(x, pos=None):
    if x < 0 or x > len(df['date']) - 1:
        return ''
    return df['date'][int(x)]
ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
#显示出来
plt.show()

Here, we have defined 2 methods: the first method date_to_num is mainly used to convert the acquired time data into standard date data; the second method is to replace the time value according to the value of X.

Among them, the set_major_formatter method is an operation library that replaces the value with the time value, and the function of plt.setup is to set the X-axis label tilt 45 degrees and right alignment. After running, our K-line chart is displayed perfectly, as shown in the following figure:
Perfect candlestick chart

Moving average chart

Although we have implemented the K-line chart, have you discovered that in fact, most stock trading software K-line charts are actually marked with moving averages, such as the commonly used 5-day moving average, 10-day moving average, and 30 moving average. Therefore, we need to add the moving average to our candlestick chart. The way to calculate the moving average is as follows:

df["SMA5"] = df["close"].rolling(5).mean()
df["SMA10"] = df["close"].rolling(10).mean()
df["SMA30"] = df["close"].rolling(30).mean()

You can get the moving average value of the stock with 3 lines of code. Then, we can use the above ax to draw the moving average, the added code is as follows:

ax.plot(np.arange(0, len(df)), df['SMA5'])  # 绘制5日均线
ax.plot(np.arange(0, len(df)), df['SMA10'])  # 绘制10日均线
ax.plot(np.arange(0, len(df)), df['SMA30'])  # 绘制30日均线

Here, the X axis is also set to a number, and these two pieces of code are added to the K-line chart code above, and the X axis will naturally be replaced with time. After running, the display effect is as shown in the figure below:
K-line chart and moving average chart
Here the 5-day moving average is blue, the 10-day moving average is orange, and the 30-day moving average is green. If you need to set the color yourself, you can add the color parameter to the drawing method of each moving average.

Careful readers must find that the 30-day moving average has only the last segment. This is because the previous 29 days are less than 30 days and the moving average cannot be calculated. The same is true for the 5 and 10-day moving averages.

Volume

Finally, we also need to plot the volume. In most stock trading software, the top is a K-line chart, and the bottom generally corresponds to the trading volume. In this way, you can visually see the changes in the data when you compare it.

However, because there is a K-line chart above, there are two pictures in the same canvas, and they share the same X axis, then we need to change the basic parameters, the code is as follows:

import mpl_finance as mpf
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as ticker
import numpy as np
#创建绘图的基本参数
fig, axes = plt.subplots(2, 1, sharex=True, figsize=(15, 10))
ax1, ax2 = axes.flatten()

#获取刚才的股票数据
df = pd.read_excel("歌尔股份k.xlsx")
mpf.candlestick2_ochl(ax1, df["open"], df["close"], df["high"], df["low"], width=0.6, colorup='r',colordown='green',alpha=1.0)
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.strftime('%Y-%m-%d'))
def format_date(x, pos=None):
    if x < 0 or x > len(df['date']) - 1:
        return ''
    return df['date'][int(x)]
df["SMA5"] = df["close"].rolling(5).mean()
df["SMA10"] = df["close"].rolling(10).mean()
df["SMA30"] = df["close"].rolling(30).mean()
ax1.plot(np.arange(0, len(df)), df['SMA5'])  # 绘制5日均线
ax1.plot(np.arange(0, len(df)), df['SMA10'])  # 绘制10日均线
ax1.plot(np.arange(0, len(df)), df['SMA30'])  # 绘制30日均线
ax1.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
#显示出来
plt.show()

Here, we set the drawing canvas to 2 rows and 1 column. At the same time, change all the data drawn above to ax1, so that the moving average and candlestick chart will be drawn to the first subgraph.

Next, we need to plot the volume, but we know that the general stock trading software sets the volume on the rising day in red, and plots the falling volume in green. Therefore, the first thing we have to do is to classify the acquired stock data according to the day's fluctuations. The specific code is as follows:

red_pred = np.where(df["close"] > df["open"], df["volume"], 0)
blue_pred = np.where(df["close"] < df["open"], df["volume"], 0)

As shown in the code above, we filter the data by np.where(condition, x, y). Here, if the condition is satisfied, X is output, and if the condition is not satisfied, Y is output. In this way, we classify the up and down volume data.

Finally, we draw the volume directly through the histogram method, the specific code is as follows:

ax2.bar(np.arange(0, len(df)), red_pred, facecolor="red")
ax2.bar(np.arange(0, len(df)), blue_pred, facecolor="blue")

Add all these 4 lines of code to the front of the plt.show() code.

After running, the output effect chart is as follows:
K-line chart, moving average and volume
K-line chart, moving average chart and trading volume are here, the next article introduces the relevant knowledge of the oscillator KDJ.

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/112737260