🐍

初めての cryptowatch-sdk

2021/03/21に公開

cryptowatch-sdk

https://github.com/cryptowatch/cw-sdk-python

install

  • python 3.8 で 仮想環境作成後
$ pip install cryptowatch-sdk
$ pip install pandas 
$ pip install jupyter
$ pip install matplotlib

example

import cryptowatch as cw
import pandas as pd
from datetime import datetime, timedelta
# 取得できるマーケットを確認
cw.markets.list()

# <MarketAPIResponse([<Market(bitfinex:btcusd)>, 
# <Market(bitfinex:ltcusd)>, <Market(bitfinex:ltcbtc)>,
# <Market(bitfinex:ethusd)>, <Market(bitfinex:ethbtc)>,
# <Market(bitfinex:etcbtc)>, <Market(bitfinex:etcusd)>,
# <Market(bitfinex:bfxbtc)>, <Market(bitfinex:bfxusd)>,..........
# Kraken 取引所のマーケットリストを全部取得
kraken = cw.markets.list("kraken")
# マーケットリスト
kraken.markets
# [<Market(kraken:btceur)>,
#  <Market(kraken:btcusd)>,
#  <Market(kraken:btcgbp)>,
#  <Market(kraken:btcjpy)>,
#  ...

# 一つマーケットを確認してみる
mkt = kraken.markets[0]
ex, pair = mkt.exchange, mkt.pair

# 1分足
ticker = f"{ex.upper()}:{pair.upper()}"
candles = cw.markets.get(ticker, ohlc=True, periods=["1M"])
# 1分足で作ると、.of_1m というアトリビュートが作成されて
# [close_timestamp, open, high, low, close, volume, volume_quote]
# が取れる
candles.of_1m[:3]

# [[1616253060.0,
#   49977.3,
#   50000.0,
#   49977.2,
#   49992.9,
#   1.68291233,
#   84136.900202649],
#  [1616253120.0, 49992.8, 49999.8, 49981.6, 49992.7, 0.59392539, 29692.7586749],
#  [1616253180.0,
#   49992.6,
#   49996.6,
#   49980.0,
#   49980.0,
#   0.38491781,
#   19239.863931146]]

# pandas dataframe に入れる
df = pd.DataFrame(candles.of_1m, 
                  columns=["close_timestamp", "open", "high", "low", "close", "volume", "volume_quote"],
                 )
df["close_timestamp"] = df["close_timestamp"].apply(datetime.utcfromtimestamp)
df = df.set_index("close_timestamp")
df.tail()

                        open     high      low    close    volume  \
close_timestamp                                                     
2021-03-21 07:47:00  48152.2  48152.2  48129.9  48129.9  0.459454   
2021-03-21 07:48:00  48130.0  48133.3  48130.0  48130.0  0.030087   
2021-03-21 07:49:00  48129.9  48131.4  48115.0  48115.0  0.057104   
2021-03-21 07:50:00  48115.1  48115.1  48050.2  48050.2  2.548961   
2021-03-21 07:51:00  48050.1  48067.0  48050.1  48062.2  0.614476   

                      volume_quote  
close_timestamp                     
2021-03-21 07:47:00   22113.690407  
2021-03-21 07:48:00    1448.133279  
2021-03-21 07:49:00    2748.319570  
2021-03-21 07:50:00  122510.591164  
2021-03-21 07:51:00   29528.191528  

(続きはあとで書く)

Discussion