初次接触量化的一些代码和资源,记录以自用!
获取全部股票代码和名字(参考地址)
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14

获取股票五日数据:(参考地址)
参考公式:(实时数据+前四天收盘价)/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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
