-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_tracker.py
More file actions
146 lines (111 loc) · 4.64 KB
/
crypto_tracker.py
File metadata and controls
146 lines (111 loc) · 4.64 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import requests
from dataclasses import dataclass
from typing import Final
from typing import List
# ---- URLs ----
BASE_URL: Final[str]="https://api.coingecko.com/api/v3/coins/markets"
HISTORY_URL: Final[str]= "https://api.coingecko.com/api/v3/coins/{id}/market_chart"
CURR_URL: Final[str]= "https://api.coingecko.com/api/v3/simple/supported_vs_currencies"
# ---=-----------Class---------------------
@dataclass()
class Coin:
id:str
name:str
symbol:str
current_price: float
high_24h: float
low_24h: float
price_change_24h: float
price_change_percentage_24h: float
# ---------------- Get Coins --------------
@st.cache_data(show_spinner=False)
def get_coins(vs_currency: str='usd', top_n:int =20) -> List["Coin"]:
payload = {
'vs_currency':vs_currency,
'order':'market_cap_desc',
'per_page':top_n,
'page':1
}
try:
response = requests.get(BASE_URL, params=payload)
response.raise_for_status()
data = response.json()
return[
Coin(
id = item['id'],
name = item['name'],
symbol = item['symbol'],
current_price = item['current_price'],
high_24h = item['high_24h'],
low_24h = item['low_24h'],
price_change_24h = item['price_change_24h'],
price_change_percentage_24h = item['price_change_percentage_24h']
) for item in data
]
except Exception as e:
st.error(f"Error Fetching Coins: {e}")
return []
# ----------------Get All Currencies --------------------------
@st.cache_data(show_spinner=False)
def get_supported_currencies():
response = requests.get(CURR_URL)
if response.status_code == 200:
return sorted(response.json())
else:
return['inr'] # fallback
# -------------------- Display Coin History ---------------------
@st.cache_data(show_spinner=False)
def get_price_history(coin_id:str, vs_currency:str ='usd', days:str ='7') -> pd.DataFrame:
url = HISTORY_URL.format(id=coin_id)
params = {'vs_currency' : vs_currency, 'days' : days}
try:
response = requests.get(url, params = params)
response.raise_for_status()
prices = response.json()['prices']
df = pd.DataFrame(prices, columns=['timestamp','price'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
return df
except Exception as e:
st.warning(f"Error Fetching History: {e}")
return pd.DataFrame()
# ------------ Streamlit UI/UX -------------------------------------
st.set_page_config(page_title = "Crypto Tracker", layout="wide")
st.title("💰 Real Time Cryptocurrency Tracker")
# Currency dropdown
currency = st.sidebar.selectbox("Select Currency", get_supported_currencies())
# SLider for topcoins
top_n = st.sidebar.slider("Number of Top Coins",min_value=5,max_value=50,value=20)
# Selectbox for duration
duration = st.sidebar.selectbox("Time Duration (days)", ['1','7','30','90','180','365','max'])
coins = get_coins(currency, top_n)
if coins:
coin_names = [f"{coin.name}({coin.symbol.upper()})" for coin in coins]
selected_coin = st.sidebar.selectbox("Select Coin", coin_names)
coin_obj = coins[coin_names.index(selected_coin)]
st.subheader(f"{coin_obj.name} ({coin_obj.symbol.upper()})")
st.metric("Current Price", f"{coin_obj.current_price:,.2f} {currency.upper()}")
col1,col2,col3 = st.columns(3)
col1.metric("24 High", f"{coin_obj.high_24h:,.2f}")
col2.metric("24 Low", f"{coin_obj.low_24h:,.2f}")
col3.metric("24 Change (24h)", f"{coin_obj.price_change_percentage_24h:,.2f}%")
# ---------------------------------- Matplots -----------------------------------
df_history = get_price_history(coin_obj.id, vs_currency=currency, days=duration)
if not df_history.empty:
st.line_chart(df_history.set_index('timestamp')['price'])
else:
st.info("No Historical Data Available")
# -------------------------- Table Of Top Coins -------------------------------
st.subheader(f"🏆Top {top_n} Coins by Market Cap ({currency.upper()})")
coin_table = pd.DataFrame([{
'Name' : coin.name,
'Symbol': coin.symbol.upper(),
'Price' :f"{coin.current_price:,.2f} {currency.upper()}",
'24h Change (%)': f"{coin.price_change_percentage_24h:,.2f}",
'Market Cap' : f"{coin.current_price*1_000_000:.0f}"
} for coin in coins])
st.dataframe(coin_table, use_container_width=True)
else:
st.error("No Coins Data Available")