当前位置:网站首页>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 .
边栏推荐
- The programmer reality show is coming again! Hulan, as the host, carried the lamp to fill the knowledge. The SSS boss had a bachelor's degree in pharmacy. Zhu Jun and Zhang Min from Tsinghua joined th
- Lao Ye's blessing
- [rust submission] review impl trail and dyn trail in rust
- Development of trading system (I) -- Introduction to trading system
- [team learning] SQL programming language notes - task04
- Oracle-sqlload import external data details
- ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language
- Create SQLite table with shell script and add SQL statement -- General
- Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning
- TensorFlow,危!抛弃者正是谷歌自己
猜你喜欢

Crawl Sina Weibo fans

Why can banana be a random number generator? Because it is the "king of radiation" in the fruit industry

Development of trading system (I) -- Introduction to trading system

练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。

马斯克:推特要学习微信,让10亿人「活在上面」成为超级APP

严重的PHP缺陷可导致QNAP NAS 设备遭RCE攻击

Google founder brin's second marriage broke up: it was revealed that he had filed for divorce from his Chinese wife in January, and his current fortune is $631.4 billion

現在,耳朵也要進入元宇宙了

Cesium 加载显示热力图
![[harmony OS] [arkui] ETS development graphics and animation drawing](/img/9d/0ac2b3d8bcdcd610767df930e2fa4e.png)
[harmony OS] [arkui] ETS development graphics and animation drawing
随机推荐
Tai Chi graphics 60 lines of code to achieve classic papers, 0.7 seconds to get Poisson disk sampling, 100 times faster than numpy
Lao Ye's blessing
Maintenant, les oreilles vont entrer dans le métacosme.
Jilin University 22 spring March "official document writing" assignment assessment-00029
[harmony OS] [arkui] ETS development graphics and animation drawing
Cesium 拖拽3D模型
完美洗牌问题
[harmony OS] [ark UI] basic ETS context operations
Comprehensive operation of financial risk management X of Dongcai
威马招股书拆解:电动竞争已结束,智能排位赛刚开始
Jilin University 22 spring March new development English comprehensive course (I) assignment assessment-00080
OpenSUSE environment PHP connection Oracle
Time management understood after working at home | community essay solicitation
IE寿终正寝,网友们搞起了真·墓碑……
Mstp+vrrp+ospf implements a three-tier architecture
Jilin University 22 spring March "career design" assignment assessment-00072
ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language
【Harmony OS】【ARK UI】ETS 上下文基本操作
Program. Launch (xxx) open file
程序员真人秀又来了!呼兰当主持挑灯狂补知识,SSS大佬本科竟是药学,清华朱军张敏等加入导师团...