-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckagain.py
88 lines (65 loc) · 3.96 KB
/
checkagain.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 yfinance as yf
import pandas as pd
from datetime import datetime, timedelta
import csv
import os
# Function to fetch and display the current Bitcoin price with a timestamp
def check_btc_price():
# Fetch the latest Bitcoin price data from Yahoo Finance
btc_data = yf.download('BTC-USD', period='1d', interval='1m') # Using 1-minute interval to get the latest data
current_price = btc_data['Close'].iloc[-1] # Get the most recent closing price
# Get the current timestamp
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Output the current price and timestamp
print(f"Timestamp: {timestamp}")
print(f"Current Bitcoin price: {current_price:.2f} USD")
return current_price
# Function to check the predicted price from 24 hours ago in the log file and calculate percentage difference
def check_prediction_24_hours_ago(log_file, accuracy_log_file):
try:
# Load the log file into a pandas DataFrame
df = pd.read_csv(log_file)
# Convert 'Timestamp' column to datetime format for accurate comparison
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
# Calculate the target time window for 24 hours ago
target_time_24_hours_ago = datetime.now() - timedelta(days=1)
time_window_start = target_time_24_hours_ago - timedelta(hours=1) # 1 hour before
time_window_end = target_time_24_hours_ago + timedelta(hours=1) # 1 hour after
# Filter for entries within the time window
filtered_df = df[(df['Timestamp'] >= time_window_start) & (df['Timestamp'] <= time_window_end)]
if filtered_df.empty:
print(f"No prediction found within 1 hour of 24 hours ago: {target_time_24_hours_ago}.")
return None # Return None if no prediction is found
# Find the entry closest to the target time within the filtered data
closest_row = filtered_df.iloc[(filtered_df['Timestamp'] - target_time_24_hours_ago).abs().argsort()[:1]]
# Fetch the predicted price from the closest timestamp
closest_timestamp = closest_row['Timestamp'].values[0]
predicted_price_24_hours_ago = closest_row['Predicted Price'].values[0]
# Fetch the current Bitcoin price
current_price = check_btc_price()
# Calculate the percentage difference
percentage_difference = ((current_price - predicted_price_24_hours_ago) / predicted_price_24_hours_ago) * 100
# Output the results
print(f"Timestamp 24 Hours Ago (Closest): {closest_timestamp}")
print(f"Predicted Bitcoin Price 24 Hours Ago: {predicted_price_24_hours_ago:.2f} USD")
print(f"Current Bitcoin Price: {current_price:.2f} USD")
print(f"Percentage Difference: {percentage_difference:.2f}%")
# Log the results to the accuracy CSV file with the desired format
header = ['Current Timestamp', 'Predicted Timestamp', 'Predicted Price', 'Actual Price', 'Percentage Difference']
# Check if the accuracy log file exists and add the header if not
file_exists = os.path.isfile(accuracy_log_file)
with open(accuracy_log_file, 'a', newline='') as file:
writer = csv.writer(file)
if not file_exists or os.path.getsize(accuracy_log_file) == 0:
writer.writerow(header) # Write the header if the file doesn't exist or is empty
writer.writerow([datetime.now().strftime('%Y-%m-%d %H:%M:%S'), closest_timestamp,
predicted_price_24_hours_ago, current_price, percentage_difference])
return percentage_difference # Return percentage difference for further use if needed
except Exception as e:
print(f"Error occurred: {str(e)}")
return None
# Example usage: Check prediction made 24 hours ago
prediction_log_file = 'btc_price_prediction_log.csv'
accuracy_log_file = 'btc_price_accuracy_check-2.csv'
# Calculate percentage difference
percentage_difference = check_prediction_24_hours_ago(prediction_log_file, accuracy_log_file)