LoginSignup
14
23

More than 5 years have passed since last update.

Python - bitflyerから一定間隔でビットコインレートBTC/JPYを取得してファイルに保存

Posted at

Bitflyerから一定間隔でビットコインレートを取得してファイルに保存

pybitflyerのインストール

pip install pybitflyer

API Key

登録後以下から取得可能
https://lightning.bitflyer.jp/developer

bitFlyer Lightning API

・制限
https://lightning.bitflyer.jp/docs?lang=ja&_ga=2.103916546.34290189.1503318908-1417182779.1500704462

API制限

HTTP API は、以下のとおり呼出回数を制限いたします。

Private API は 1 分間に約 200 回を上限とします。
IP アドレスごとに 1 分間に約 500 回を上限とします。


1 日の平均約定単価が 0.01 未満のユーザーは、翌日の Private API 呼出回数が 1 分間に約 10 回まで制限されることがあります。
マニュアル発注は制限されません。

・Tickerの取得

ソース

5秒間隔で取得

import pybitflyer
import time
from datetime import datetime
from pytz import timezone
from dateutil import parser
import pytz

TICKER_KEYS = ["product_code", 
                "timestamp", 
                "tick_id", 
                "best_bid", 
                "best_ask",
                "best_bid_size",
                "best_ask_size",
                "total_bid_depth",
                "total_ask_depth",
                "ltp",
                "volume",
                "volume_by_product"]

API_KEY = "XXXXXXXXXXXXXXXXXXXXXX" # Bitflyerログイン後のマイページから取得
API_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXX" # Bitflyerログイン後のマイページから取得

def to_jst(datestr):
    return pytz.timezone('UTC').localize(parser.parse(datestr)).astimezone(timezone('Asia/Tokyo'))

if __name__ == '__main__':
    api = pybitflyer.API(api_key = API_KEY, api_secret = API_SECRET)

    now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    ticker_data = open(now + "_ticker_data.tsv", "w")
    for ticker_key in TICKER_KEYS:
        ticker_data.write(ticker_key + "\t")
    ticker_data.write("\n")
    while True:
        ticker = api.ticker(product_code="BTC_JPY")
        for ticker_key in TICKER_KEYS:
            if ticker_key == "timestamp":
                data = to_jst(str(ticker[ticker_key]).replace("T", " ")).strftime("%Y/%m/%d %H:%M:%S")
            else:
                data = str(ticker[ticker_key])
            ticker_data.write(data + "\t")
        ticker_data.write("\n")
        ticker_data.flush()
        time.sleep(5)
    ticker_data.close()

結果

cap.PNG

備考

結構接続エラーで落ちるため、ちゃんとやるならエラー対策が必要。

14
23
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
23