LoginSignup
7
8

More than 5 years have passed since last update.

CryptoCompare API で仮想通貨の日毎の価格を取得する

Posted at

漢なら, 仮想通貨の価格ヒストリを取得したいですね!

やりましょう!

CryptCompare API を使う

"cryptocurrency historical price" などで検索すると, CryptoCompare API がおすすめ, みたいな結果が帰ってきます.

なんだかいろいろデータが取れそうでわくわくしますね!

API limit も 6000 回/時間なので普通に利用する場合では制限にひっかかることはないでしょう.

daily の価格を取得する.

limit で取得したい日の数を指定します.
toTs を指定した場合, toTs で指定した日付(unixtime)から, limit
日ぶんを遡って取得します.

fsym, tsym のシンボルは大文字で指定する必要があります.

CCCAGG は CryptoCompare Current Aggregate の略でした.

python requests を使って, python
で ZEC/JPY の日毎の価格を取得してみます.

#!/usr/bin/env python
# 
import json
import os
import sys
import datetime
import requests

def main():
    payload={'fsym': "ZEC", 'tsym': "JPY", 'limit':365}
    r = requests.get("https://min-api.cryptocompare.com/data/histoday", params=payload)
    j = r.json()
    print(j["Response"])
    if j["Response"] == "Success":
        for d in j["Data"]:
            timestamp = d["time"]
            open_price = d["open"]

            textualdate = datetime.datetime.fromtimestamp(timestamp)
            print("{0}, {1}, open = {2}".format(timestamp, textualdate, open_price))

main()
Success
1486857600, 2017-02-12 09:00:00, open = 3735.53
1486944000, 2017-02-13 09:00:00, open = 3662.07
1487030400, 2017-02-14 09:00:00, open = 3571.5
1487116800, 2017-02-15 09:00:00, open = 3791.85
1487203200, 2017-02-16 09:00:00, open = 3984.13
1487289600, 2017-02-17 09:00:00, open = 3831.4

Voila! 日毎の価格の取得に成功しました!

TODO

  • timezone を考慮する
  • hourly の価格を取得する.
  • 取引所ごとの価格の平均をとってみる
7
8
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
7
8