Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit b4a904f

Browse files
committed
improve plot
1 parent 07e5513 commit b4a904f

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

plot_prices.py

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def read_csv(path):
1717
data[timestamp] = price
1818
return data
1919

20-
def fetch_prices(timestamp, api_key):
20+
def fetch_prices(timestamp):
2121
url = "https://min-api.cryptocompare.com/data/v2/histominute"
2222
params = {
2323
'fsym': 'ETH',
@@ -29,67 +29,85 @@ def fetch_prices(timestamp, api_key):
2929
response = requests.get(url, params=params)
3030
if response.status_code == 200:
3131
data = response.json()
32-
data_len = len(data['Data']['Data'])
33-
last_item = data['Data']['Data'][data_len - 1]
32+
api_data = data['Data']['Data']
33+
data_len = len(api_data)
34+
last_item = api_data[data_len - 1]
3435
if data['Response'] == 'Success' and data_len > 0:
3536
if timestamp - last_item['time'] <= 60:
36-
# allowing one min diff
37-
return data['Data']['Data'][0]['close']
37+
return last_item['time'], last_item['close']
3838
else:
39-
return 0
39+
return last_item['time'], 0
4040
def main():
4141
csv_data = read_csv('uni_prices.csv')
4242

43-
timestamps = []
44-
csv_prices = []
43+
uni_timestamps = []
44+
uni_prices = []
45+
api_timestamps = []
4546
api_prices = []
4647

4748
for ts, price in csv_data.items():
48-
api_price = fetch_prices(ts, api_key)
49+
(api_ts, api_price)= fetch_prices(ts)
4950
if api_price:
50-
timestamps.append(ts)
51-
csv_prices.append(price)
51+
uni_timestamps.append(ts)
52+
uni_prices.append(price)
53+
api_timestamps.append(api_ts)
5254
api_prices.append(api_price)
5355

54-
print(timestamps);
55-
print(csv_prices);
56+
print(uni_timestamps);
57+
print(uni_prices);
58+
print(api_timestamps);
5659
print(api_prices);
5760

58-
# timestamps = [1749105683, 1749102011, 1749098375, 1749094775, 1749091175, 1749087551, 1749083951, 1749080363, 1749076763, 1749073079, 1749069491, 1749065879, 1749062255, 1749058619, 1749055019, 1749051419, 1749047807, 1749044183, 1749040559, 1749036923, 1749033275, 1749029675, 1749026075, 1749022427, 1749018779, 1749015179, 1749011567, 1749007979, 1749004343, 1749000731, 1748997119, 1748993519, 1748989919, 1748986259, 1748982671, 1748979047, 1748975447, 1748971847, 1748968247, 1748964647, 1748961035, 1748957387, 1748953775, 1748950187, 1748946587, 1748942951, 1748939291, 1748935667, 1748932055, 1748928443]
59-
# csv_prices = [2609.8080762655322, 2621.480490403204, 2624.393160328884, 2622.7686279534264, 2619.7231588548534, 2610.3963278671695, 2609.545936613066, 2608.729875162142, 2614.1021109699313, 2612.849426415079, 2609.528150105592, 2629.978419276374, 2639.693585306237, 2641.1199286093947, 2651.1515553967374, 2653.429993570316, 2610.912013801284, 2616.554217935566, 2623.7705098568176, 2634.658956898744, 2640.6144521020756, 2634.892883303356, 2637.2214214676064, 2626.727496929253, 2620.77461018866, 2628.379186142654, 2631.9788198198744, 2635.816798914804, 2617.5044214035506, 2610.3798778314335, 2595.2227034083376, 2600.5514233505082, 2595.497388260199, 2608.125684065392, 2605.341895595558, 2620.625110387102, 2614.4468008942767, 2620.2157734437033, 2616.703457689552, 2643.3060943956352, 2628.4498082456616, 2620.172618189739, 2603.6126702299416, 2613.240784817929, 2612.434634630224, 2601.7559333260524, 2601.6135961284067, 2613.2477388445636, 2612.322965102058, 2599.466433363167]
60-
# api_prices = [2611.79, 2615.1, 2630.77, 2629.6, 2627.53, 2606.69, 2612.21, 2609.82, 2612.91, 2618.81, 2610.49, 2626.76, 2636.21, 2638.42, 2655.51, 2648.87, 2614.97, 2621.86, 2622.5, 2627.96, 2636.65, 2632.8, 2639.91, 2627.62, 2624.14, 2621.03, 2625.66, 2634.53, 2614.45, 2612.09, 2589.82, 2595.93, 2600.22, 2611.75, 2598.33, 2626.38, 2614.02, 2623.73, 2610.51, 2642.56, 2632.72, 2621.35, 2608.12, 2607.72, 2616.27, 2601.82, 2604.33, 2609.74, 2604.84, 2600.63]
61-
dates = [datetime.fromtimestamp(ts) for ts in timestamps]
61+
uni_dates = [datetime.fromtimestamp(ts) for ts in uni_timestamps]
62+
api_dates = [datetime.fromtimestamp(ts) for ts in api_timestamps]
6263

6364
plt.figure(figsize=(14, 7))
6465

65-
# Plot with different colors and markers
66-
plt.plot(dates, csv_prices, label='UNI Price', marker='o', color='blue', markersize=5)
67-
plt.plot(dates, api_prices, label='API Price', marker='x', color='green', markersize=5)
66+
# Plot each series with their actual timestamps
67+
plt.plot(uni_dates, uni_prices, label='UNI Price', marker='o', color='blue', markersize=5)
68+
plt.plot(api_dates, api_prices, label='API Price', marker='x', color='green', markersize=5)
6869

6970
plt.title('ETH Price Comparison: UNI vs API', fontsize=14)
7071
plt.xlabel('Time', fontsize=12)
7172
plt.ylabel('Price (USD)', fontsize=12)
7273
plt.legend(fontsize=12)
7374

74-
# Add more grid lines
75-
plt.grid(True, which='both', ls='-', alpha=0.5)
75+
# Set unified time range for both series
76+
all_dates = uni_dates + api_dates
77+
x_min = min(all_dates)
78+
x_max = max(all_dates)
79+
plt.xlim(x_min, x_max)
7680

7781
ax = plt.gca()
78-
79-
# X-axis: grid/ticks every 30 minutes
82+
8083
ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))
8184
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d %H:%M'))
82-
83-
# Y-axis: grid/ticks every $1
85+
8486
ax.yaxis.set_major_locator(MultipleLocator(2))
85-
ax.yaxis.set_minor_locator(MultipleLocator(2))
86-
87-
# Grid lines
87+
ax.yaxis.set_minor_locator(MultipleLocator(1))
88+
8889
ax.grid(which='major', axis='both', linestyle='-', alpha=0.7)
8990
ax.grid(which='minor', axis='both', linestyle=':', alpha=0.4)
9091

91-
plt.gcf().autofmt_xdate()
9292

93+
differences = [abs(u - a) for u, a in zip(uni_prices, api_prices)]
94+
max_diff = max(differences)
95+
max_diff_index = differences.index(max_diff)
96+
97+
# Get corresponding timestamps
98+
max_uni_ts = uni_timestamps[max_diff_index]
99+
max_api_ts = api_timestamps[max_diff_index]
100+
max_time_str = datetime.fromtimestamp(max_uni_ts).strftime('%Y-%m-%d %H:%M:%S')
101+
102+
# Add text annotation
103+
text_str = f"Max Difference: {max_diff:.4f}\nAt UNI timestamp: {max_time_str}"
104+
plt.text(0.02, 0.02, text_str,
105+
transform=ax.transAxes,
106+
fontsize=10,
107+
bbox=dict(facecolor='white', alpha=0.8))
108+
109+
plt.gcf().autofmt_xdate()
93110
plt.show()
111+
94112
if __name__ == "__main__":
95113
main()

0 commit comments

Comments
 (0)