Reading and visualization of stock data based on python (K-line chart)

1. Read the data

TuShare is a free and open source python financial data interface package. Mainly realize the process from data collection, cleaning and processing to data storage of financial data such as stocks, which can provide financial analysts with fast, clean, and diverse data that are easy to analyze, greatly reducing their workload in terms of data sources. Make them more focused on the research and implementation of strategies and models. Considering the advantages of the Python pandas package in financial quantitative analysis, most of the data formats returned by TuShare are pandas DataFrame types, which is very convenient for data analysis and visualization with pandas/NumPy/Matplotlib. Of course, if you are used to using Excel or relational databases for analysis, you can also use the data storage function of TuShare to save all the data locally for analysis. At the request of some users, starting from version 0.2.5, TuShare is compatible with both Python 2.x and Python 3.x. Some codes have been refactored and some algorithms have been optimized to ensure efficient and stable data acquisition.
The power of python lies in the brothers of a bunch of third-party libraries. The same is true for stocks. Today I will tell you about this library. It is the tushare library (official address: http://tushare.org/), and you can use it directly Pip installation, take Windows installation as an example.
The installation process can refer to my previous blog: Click to view the python installation third-party library tutorial with commands.
I will take the jupyter notebook installation as an example to show you: After the
Insert picture description here
installation is complete, the next step is to obtain stock data.

import tushare as ts
df = ts.get_k_data('000002',start = "2010-01-01",end = '2020-01-01')
df.set_index('date',inplace = True)
df.head()

Obtaining the data result display:
Insert picture description here
Our following case is carried out through Vanke A, so let's get the data of Vanke A first, and get the listed company Vanke from 2009-01-01 to 2019-01-01 through the get_k_data() function. For daily-level stock data, the parameter "000002" is the stock code of Vanke.

import tushare as ts
df = ts.get_k_data('000002',start ='2009-01-01',end='2019-01-01')
df.head()

Result display:
'' Insert picture description here
Note: open opening price, close closing price, high is the highest price, low is the lowest price, volume is the trading volume, and code is the stock code.
If you want to get data and write it into an Excel workbook, we can press the following code:

df.to_excel('D:\\Python\\股价数据.xlsx',index = False)

Note: The index parameter False means that the original row index is ignored, and an Excel file named "stock data.xlsx" is generated after running.
For example, I set to save the file under D:\Python\. You don't need to set it at all. As long as you know where the default folder is, you must find it easily.

2. Draw stock trends

Draw a stock chart below:

import tushare as ts
import matplotlib.pyplot as plt

df = ts.get_k_data('000002',start ='2009-01-01',end='2019-01-01')
plt.rcParams['font.sans-serif']=['simHei']#用来正常显示中文
df['close'].plot(title = '股票走势图')

Result display: The
Insert picture description here
above uses the plot() function, and we can also directly use some functions of the Matplotlib library.

import tushare as ts
from datetime import datetime
import matplotlib.pyplot as plt
df = ts.get_k_data('000002',start ='2009-01-01',end='2019-01-01')
#要注意细节,调整日期格式,让横坐标的显示效果更佳清洗,美观
df['date'] = df['date'].apply(lambda x:datetime.strptime(x,'%Y-%m-%d'))
plt.plot(df['date'],df['close'])
plt.show()

Show running results:
Insert picture description here

3. Draw a candlestick chart

1. Install the library for drawing K-line graphs

There are two kinds of K line. If the closing price of the day is higher than the opening price, the price rises on the day, which is called a Yang line. Usually painted as a red line.
On the contrary, the price rises on the day, which is called Yinxian, which is usually drawn as a green line.
Python also uses a library when drawing candlesticks, the mpl_finance library, refer to the previous push installation tutorial, the candlestick_ochl() function in the library draws candlestick graphs, and the following cases are written in python for reference.
Screenshot of successful installation: (jupyter notebook installation method)
Insert picture description here
2. Get data

import tushare as ts
import matplotlib.pyplot as plt
import mpl_finance as mpf
import seaborn as sns  #引入图表美化库
sns.set()#激活
df = ts.get_k_data('000002','2019-06-01','2019-09-30')
df.head()

Operation result:
Insert picture description here
3. Date format adjustment and table conversion

from matplotlib.pylab import date2num
import datetime  #导入日期格式涉及的两个库

def date_to_num(dates):
    num_time = []
    for date in dates:
        date_time = datetime.datetime.strptime(date,'%Y-%m-%d')
        num_date = date2num(date_time)
        num_time.append(num_date)
    return num_time
df_arr = df.values  #将DataFrame中数据转换成二维数组
df_arr[:,0] = date_to_num(df_arr[:,0])#  将二维数组中日期转换成数字格式

4. Draw K line

fig,ax = plt.subplots(figsize = (15,6))
mpf.candlestick_ochl(ax,df_arr,width = 0.6,colorup = 'r',colordown = 'g',alpha = 1.0)
plt.grid(True)#绘制网格线
ax.xaxis_date()#设置x轴的刻度线格式为常规日期格式

Display of running results:
Insert picture description here
add moving average chart: take adding 5-day moving average and 10-day moving average as an example

df['MA5'] = df['close'].rolling(5).mean()
df['MA10'] = df['close'].rolling(10).mean()

View it as a chart:

plt.rcParams['font.sans-serif'] = ['SimHei'] #设置正常中文显示
fig,ax = plt.subplots(figsize = (15,6))
mpf.candlestick_ochl(ax,df_arr,width = 0.6,colorup = "r",colordown = 'g',alpha = 1.0) #绘制K直线
plt.plot(df_arr[:,0],df["MA5"])
plt.plot(df_arr[:,0],df['MA10'])
plt.grid(True)  #绘制网格线
plt.title('万科A')#标题
plt.xlabel('日期')#X轴
plt.ylabel('价格')#y轴
ax.xaxis_date()

Operation result:
Insert picture description here
We add a daily volume bar chart on the basis of the above. code show as below:

fig,axes = plt.subplots(2,1,sharex = True,figsize = (15,8))
ax1,ax2 = axes.flatten()

#绘制第一张子图:K线图和均线图
mpf.candlestick_ochl(ax1,df_arr,width = 0.6,colorup = 'r',colordown = 'g',alpha = 1.0)
ax1.plot(df_arr[:,0],df['MA5'])  #绘制5日均线
ax1.plot(df_arr[:,0],df['MA10'])#绘制10日均线

ax1.set_title('万科A')
ax1.set_ylabel('价格')
ax1.grid(True)
ax1.xaxis_date()

#绘制第二张子图:每日成交柱形图
ax2.bar(df_arr[:,0],df_arr[:,5])
ax2.set_xlabel('日期')
ax2.set_ylabel('成交量')
ax2.grid(True)
ax2.xaxis_date()

Running results:
Insert picture description here
This article mainly introduces the use of three data analysis tools, NumPy, pandas, and Matplotlib, to draw pictures and use stock data to perform some operations. You can also use this data to do some data analysis and practice.

Guess you like

Origin blog.csdn.net/qq_44176343/article/details/109903512