1、获取历史数据
1.1获取股票历史行情数据
df = ts_pro.daily(ts_code='6000848.SH',start_date='20211102',end_date='20211112')
1.2获取涨停跌停股票数据
df = ts_pro.limit_list()
1.3获取期货合约信息
df = ts_pro.fut_basic(exchange='DCE',fut_type='1')
1.4获取期货历史行情数据
df = ts_pro.fut_daily(ts_code='RB2005.SHF')
1.5获取期权合约数据
df = ts_pro.opt_basic(exchange='DCE')
1.6获取期权历史行情数据
df = ts_pro.opt_daily(ts_code='M1805-C-3000.DCE')
1.7获取新闻快讯
df = ts_pro.news(src='sina',start_date='20191101',end_date='20191102')
1.8获取通用行情接口
df = ts.pro_bar(ts_code='00001.SZ',start_date='20191101',end_date='20191102',asset='E',freq='D')
2、通过新浪微博API获取历史数据
from urllib import request
import json
import pandas as pd
import pandas_datareader.data as web
def get_stock_data(id,scale,data_len):
url='http://quotes.sina.cn/cn/api/json_v2.php/CN_MarketDataService.getKlineData?symbol={0}&scale={1}&datalen={2}'.format(id,scale,data_len)
req=request.Request(url)
rsp=request.urlopen(req)
res=rsp.read()
res_json=json.loads(res)
bar_list=[]
res_json.reverse()
for dict in res_json:
bar={}
bar['date']=dict['day']
bar['open']=float(dict['open'])
bar['high'] = float(dict['high'])
bar['low'] = float(dict['low'])
bar['close'] = float(dict['close'])
bar['vol'] = int(dict['volume'])
bar_list.append(bar)
df = pd.DataFrame(data=bar_list)
return df
3、通过新浪财经API获取期货历史数据
def get_future_date(id,scale):
url='http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesMiniKLine{0}m?symbol={1}'.format(scale,id)
req=request.Request(url)
rsp=request.urlopen(req)
res=rsp.read()
res_json=json.loads(res)
print(res_json)
bar_list=[]
res_json.reverse()
for line in res_json:
bar = {}
bar['date'] = line[0]
bar['open'] = float(line[1])
bar['high'] = float(line[2])
bar['low'] = float(line[3])
bar['close'] = float(line[4])
bar['vol'] = int(line[5])
bar_list.append(bar)
df = pd.DataFrame(data=bar_list)
return df
4、通过pandas获取股票历史数据
import pandas_datareader.data as web
data = web.DataReader('600789.SS',data_source='yahoo',start='2019-01-01',end='2019-10-01')
print(data)
|