Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ccxt
|
2 |
+
import pandas as pd
|
3 |
+
from datetime import datetime, timedelta
|
4 |
+
from prophet import Prophet
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
# Initialize the exchange
|
9 |
+
binance = ccxt.bitget()
|
10 |
+
symbol = "BTC/USDT"
|
11 |
+
|
12 |
+
def fetch_btc_usdt_data(start_date, end_date):
|
13 |
+
since = int(datetime.strptime(start_date, '%Y-%m-%d').timestamp() * 1000)
|
14 |
+
end = int(datetime.strptime(end_date, '%Y-%m-%d').timestamp() * 1000)
|
15 |
+
timeframe = '1d' # Daily data
|
16 |
+
data = []
|
17 |
+
|
18 |
+
while since < end:
|
19 |
+
ohlcv = binance.fetch_ohlcv(symbol, timeframe, since, limit=1000)
|
20 |
+
if not ohlcv:
|
21 |
+
break
|
22 |
+
since = ohlcv[-1][0] + 1
|
23 |
+
data.extend(ohlcv)
|
24 |
+
|
25 |
+
# Convert data to DataFrame
|
26 |
+
columns = ['timestamps', 'open', 'high', 'low', 'close', 'volume']
|
27 |
+
df = pd.DataFrame(data, columns=columns)
|
28 |
+
df['timestamps'] = pd.to_datetime(df['timestamps'], unit='ms')
|
29 |
+
df.set_index('timestamps', inplace=True)
|
30 |
+
|
31 |
+
# Return the DataFrame
|
32 |
+
return df
|
33 |
+
|
34 |
+
def train_and_forecast(df):
|
35 |
+
# Prepare the data for Prophet
|
36 |
+
df_prophet = df[['close']].reset_index()
|
37 |
+
df_prophet.rename(columns={'timestamps': 'ds', 'close': 'y'}, inplace=True)
|
38 |
+
|
39 |
+
# Train the Prophet model
|
40 |
+
model = Prophet(daily_seasonality=True)
|
41 |
+
model.fit(df_prophet)
|
42 |
+
|
43 |
+
# Make a future dataframe for predictions (e.g., next 30 days)
|
44 |
+
future = model.make_future_dataframe(periods=30, freq='D')
|
45 |
+
|
46 |
+
# Get the forecast
|
47 |
+
forecast = model.predict(future)
|
48 |
+
|
49 |
+
return forecast, model
|
50 |
+
|
51 |
+
def plot_forecast(forecast, model):
|
52 |
+
# Plot the forecast
|
53 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
54 |
+
model.plot(forecast, ax=ax)
|
55 |
+
|
56 |
+
# Plot the components
|
57 |
+
fig2, ax2 = plt.subplots(figsize=(10, 6))
|
58 |
+
model.plot_components(forecast)
|
59 |
+
|
60 |
+
return fig, fig2
|
61 |
+
|
62 |
+
# Streamlit UI
|
63 |
+
st.title("BTC/USDT Price Forecasting")
|
64 |
+
st.markdown("""
|
65 |
+
This app uses Facebook's Prophet model to forecast the future prices of BTC/USDT based on historical data from Bitget.
|
66 |
+
You can select the start and end dates to get a prediction for the next 30 days.
|
67 |
+
""")
|
68 |
+
|
69 |
+
# Date input
|
70 |
+
start_date = st.date_input('Start Date', datetime.today() - timedelta(days=365))
|
71 |
+
end_date = st.date_input('End Date', datetime.today())
|
72 |
+
|
73 |
+
if start_date and end_date:
|
74 |
+
# Fetch the data
|
75 |
+
df = fetch_btc_usdt_data(start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'))
|
76 |
+
|
77 |
+
# Train and forecast
|
78 |
+
forecast, model = train_and_forecast(df)
|
79 |
+
|
80 |
+
# Plot the forecast and components
|
81 |
+
st.subheader("Forecast Plot")
|
82 |
+
fig1, fig2 = plot_forecast(forecast, model)
|
83 |
+
|
84 |
+
st.pyplot(fig1) # Forecast plot
|
85 |
+
st.pyplot(fig2) # Components plot
|