最近一两年内,股票量化分析逐渐成为热门话题。而从事这一领域工作的第一步,就是获取全面且准确的股票数据。因为无论是实时交易数据、历史交易记录、财务数据还是基本面信息,这些数据都是我们进行量化分析时不可或缺的宝贵资源。我们的主要任务是从这些数据中提炼出有价值的信息,为我们的投资策略提供有力的指导。
在数据探索的旅途中,我尝试了多种方法,包括自编网易股票页面爬虫、申万行业数据爬虫,以及同花顺问财的爬虫,甚至还使用了聚宽的免费数据API。然而,爬虫作为数据源常常显得不够稳定,给我们的量化分析带来不小的困扰。
在量化分析领域,实时且准确的数据接口是成功的基石。经过多次实际测试,我将已确认可用的数据接口分享给正在从事量化分析的朋友们,希望能够对你们的研究和工作有所帮助,接下来我会用Python、JavaScript(Node.js)、Java、C#和Ruby五种主流语言的实例代码给大家逐一演示一下如何获取各类股票数据:
1、python
python
import requests
url = "http://api.mairui.club/hscp/sszs/000001/b997d4403688d5e66a"
response = requests.get(url)
data = response.json()
print(data)
2、JavaScript (Node.js)
javascript
const axios = require('axios');
const url = "http://api.mairui.club/hscp/sszs/000001/b997d4403688d5e66a";
axios.get(url)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
3、Java
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/hscp/sszs/000001/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#
csharp
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/hscp/sszs/000001/b997d4403688d5e66a";
HttpResponseMessage response = await client.GetAsync(url);
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
5、Ruby
ruby
require 'net/http'
require 'json'
url = URI("http://api.mairui.club/hscp/sszs/000001/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
返回的数据:
json
[{"mc":"金融业","dm":"825500","ind":"","outd":""},{"mc":"中价股指数","dm":"801842","ind":"2024-07-01","outd":""},{"mc":"300红利质量全收益","dm":"932316CNY010","ind":"2024-07-08","outd":""},{"mc":"800红利质量净收益","dm":"932318CNY020","ind":"2024-07-08","outd":""},{"mc":"800红利质量全收益","dm":"932318CNY010","ind":"2024-07-08","outd":""},{"mc":"300红利质量净收益","dm":"932316CNY020","ind":"2024-07-08","outd":""},{"mc":"中标A股红利指数","dm":"818001","ind":"2024-08-01","outd":""},{"mc":"中证全指(净)USD(CNH)","dm":"000985USD320","ind":"2024-08-13","outd":""},{"mc":"中证全指(净)USD","dm":"000985USD220","ind":"2024-08-13","outd":""},{"mc":"中证全指(全)USD","dm":"000985USD210","ind":"2024-08-13","outd":""},{"mc":"中证全指(全)USD(CNH)","dm":"000985USD310","ind":"2024-08-13","outd":""},{"mc":"恒生沪深港通大湾区高","dm":"HSI169","ind":"2024-09-09","outd":""},{"mc":"恒生沪深港通中国股息","dm":"HSI431","ind":"2024-09-09","outd":""},{"mc":"中证A500","dm":"000510","ind":"2024-09-23","outd":""},{"mc":"ESG基准","dm":"980078","ind":"2024-08-29","outd":""},{"mc":"华证ESG领先","dm":"999102CH","ind":"2024-09-09","outd":""}]
所属指数
API接口:http://api.mairui.club/hscp/sszs/股票代码(如000001)/b997d4403688d5e66a
接口说明:根据《股票列表》得到的股票代码获取上市公司的所属指数。
数据更新:每天15:30开始更新,次日凌晨3点前完成
字段名称 | 数据类型 | 字段说明 |
---|---|---|
mc | string | 指数名称 |
dm | string | 指数代码 |
ind | string | 进入日期yyyy-MM-dd |
outd | string | 退出日期yyyy-MM-dd |
【重要提示】
上方所有演示中的API接口Url链接结尾的b997d4403688d5e66a,均为数据请求licence证书,因为这个证书是官方测试证书,仅可用于验证各个接口的有效性,所以这个证书限制了只可请求股票代码为000001的数据,正式环境中是不能使用的,证书可以自己去申请一个替换掉就好了,证书是免费申请的:https://www.mairui.club/getlicence.html,替换成自己申请的证书就可以请求任何股票数据了。