获取股票列表,获取股票五日数据,新浪财经实时数据接口和历史数据接口

初次接触量化的一些代码和资源,记录以自用!

获取全部股票代码和名字(参考地址

import requests
import json
import re

url = 'http://87.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112406526563715394427_1631116233755&pn=1&pz=10000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23&fields=f12,f14'

response = requests.get(url).text
# print(response)
pattern = re.compile(r'"diff":(.*?)}}')
data_json = pattern.findall(response)[0]

response = json.loads(data_json)
for i in response:
    print(i)

在这里插入图片描述
获取股票五日数据:(参考地址
参考公式:(实时数据+前四天收盘价)/5
主要是实时数据接口历史数据接口的使用
实时数据接口:‘https://hq.sinajs.cn/etag.php?list={id}’
在这里插入图片描述
历史数据接口:https://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol={id}&scale=60&ma=5&datalen=20’
最近二十天左右的每5分钟数据
http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=sz000001&scale=5&ma=5&datalen=1023
(参数:股票编号、分钟间隔(5、15、30、60)、均值(5、10、15、20、25)、查询个数点(最大值242))

获取的数据是类似下面的json数组:日期、开盘价、最高价、最低价、收盘价、成交量:

全部代码:


import datetime
import json
import requests
from fake_useragent import UserAgent

ua = UserAgent()
headers = {'User-Agent':ua.random,'Referer':'https://finance.sina.com.cn'}
id='sz000001'
url = f'https://hq.sinajs.cn/etag.php?list={id}'
response = requests.get(url, headers=headers).text
current_price=response.split(",")[3]
print('当前价格',current_price)
url = f'https://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol={id}&scale=60&ma=5&datalen=20'
response = requests.get(url, headers=headers).text
response=json.loads(response)
flag=-1
for i,item in enumerate(response):
    if str(datetime.date.today()) in item['day']:
        flag=i
        break
if flag==-1:
    flag=len(response)
day1=response[flag-13]['close']
day2=response[flag-9]['close']
day3=response[flag-5]['close']
day4=response[flag-1]['close']

print('前四天收盘价:',day1,day2,day3,day4)
ave=(float(current_price)+float(day1)+float(day2)+float(day3)+float(day4))/5
print('五日线:',ave)

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值