Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coefficient variable for crypto assets
import time
import json
import datetime
import requests
import numpy as np
import pandas as pd
headers = {'Content-Type': 'application/json'}
api_url_base_coingecko = 'https://api.coingecko.com/api/v3/coins/'
vs_currency = 'usd'
days = '30'
def getCoinData(ids, vs, days):
api_url_coingecko = '{0}/{1}/ohlc?vs_currency={2}&days={3}'.format(api_url_base_coingecko, ids, vs, days)
res = requests.get(api_url_coingecko, headers=headers)
if res.status_code == 200:
df = pd.read_json(res.content.decode('utf-8'))
df.columns = ['timestamp', 'Open', 'High', 'Low', 'Close']
df['timestamp'] = df['timestamp']/1000
df['timestamp'] = df['timestamp'].astype(int)
df['timestamp'] = df['timestamp'].map(datetime.datetime.utcfromtimestamp)
df.index = df['timestamp']
feature_name = ['Close']
df = df[feature_name]
df.interpolate(limit_direction='both', inplace=True)
df.columns = [ids]
return df
else:
return None
coins = ['bitcoin', 'ethereum', 'chainlink', 'ripple', 'litecoin', 'polkadot']
df = pd.DataFrame()
for coin in coins:
df = pd.concat([df, getCoinData(coin, vs_currency, days)], axis=1, sort=True, join='outer')
df.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment