github地址
https://github.com/drip-trader/dripjs
結(jié)構(gòu)圖:
功能
- 負(fù)責(zé)獲取數(shù)據(jù)的功能項(xiàng)目
platform使用方法
- import
import { IntelFactory, BitmexSpy } from 'dripjs';
// 生成指定交易所的數(shù)據(jù)接口
const bitmexSpy = IntelFactory.create(BitmexSpy, {
apiKey: `你的bitmex交易所apiKey`,
apiSecret: `你的bitmex交易所apiSecret`,
testnet: true,
});
const pair = 'XBTUSD';
// 訂閱逐筆數(shù)據(jù)
bitmexSpy.getTicker$(pair).subscribe((res) => {
console.log(res);
});
setTimeout(() => {
console.log('do stop ticker subscription');
// 不用時(shí)动分,別忘記退訂
bitmexSpy.stopTicker(pair);
}, 5000);
- require
const dripjs = require("dripjs")
// 生成指定交易所的數(shù)據(jù)接口
const bitmexSpy = dripjs.IntelFactory.create(dripjs.BitmexSpy, {
apiKey: `你的bitmex交易所apiKey`,
apiSecret: `你的bitmex交易所apiSecret`,
testnet: true,
})
const pair = 'XBTUSD';
// 訂閱逐筆數(shù)據(jù)
bitmexSpy.getTicker$(pair).subscribe((res) => {
console.log(res)
});
setTimeout(() => {
console.log('do stop ticker subscription');
// 不用時(shí)毅糟,別忘記退訂
bitmexSpy.stopTicker(pair);
}, 5000);
以上方法可以通過此鏈接,在線測(cè)試
輸出結(jié)果
輸出結(jié)果中,high和low表示的是當(dāng)天最高價(jià)格和最低價(jià)格
顯示為0的原因是澜公,bitmex交易所不提供這樣的websocket數(shù)據(jù)姆另,不要說是bug呀??
程序構(gòu)造
API接口
目前只開發(fā)了5個(gè)主要接口,以后根據(jù)需要還會(huì)繼續(xù)增加接口
方法 | 方式 | 說明 |
---|---|---|
getSymbols | 一次 | 獲取全部商品信息 |
getSymbol | 一次 | 獲取指定商品信息 |
getBars | 一次 | 獲取 K 線 |
getTicker$ | 實(shí)時(shí) | 獲取逐筆情報(bào) |
getDepth$ | 實(shí)時(shí) | 獲取訂單薄 |
getTransaction$ | 實(shí)時(shí) | 獲取成交信息 |
library使用方法
使用方法和上面platform使用方法一樣坟乾,只不過安裝解耦的子模塊:
npm install dripjs-intelligence
然后在import
引入時(shí)模塊名稱將dripjs
框架模塊改為dripjs-intelligence
import { IntelFactory, BitmexSpy } from 'dripjs-intelligence';
// 生成指定交易所的數(shù)據(jù)接口
const bitmexSpy = IntelFactory.create(BitmexSpy, {
...
tool使用方法
(一)服務(wù)端使用
-
結(jié)構(gòu)圖
有些同學(xué)以為在墻內(nèi)迹辐,無法通過library正常使用,需要通過翻墻手段甚侣。
drip.js提供tool的方式明吩,只需租用墻外服務(wù)器,通過以下命令安裝數(shù)據(jù)服務(wù):
1殷费、安裝新建空nodejs項(xiàng)目印荔,通過npm安裝工具包:
npm install dripjs-intelligence
2、package.json
的 scripts
中加入:
"scripts": {
"service": "intel-service"
},
3详羡、設(shè)置數(shù)據(jù)服務(wù)的配置文件
- 項(xiàng)目根目錄中創(chuàng)建
config
文件夾仍律,在里面新建default.json
,并在里面配置如下內(nèi)容:
{
"container": {
"intelService": {
"port": 6531,
"username": "test",
"password": "test"
}
},
"exchange": {
"crypto": {
"bitmex": {
"apiKey": "zzz",
"apiSecret": "xxx"
},
"bitmexTestNet": {
"apiKey": "ccc",
"apiSecret": "cccx"
}
}
}
}
intelService
中的配置項(xiàng)殷绍,port
為服務(wù)器開啟的端口染苛,username
、password
是客戶端連接時(shí)需要傳遞的用戶名
和密碼
主到。
exchange
中為各交易所的apikey
及密碼
,只需配置想要用的交易所即可茶行。
4、運(yùn)行數(shù)據(jù)服務(wù)器
npm run service
-
運(yùn)行圖片
(二)客戶端使用
import { IntelClient } from 'dripjs-intelligence';
import { SupportedExchange } from 'dripjs-types';
async function test() {
const client = new IntelClient({
ip: '127.0.0.1',
port: 6531,
username: 'test',
password: 'test',
});
const exchange = SupportedExchange.Bitmex;
const pair = 'XBTUSD';
// 獲取商品列表
const symbols = await client.getSymbols(exchange);
console.log('symbols: ', JSON.stringify(symbols));
// 訂閱單商品數(shù)據(jù)
client.ticker$(exchange, pair).subscribe((res) => {
console.log('ticker: ', JSON.stringify(res));
});
setTimeout(() => {
// 不需要數(shù)據(jù)時(shí)登钥,別忘記退訂
client.stopTicker(exchange, pair);
}, 3000);
// 訂閱深度數(shù)據(jù)
client.depth$(exchange, pair).subscribe((res) => {
console.log('depth: ', JSON.stringify(res));
});
setTimeout(() => {
client.stopDepth(exchange, pair);
}, 3000);
// 訂閱成交數(shù)據(jù)
client.transaction$(exchange, pair).subscribe((res) => {
console.log('transaction: ', JSON.stringify(res));
});
setTimeout(() => {
client.stopTransaction(exchange, pair);
}, 3000);
setTimeout(() => {
// 不用時(shí)別忘記斷開與服務(wù)器的連接
client.disconnect();
}, 6000);
}
test().catch(e => {
console.log('error: ', e.message);
});
IntelClient
的構(gòu)造函數(shù)中的username
和password
要和IntelServer的username
和password
一致畔师,這樣可以保證自己的數(shù)據(jù)服務(wù)不被其他人盜用。
以上示例中為現(xiàn)有實(shí)現(xiàn)功能牧牢,之后會(huì)隨時(shí)補(bǔ)充看锉。
待實(shí)現(xiàn)功能
- 指定多種商品,同時(shí)訂閱
例如
import { IntelFactory, BitmexSpy } from 'dripjs';
bitmexSpy.getTicker$('XBTUSD', 'ADAH19').subscribe((res) => {
console.log(res);
});
輸出:
{
"XBTUSD": {
"ask": 3804,
"bid": 3803.5,
...
},
"ADAH19": {
...
}
}