Created
November 6, 2020 10:12
-
-
Save yuyasugano/200ab7e250b85224382751dcc30dce34 to your computer and use it in GitHub Desktop.
Coefficient variable for crypto assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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