Recently affected by the global environment , U.S. stocks are like riding a roller coaster , I feel like I'm witnessing historical moments every day . And our market has not stopped recently , But it also gives us an opportunity to make a bottom search , But opportunities are for those who are prepared , If you want to seize the opportunity, you have to know how to analyze the data , If you want to analyze the data, you have to get the transaction data first , Let's talk about it today  Python How to get stock market trading data .

TuShare Tools

Tushare   It's a free one 、 Open source Python   Financial data interface package . It is mainly used to collect financial data such as stocks from data 、 The process from cleaning and processing to data storage , Be able to provide financial analysts with quick 、 Clean and tidy 、 And a variety of easy to analyze data , Greatly reduce their workload in data acquisition , Make them more focused on the research and implementation of strategies and models .


Tushare The operation of pandas Module support , So you need to install it first , And then install it tushare

$ pip3 install pandas …… Ignore log 
$ pip3 install tushare …… Ignore log

After installation , Will see Successfully installed The installation is successful . Let's take a brief look at the functions it provides .

Functional specifications

1、 Get historical data

get_hist_data(): Get historical trading data of individual stocks ( Including moving average data ), You can get the date through parameter setting k Line 、 Zhou k Line 、 month k Line , as well as 5 minute 、15 minute 、30 Minutes and 60 minute k Line data . Let's introduce the package below , Then you can execute this method to obtain the historical trading data of Shanghai stock index .

# Introduction package import tushare as tu
# Get the data of Shanghai stock index for three years tu.get_hist_data('000001')
# Of course, we can only get data over a period of time tu.get_hist_data('000001',start='2020-01-05',end='2020-02-05')

Parameter description :

  • code: Stock code , namely 6 Digit code , Or index code (sh= The Shanghai composite index sz= Shenzhen Index hs300= Shanghai and Shenzhen 300 Index sz50= The Shanghai 50 zxb= Small and medium-sized plate cyb= The gem )
  • start: Start date , Format YYYY-MM-DD
  • end: End date , Format YYYY-MM-DD
  • ktype: data type ,D= Japan k Line W= Zhou M= month 5=5 minute 15=15 minute 30=30 minute 60=60 minute , The default is D
  • retry_count: When the network is abnormal, the number of retries , The default is 3
  • pause: Seconds to pause when retrying , The default is 0

Return value description :

  • date: date
  • open: Opening price
  • high: Highest price
  • close: Closing price
  • low: The lowest price
  • volume: volume
  • price_change: Price changes
  • p_change: applies
  • ma5:5 Average daily price
  • ma10:10 Average daily price
  • ma20:20 Average daily price
  • v_ma5:5 Daily average
  • v_ma10:10 Daily average
  • v_ma20:20 Daily average
  • turnover: Turnover rate [ notes : There is no such item in the index ]

2、 Get the real market

get_today_all(): One time access to the current trading of all stock market data ( If it's a holiday , That is, the last trading day ).

# Introduction package import tushare as tu
# Get the current price of all the stocks tu.get_today_all()


Return value description :

  • code: Code
  • name: name
  • changepercent: applies
  • trade: Present price
  • open: Opening price
  • high: Highest price
  • low: The lowest price
  • settlement: Yesterday's closing price
  • volume: volume
  • turnoverratio: Turnover rate
  • amount: Clinch a deal amount
  • per: P / E ratio
  • pb: Market to net ratio
  • mktcap: The total market value
  • nmc: Current market value

3、 Get real time transaction data

get_realtime_quotes(): Real time pen splitting data can be obtained , It can obtain the current quotation and transaction information of the stock in real time , Real time monitoring of trading volume and price changes .

# Introduction package import tushare as tu
# Obtain the real-time data of Maotai and Gree stocks data = tu.get_realtime_quotes(['600519','000651'])
# You can also set to display only certain values data[['code','name','price','bid','ask','volume','amount','time']]
# Or get the Shanghai index Shenzhen Index Shanghai and Shenzhen 300 Index The Shanghai 50 Small and medium-sized plate The gem tu.get_realtime_quotes(['sh','sz','hs300','sz50','zxb','cyb'])



Return value description :

  • 0:name, Stock name
  • 1:open, Today's opening price
  • 2:pre_close, Yesterday's closing price
  • 3:price, Current price
  • 4:high, Today's highest price
  • 5:low, Today's lowest price
  • 6:bid, Bidding price , namely “ Buy one ” offer
  • 7:ask, The auction price , namely “ Sell one ” offer
  • 8:volume, volume maybe you need do volume/100
  • 9:amount, Clinch a deal amount ( element CNY)
  • 10:b1_v, Commission buy one ( Stroke number bid volume)
  • 11:b1_p, Commission buy one ( Price bid price)
  • 12:b2_v,“ Buy two ”
  • 13:b2_p,“ Buy two ”
  • 14:b3_v,“ Buy three ”
  • 15:b3_p,“ Buy three ”
  • 16:b4_v,“ Buy four ”
  • 17:b4_p,“ Buy four ”
  • 18:b5_v,“ Buy five ”
  • 19:b5_p,“ Buy five ”
  • 20:a1_v, Commission sale one ( Stroke number ask volume)
  • 21:a1_p, Commission sale one ( Price ask price)
  • …… Ignore the part
  • 30:date, date ;
  • 31:time, Time ;

4、 Market index list

get_index(): Get the real-time market list of the market index , In the form of a table showing the market index real-time market .

# Introduction package import tushare as tu
# Access to the market data = tu.get_index()


Return value description :

  • code: Index code
  • name: Index name
  • change: applies
  • open: Opening point
  • preclose: Yesterday's closing point
  • close: Closing point
  • high: The highest point
  • low: The lowest point
  • volume: volume ( hand )
  • amount: Clinch a deal amount ( One hundred million yuan )

5、 Big deal data

get_sina_dd(): Get big deal data , Default is greater than or equal to 400 hand , The data comes from sina finance .

# Introduction package import tushare as tu
# Get the big order transaction data of the current date of Maotai , Default 400 hand tu.get_sina_dd('600519', date='2020-03-27')
# Get the deal 100 Hand over data tu.get_sina_dd('600519', date='2020-03-27', vol=100)


Parameter description :

  • code: Stock code , namely 6 Digit code
  • date: date , Format YYYY-MM-DD
  • vol: Hand count , The default is 400 hand , Enter a numeric parameter
  • retry_count:int, Default 3, In case of network problems, the number of repeated execution
  • pause : int, Default 0, The number of seconds to pause during repeated requests for data , Prevent the problem of too short request interval

Return value description :

  • code: Code
  • name: name
  • time: Time
  • price: Current price
  • volume: Clincher
  • preprice : Last price
  • type: Type of business 【 Buy 、 Offer to sell 、 Neutral disc 】


This article briefly introduces Tushare A small part of the tool's functionality , Through these, we can get a lot of analysis data , Of course, it has many powerful interface functions , If you are interested, I will introduce you later , Or you can visit its website directly to learn more .

Reference resources

Tushare Official website :http://tushare.waditu.com/