LoginSignup
0
1

More than 5 years have passed since last update.

cryptowat APIからいろんな取引所のbitcoin価格を取得する

Posted at

cryptowat APIからいろんな取引所のbitcoin価格を取得するコード

import requests,time
from datetime import datetime,timedelta
import pandas as pd

def get_price(before, after, periods, strExchange):
    url = 'https://api.cryptowat.ch/markets/' + strExchange + '/ohlc'
    query = {
        'before':before,
        'after':after,
        'periods':periods,
        }
    res = requests.get(url ,params=query).json()['result'][periods]
    return res

def get(url='https://api.cryptowat.ch/pairs/btcusd'):
    res = requests.get(url).json()
    return res

unixTime = lambda y,m,d,h: int(time.mktime(datetime(y,m,d,h).timetuple()))

list_markets=get(url='https://api.cryptowat.ch/pairs/btcusd')['result']['markets']
list_markets_cur_exhange=[]
for ret in list_markets:
    list_markets_cur_exhange.extend([ret['exchange']+r'/'+ret['pair']])

# now = datetime.now()
# print get_price(unixTime(now.year,now.month,now.day,now.hour), unixTime(now.year,now.month,now.day,now.hour), u'3600', u'bitfinex/btcusd')[0][4]


data = []
i = 0
strExchangeList = list_markets_cur_exhange
now = datetime(2018, 9, 24, 11)

y,m,d,h = now.year, now.month, now.day, 11

NewDate = now - timedelta(days=160)
strDate = NewDate.strftime('%Y/%m/%d %H:%M:%S')

y2,m2,d2,h2 = NewDate.year, NewDate.month, NewDate.day, 11
df=pd.DataFrame(columns=list_markets_cur_exhange)
for eachEchange in strExchangeList:
    ret = get_price(unixTime(y,m,d,h), unixTime(y2,m2,d2,h2), u'86400', eachEchange)
    ret=pd.DataFrame(ret, columns=['CloseTime', 'OpenPrice', 'HighPrice', 'LowPrice', 'ClosePrice', '_','Volume'])
    ret=ret.set_index('CloseTime',drop=True)
    df[eachEchange]=ret['ClosePrice']

df['utc']=0
for each in df.index:
    df.loc[each,'utc']=datetime.utcfromtimestamp(each)
df=df.set_index('utc', drop=False)
print(df)
df.to_csv('out.csv')
0
1
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
1