-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTC_analysis.py
More file actions
33 lines (26 loc) · 867 Bytes
/
BTC_analysis.py
File metadata and controls
33 lines (26 loc) · 867 Bytes
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
import requests
import csv
from datetime import datetime
# API endpoint
url = "https://api.alternative.me/fng/"
# Fetch data
response = requests.get(url)
data = response.json()
# Extract relevant data
fear_and_greed = data["data"][0]
value = fear_and_greed["value"]
classification = fear_and_greed["value_classification"]
timestamp = datetime.fromtimestamp(int(fear_and_greed["timestamp"]))
# Save to CSV
file_name = "fear_and_greed_index.csv"
headers = ["Date", "Index Value", "Classification"]
try:
with open(file_name, "a", newline="") as csvfile:
writer = csv.writer(csvfile)
# Write headers if file is empty
if csvfile.tell() == 0:
writer.writerow(headers)
writer.writerow([timestamp, value, classification])
print("Data saved successfully.")
except Exception as e:
print(f"Error saving data: {e}")