轉自 https://www.joinquant.com/view/community/detail/24862
打算做一個選股工具給自己用,選來選去還是發現聚寬的數據比較合適。
在開發上我使用NodeJS來做數據的採集,然後需要Docker容器化。我沒有直接使用聚寬提供的SDK來完成開發,而是直接使用了聚寬的Restful API來獲取數據。
話不多說,先貼上數據獲取的部分代碼:
//package.json中需要引如axios和csvtojson
const axios = require('axios');
const csvtojson = require("csvtojson");
class JQService {
static singleton() {
if (!this.instance) {
this.instance = new JQService();
}
return this.instance;
}
constructor() {
this.token = null;
this.isRefreshToken = false;
this.axiosInstance = axios.create({
baseURL: 'https://dataapi.joinquant.com',
timeout: 30000,
headers: { 'Content-Type': 'application/json' }
});
}
async _sendRequest(params) {
if (!this.token && params.method != 'get_token') {
await this.refreshToken();
}
if (params.method != 'get_token' && !this.token) return null;
const postBody = params.method != 'get_token' ? Object.assign({}, params, { token: this.token }) : params;
console.log(postBody)
const { status, headers, data } = await this.axiosInstance.post('/apis', postBody);
if (status != 200) { throw '聚寬數據請求失敗' }
if (headers['content-type'].includes('text/plain')) {
if (data.includes('error')) {
this.token = null;
throw data
} else {
if (params.method == 'get_token') {
this.token = data;
return data;
} else {
//Parse csv plain text to json
return await csvtojson().fromString(data);
}
}
}
}
async refreshToken() {
if (this.isRefreshToken) { return }
this.isRefreshToken = true;
const params = {
"method": "get_token",
"mob": "你的電話號碼",
"pwd": "你的密碼"
}
const token = await this._sendRequest(params);
console.log('取得Token:', token);
this.isRefreshToken = false;
}
async getSymbolPrice(symbol = "A2005.XDCE", period = "60m", count = 100, endDate) {
const date = (new Date()).toLocaleDateString()
const arr = date.split('/');
const params = {
"method": "get_bars",
"code": symbol,
"count": count,
"unit": period,
"end_date": endDate
}
const price = await this._sendRequest(params);
return price
}
}
module.exports = JQService;
這部分代碼其實比較簡單,提供了一個單例來獲取聚寬的數據。聚寬API都是使用同一個接口,然後通過在post body中寫上具體的方法,來獲取特定接口的數據。稍微需要注意點就是token的處理,會在沒有token或者是token過期的時候先獲取token。
聚寬返回的數據最開始我沒注意文檔,後來發現大部分結果都是csv的格式。在程序處理上直接使用csv數據不是太方便,因此需要把它轉爲json的格式。在各個語言平臺下應該都有相因的工具能轉化就不多說了。
接下來是代碼的入口文件的主要代碼index.js
const JQService = require('./你的路徑/jqService');
(async function main() {
await JQService.getInstance().getSymbolPrice()
})();
然後附上Dockerfile的內容:
FROM node:10.15.3
RUN mkdir -p /workspace
COPY . /workspace
WORKDIR /workspace
RUN npm install
ENTRYPOINT ["node"]
CMD ["src/index.js"]
對了,在項目的根目錄裏的package.json中記得添加代碼中依賴的比如axios等第三方庫進去。
接下來就是構建docker鏡像
docker build -t jqdemo .
接下來你可以使用這個鏡像
docker run --name jqdemo-container jqdemo
整個過程還是比較簡單的,如果有疑問歡迎來交流。