功能項(xiàng)目(一)intelligence數(shù)據(jù)獲取

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');
  // 不用時,別忘記退訂
  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');
  // 不用時,別忘記退訂
  bitmexSpy.stopTicker(pair);
}, 5000);

以上方法可以通過此鏈接,在線測試

輸出結(jié)果

輸出結(jié)果中,high和low表示的是當(dāng)天最高價格和最低價格
顯示為0的原因是,bitmex交易所不提供這樣的websocket數(shù)據(jù),不要說是bug呀??

程序構(gòu)造

API接口

目前只開發(fā)了5個主要接口,以后根據(jù)需要還會繼續(xù)增加接口

方法 方式 說明
getSymbols 一次 獲取全部商品信息
getSymbol 一次 獲取指定商品信息
getBars 一次 獲取 K 線
getTicker$ 實(shí)時 獲取逐筆情報
getDepth$ 實(shí)時 獲取訂單薄
getTransaction$ 實(shí)時 獲取成交信息

library使用方法

使用方法和上面platform使用方法一樣,只不過安裝解耦的子模塊:

npm install dripjs-intelligence

然后在import引入時模塊名稱將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.jsonscripts中加入:

  "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ù)器開啟的端口,usernamepassword是客戶端連接時需要傳遞的用戶名密碼。
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ù)時,別忘記退訂
    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(() => {
    // 不用時別忘記斷開與服務(wù)器的連接
    client.disconnect();
  }, 6000);
}

test().catch(e => {
  console.log('error: ', e.message);
});

IntelClient的構(gòu)造函數(shù)中的usernamepassword要和IntelServer的usernamepassword一致,這樣可以保證自己的數(shù)據(jù)服務(wù)不被其他人盜用。
以上示例中為現(xiàn)有實(shí)現(xiàn)功能,之后會隨時補(bǔ)充。

待實(shí)現(xiàn)功能

  • 指定多種商品,同時訂閱
    例如
import { IntelFactory, BitmexSpy } from 'dripjs';

bitmexSpy.getTicker$('XBTUSD', 'ADAH19').subscribe((res) => {
  console.log(res);
});

輸出: 
{
    "XBTUSD": {
        "ask": 3804,
        "bid": 3803.5,
         ...
    },
    "ADAH19": {
        ...
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容