python a股行情_使用Python下载A股行情的几种方法
使用Python下載A股行情的幾種方法
1. tushare
Tushare是一個(gè)免費(fèi)、開源的python財(cái)經(jīng)數(shù)據(jù)接口包。主要實(shí)現(xiàn)對(duì)股票等金融數(shù)據(jù)從數(shù)據(jù)采集、清洗加工 到 數(shù)據(jù)存儲(chǔ)的過程,能夠?yàn)榻鹑诜治鋈藛T提供快速、整潔、和多樣的便于分析的數(shù)據(jù),為他們?cè)跀?shù)據(jù)獲取方面極大地減輕工作量,使他們更加專注于策略和模型的研究與實(shí)現(xiàn)上。 Tushare運(yùn)行三年多,已經(jīng)更新到了Pro版。數(shù)據(jù)內(nèi)容將擴(kuò)大到包含股票、基金、期貨、債券、外匯、行業(yè)大數(shù)據(jù),同時(shí)包括了數(shù)字貨幣行情等區(qū)塊鏈數(shù)據(jù)的全數(shù)據(jù)品類的金融大數(shù)據(jù)平臺(tái),為各類金融投資和研究人員提供適用的數(shù)據(jù)和工具。首先使用pip安裝tushare:pip install tushare然后在tushare.pro注冊(cè)用戶,注意獲取你自己的token。使用daily函數(shù)獲取日線數(shù)據(jù)。
# 導(dǎo)入tushare庫(kù)
import tushare as ts
# 設(shè)置token
ts.set_token('your token here')
# 初始化pro接口
pro = ts.pro_api()
# 獲取日線數(shù)據(jù)
df = pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718'
2. baostock
證券寶http://www.baostock.com是一個(gè)免費(fèi)、開源的證券數(shù)據(jù)平臺(tái)(無需注冊(cè))。提供大量準(zhǔn)確、完整的證券歷史行情數(shù)據(jù)、上市公司財(cái)務(wù)數(shù)據(jù)等。通過python API獲取證券數(shù)據(jù)信息,滿足量化交易投資者、數(shù)量金融愛好者、計(jì)量經(jīng)濟(jì)從業(yè)者數(shù)據(jù)需求。首先使用pip安裝baostock:pip install baostock然后使用query_history_k_data_plus函數(shù)獲取日線數(shù)據(jù)
import baostock as bs
import pandas as pd
#### 登陸系統(tǒng) ####
lg = bs.login()
#### 獲取滬深A(yù)股歷史K線數(shù)據(jù) ####
rs_result = bs.query_history_k_data_plus("sh.600000", start_date='2017-07-01', end_date='2017-12-31', frequency="d", adjustflag="3")
df_result = rs_result.get_data()
#### 登出系統(tǒng) ####
bs.logout()
3. akshare
AkShare是基于 Python 的財(cái)經(jīng)數(shù)據(jù)接口庫(kù), 目的是實(shí)現(xiàn)對(duì)股票、期貨、期權(quán)、基金、外匯、債券、指數(shù)、數(shù)字貨幣等金融產(chǎn)品的基本面數(shù)據(jù)、實(shí)時(shí)和歷史行情數(shù)據(jù)、衍生數(shù)據(jù)從數(shù)據(jù)采集、數(shù)據(jù)清洗到數(shù)據(jù)落地的一套工具, 主要用于學(xué)術(shù)研究目的。首先使用pip安裝akshare:pip install akshare然后使用stock_zh_a_spot獲取日線數(shù)據(jù)
import akshare as ak
stock_zh_a_spot_df = ak.stock_zh_a_spot()
print(stock_zh_a_spot_df)
4. dtshare
DTShare主要實(shí)現(xiàn)了股票、期貨、指數(shù)、新聞資訊和一些另類數(shù)據(jù)的開發(fā)與接口服務(wù)。在這里,要感謝各類網(wǎng)站提供的優(yōu)質(zhì)的數(shù)據(jù),經(jīng)過我們的整理,希望對(duì)有需要的用戶有所幫助。首先使用pip安裝dtshare:pip install dtshare然后使用stock_zh_a_spot獲取日線數(shù)據(jù)
import dtshare as dt
stock_df = dt.stock_zh_a_spot()
print(stock_df)
5. 聚寬JQData
JQData是聚寬數(shù)據(jù)團(tuán)隊(duì)專門為金融機(jī)構(gòu)、學(xué)術(shù)團(tuán)體和量化研究者們提供的本地量化金融數(shù)據(jù)服務(wù)。使用JQData,可快速查看和計(jì)算金融數(shù)據(jù),無障礙解決本地、Web、金融終端調(diào)用數(shù)據(jù)的需求。歷經(jīng)3年沉淀,15萬寬客及數(shù)百家機(jī)構(gòu)投研交易驗(yàn)證。 為了滿足用戶的需求,聚寬數(shù)據(jù)團(tuán)隊(duì)在JQData中不僅提供了全面的基礎(chǔ)金融數(shù)據(jù),包括滬深A(yù)股行情數(shù)據(jù),上市公司財(cái)務(wù)數(shù)據(jù),場(chǎng)內(nèi)基金數(shù)據(jù),場(chǎng)外基金數(shù)據(jù),指數(shù)數(shù)據(jù),期貨數(shù)據(jù),期權(quán)數(shù)據(jù)、債券數(shù)據(jù)以及宏觀經(jīng)濟(jì)數(shù)據(jù);除此之外,JQData還針對(duì)因子數(shù)據(jù)和特色數(shù)據(jù),引進(jìn)了聚寬因子庫(kù),輿情數(shù)據(jù),Alpha特色因子,技術(shù)分析指標(biāo)因子,tick數(shù)據(jù),助您更好的完成量化研究和投資決策。首先使用pip安裝jqdatasdk:pip install jqdatasdk然后需要在聚寬官網(wǎng)上注冊(cè),并提交申請(qǐng)。使用get_price獲取日線數(shù)據(jù)
from jqdatasdk import *
#鑒權(quán),賬號(hào)是申請(qǐng)時(shí)所填寫的手機(jī)號(hào);密碼為聚寬官網(wǎng)登錄密碼
auth('賬號(hào)','密碼')
# 獲取獲得000001.XSHG在2015年01月31日前2個(gè)交易日的數(shù)據(jù)
df = get_price('000001.XSHE',end_date='2015-01-31', frequency='daily')
6. 蜂鳥數(shù)據(jù)Trochil
蜂鳥數(shù)據(jù)是輕量級(jí)金融終端,提供主流金融市場(chǎng)的實(shí)時(shí)報(bào)價(jià)和歷史數(shù)據(jù)。我們提供高度統(tǒng)一和強(qiáng)大的REST API和Websocket API,讓您輕松獲取金融數(shù)據(jù)。 登錄蜂鳥數(shù)據(jù),注冊(cè)成為會(huì)員,即可免費(fèi)使用API服務(wù),每天可免費(fèi)調(diào)用250次REST API,并試用7天Websocket,適用于所有接口和市場(chǎng)。蜂鳥數(shù)據(jù)Trochil通過API獲取數(shù)據(jù)
## 撰寫自定義函數(shù),通過API獲取數(shù)據(jù)
def fetch_trochil(url: str,
params: Dict[str, str],
attempt: int = 3,
timeout: int = 3) -> Dict[str, Any]:
"""裝飾requests.get函數(shù)"""
for i in range(attempt):
try:
resp = requests.get(url, params, timeout=timeout)
resp.raise_for_status()
data = resp.json()["data"]
if not data:
raise Exception("empty dataset")
return data
except (ConnectionError, Timeout) as e:
print(e)
i += 1
time.sleep(i * 0.5)
def fetch_cnstocks(apikey: str) -> pd.DataFrame:
"""從蜂鳥數(shù)據(jù)獲取A股產(chǎn)品列表"""
url = "https://api.trochil.cn/v1/cnstock/markets"
params = {"apikey": apikey}
res = fetch_trochil(url, params)
return pd.DataFrame.from_records(res)
def fetch_daily_ohlc(symbol: str,
date_from: dt.datetime,
date_to: dt.datetime,
apikey: str) -> pd.DataFrame:
"""從蜂鳥數(shù)據(jù)獲取A股日?qǐng)D歷史K線"""
url = "https://api.trochil.cn/v1/cnstock/history"
params = {
"symbol": symbol,
"start_date": date_from.strftime("%Y-%m-%d"),
"end_date": date_to.strftime("%Y-%m-%d"),
"freq": "daily",
"apikey": apikey
}
res = fetch_trochil(url, params)
return pd.DataFrame.from_records(res)
def fetch_index_ohlc(symbol: str,
date_from: dt.datetime,
date_to: dt.datetime,
apikey: str) -> pd.DataFrame:
"""獲取股指的日?qǐng)D歷史數(shù)據(jù)"""
url = "https://api.trochil.cn/v1/index/daily"
params = {
"symbol": symbol,
"start_date": date_from.strftime("%Y-%m-%d"),
"end_date": date_to.strftime("%Y-%m-%d"),
"apikey": apikey
}
res = fetch_trochil(url, params)
return pd.DataFrame.from_records(res)
以上是使用Python下載A股行情的幾種方法,歡迎大家私信補(bǔ)充。
總結(jié)
以上是生活随笔為你收集整理的python a股行情_使用Python下载A股行情的几种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汽车IVI中控开发入门及进阶(九):显示
- 下一篇: 蓝桥杯算法训练KAc给糖果贪心-pyth