最近一两年内,股票量化分析逐渐成为热门话题。而从事这一领域工作的第一步,就是获取全面且准确的股票数据。因为无论是实时交易数据、历史交易记录、财务数据还是基本面信息,这些数据都是我们进行量化分析时不可或缺的宝贵资源。我们的主要任务是从这些数据中提炼出有价值的信息,为我们的投资策略提供有力的指导。
在数据探索的旅途中,我尝试了多种方法,包括自编网易股票页面爬虫、申万行业数据爬虫,以及同花顺问财的爬虫,甚至还使用了聚宽的免费数据API。然而,爬虫作为数据源常常显得不够稳定,给我们的量化分析带来不小的困扰。
在量化分析领域,实时且准确的数据接口是成功的基石。经过多次实际测试,我将已确认可用的数据接口分享给正在从事量化分析的朋友们,希望能够对你们的研究和工作有所帮助,接下来我会用Python、JavaScript(Node.js)、Java、C#和Ruby五种主流语言的实例代码给大家逐一演示一下如何获取各类股票数据:
1、python
python
import requests
url = "http://api.mairui.club/hscp/jdlr/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/jdlr/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/jdlr/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/jdlr/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/jdlr/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
[{"date":"2024-06-30","income":"7,713,200.00","expend":"2,189,200.00","profit":"3,208,700.00","totalp":"3,197,700.00","reprofit":"2,587,900.00","basege":"1.2300","ettege":"1.2300","otherp":"-35,600.00","totalcp":"2,552,300.00"},{"date":"2024-03-31","income":"3,877,000.00","expend":"1,082,000.00","profit":"1,855,400.00","totalp":"1,852,500.00","reprofit":"1,493,200.00","basege":"0.6600","ettege":"0.6600","otherp":"34,500.00","totalcp":"1,527,700.00"},{"date":"2023-12-31","income":"16,469,900.00","expend":"4,767,700.00","profit":"5,792,800.00","totalp":"5,771,800.00","reprofit":"4,645,500.00","basege":"2.2500","ettege":"2.2500","otherp":"-37,200.00","totalcp":"4,608,300.00"},{"date":"2023-09-30","income":"12,763,400.00","expend":"3,517,300.00","profit":"4,904,700.00","totalp":"4,899,300.00","reprofit":"3,963,500.00","basege":"1.9400","ettege":"1.9400","otherp":"-83,800.00","totalcp":"3,879,700.00"},{"date":"2023-06-30","income":"8,861,000.00","expend":"2,431,200.00","profit":"3,193,700.00","totalp":"3,193,300.00","reprofit":"2,538,700.00","basege":"1.2000","ettege":"1.2000","otherp":"-37,500.00","totalcp":"2,501,200.00"}]
近一年各季度利润
API接口:http://api.mairui.club/hscp/jdlr/股票代码(如000001)/b997d4403688d5e66a
接口说明:根据《股票列表》得到的股票代码获取上市公司近一年各个季度的利润。按截止日期倒序。
字段名称 | 数据类型 | 字段说明 |
---|---|---|
date | string | 截止日期yyyy-MM-dd |
income | string | 营业收入(万元) |
expend | string | 营业支出(万元) |
profit | string | 营业利润(万元) |
totalp | string | 利润总额(万元) |
reprofit | string | 净利润(万元) |
basege | string | 基本每股收益(元/股) |
ettege | string | 稀释每股收益(元/股) |
otherp | string | 其他综合收益(万元) |
totalcp | string | 综合收益总额(万元) |
【重要提示】
上方所有演示中的API接口Url链接结尾的b997d4403688d5e66a,均为数据请求licence证书,因为这个证书是官方测试证书,仅可用于验证各个接口的有效性,所以这个证书限制了只可请求股票代码为000001的数据,正式环境中是不能使用的,证书可以自己去申请一个替换掉就好了,证书是免费申请的:https://www.mairui.club/getlicence.html,替换成自己申请的证书就可以请求任何股票数据了。