-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata.py
51 lines (36 loc) · 1.42 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
import pandas as pd
from datetime import datetime, timedelta
def get_price_data(api_key, coin_id, days):
"""
Retrieves historical price data from the CoinGecko API.
Args:
api_key (str): Your CoinGecko API key.
coin_id (str): The ID of the cryptocurrency to retrieve data for.
days (int): The number of days of historical data to retrieve.
Returns:
pandas.DataFrame: A DataFrame containing the historical price data.
"""
today = datetime.now()
start_date = (today - timedelta(days=days)).strftime('%d-%m-%Y')
end_date = today.strftime('%d-%m-%Y')
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={start_date}&to={end_date}"
response = requests.get(url, headers={"Accept": "application/json", "X-CoinGecko-API-Key": api_key})
data = response.json()
prices = data['prices']
df = pd.DataFrame(prices, columns=['time', 'price'])
df['time'] = pd.to_datetime(df['time'], unit='ms')
df['price'] = pd.to_numeric(df['price'])
df.set_index('time', inplace=True)
return df
def calculate_returns(prices):
"""
Calculates daily returns from a DataFrame of prices.
Args:
prices (pandas.DataFrame): A DataFrame of prices.
Returns:
pandas.DataFrame: A DataFrame of daily returns.
"""
returns = prices.pct_change(1)
returns.iloc[0] = 0
return returns