QAway-to commited on
Commit
0615a90
·
1 Parent(s): 7b9c77c

Nothing but VC

Browse files
Files changed (1) hide show
  1. core/visual_comparison.py +94 -0
core/visual_comparison.py CHANGED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Module: visual_comparison.py
3
+ Purpose: Interactive crypto pair comparison (Plotly + CoinGecko)
4
+ """
5
+
6
+ import requests
7
+ import pandas as pd
8
+ import plotly.graph_objects as go
9
+
10
+ COINGECKO_API = "https://api.coingecko.com/api/v3"
11
+
12
+
13
+ def get_coin_history(coin_id: str, days: int = 180):
14
+ """Fetch historical market data for given coin from CoinGecko API."""
15
+ url = f"{COINGECKO_API}/coins/{coin_id}/market_chart?vs_currency=usd&days={days}"
16
+ r = requests.get(url)
17
+ r.raise_for_status()
18
+ data = r.json()
19
+ df = pd.DataFrame(data["prices"], columns=["timestamp", "price"])
20
+ df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
21
+ return df
22
+
23
+
24
+ def build_price_chart(pair: tuple[str, str], days: int = 180):
25
+ """Build comparative price chart for selected pair."""
26
+ coin_a, coin_b = pair
27
+
28
+ df_a = get_coin_history(coin_a, days)
29
+ df_b = get_coin_history(coin_b, days)
30
+
31
+ fig = go.Figure()
32
+ fig.add_trace(go.Scatter(
33
+ x=df_a["timestamp"],
34
+ y=df_a["price"],
35
+ name=f"{coin_a.capitalize()} / USD",
36
+ line=dict(width=2),
37
+ ))
38
+ fig.add_trace(go.Scatter(
39
+ x=df_b["timestamp"],
40
+ y=df_b["price"],
41
+ name=f"{coin_b.capitalize()} / USD",
42
+ line=dict(width=2),
43
+ ))
44
+
45
+ fig.update_layout(
46
+ template="plotly_dark",
47
+ height=480,
48
+ margin=dict(l=40, r=20, t=30, b=40),
49
+ xaxis_title="Date",
50
+ yaxis_title="Price (USD)",
51
+ legend_title="Asset",
52
+ hovermode="x unified",
53
+ )
54
+
55
+ return fig
56
+
57
+
58
+ def build_volatility_chart(pair: tuple[str, str], days: int = 180):
59
+ """Build comparative volatility chart for selected pair."""
60
+ coin_a, coin_b = pair
61
+
62
+ df_a = get_coin_history(coin_a, days)
63
+ df_b = get_coin_history(coin_b, days)
64
+
65
+ df_a["returns"] = df_a["price"].pct_change() * 100
66
+ df_b["returns"] = df_b["price"].pct_change() * 100
67
+
68
+ fig = go.Figure()
69
+ fig.add_trace(go.Scatter(
70
+ x=df_a["timestamp"],
71
+ y=df_a["returns"],
72
+ name=f"{coin_a.upper()} Daily Change (%)",
73
+ mode="lines",
74
+ line=dict(width=1.6),
75
+ ))
76
+ fig.add_trace(go.Scatter(
77
+ x=df_b["timestamp"],
78
+ y=df_b["returns"],
79
+ name=f"{coin_b.upper()} Daily Change (%)",
80
+ mode="lines",
81
+ line=dict(width=1.6),
82
+ ))
83
+
84
+ fig.update_layout(
85
+ template="plotly_dark",
86
+ height=400,
87
+ margin=dict(l=40, r=20, t=30, b=40),
88
+ xaxis_title="Date",
89
+ yaxis_title="Daily Change (%)",
90
+ legend_title="Volatility",
91
+ hovermode="x unified",
92
+ )
93
+
94
+ return fig