-
Notifications
You must be signed in to change notification settings - Fork 0
/
primetrade.py
90 lines (63 loc) · 2.45 KB
/
primetrade.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
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
import requests
import pandas as pd
import time
from openpyxl import Workbook
API_URL = "https://api.coingecko.com/api/v3/coins/markets"
PARAMS = {
"vs_currency": "usd",
"order": "market_cap_desc",
"per_page": 50,
"page": 1,
"sparkline": False
}
EXCEL_FILE = "crypto_data.xlsx"
def fetch_crypto_data():
response = requests.get(API_URL, params=PARAMS)
if response.status_code == 200:
return response.json()
else:
print(f"Error fetching data: {response.status_code}")
return []
def analyze_data(data):
df = pd.DataFrame(data)
top_5 = df.nlargest(5, "market_cap")[["name", "market_cap"]]
avg_price = df["current_price"].mean()
highest_change = df.loc[df["price_change_percentage_24h"].idxmax(), ["name", "price_change_percentage_24h"]]
lowest_change = df.loc[df["price_change_percentage_24h"].idxmin(), ["name", "price_change_percentage_24h"]]
analysis = {
"Top 5 Market Cap": top_5,
"Average Price": avg_price,
"Highest Change": highest_change,
"Lowest Change": lowest_change
}
return analysis
def write_to_excel(data, analysis):
df = pd.DataFrame(data)[["name", "symbol", "current_price", "market_cap", "total_volume", "price_change_percentage_24h"]]
writer = pd.ExcelWriter(EXCEL_FILE, engine="openpyxl")
df.to_excel(writer, index=False, sheet_name="Live Data")
analysis_df = pd.DataFrame({
"Metric": ["Top 5 Market Cap", "Average Price", "Highest Change", "Lowest Change"],
"Value": [
analysis["Top 5 Market Cap"].to_string(index=False),
f"${analysis['Average Price']:.2f}",
f"{analysis['Highest Change']['name']} ({analysis['Highest Change']['price_change_percentage_24h']:.2f}%)",
f"{analysis['Lowest Change']['name']} ({analysis['Lowest Change']['price_change_percentage_24h']:.2f}%)"
]
})
analysis_df.to_excel(writer, index=False, sheet_name="Analysis")
writer.close()
def main():
while True:
print("Fetching data...")
data = fetch_crypto_data()
if data:
print("Analyzing data...")
analysis = analyze_data(data)
print("Updating Excel...")
write_to_excel(data, analysis)
print("Excel updated. Waiting 5 minutes...")
else:
print("No data fetched. Retrying in 5 minutes...")
time.sleep(300)
if __name__ == "__main__":
main()