“This is the ninth day of my participation in the Gwen Challenge.

Start by installing the Tushare library that can capture stock price data. The PIP installation is recommended. The basic stock data can be obtained with the following three lines of code.

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

Line 1 introduces the Tushare library and abbreviates it to TS. The second line of code obtains the daily stock line level data of vanke from 2009-01-01 to 2019-01-01 by get_k_data() function, where the first parameter ‘000002’ represents the stock code (‘000002’ corresponds to Vanke A). The second argument start indicates the start date, and the third argument end indicates the end date. Here you get a two-dimensional table in DataFrame format and assign it to the variable df. This line of code can also be abbreviated as follows.

df = ts.get_k_data('000002',start = '2009-01-01',end='2019-01-01')
Copy the code

Line 3 fetches the first five lines of the two-dimensional table with df.head(), resulting in something like the image below.

In the figure above, date is the trading date, open is the opening price, close is the closing price, high is the highest price, low is the lowest price, volume is the trading volume, and code is the stock code.

To write the captured data to an Excel workbook, the code is as follows.

Df.to_excel (' XLSX ',index = False)Copy the code

The relative file path is used here, and the index parameter is set to False, indicating that the original row index is ignored. After running, an Excel workbook named “stock data.xlsx” will be generated in the folder where the code files are located.

2. Plot stock price movements

The stock price data obtained below are presented in the form of visual charts. To draw directly with the PANDAS library, use the set_index() function to set the date to the row index.

df.set_index('date',inplace = True)
Copy the code

Part of the two-dimensional table at this time is shown in the figure below.

Then use the plot() function in pandas to plot the stock price. Because stock price charts are line charts, which the plot() function plots by default, you do not need to specify the KIND parameter. In the financial field, the closing price is usually used as the day’s price to draw the stock price chart, so the close column is selected here.

df['close'].plot()
Copy the code

By default, the plot() function uses the row index as the abscissa, and because the date was set as the row index, the plot looks like this.

If you want to add a title to your chart, you can set the title parameter in the plot() function as follows

Import matplotlib.pyplot as PLT plt.rcparams ['font. Sans-serif '] = ['SiHei'] df['close'].plot(title = 'vank stock price chart ')Copy the code

The drawing effect is shown below.

Draw diagrams directly from the Matplotlib library

Because df [‘ date ‘] is a string of type string, if directly used for drawing, abscissa can appear very dense, affect beautiful, so here through a datetime. Datetime. Strptime () function is transformed into timestamp timestamp format, The Matplotlib library then automatically displays the date at intervals.