当前位置:网站首页>AI quantitative transaction (II) -- tushare financial data framework

AI quantitative transaction (II) -- tushare financial data framework

2022-06-25 04:02:00 Tianshan old demon

One 、Tushare brief introduction

1、Tushare brief introduction

Tushare It's a free one 、 Open source python Financial data interface package , At present for Tushare Pro edition , 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 diverse data for easy analysis .Tushare Most of the data format returned is pandas DataFrame type , It's very easy to use pandas、NumPy、Matplotlib Data analysis and Visualization .

 

2、Tushare install

Github: https://github.com/waditu/Tushare

pip install tushare lxml

pip install beautifulsoup4

3、Token Generate

Tushare You need to register an account to use , And generate Token.

I invite you to register for the link :Tushare Big data community

Registered successfully , Sign in Tushare, Click personal information settings :

 

At the interface Token Page to find personal Token,Token It's using Tushare Unique credentials for the interface , If a leak is found , You can refresh to generate new Token.

import tushare as ts


if __name__ == '__main__':
    print(ts.__version__)
    # Set up Token
    ts.set_token('xxx0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()

# output:
# 1.2.45

4、Tushare API brief introduction

Tushare The data interface is divided into Shanghai and Shenzhen stocks 、 Index 、 fund 、 futures 、 option 、 bond 、 foreign exchange 、 Hong Kong 、 Industry economy 、 macroeconomic 、 There are eleven categories of featured big data , The stocks in Shanghai and Shenzhen are divided into basic data 、 Market data 、 Financial data 、 Four types of interfaces for market reference data .Tushare It also provides basic data related to blockchain 、 Market data 、 Three types of interfaces for information announcement , And sina finance 、 Oriental wealth 、 flush 、 Cloud Finance 、 The macro-economy of financial websites such as wall street news 、 foreign exchange 、A stocks 、 Blockchain 、 US stock 、 oil 、 gold 、 Gold and foreign exchange 、 Hong Kong 、 goods 、 bond 、 company 、 market 、 The focus of 、 Financial information such as the central bank .

Tushare API The interface needs to obtain the corresponding access rights according to the number of points of the registered account , Insufficient points may lead to API The interface does not have permission to access , Blockchain related interfaces require donations to obtain corresponding permissions .

Tushare API Interface use reference :Tushare Big data community

Two 、Tushare Stock data interface

1、 Stock list

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
    # data = ts_api.query('stock_basic', exchange='', list_status='L',
    #                     fields='ts_code,symbol,name,area,industry,list_date')
    print(data)

Get basic stock information data , Including stock code 、 name 、 Listing date , industry 、 Concept, etc .

2、IPO IPO

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.new_share(start_date='20190101', end_date='20190901')
    print(data)

Get the listing list data of new shares

3、 Daily market

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.daily(ts_code='000001.SZ', start_date='20190101', end_date='20190901')
    print(data)

Trading day every day 15 spot ~16 Between points . This interface is not restored to the market , No data will be provided during the suspension period .

4、 Weekly market

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.weekly(ts_code='000001.SZ', start_date='20180101', end_date='20181101',
                         fields='ts_code,trade_date,open,high,low,close,vol,amount')
    print(data)

obtain A Weekly stock market

5、 Monthly market

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.monthly(ts_code='000001.SZ', start_date='20180101', end_date='20181101',
                          fields='ts_code,trade_date,open,high,low,close,vol,amount')
    print(data)

obtain A Stock month line data .

6、 General quotation interface

pro_bar The interface integrates the stock ( Unrecovered right 、 The former restoration right 、 Post restoration right )、 Index 、 Digital currency 、ETF fund 、 futures 、 Market data of options , In the future, there will be all trading data including foreign exchange , Provide minute data at the same time .

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts.pro_bar(api=ts_api, ts_code='000009.SZ', adj='qfq', start_date='20170101', end_date='20181011', ma=[5],
                      freq='D')
    print(data)

obtain A Shares of K Line data , With complex weight parameter “adj”、 Mean square parameter “ma”、 Data frequency parameter “freq”.

7、 Get all the data of the stock

import tushare
import os
import datetime
import timedelta


def fetch_kline_data(code):
    filename = 'your path'
    if not os.path.exists(filename):
        end_date = datetime.strftime(datetime.now(), '%Y%m%d')# Get the current time
        outputflag = True
        api = tushare.pro_api()
        while outputflag:# Circular judgement , Until the returned data is empty
           data = tushare.pro_bar(pro_api=api,ts_code=code,
                                end_date=end_date,asset='E', adj=None, freq='D')
           if data.empty == True:
               outputflag = False
           else:
                # Calculate the deadline for the next data request
                next_end_date = datetime.strptime(data.iloc[-1]['trade_date'],
                                              '%Y%m%d') - timedelta(hours=24)
                end_date = datetime.strftime(next_end_date, '%Y%m%d')
                # Write csv file
                if os.path.exists(filename):
                    data.to_csv(filename, header=None, mode='a')# Append write mode
                else:
                    data.to_csv(filename, header=None, mode='a')

3、 ... and 、Tushare Financial data interface of listed companies

1、 Income statement

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('xxxx0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.income(ts_code='600001.SH', start_date='20190101', end_date='20190901')
    print(data)

Obtain the financial income statement data of the listed company

2、 Balance sheet

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.balancesheet(ts_code='600000.SH', start_date='20190101', end_date='20190901',
                               fields='ts_code,ann_date,f_ann_date,end_date,report_type,comp_type,cap_rese')
    print(data)

Get the balance sheet of the listed company .

3、 Cash flow statement

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.cashflow(ts_code='600000.SH', start_date='20190101', end_date='20190901')
    print(data)

Obtain the cash flow statement of the listed company .

4、 Business forecast

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.forecast(ann_date='20190131',
                           fields='ts_code,ann_date,end_date,type,p_change_min,p_change_max,net_profit_min')
    print(data)

Obtain performance forecast data .

5、 Dividend and share distribution data

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.dividend(ts_code='600848.SH', fields='ts_code,div_proc,stk_div,record_date,ex_date')
    print(data)

Dividend and share distribution data .

6、 Results letters

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.express(ts_code='600000.SH', start_date='20180101', end_date='20180701',
                          fields='ts_code,ann_date,end_date,revenue,operate_profit,total_profit,n_income,total_assets')
    print(data)

Obtain the performance express of listed companies .

7、 Financial indicator data

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.fina_indicator(ts_code='600000.SH')

    print(data)

Obtain financial index data of listed companies , To avoid server stress , At this stage, each request can return at most 60 Bar record , More data can be requested multiple times by setting the date .

8、 Financial audit opinion

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.fina_audit(ts_code='600000.SH', start_date='20100101', end_date='20180808')
    print(data)

Obtain the data of regular financial audit opinions of listed companies

9、 Main business composition

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.fina_mainbz(ts_code='000627.SZ', type='P')
    print(data)

The main business composition of acquiring a stock

df = ts_api.fina_mainbz_vip(period='20181231', type='P', fields='ts_code,end_date,bz_item,bz_sales')

Obtain the main business composition of all stocks in a quarter

10、 Acquisition of complete financial indicators of listed companies

import tushare
import datetime
import os
import timedelta


def fetch_finance_indicator(code):
    filename = 'your path'
    if not os.path.exists(filename):
        end_date = datetime.strftime(datetime.now(), '%Y%m%d')
        outputflag = True
        api = tushare.pro_api()
        while outputflag:
            data = api.fina_indicator(ts_code=code, end_date=end_date)
            if data.empty == True:
                outputflag = False
            else:
                next_end_date = datetime.strptime(
                    data.iloc[-1]['end_date'], '%Y%m%d') - timedelta(hours=24)
                end_date = datetime.strftime(next_end_date, '%Y%m%d')
                if os.path.exists(filename):
                    data.to_csv(filename, header=None, mode='a')
                else:
                    data.to_csv(filename)

Four 、Tushare Index interface

1、 Basic information of index

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.index_basic(market='CSI')
    print(data)

Get basic index information .

MSCI:MSCI Index

CSI: China Securities Index

SSE: Shanghai Stock Exchange Index

SZSE: Shenzhen Stock Exchange Index

CICC: CICC index

SW: Shenwan index

2、 Index daily market

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.index_daily(ts_code='399300.SZ', start_date='20190101', end_date='20190910')
    print(data)

Get index daily quotes , You can also use bar Interface acquisition . Due to server pressure , At present, the rule is to fetch at most in a single call 8000 rows , You can set start and end Date completion . The index can also be quoted through General quotation interface get data .

3、 Index weekly market

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.index_weekly(ts_code='000001.SH', start_date='20180101', end_date='20190329',
                               fields='ts_code,trade_date,open,high,low,close,vol,amount')
    print(data).

Get the weekly market of the index , Single maximum 1000 rows , Available in batches , There is no limit to the total amount .

4、 Index monthly line Market

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.index_monthly(ts_code='000001.SH', start_date='20180101', end_date='20190930',
                                fields='ts_code,trade_date,open,high,low,close,vol,amount')
    print(data)

Get the monthly index line , Update once a month , Single maximum 1000 rows , It can be obtained many times , There is no limit to the total amount . Users need to at least 600 Points can be used to get , The more points, the higher the frequency .

5、 Index components and weights

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.index_weight(index_code='399300.SZ', start_date='20180901', end_date='20190930')
    print(data)

Obtain various index components and weights , Monthly data  , For daily index components and weights , Users need to at least 400 Points can be used to get .

5、 ... and 、Tushare Market reference data interface

1、 Hong Kong stock connect 10 Large trading shares

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.ggt_top10(trade_date='20190925')
    print(data)

Get daily transaction data of Hong Kong stock connect , Including the Shanghai Stock Exchange 、 Shenzhen detailed data .

2、 Summary of margin trading

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.margin(trade_date='20190925')
    print(data)

Obtain the daily transaction summary data of margin trading .

3、 Details of margin trading

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.margin_detail(trade_date='20190925')
    print(data)

Obtain the daily details of margin trading in Shanghai and Shenzhen .

4、 Top ten shareholders

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.top10_holders(ts_code='600000.SH', start_date='20190101', end_date='20191231')
    print(data)

Get the data of the top ten shareholders of listed companies , Including information such as the number and proportion of holdings .

5、 Top ten circulating shareholders

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.top10_floatholders(ts_code='600000.SH', start_date='20190101', end_date='20191231')
    print(data)

Obtain the data of the top ten circulating shareholders of listed companies .

6、 Daily details of dragon and tiger list

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.top_list(trade_date='20190925')
    print(data)

Daily transaction details of dragon and tiger list , Single maximum 10000, Users need to at least 300 Points can be used to get .

7、 Transaction details of longhubang institutions

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.top_inst(trade_date='20190925')
    print(data)

Transaction details of longhubang institutions , Single maximum 10000, Users need to at least 300 Points can be used to get .

8、 block trade

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.block_trade(trade_date='20190925')
    print(data)

block trade , Single maximum 1000 strip , There is no limit to the total amount ,300 Integral is adjustable , Limit the number of times per minute , exceed 5000 The integral is unlimited .

9、 Stock account opening data

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.stk_account(start_date='20190101', end_date='20191231')
    print(data)

Obtain stock account opening data , The statistical cycle is one week ,600 Integral is adjustable .

10、 Number of shareholders

import tushare as ts


if __name__ == '__main__':
    # Set up Token
    ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
    # Initialization interface
    ts_api = ts.pro_api()
    data = ts_api.stk_holdernumber(ts_code='300209.SZ', start_date='20190101', end_date='20191231')
    print(data)

Obtain the number of shareholders of the listed company , Data are published irregularly . Single maximum 3000, There is no limit to the total amount ,600 Integral is adjustable , The basic points are retrieved every minute 100 Time ,5000 The points above are unlimited .

原网站

版权声明
本文为[Tianshan old demon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210536403116.html