【仮想通貨の自動売買を目指して】PythonでbitFlyer API

2021.09.28
2024.03.24
仮想通貨
bitFlyerPython

はじめに

仮想通貨の自動売買に向けて、PythonからAPIでbitFlyerの売買ができるようにします。

bitFlyerでの自動売買を目指すので、bitFlyer APIについてとbitFlyer APIをPythonで動かすときの便利なライブラリの紹介をします。

前回は過去のチャートの取得を行いました。

【仮想通貨の自動売買を目指して】過去チャート取得

【仮想通貨の自動売買を目指して】過去チャート取得

はじめに 仮想通貨の自動売買に向けて、まずはとにかくデータを集めます。 今回は**過去のチャー

bitFlyer API

bitFlyer APIは主に4種類公開されています。

  • bitFlyer Lightning API : レート情報や取引ができるAPI
  • bitFlyer Lightning API Playground : bitFlyer Lightning APIを試せるサービス
  • chainFlyer API : ブロックチェーンのトランザクションやアドレスを取得できるAPI
  • Echo API : 法人向けのビットコイン送付サービス
API | 暗号資産(仮想通貨)ビットコイン(Bitcoin)の購入/販売所/取引所【bitFlyer(ビットフライヤー)】

API | 暗号資産(仮想通貨)ビットコイン(Bitcoin)の購入/販売所/取引所【bitFlyer(ビットフライヤー)】

bitFlyer(ビットフライヤー)では開発者向けに各種APIを公開しています。

今回主に利用するのはbitFlyer Lightning APIになります。

bitFlyer Lightning API

bitFlyer Lightning APIでは仮想通貨のレートの情報や取引などが可能です。

このAPIは、APIキーの認証が不要なPublic API認証が必要なPrivate APIがあります。

Bitcoin Exchange - bitFlyer Lightning

Bitcoin Exchange - bitFlyer Lightning

bitFlyer Lightning is an exchange (trading platform) where users can trade bitcoins. The exchange rates are determined based on the balance between the supply and demand. Users can sell JPY and obtain bitcoins in exchange, or they can sell bitcoins in exchange for JPY. The practice is similar to retail foreign exchange (FX) trading.

bitFlyer Lightning APIを利用する準備

bitFlyer APIを利用するためにはAPI KeyとAPI Secretを作成する必要があります。

bitFlyer Lightningにログインし、メニューから「API」を選択します。

「新しいAPIキーを追加」をクリックし、APIで利用できる権限を選択して、APIキーを作成します。

API KeyとAPI Secretの取り扱いには注意しましょう。

PythonでbitFlyer API

PythonでbitFlyer APIを利用する場合は、pybitflyerが便利です。

GitHub - yagays/pybitflyer: Python wrapper for bitFlyer's REST API.

GitHub - yagays/pybitflyer: Python wrapper for bitFlyer's REST API.

Python wrapper for bitFlyer's REST API. Contribute to yagays/pybitflyer development by creating an account on GitHub.

2021年9月時点で最後のコミットが4年前となっており、あまりメンテナンスはされていないのかもしれませんが、問題なく使えそうです。使えない場合は、requestなどを利用してAPIをそのまま叩く必要があります。

pybitflyerの使い方

以下のように使います。API KeyとAPI Secretは環境変数から取得しています。

1import pybitflyer
2import os
3
4api_key = os.environ.get('API_KEY')
5api_secret = os.environ.get('API_SECRET')
6api = pybitflyer.API(api_key=api_key, api_secret=api_secret)

Public APIのみを使う場合は、api_keyapi_secretはなくても問題ありません。

インスタンスから以下のようにbitFlyer APIが使えるようになっています。

1ticker = api.ticker(product_code="BTC_JPY")
2
3board = api.board(product_code="BTC_JPY")

下記のコードを見ると使えるAPIと必要なパラメータがわかります。

pybitflyer/pybitflyer/pybitflyer.py at master · yagays/pybitflyer

pybitflyer/pybitflyer/pybitflyer.py at master · yagays/pybitflyer

Python wrapper for bitFlyer's REST API. Contribute to yagays/pybitflyer development by creating an account on GitHub.

それぞれのAPIでできることを確認するときは、bitFlyer APIのドキュメントを確認します。

Bitcoin Exchange - bitFlyer Lightning

Bitcoin Exchange - bitFlyer Lightning

bitFlyer Lightning is an exchange (trading platform) where users can trade bitcoins. The exchange rates are determined based on the balance between the supply and demand. Users can sell JPY and obtain bitcoins in exchange, or they can sell bitcoins in exchange for JPY. The practice is similar to retail foreign exchange (FX) trading.

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee