To learn Xi quantitative finance, the first step is to find a way to obtain transaction data, such as daily ** price, opening price, number of transactions, etc. There are many software on the market, such as Oriental Fortune, Xueqiu, and trading software of major ** companies, etc., which can be queried, but if you want to use python to obtain it in batches through programs, you have to obtain it through open source programs. Today, we introduce an open-source database, Tushare, which can easily obtain the financial data you need.
In order to interface the tushare service in Python, you need to install the tushare command locally, and the installation command is as follows:
pip install tushare
For more information, please visit the official website of Tushare.
Now tushare has been upgraded to the pro version, the various commands are quite different from before, the old version of the command can still be used, but I don't know when it will be invalid, so the latest usage is used here.
According to the introduction, what Tushare provides is no longer directly scraped from the Internet, but collected and sorted by the community and stored in the database after quality control before being provided to users.
To use tushare, you need to register an account on the official website first, register and get 100 points, complete your personal information and send 20 points, and 120 points can use some basic functions. Then apply for an interface token on the homepage, and you need to load the token to call the TuShare service.
The first step is to load the tushare library in Python.
importtushare as ts
The second step is to load the token
token = 'your token'
ts.set_token(token)
The third step is to initialize the interface.
pro = ts.pro_api()
You can also load tokens directly in the initialization interface, for example.
pro = ts.pro_api(token)
The fourth step is to obtain daily data as an example, use the daily interface to obtain the data and return the data in the form of a dataframe.
df = pro.daily(ts_code='000001.sz', start_date='20230101', end_date='20231130')
The parameters required for daily are ts code, start date, end date, trade date, and the date format is filled in yyyymmdd, such as 20231130.
The data returned is shown below.
The parameters are explained below.
You can also get all the data for a given day.
df_1 = pro.daily(trade_date='20231130')
Overall, it's very convenient to use Tushare, and you can easily get your transaction data with just three or four lines.
But after all, TuShare is open source data, a large number of high-frequency calls to API will cause the server to face a crash, in order to ensure the normal use of the server, the public welfare TuShare team designed a point system. 120 credits can only guarantee the most basic use, some more advanced features, higher frequency access need to get more points.
According to the official website, there are many ways to earn points, such as recommending use, contributing**, submitting bugs, etc. But it is estimated that the most practical way is to pay money, pay 200 to get 2000 points, basically you can use most of the interfaces, and it is permanent.