WEB系エンジニアの仮想通貨ブログ

仮想通貨初めてみました。

bitFlyerのSFDを表示するスクリプト (JavaScript)

bitflyer のSFDが酷いらしいので、どれぐらいで推移しているかをリアルタイムで確認できるようにスクリプトを書いて見ました。
 
APIは以下のように公開されているので、ここをちょろっといじるだけでできてしまいます。
ビットコイン取引所【bitFlyer Lightning】
 

こんな感じ

const PubNub = require('pubnub');
const pubnub = new PubNub({
  subscribeKey: 'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f'
});
const CN_EXECUTION    = 'lightning_executions_BTC_JPY';
const CN_EXECUTION_FX = 'lightning_executions_FX_BTC_JPY';


let price, fxPrice;
pubnub.addListener({
  message: function (message) {
    if (message.channel === CN_EXECUTION) {
      price = message.message[0].price;
    } else {
      fxPrice = message.message[0].price;
    }
    printSfd();
  }
});

pubnub.subscribe({
  channels: [CN_EXECUTION, CN_EXECUTION_FX]
});

function printSfd() {
  if (!price || !fxPrice) {
    return;
  }
  const sfd = floor((fxPrice / price - 1) * 100, 3);
  const title = tiltleMark(sfd);
  console.log(title, sfd);
}

function floor(val, digit) {
  const floorNum = Math.pow(10, digit);
  return (Math.floor(val * floorNum) / floorNum);
}

function tiltleMark(sfd) {
  let mark = '    ';
  if (sfd > 10) {
    mark = '■■■■';
  } else if (sfd > 9.9) {
    mark = '□□□□';
  }
  return mark;
}
$ node index.js
     9.883
     9.894
□□□□ 9.996
□□□□ 9.996
□□□□ 9.996
□□□□ 9.996
     9.895
     9.895
     9.896
     9.897
     9.897
     9.897
     9.897
□□□□ 9.914
□□□□ 9.922
□□□□ 9.922
□□□□ 9.905