Convert Figma logo to code with AI

waditu logotushare

TuShare is a utility for crawling historical data of China stocks

12,780
4,281
12,780
607

Top Related Projects

8,990

AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库

25,149

基于Python的开源量化交易平台开发框架

5,323

A extendable, replaceable Python algorithmic backtest && trading framework supporting multiple securities

QUANTAXIS 支持任务调度 分布式部署的 股票/期货/期权 数据/回测/模拟/交易/可视化/多账户 纯本地量化解决方案

Python wrapper for TA-Lib (http://ta-lib.org/).

Quick Overview

TuShare is an open-source Python library for fetching historical and real-time financial market data, primarily focused on Chinese markets. It provides easy access to stock quotes, financial statements, macroeconomic data, and more, making it a valuable tool for financial analysis and research in the Chinese market context.

Pros

  • Comprehensive coverage of Chinese financial market data
  • Easy-to-use API with Pandas DataFrame integration
  • Regular updates and active community support
  • Free access to basic data, with pro version available for advanced features

Cons

  • Limited documentation in English, primarily Chinese-focused
  • Some data sources may require a paid subscription
  • Potential for API rate limiting or data access restrictions
  • May not be as comprehensive for non-Chinese markets

Code Examples

Fetching daily stock data:

import tushare as ts

# Set your TuShare API token
ts.set_token('your_token_here')

# Initialize the pro API
pro = ts.pro_api()

# Get daily stock data for a specific stock
df = pro.daily(ts_code='000001.SZ', start_date='20220101', end_date='20221231')
print(df.head())

Getting company financial statements:

import tushare as ts

pro = ts.pro_api()

# Fetch income statement data
income_statement = pro.income(ts_code='000001.SZ', start_date='20220101', end_date='20221231')
print(income_statement.head())

Retrieving macroeconomic data:

import tushare as ts

pro = ts.pro_api()

# Get GDP data
gdp_data = pro.gdp(start_q='2022Q1', end_q='2022Q4')
print(gdp_data)

Getting Started

  1. Install TuShare:

    pip install tushare
    
  2. Sign up for a TuShare account and obtain an API token from the TuShare website.

  3. Set up your API token in your Python script:

    import tushare as ts
    ts.set_token('your_token_here')
    pro = ts.pro_api()
    
  4. Start fetching data using the various API functions available in the pro object.

Competitor Comparisons

8,990

AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库

Pros of akshare

  • More comprehensive data coverage, including global markets and alternative data sources
  • Faster data retrieval and processing capabilities
  • More frequent updates and active community support

Cons of akshare

  • Steeper learning curve due to more complex API structure
  • Less stable API, with more frequent changes and updates
  • Potentially higher resource consumption due to broader data coverage

Code comparison

akshare:

import akshare as ak

stock_zh_a_spot_df = ak.stock_zh_a_spot()
print(stock_zh_a_spot_df)

tushare:

import tushare as ts

df = ts.get_today_all()
print(df)

Both libraries provide similar functionality for retrieving stock data, but akshare offers more detailed options and a wider range of data sources. The akshare code example demonstrates retrieving real-time A-share stock data, while the tushare example fetches all stock data for the current day.

akshare's API tends to be more specific and granular, allowing for more precise data retrieval. In contrast, tushare's API is generally simpler and more straightforward, which can be beneficial for users with less complex requirements or those new to financial data analysis in Python.

25,149

基于Python的开源量化交易平台开发框架

Pros of vnpy

  • More comprehensive trading platform with support for multiple asset classes
  • Offers a complete trading system including risk management and backtesting
  • Provides a modular architecture allowing for easy customization and extension

Cons of vnpy

  • Steeper learning curve due to its complexity and extensive features
  • Requires more system resources to run compared to tushare
  • May be overkill for users only interested in data retrieval and analysis

Code Comparison

vnpy example (event-driven architecture):

from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp

def main():
    qapp = create_qapp()
    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)
    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()
    qapp.exec_()

tushare example (data retrieval):

import tushare as ts

df = ts.get_hist_data('000001')
print(df.head())

Summary

vnpy is a full-featured trading platform suitable for professional traders and institutions, while tushare focuses primarily on financial data retrieval and analysis. vnpy offers more comprehensive functionality but requires more resources and has a steeper learning curve. tushare is simpler to use and more lightweight, making it ideal for researchers and analysts primarily interested in data access.

5,323

A extendable, replaceable Python algorithmic backtest && trading framework supporting multiple securities

Pros of rqalpha

  • More comprehensive backtesting and simulation capabilities
  • Supports multiple asset classes (stocks, futures, options)
  • Provides a complete trading system framework

Cons of rqalpha

  • Steeper learning curve due to its complexity
  • Less focus on data retrieval compared to Tushare
  • Requires more setup and configuration

Code Comparison

rqalpha:

from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    order_shares(context.s1, 100)

Tushare:

import tushare as ts

df = ts.get_k_data('000001', start='2019-01-01', end='2019-12-31')
print(df.head())

Summary

rqalpha is a more comprehensive trading system framework with advanced backtesting capabilities, while Tushare focuses primarily on financial data retrieval. rqalpha offers support for multiple asset classes and provides a complete trading ecosystem, but it comes with a steeper learning curve. Tushare, on the other hand, is simpler to use for data acquisition but lacks the advanced trading and backtesting features of rqalpha.

QUANTAXIS 支持任务调度 分布式部署的 股票/期货/期权 数据/回测/模拟/交易/可视化/多账户 纯本地量化解决方案

Pros of QUANTAXIS

  • More comprehensive, offering a full-stack quantitative trading and investment solution
  • Provides advanced features like backtesting, real-time monitoring, and risk management
  • Supports multiple asset classes, including stocks, futures, and cryptocurrencies

Cons of QUANTAXIS

  • Steeper learning curve due to its complexity and extensive features
  • Less focused on data retrieval compared to Tushare
  • May be overkill for users who only need basic financial data

Code Comparison

QUANTAXIS example:

import QUANTAXIS as QA

data = QA.QA_fetch_stock_day_adv('000001', '2019-01-01', '2019-12-31')
QA.QA_Risk.QA_backtest_risk_analysis(account)

Tushare example:

import tushare as ts

df = ts.get_hist_data('000001', start='2019-01-01', end='2019-12-31')
print(df.head())

QUANTAXIS offers more advanced functionality for quantitative analysis and backtesting, while Tushare focuses on simple and straightforward data retrieval. QUANTAXIS is better suited for complex trading strategies and risk management, whereas Tushare is ideal for basic financial data access and analysis.

Python wrapper for TA-Lib (http://ta-lib.org/).

Pros of ta-lib-python

  • Extensive library of technical analysis functions
  • Well-established and widely used in the financial industry
  • Supports multiple programming languages beyond Python

Cons of ta-lib-python

  • Requires separate C library installation, which can be complex
  • Less focused on Chinese market-specific data and analysis
  • May have a steeper learning curve for beginners

Code Comparison

ta-lib-python:

import talib
import numpy as np

close_prices = np.random.random(100)
sma = talib.SMA(close_prices, timeperiod=20)
rsi = talib.RSI(close_prices, timeperiod=14)

tushare:

import tushare as ts

df = ts.get_k_data('000001', start='2019-01-01', end='2019-12-31')
ma20 = df['close'].rolling(window=20).mean()

Key Differences

  • tushare focuses on providing Chinese market data and simple analysis tools
  • ta-lib-python offers a comprehensive set of technical indicators for any market
  • tushare is more user-friendly for accessing and analyzing Chinese stock data
  • ta-lib-python provides more advanced technical analysis capabilities
  • tushare is easier to install and use for beginners, especially those focused on the Chinese market

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

TuShare

Tushare Pro版已发布,请访问新的官网了解和查询数据接口! https://tushare.pro

TuShare是实现对股票/期货等金融数据从数据采集、清洗加工 到 **数据存储**过程的工具,满足金融量化分析师和学习数据分析的人在数据获取方面的需求,它的特点是数据覆盖范围广,接口调用简单,响应快速。

欢迎关注扫描TuShare的微信公众号“挖地兔”,更多资源和信息与您分享。另外,由于tushare官网在重新设计和开发,最新接口的使用文档都会在挖地兔公众号发布,所以,请扫码关注,谢谢!

QQ交流群:

  • 一群(已满):14934432
  • 二群(付费高级用户群,可获得更多支持及参与圈子活动):658562506
  • 三群(免费):665480579
  • 四群 (免费) :527416821

Dependencies

python 2.x/3.x

pandas

Installation

Upgrade

pip install tushare --upgrade

Quick Start

Example 1. 获取个股历史交易数据(包括均线数据):

import tushare as ts

ts.get_hist_data('600848') #一次性获取全部数据
另外,参考get_k_data函数

结果显示:

日期 ,开盘价, 最高价, 收盘价, 最低价, 成交量, 价格变动 ,涨跌幅,5日均价,10日均价,20日均价,5日均量,10日均量,20日均量,换手率

			 open    high   close     low     volume    p_change  ma5 \
date                                                                     
2012-01-11   6.880   7.380   7.060   6.880   14129.96     2.62   7.060   
2012-01-12   7.050   7.100   6.980   6.900    7895.19    -1.13   7.020   
2012-01-13   6.950   7.000   6.700   6.690    6611.87    -4.01   6.913   
2012-01-16   6.680   6.750   6.510   6.480    2941.63    -2.84   6.813   
2012-01-17   6.660   6.880   6.860   6.460    8642.57     5.38   6.822   
2012-01-18   7.000   7.300   6.890   6.880   13075.40     0.44   6.788   
2012-01-19   6.690   6.950   6.890   6.680    6117.32     0.00   6.770   
2012-01-20   6.870   7.080   7.010   6.870    6813.09     1.74   6.832 

			 ma10    ma20      v_ma5     v_ma10     v_ma20     turnover  
date                                                                  
2012-01-11   7.060   7.060   14129.96   14129.96   14129.96     0.48  
2012-01-12   7.020   7.020   11012.58   11012.58   11012.58     0.27  
2012-01-13   6.913   6.913    9545.67    9545.67    9545.67     0.23  
2012-01-16   6.813   6.813    7894.66    7894.66    7894.66     0.10  
2012-01-17   6.822   6.822    8044.24    8044.24    8044.24     0.30  
2012-01-18   6.833   6.833    7833.33    8882.77    8882.77     0.45  
2012-01-19   6.841   6.841    7477.76    8487.71    8487.71     0.21  
2012-01-20   6.863   6.863    7518.00    8278.38    8278.38     0.23  

设定历史数据的时间:

ts.get_hist_data('600848',start='2015-01-05',end='2015-01-09')

			open    high   close     low    volume   p_change     ma5    ma10 \  
date                                                                            
2015-01-05  11.160  11.390  11.260  10.890  46383.57     1.26  11.156  11.212   
2015-01-06  11.130  11.660  11.610  11.030  59199.93     3.11  11.182  11.155   
2015-01-07  11.580  11.990  11.920  11.480  86681.38     2.67  11.366  11.251   
2015-01-08  11.700  11.920  11.670  11.640  56845.71    -2.10  11.516  11.349   
2015-01-09  11.680  11.710  11.230  11.190  44851.56    -3.77  11.538  11.363   
 			ma20     v_ma5    v_ma10     v_ma20 	 turnover  
date                                                        
2015-01-05  11.198  58648.75  68429.87   97141.81     1.59  
2015-01-06  11.382  54854.38  63401.05   98686.98     2.03  
2015-01-07  11.543  55049.74  61628.07  103010.58     2.97  
2015-01-08  11.647  57268.99  61376.00  105823.50     1.95  
2015-01-09  11.682  58792.43  60665.93  107924.27     1.54  

复权历史数据 获取历史复权数据,分为前复权和后复权数据,接口提供股票上市以来所有历史数据,默认为前复权。如果不设定开始和结束日期,则返回近一年的复权数据,从性能上考虑,推荐设定开始日期和结束日期,而且最好不要超过一年以上,获取到数据后,请及时在本地存储。

ts.get_h_data('002337') #前复权
ts.get_h_data('002337',autype='hfq') #后复权
ts.get_h_data('002337',autype=None) #不复权
ts.get_h_data('002337',start='2015-01-01',end='2015-03-16') #两个日期之间的前复权数据

Example 2. 一次性获取最近一个日交易日所有股票的交易数据(结果显示速度取决于网速)

ts.get_today_all()

结果显示:

代码,名称,涨跌幅,现价,开盘价,最高价,最低价,最日收盘价,成交量,换手率

	  code    name     changepercent  trade   open   high    low  settlement \  
0     002738  中矿资源         10.023  19.32  19.32  19.32  19.32       17.56   
1     300410  正业科技         10.022  25.03  25.03  25.03  25.03       22.75   
2     002736  国信证券         10.013  16.37  16.37  16.37  16.37       14.88   
3     300412  迦南科技         10.010  31.54  31.54  31.54  31.54       28.67   
4     300411  金盾股份         10.007  29.68  29.68  29.68  29.68       26.98   
5     603636  南威软件         10.006  38.15  38.15  38.15  38.15       34.68   
6     002664  信质电机         10.004  30.68  29.00  30.68  28.30       27.89   
7     300367  东方网力         10.004  86.76  78.00  86.76  77.87       78.87   
8     601299  中国北车         10.000  11.44  11.44  11.44  11.29       10.40   
9     601880   大连港         10.000   5.72   5.34   5.72   5.22        5.20   
10    000856  冀东装备         10.000   8.91   8.18   8.91   8.18        8.10  
		volume  	 turnoverratio  
0        375100        1.25033  
1         85800        0.57200  
2       1058925        0.08824  
3         69400        0.51791  
4        252220        1.26110  
5       1374630        5.49852  
6       6448748        9.32700  
7       2025030        6.88669  
8     433453523        4.28056  
9     323469835        9.61735  
10     25768152       19.51090  

Example 3. 获取历史分笔数据

import tushare as ts

df = ts.get_tick_data('600848',date='2014-01-09')
df.head(10)

结果显示:

成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型

Out[3]: 
 	 time  		price change  volume  amount  type
0    15:00:00   6.05     --       8    4840   卖盘
1    14:59:55   6.05     --      50   30250   卖盘
2    14:59:35   6.05     --      20   12100   卖盘
3    14:59:30   6.05  -0.01     165   99825   卖盘
4    14:59:20   6.06   0.01       4    2424   买盘
5    14:59:05   6.05  -0.01       2    1210   卖盘
6    14:58:55   6.06     --       4    2424   买盘
7    14:58:45   6.06     --       2    1212   买盘
8    14:58:35   6.06   0.01       2    1212   买盘
9    14:58:25   6.05  -0.01      20   12100   卖盘
10   14:58:05   6.06     --       5    3030   买盘

Example 4. 获取实时交易数据(Realtime Quotes Data)

df = ts.get_realtime_quotes('000581') #Single stock symbol
df[['code','name','price','bid','ask','volume','amount','time']]

结果显示:

名称、开盘价、昨价、现价、最高、最低、买入价、卖出价、成交量、成交金额...more in docs

   code    name     price  bid    ask    volume   amount        time
0  000581  威孚高科  31.15  31.14  31.15  8183020  253494991.16  11:30:36 
  

请求多个股票方法(一次最好不要超过30个):

ts.get_realtime_quotes(['600848','000980','000981']) #symbols from a list
ts.get_realtime_quotes(df['code'].tail(10)) #from a Series

更多文档

https://tushare.pro

http://tushare.org/

Change Logs

1.2.17 2018/11/24

  • Pro版增加期货数据
  • Pro版增加A股周/月数据
  • Pro版增加通用行情pro_bar接口股票/基金/期货/数据货币行情的支持,同时支持股票的复权行情

1.2.15 2018/10/15

  • 增加通用行情pro_bar接口
  • 优化set_token功能

1.2.12 2018/08/10

1.0.5 2017/11/12

  • 新增可转债数据
  • 增加长连接关闭函数
  • 修复部分bug

1.0.2 2017/10/29

  • 新增bar接口,支持更稳定的股票、ETF、期货期权、港股、中概股等品种
  • 新增tick接口,支持以上品种的成交数据
  • 新增沪深港通每日资金流向数据
  • 修复了部分bug

0.9.2 2017/09/13

  • 新增数据货币行情数据接口,同时支持火币、okcoin、中国比特币
  • 部分bug修复

0.8.8 2017/08/29

  • 新增分红送股数据(包含历史)
  • 新增get_day_all接口
  • 新增BDI接口

0.8.0 2017/06/05

  • 新增期货行情数据6个接口,感谢debugo贡献代码
  • 修复部分bug

0.7.6 2017/05/16

  • get_today_all接口数据补齐
  • forecast_data mac下编码问题修复

0.7.0 2017/03/12

  • get_today_all接口提速
  • 版本累积更新

0.6.2 2016/12/03

  • 新增十大股东和十大流通股接口 top10_holders
  • 新增全球实时指数列表接口 global_realtime
  • 修复部分bug

0.6.1 2016/11/22

  • 修正get_k_databug
  • 修正实盘交易登录问题

0.5.6 2016/11/06

  • 新增全新行情数据接口get_k_data(请关注tushare公众号“挖地兔”后查看历史文章《全新的免费行情数据接口》)
  • 修复程序和文档bug

0.5.1 2016/10/16

  • 新增实盘交易接口
  • 修复bug

0.4.9 2016/03/26

  • 新增申万行业分类get_industry_classified(standard='sw')
  • 新增交易日历trade_cal()
  • 修复bug

0.4.3 2015/12/24

  • 新增电影票房数据
  • 修复部分bug

0.4.1 2015/11/27

  • 新增sina大单数据
  • 修改当日分笔bug
  • 深市融资融券数据修复

0.3.9 2015/10/13

  • 新增期权隐含波动率数据
  • 修复指数成份及权重接口问题

0.3.8 2015/09/19

  • 沪深300成份股和权重接口问题修复
  • 其它bug的修复

0.3.5 2015/07/27

  • 部分代码修正

0.3.4 2015/06/15

  • 新增‘龙虎榜’模块

    1. 每日龙虎榜列表
    2. 个股上榜统计
    3. 营业部上榜统计
    4. 龙虎榜机构席位追踪
    5. 龙虎榜机构席位成交明细
  • 修改get_h_data数据类型为float

  • 修改get_index接口遗漏的open列

  • 合并GitHub上提交的bug修复

0.2.8 2015/04/28

  • 新增大盘指数实时行情列表
  • 新增大盘指数历史行情数据(全部)
  • 新增终止上市公司列表(退市)
  • 新增暂停上市公司列表
  • 修正融资融券明细无日期的缺陷
  • 修正get_h_data部分bug

0.2.6

  • 新增沪市融资融券列表
  • 新增沪市融资融券明细列表
  • 新增深市融资融券列表
  • 新增深市融资融券明细列表
  • 修正复权数据数据源出现null造成异常问题(对大约300个股票有影响)

0.2.5 2015/04/16

  • 完成python2.x和python3.x兼容性支持
  • 部分算法优化和代码重构
  • 新增中证500成份股
  • 新增当日分笔交易明细
  • 修正分配预案(高送转)bug

0.2.3 2015/04/11

  • 新增“新浪股吧”消息和热度
  • 新增新股上市数据
  • 修正“基本面”模块中数据重复的问题
  • 修正历史数据缺少一列column(数据来源问题)的bug

0.2.0 2015/03/17

  • 新增历史复权数据接口
  • 新增即时滚动新闻、信息地雷数据
  • 新增沪深300指数成股份及动态权重、
  • 新增上证50指数成份股
  • 修改历史行情数据类型为float

0.1.9 2015/02/06

  • 增加分类数据
  • 增加数据存储示例

0.1.6 2015/01/27

  • 增加了重点指数的历史和实时行情
  • 更新docs

0.1.5 2015/01/26

  • 增加基本面数据接口
  • 发布一版使用手册,开通TuShare docs网站

0.1.3 2015/01/13

  • 增加实时交易数据的获取
  • Done for crawling Realtime Quotes data

0.1.1 2015/01/11

  • 增加tick数据的获取

0.1.0 2014/12/01

  • 创建第一个版本
  • 实现个股历史数据的获取