tushare ID:497274
准备
本次数据的获取是通过tushare平台获取,该平台整理股票、基金、期货、债券、外汇等各种全面的数据。 tushare网页链接:https://tushare.pro 在该平台注册账号,并在个人主页中找到自己token
获取代码
1.获取所需股票的编码、行业、上市日期等基本信息
def stock_basic():
save_path = "data/stock_basic.csv"
if os.path.exists(save_path):
df = pd.read_csv(save_path)
else:
df = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
df.to_csv(save_path)
return df
2. 通过ts_code循环获取每家公司的历史股价
def get_qfq(ts_code,start_date,end_date):
time.sleep(1)
df = ts.pro_bar(ts_code=ts_code, adj='qfq', start_date=start_date, end_date=end_date,factors=['tor', 'vr'])
return df
def get_all_company_qfq(end_date):
stock_basic_df = stock_basic()
rows, cols = stock_basic_df.shape
for row in tqdm(range(rows)):
ts_code = stock_basic_df.loc[row,"ts_code"]
list_date = stock_basic_df.loc[row,"list_date"]
save_path = "data/qfq/{}_{}_{}_qfq.csv".format(ts_code,list_date,end_date)
company_qfq = get_qfq(ts_code, str(list_date), end_date)
if company_qfq is not None:
company_qfq_dropna = company_qfq.dropna(axis=0,how="any")
company_qfq_dropna.to_csv(save_path)
print("saving {} ".format(save_path))
由于改数据时要用于分析的,所以都经过了前复权的调整 4. 完整代码
import os
import pandas as pd
import time
from tqdm import tqdm
import tushare as ts
ts.set_token("你的token")
pro = ts.pro_api()
def stock_basic():
save_path = "data/stock_basic.csv"
if os.path.exists(save_path):
df = pd.read_csv(save_path)
else:
df = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
df.to_csv(save_path)
return df
def get_qfq(ts_code,start_date,end_date):
time.sleep(1)
df = ts.pro_bar(ts_code=ts_code, adj='qfq', start_date=start_date, end_date=end_date,factors=['tor', 'vr'])
return df
def get_all_company_qfq(end_date):
stock_basic_df = stock_basic()
rows, cols = stock_basic_df.shape
for row in tqdm(range(rows)):
ts_code = stock_basic_df.loc[row,"ts_code"]
list_date = stock_basic_df.loc[row,"list_date"]
save_path = "data/qfq/{}_{}_{}_qfq.csv".format(ts_code,list_date,end_date)
company_qfq = get_qfq(ts_code, str(list_date), end_date)
if company_qfq is not None:
company_qfq_dropna = company_qfq.dropna(axis=0,how="any")
company_qfq_dropna.to_csv(save_path)
print("saving {} ".format(save_path))
if __name__ == "__main__":
ts_code = '000001.SZ'
start_date = '20000101'
end_date = '20220319'
get_all_company_qfq(end_date)
后续将会推出通过神经网络预测未来股价的教学
|