WindowsサーバにInfluxDB+Chronografをインストールしてbitflyerのティッカーを可視化する
やってることは下記ページそのままだけど、Grafana→Chronograf、pybitflyer→ccxtに変えている。
InfluxDBのインストール手順は下記ページを参考にした。
InfluxDBのインストール
下記のURLから必要なツールをダウンロードする。
解凍して実行ファイルを適当な場所においてパスを通しておく(influxdb、chronografをまとめて下記に置いた)。
C:\Program Files\InfluxDB
InfluxDBの起動
設定ファイルを修正してデータベースの置き場所を変える。
データベースの場所は F:\bitcoin\influxdb、設定ファイルはF:\bitcoin\influxdb\influxdb.confに置いた。
code:influxdb.conf
# Where the metadata/raft database is stored
# dir = "/var/lib/influxdb/meta"
dir = "F:\\bitcoin\\influxdb\\meta"
# The directory where the TSM storage engine stores TSM files.
# dir = "/var/lib/influxdb/data"
dir = "F:\\bitcoin\\influxdb\\data"
# The directory where the TSM storage engine stores WAL files.
# wal-dir = "/var/lib/influxdb/wal"
wal-dir = "F:\\bitcoin\\influxdb\\wal"
修正した設定ファイルを指定してInfluxDBを起動する
$ influxd --config F:\bitcoin\influxdb\influxdb.conf
InfluxDBへのtickerデータの書き込み
tickerデータを貯めるデータベースを作成するため、CLIツールを起動してSQLコマンドを打ち込む(ChronografのWebUIからでもこの操作はできるみたい)。
$ influx
code:sql
CREATE DATABASE bitflyer
InfluxDBへの書き込みはPythonから行う。Python向けのクライアントライブラリとccxtをインストールする。
$ pip install ccxt
$ pip install influxdb
以下のコードを動かすと1秒周期でtickerを書き込む。
code:ticker.py
from time import sleep
from influxdb import InfluxDBClient
import ccxt
client = InfluxDBClient("localhost", "8086", 'root', 'root', 'bitflyer')
api = ccxt.bitflyer()
while True:
ticker = api.public_get_getticker(params={'product_code':'FX_BTC_JPY'})
print(ticker)
json_body = [{
"measurement": "lightning_ticker_FX_BTC_JPY",
"fields":ticker}]
client.write_points(json_body)
sleep(1)
Chronografの起動とInfluxDB接続
chronografを起動(起動したディレクトリに何らかの設定ファイルが保存される様子)。
$ cd F:\bitcoin\influxdb\
$ chronograf
Chronografのページをブラウザで開く
InfluxDBの接続設定(初期値のままでOK)を入力して、Dashboards・Kapacitorの設定はスキップ、
https://gyazo.com/6022bfe8b304041a30c444800040e82a
ダッシュボードにtickerグラフを追加
Dashboards→Create Dashboard→Add Data→Queries→bitflyer.autogen→lightning_ticker_FX_BTC_JPY→best_ask/best_bidを選択。Vitualizationタブに移動してLineを選択し、右上のチェックマークを押す。
https://gyazo.com/297265d1815db290943eb8ae607788d2
以上。