0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pyhonのccxtについてのまとめ

0
Posted at

ccxt(CryptoCurrency eXchange Trading Library)とは

暗号資産(仮想通貨)取引所のAPIを共通インターフェースで扱えるPythonライブラリです。
1つの書き方で Binance / Bybit / OKX / Bitfinex / Coinbase など多数の取引所に対応できます。

1. ccxtで何ができる?

・仮想通貨の 価格取得(Ticker / OHLCV)
・板情報(Order Book)の取得
・残高確認
・注文(成行・指値)
・複数取引所を同じコードで切り替え
・自動売買(ボット)開発

2. 対応取引所

bitFlyer
Coincheck
bitbank
Zaif
など海外を含め 100以上 の取引所に対応しています。
※GMOcoinは対応していませんでした。

テストコード

import ccxt
import matplotlib.pyplot as plt
import numpy as np

# 日本国内の取引所
exchanges = {
    "bitFlyer": ccxt.bitflyer(),
    "Coincheck": ccxt.coincheck(),
    "bitbank": ccxt.bitbank(),
}

symbol = "BTC/JPY"
prices = {}

for name, exchange in exchanges.items():
    try:
        exchange.enableRateLimit = True
        exchange.load_markets()

        if symbol in exchange.symbols:
            ticker = exchange.fetch_ticker(symbol)
            prices[name] = ticker["last"]

    except Exception as e:
        print(f"{name}: {e}")

# ===== 平均価格 =====
avg_price = np.mean(list(prices.values()))

# ===== 平均からの差分 =====
diffs = {k: v - avg_price for k, v in prices.items()}

# ===== グラフ表示 =====
plt.figure()
plt.bar(diffs.keys(), diffs.values())
plt.axhline(0)
plt.title("BTC/JPY Price Difference from Average (Japan Exchanges)")
plt.xlabel("Exchange")
plt.ylabel("Difference from Avg (JPY)")
plt.show()

コードの解説

対象通貨ペアと価格格納用変数

symbol = "BTC/JPY"
prices = {}

symbol
→ ビットコイン/円(日本取引所向け)
prices
→ 各取引所のBTC価格を保存する辞書

各取引所から価格を取得

for name, exchange in exchanges.items():

レート制限対応

exchange.enableRateLimit = True
exchange.load_markets()

・API制限超過を防ぐ設定
・load_markets()
→ その取引所が扱っている通貨ペア一覧を取得

通貨ペアが存在するか確認

if symbol in exchange.symbols:

・取引所によっては BTC/JPY が存在しない可能性があるため

最新価格(last)を取得

ticker = exchange.fetch_ticker(symbol)
prices[name] = ticker["last"]

fetch_ticker()
→ 現在の価格情報を取得

ticker["last"]
→ 直近の約定価格(最後に成立した価格)

prices に保存

実行結果

スクリーンショット 2025-12-16 7.04.36.png

各取引所の解釈

🔵 bitFlyer
約 −13,000円
他の取引所平均より かなり安い
「割安にBTCが買える」可能性がある

🔵 Coincheck
ほぼ 0(+数百円)
ほぼ平均価格
日本市場の「基準的な価格」

🔵 bitbank
約 +13,000円
平均より かなり高い
「高く売れる」可能性がある

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?