The key to Python quantization is the visualization of financial data, both the traditional K-chart and now the strategic analysis, which require a lot of visual charts. Specific to programming code, is to use Python drawing module library drawing, such as the traditional Python drawing module library Matplotlib,Seaborn, etc. \

The Tushare library is used to retrieve and analyze stock and financial data for Pandas. The library is designed to be used to retrieve and analyze stock and financial data for Pandas. To facilitate the decision-making of enterprises. The main steps of financial mapping are as follows:

Get stock data source

Tushare is a free, open source Python financial data interface package. It mainly realizes 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 for easy analysis, greatly reducing their workload in data acquisition and making them more focused on the research and implementation of strategies and models. Given the advantages of Python’s Pandas package for financial quantitative analysis, the vast majority of the data returned by Tushare is in Pandas’ DataFrame format, making it easy to analyze and visualize data using Pandas,NumPy, and Matplotlib.

Tushare’s official website

http://tushare.org
Copy the code

For details about the stock symbols used in Tushare, see the search page for stock symbols provided by Eastmoney.com, as shown in Figure 7-63

http://quote.eastmoney.com/stocklist.html
Copy the code

Figure 763 Stock symbol query page

Install Tushare using PIP. Before installing Tushare, install the LXML, Requests, and BS4 modules using the PIP command.

pip install tushare
Copy the code

\

Display stock history data

The get_HIST_data () function in TuShare is used to get the historical data up to the last three years. Obtain a stock history trading data (including average data), you can obtain daily K line, weekly K line, monthly K line through parameter Settings, as well as 5 minutes, 15 minutes, 30 minutes and 60 minutes K line data. This interface can only obtain daily data of nearly 3 years, which is suitable for stock selection and analysis with average data.

Parameter Description:

  • (SH = SSE Index SZ = SHENZHEN Component Index HS300 = CSI 300 Index SZ50 = SSE 50 ZXB = SME Board CYB = GEM)
  • Start: indicates the start date in the format YYYY-MM-DD
  • “End” : indicates the end date in the format YYYY-MM-DD
  • Ktype: indicates the data type. D= Day K line W= week M= month 5=5 minutes 15=15 minutes 30=30 minutes 60=60 minutes. The default value is D
  • Retry_count: The number of retries after a network exception. The default value is 3
  • Pause: Number of pause seconds during retry. The default value is 0

Return value description:

  • Date: the date
  • Open: open price
  • -Blair: It’s the highest price
  • -Penny: Close.
  • -Penny: I have a low price.
  • Volume:
  • Price_change: price change
  • P_change: up or down
  • Ma5:5 average daily price
  • Ma10:10 average daily price
  • Ma20:20 daily average price
  • Average v_ma5:5
  • Average v_ma10:10
  • Average v_ma20:20
  • Turnover rate: turnover rate [Note: Index does not have this item]

The DataFrame is returned by get_HIST_data () for Tushare. The DataFrame has many columns and is not displayed in the console. So use Pandas’ to_csv() function to save it to hist_data.csv.

import tushare as ts
data = ts.get_hist_data('600848',start='2018-03-01',end='2018-03-31')
data.to_csv('hist_data.csv' )
Copy the code

Run the script and open the HIST_data. CSV file in notepad.

Looking at the HIST_data.csv file, we found that the historical trading data for the stock symbol 600848 in March 2018 is listed in descending order by trading date. But the dates shown on the line chart are usually arranged in ascending order. So you also sort the index property of the DataFrame object. Using the sort_index() function of Pandas’ DataFrame object, order transaction dates in ascending order from smallest to largest. This line chart is in line with the normal habit of looking at historical transaction data. The complete code is as follows.

Import tushare as TS data = ts.get_HIST_data ('600848',start='2018-03-01',end='2018-03-31' data.sort_index() data.to_csv('hist_data.csv' )Copy the code

Once you have the stock code 600848 trading history as of March 2018, you can use Maplotlib to draw a line chart of the stock history. The horizontal axis of the line chart is the trading date of the stock history, and the vertical axis is the open price of the stock history.

import tushare as ts import matplotlib.pyplot as plt from datetime import datetime data = Ts.get_hist_data ('600848',start='2018-03-01',end='2018-03-31') # [datetime.strptime(d, '%Y-%m-%d').toordinal() for d in data.index ] plt.plot_date( xs , data['open'] , 'b-') plt.gcf().autofmt_xdate()Copy the code

Run the script to output the following graphics.

The “get_HIST_data ()” function is used to return the stock history of the Pandas DataFrame. To use Matplotlib’s plot_date() plotting function, you need to convert Gregoian Calendar type data that the function recognizes.

xs = [datetime.strptime(d, '%Y-%m-%d').toordinal() for d in data.index ]
Copy the code

The above expression statement is equivalent to the following statement.

xs = []
for date in data.index:
    print( date )
    transDate = datetime.strptime( date , '%Y-%m-%d')
    xs.append( transDate.toordinal())
Copy the code

Draw the line chart of the date of the X axis can also use the matplotlib. Dates. Strpdate2num () function transformation, the complete code is as follows.

import tushare as ts import matplotlib.pyplot as plt import matplotlib.dates as mdates data = Ts.get_hist_data ('600848',start='2018-03-01',end='2018-03-31') # [mdates.strpdate2num('%Y-%m-%d')(d ) for d in data.index ] plt.plot_date( xs , data['open'] , 'b-') plt.gcf().autofmt_xdate()Copy the code

Run the script to output the following graphics.

Get 60 minutes k line data.

import tushare as ts import matplotlib.pyplot as plt import matplotlib.dates as mdates data = ts.get_hist_data('600848', Ktype ='60') xs = [mdates.strpDate2num ('%Y-%m-%d %H:% m :%S')(d) for d in data.index] ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) plt.plot_date(xs, data['open'],'-' , label='open') plt.legend(loc=0 ) plt.gcf().autofmt_xdate() plt.show()Copy the code

Run the script to output the following graphics.

Figure 768 k-chart of stock symbol 600848 within 60 minutes

We will send a free copy of Python 3 Data Analysis and Machine Learning published by Peking University Press.

\

Hot recommended in Python create WeChat robot in Python robot to monitor WeChat group chat in Python for cameras and real-time control face open source project | beautification LeetCode warehouse is recommended in Python Python Chinese community’s several kind of public service, announced notice | Python Chinese Community award for articles \

This article is excerpted from Python 3 Data Analysis and Machine Learning In Action, published by Peking University Press