Spaces:
Sleeping
Sleeping
Oscar Wang
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,12 @@ import yfinance as yf
|
|
3 |
import plotly.graph_objects as go
|
4 |
from statsmodels.tsa.arima.model import ARIMA
|
5 |
import pandas as pd
|
|
|
6 |
|
7 |
-
|
|
|
|
|
|
|
8 |
eth = yf.Ticker("ETH-USD")
|
9 |
if period == '1d':
|
10 |
data = eth.history(period="1d", interval="1m")
|
@@ -27,20 +31,30 @@ def fetch_eth_price(period):
|
|
27 |
|
28 |
data.index = pd.DatetimeIndex(data.index)
|
29 |
data = data.asfreq(freq) # Ensure the data has a consistent frequency
|
|
|
|
|
|
|
|
|
|
|
30 |
return data, predict_steps, freq
|
31 |
|
32 |
def make_predictions(data, predict_steps, freq):
|
|
|
33 |
model = ARIMA(data['Close'], order=(5, 1, 0))
|
34 |
model_fit = model.fit()
|
35 |
-
|
36 |
|
|
|
|
|
37 |
future_dates = pd.date_range(start=data.index[-1], periods=predict_steps+1, freq=freq, inclusive='right')
|
38 |
forecast_df = pd.DataFrame(forecast, index=future_dates[1:], columns=['Prediction'])
|
|
|
39 |
|
40 |
return forecast_df
|
41 |
|
42 |
def plot_eth(period):
|
43 |
-
data
|
|
|
44 |
forecast_df = make_predictions(data, predict_steps, freq)
|
45 |
|
46 |
fig = go.Figure()
|
|
|
3 |
import plotly.graph_objects as go
|
4 |
from statsmodels.tsa.arima.model import ARIMA
|
5 |
import pandas as pd
|
6 |
+
import logging
|
7 |
|
8 |
+
# Set up logging
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
|
11 |
+
def fetch_eth_price(period, past_data_limit=None):
|
12 |
eth = yf.Ticker("ETH-USD")
|
13 |
if period == '1d':
|
14 |
data = eth.history(period="1d", interval="1m")
|
|
|
31 |
|
32 |
data.index = pd.DatetimeIndex(data.index)
|
33 |
data = data.asfreq(freq) # Ensure the data has a consistent frequency
|
34 |
+
|
35 |
+
# Limit the amount of past data used for training
|
36 |
+
if past_data_limit is not None:
|
37 |
+
data = data.iloc[-past_data_limit:]
|
38 |
+
|
39 |
return data, predict_steps, freq
|
40 |
|
41 |
def make_predictions(data, predict_steps, freq):
|
42 |
+
logging.info("Starting model training...")
|
43 |
model = ARIMA(data['Close'], order=(5, 1, 0))
|
44 |
model_fit = model.fit()
|
45 |
+
logging.info("Model training completed.")
|
46 |
|
47 |
+
logging.info("Generating predictions...")
|
48 |
+
forecast = model_fit.forecast(steps=predict_steps)
|
49 |
future_dates = pd.date_range(start=data.index[-1], periods=predict_steps+1, freq=freq, inclusive='right')
|
50 |
forecast_df = pd.DataFrame(forecast, index=future_dates[1:], columns=['Prediction'])
|
51 |
+
logging.info("Predictions generated.")
|
52 |
|
53 |
return forecast_df
|
54 |
|
55 |
def plot_eth(period):
|
56 |
+
# Use a past data limit to speed up the process (e.g., 500 data points)
|
57 |
+
data, predict_steps, freq = fetch_eth_price(period, past_data_limit=500)
|
58 |
forecast_df = make_predictions(data, predict_steps, freq)
|
59 |
|
60 |
fig = go.Figure()
|