LoginSignup
3

More than 3 years have passed since last update.

[python] データ数を指定してCryptowatchからデータを取得する Get a specified num of data from Cryptowatch

Last updated at Posted at 2018-06-11

Cryptowatchから(a)のようにデータをリクエストすると500データしか取得できないので、500データ以上取得するためのコード+pandasに落とし込むコードを書いたのでシェアします。
If you send request code to cryptowatch like below(a), you get 500 data but I assume a lot of people want more data. so I am going to share the code to get more than 500 data at a time and put them in padas dataframe.

参考

Cryptowatch API: https://cryptowat.ch/docs/api#price

(a) 500データ取得時 When getting 500 data
※paramsには欲しい時間足を入れます (e.g 1分足 = "60", 5分足 = "300")

price.py
import json
import requests
price = json.loads(requests.get("https://api.cryptowat.ch/markets/bitflyer/btcfxjpy/ohlc",params="60").text)["result"]
print(price)

price.py実行結果 result of execution
3.png

(b)全体のコード the whole code
6000 data from Cryptowatch -> remove unnecessary data -> pandas

crypto.py
import json
import requests
import pandas as pd
import datetime
import time

def crypto_watch(candle,before,size):
    url="https://api.cryptowat.ch/markets/bitflyer/btcfxjpy/ohlc?periods="
    candle=candle*60
    period=("%s"%(candle))
    period=[period]
    beforeurl="&before="
    afterurl="&after="
    #The maximum size of data is 6000 depending on a timing (sometimes you get less than 6000)
    after= before-(candle*size)
    URL=("%s%s%s%s%s%s"%(url,candle,afterurl,after,beforeurl,before))
    #e.g https://api.cryptowat.ch/markets/bitflyer/btcjpy/ohlc?periods=86400&after=1483196400
    res=json.loads(requests.get(URL).text)["result"]
    data = []
    for i in period:
        row = res[i]
        for column in row:
            if column[4] != 0:
                column = column[0:6]
                data.append(column)
    date = [price[0] for price in data]
    priceOpen = [int(price[1]) for price in data]
    priceHigh = [int(price[2]) for price in data]
    priceLow = [int(price[3]) for price in data]
    priceClose = [int(price[4]) for price in data]
    date_datetime = map(datetime.datetime.fromtimestamp, date)
    dti = pd.DatetimeIndex(date_datetime)
    df_candleStick = pd.DataFrame({"open" : priceOpen, "high" : priceHigh, "low": priceLow, "close" : priceClose}, index=dti)
    return df_candleStick

print(crypto_watch(1,round(time.time()),6000))

crypto.py 実行結果 result of execution
6_11.png

このモジュールの使い方 How to use this module
crypto_watch(candle,before,size)
candle = 時間足 (e.g 1分足 = 1, 5分足 = 5) Timeframe of candlestick (e.g 1min = 1, 5min =5)
before = 今のUNIXTIME current unixtime
size = data size (e.g 6000 data = 6000)

他にもきれいな書き方、もしくは他のコードを使用した取得方法がありそうなので、ご存じの方がいらっしゃいましたらコメントを頂ければ幸いです。
Please leave your comment if there is any ways to make the code better or any other ways to get more than 500 data
because it does not look like a perfect to me in some ways.

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
3