最近一两年内,股票量化分析逐渐成为热门话题。而从事这一领域工作的第一步,就是获取全面且准确的股票数据。因为无论是实时交易数据、历史交易记录、财务数据还是基本面信息,这些数据都是我们进行量化分析时不可或缺的宝贵资源。我们的主要任务是从这些数据中提炼出有价值的信息,为我们的投资策略提供有力的指导。
在数据探索的旅途中,我尝试了多种方法,包括自编网易股票页面爬虫、申万行业数据爬虫,以及同花顺问财的爬虫,甚至还使用了聚宽的免费数据API。然而,爬虫作为数据源常常显得不够稳定,给我们的量化分析带来不小的困扰。
在量化分析领域,实时且准确的数据接口是成功的基石。经过多次实际测试,我将已确认可用的数据接口分享给正在从事量化分析的朋友们,希望能够对你们的研究和工作有所帮助,接下来我会用Python、JavaScript(Node.js)、Java、C#和Ruby五种主流语言的实例代码给大家逐一演示一下如何获取各类股票数据:
1、python
import requests url = "http://api.mairui.club/hslt/zbgc/2024-09-05/b997d4403688d5e66a"
response = requests.get(url)
data = response.json()
print(data)
2、JavaScript (Node.js)
const axios = require('axios'); const url = "http://api.mairui.club/hslt/zbgc/2024-09-05/b997d4403688d5e66a";
axios.get(url) .then(response => { console.log(response.data); }) .catch(error => { console.log(error); });
3、Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException; public class Main { public static void main(String[] args) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://api.mairui.club/hslt/zbgc/2024-09-05/b997d4403688d5e66a")) .build(); try { HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } catch (IOException | InterruptedException e) { e.printStackTrace(); } }
}
4、C#
using System;
using System.Net.Http;
using System.Threading.Tasks; class Program
{ static async Task Main() { using (HttpClient client = new HttpClient()) { string url = "http://api.mairui.club/hslt/zbgc/2024-09-05/b997d4403688d5e66a"; HttpResponseMessage response = await client.GetAsync(url); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } }
}
5、Ruby
require 'net/http'
require 'json' url = URI("http://api.mairui.club/hslt/zbgc/2024-09-05/b997d4403688d5e66a") http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
response = http.request(request)
data = JSON.parse(response.read_body)
puts data
返回的数据:
[{"dm":"sh603978","mc":"深圳新星","p":11.32,"ztp":12.02,"zf":3.57,"cje":237313483.0,"lt":1936095812.68,"zsz":1936095812.68,"hs":11.9,"zs":0.09,"tj":"2/1","fbt":"09:35:59","zbc":2},{"dm":"sh600376","mc":"首开股份","p":2.29,"ztp":2.32,"zf":8.53,"cje":100502541.0,"lt":5907204404.18,"zsz":5907204417.92,"hs":1.71,"zs":0.88,"tj":"0/0","fbt":"09:36:03","zbc":2},{"dm":"sh600207","mc":"安彩高科","p":3.86,"ztp":3.98,"zf":6.63,"cje":264226822.0,"lt":4204746458.44,"zsz":4204746481.6,"hs":6.19,"zs":0.0,"tj":"0/0","fbt":"09:36:39","zbc":2},{"dm":"sz301153","mc":"中科江南","p":20.52,"ztp":21.01,"zf":17.19,"cje":451409792.0,"lt":4224366072.36,"zsz":7180358400.0,"hs":10.92,"zs":0.0,"tj":"0/0","fbt":"09:39:51","zbc":1}]
炸板股池
API接口:http://api.mairui.club/hslt/zbgc/日期(如2020-01-15)/b997d4403688d5e66a
备用接口:http://api1.mairui.club/hslt/zbgc/日期(如2020-01-15)/b997d4403688d5e66a
接口说明:根据日期(格式yyyy-MM-dd,从2019-11-28开始到现在的每个交易日)作为参数,得到每天的炸板股票列表,根据首次封板时间升序。
数据更新:交易时间段每10分钟
字段名称 | 数据类型 | 字段说明 |
---|---|---|
dm | string | 代码 |
mc | string | 名称 |
p | number | 价格(元) |
ztp | number | 涨停价(元) |
zf | number | 涨跌幅(%) |
cje | number | 成交额(元) |
lt | number | 流通市值(元) |
zsz | number | 总市值(元) |
zs | number | 涨速(%) |
hs | number | 转手率(%) |
tj | string | 涨停统计(x天/y板) |
fbt | string | 首次封板时间(HH:mm:ss) |
zbc | number | 炸板次数 |
【重要提示】
上方所有演示中的API接口Url链接结尾的b997d4403688d5e66a,均为数据请求licence证书,因为这个证书是官方测试证书,仅可用于验证各个接口的有效性,所以这个证书限制了只可请求股票代码为000001的数据,正式环境中是不能使用的,证书可以自己去申请一个替换掉就好了,证书是免费申请的:https://www.mairui.club/getlicence.html,替换成自己申请的证书就可以请求任何股票数据了。