最近一两年,股票量化分析越来越受欢迎了。想要入行,首先得搞定股票数据。毕竟,所有量化分析都是建立在数据之上的,实时交易、历史交易、财务、基本面,这些数据咱们都得有。咱们的目标就是挖掘这些数据中的价值,来指导咱们的投资策略。
为了找数据,我可是尝试了各种方法,自己动手写过网易、申万行业的爬虫,还试过同花顺问财的,连聚宽的免费API都用过。但爬虫这东西,数据总是不稳定,给量化分析带来不少困扰。
在量化分析领域,实时、准确的数据接口太重要了。现在我用Python、JavaScript(Node.js)、Java、C#和Ruby五种主流语言的实例代码给大家演示一下如何获取股票历史分时KDJ数据:
历史分时MACD数据是区分分时级别的,我这里演示的都是60分钟级别的历史分时MACD数据,其他级别可以参考下面的API文档自行修改参数就行了
1、python
import requests
url = "http://api.mairui.club/hszbl/macd/000001/60m/b997d4403688d5e66a"
response = requests.get(url)
data = response.json()
print(data)
2、JavaScript (Node.js)
const axios = require('axios');
const url = "http://api.mairui.club/hszbl/macd/000001/60m/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/hszbl/macd/000001/60m/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/hszbl/macd/000001/60m/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/hszbl/macd/000001/60m/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
返回的数据示例:
[{"t":"2024-07-19 10:30","diff":"0.000","dea":"0.000","macd":"0.000","ema12":"10.31","ema26":"10.31"},{"t":"2024-07-19 11:30","diff":"0.002","dea":"0.000","macd":"0.003","ema12":"10.3131","ema26":"10.3115"},{"t":"2024-07-19 14:00","diff":"0.004","dea":"0.001","macd":"0.007","ema12":"10.3188","ema26":"10.3144"},{"t":"2024-07-19 15:00","diff":"0.008","dea":"0.003","macd":"0.011","ema12":"10.3267","ema26":"10.3185"},{"t":"2024-07-22 10:30","diff":"-0.003","dea":"0.001","macd":"-0.009","ema12":"10.3057","ema26":"10.3090"},{"t":"2024-07-22 11:30","diff":"-0.009","dea":"-0.001","macd":"-0.016","ema12":"10.2941","ema26":"10.3031"},{"t":"2024-07-22 14:00","diff":"-0.016","dea":"-0.004","macd":"-0.024","ema12":"10.2796","ema26":"10.2955"},{"t":"2024-07-22 15:00","diff":"-0.019","dea":"-0.007","macd":"-0.024","ema12":"10.2720","ema26":"10.2906"},{"t":"2024-07-23 10:30","diff":"-0.015","dea":"-0.008","macd":"-0.013","ema12":"10.2763","ema26":"10.2913"},{"t":"2024-07-23 11:30","diff":"-0.014","dea":"-0.009","macd":"-0.008","ema12":"10.2769","ema26":"10.2905"},{"t":"2024-07-23 14:00","diff":"-0.012","dea":"-0.010","macd":"-0.005","ema12":"10.2774","ema26":"10.2897"}]{"t":"2024-08-30 15:00","u":"10.57","d":"10.06","m":"10.32"}
历史分时MACD
API接口:http://api.mairui.club/hszbl/macd/股票代码(如000001)/分时级别/licence证书
接口说明:根据《股票列表》得到的股票代码以及分时级别获取分时MACD数据,交易时间从远到近排序。目前 分时级别 支持5分钟、15分钟、30分钟、60分钟、日周月年级别(包括前后复权),对应的值分别是 5m(5分钟)、15m(15分钟)、30m(30分钟)、60m(60分钟)、dn(日线未复权)、dq(日线前复权)、dh(日线后复权)、wn(周线未复权)、wq(周线前复权)、wh(周线后复权)、mn(月线未复权)、mq(月线前复权)、mh(月线后复权)、yn(年线未复权)、yq(年线前复权)、yh(年线后复权) 。
数据更新:分钟级别数据盘中更新,分时越小越优先更新,如5分钟级别会每5分钟更新,15分钟级别会每15分钟更新,以此类推,日线及以上级别每天16:00更新。
字段名称 | 数据类型 | 字段说明 |
---|---|---|
t | string | 交易时间,短分时级别格式为yyyy-MM-dd HH:mm:ss,日线级别为yyyy-MM-dd |
diff | number | DIFF值 |
dea | number | DEA值 |
macd | number | MACD值 |
ema12 | number | EMA(12)值 |
ema26 | number | EMA(26)值 |
【重要提示】
上方所有演示中的API接口Url链接结尾的b997d4403688d5e66a,均为数据请求licence证书,因为这个证书是官方测试证书,仅可用于验证各个接口的有效性,所以这个证书限制了只可请求股票代码为000001的数据,正式环境中是不能使用的,证书可以自己去申请一个替换掉就好了,证书是免费申请的:https://www.mairui.club/getlicence.html,替换成自己申请的证书就可以请求任何股票数据了。