forked from mrayanasim09/python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_prices.py
113 lines (91 loc) · 3.17 KB
/
crypto_prices.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This code is made by MRayan Asim
# Packages needed:
# pip install pandas
# pip install numpy
# pip install matplotlib
# pip install yfinance
# pip install seaborn
# pip install prophet
import yfinance as yf
import matplotlib.pyplot as plt
import seaborn as sns
from prophet import Prophet
import datetime
# Download historical cryptocurrency price data from Yahoo Finance
def download_crypto_data(crypto_symbol, end_date):
crypto_data = yf.download(
crypto_symbol, start="2010-01-01", end=end_date, progress=False
)
return crypto_data
# Preprocess the data
def preprocess_data(data):
data = data.reset_index()
data.rename(columns={"Date": "ds", "Adj Close": "y"}, inplace=True)
return data[["ds", "y"]]
# Train the forecasting model using Prophet
def train_prophet_model(data):
if len(data) < 2:
raise ValueError("Dataframe has less than 2 non-NaN rows.")
model = Prophet(daily_seasonality=True)
model.fit(data)
return model
# Make predictions using the trained model
def make_predictions(model, num_days_ahead):
future = model.make_future_dataframe(periods=num_days_ahead)
forecast = model.predict(future)
return forecast
def plot_predictions(data, forecast, crypto_name, num_days_ahead):
fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(data=data, x="ds", y="y", label="Actual", ax=ax)
sns.lineplot(data=forecast, x="ds", y="yhat", label="Predicted", ax=ax)
sns.scatterplot(
data=forecast.tail(1),
x="ds",
y="yhat",
color="red",
label=f"{num_days_ahead}-Day Ahead Prediction",
ax=ax,
)
ax.set_title(f"{crypto_name} Price Prediction ({num_days_ahead}-Day Ahead)")
ax.set_xlabel("Date")
ax.set_ylabel(f"{crypto_name} Price (USD)")
plt.legend()
plt.show()
def main():
crypto_symbols = [
"BTC-USD",
"ETH-USD",
"LTC-USD",
"XRP-USD",
"ADA-USD",
"BNB-USD",
"DOGE-USD",
]
crypto_names = [
"Bitcoin",
"Ethereum",
"Litecoin",
"Ripple",
"Cardano",
"Binance Coin",
"Dogecoin",
]
end_date = datetime.datetime.now().strftime("%Y-%m-%d")
try:
# Get user input for the number of days ahead to predict
num_days_ahead = int(input("Enter the number of days ahead for prediction: "))
for i in range(len(crypto_symbols)):
crypto_symbol = crypto_symbols[i]
crypto_name = crypto_names[i]
crypto_data = download_crypto_data(crypto_symbol, end_date)
processed_data = preprocess_data(crypto_data)
# Train the Prophet model using historical data
model = train_prophet_model(processed_data)
# Make predictions for the specified number of days ahead
forecast = make_predictions(model, num_days_ahead)
# Plot the predictions
plot_predictions(processed_data, forecast, crypto_name, num_days_ahead)
except ValueError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()