netflypsb commited on
Commit
fee39f9
1 Parent(s): c8b42c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import yfinance as yf
3
+ import pandas as pd
4
+ from sklearn.linear_model import LinearRegression
5
+ from sklearn.ensemble import RandomForestRegressor
6
+ from sklearn.preprocessing import StandardScaler
7
+ from sklearn.model_selection import train_test_split
8
+ from datetime import datetime, timedelta
9
+ import numpy as np
10
+ import matplotlib.pyplot as plt
11
+
12
+ # Function to compute RSI
13
+ def compute_rsi(data, window):
14
+ diff = data.diff(1).dropna()
15
+ gain = diff.where(diff > 0, 0)
16
+ loss = -diff.where(diff < 0, 0)
17
+ avg_gain = gain.rolling(window=window, min_periods=1).mean()
18
+ avg_loss = loss.rolling(window=window, min_periods=1).mean()
19
+ rs = avg_gain / avg_loss
20
+ rsi = 100 - (100 / (1 + rs))
21
+ return rsi
22
+
23
+ # Set up the Streamlit app
24
+ st.title("Bitcoin Price Prediction")
25
+ st.write("This app uses historical data to predict future Bitcoin prices.")
26
+
27
+ # Fetch Bitcoin historical data using yfinance
28
+ btc_data = yf.download('BTC-USD', start='2020-01-01', end=datetime.today().strftime('%Y-%m-%d'))
29
+ btc_data.reset_index(inplace=True)
30
+
31
+ # Display the historical data
32
+ st.write("Historical Bitcoin Data")
33
+ st.dataframe(btc_data.tail())
34
+
35
+ # Feature engineering
36
+ btc_data['MA_10'] = btc_data['Close'].rolling(window=10).mean()
37
+ btc_data['MA_50'] = btc_data['Close'].rolling(window=50).mean()
38
+ btc_data['RSI'] = compute_rsi(btc_data['Close'], window=14)
39
+ btc_data['Return'] = btc_data['Close'].pct_change()
40
+ btc_data.dropna(inplace=True)
41
+
42
+ # Prepare features and target variable
43
+ X = btc_data[['Open', 'High', 'Low', 'Volume', 'MA_10', 'MA_50', 'RSI', 'Return']]
44
+ y = btc_data['Close']
45
+
46
+ # Split the data
47
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
48
+
49
+ # Scale the features
50
+ scaler = StandardScaler()
51
+ X_train_scaled = scaler.fit_transform(X_train)
52
+ X_test_scaled = scaler.transform(X_test)
53
+
54
+ # Train the Linear Regression model
55
+ lr_model = LinearRegression()
56
+ lr_model.fit(X_train_scaled, y_train)
57
+
58
+ # Train the Random Forest model
59
+ rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
60
+ rf_model.fit(X_train_scaled, y_train)
61
+
62
+ # Predict future prices using ensemble method
63
+ future_dates = [btc_data['Date'].iloc[-1] + timedelta(days=x) for x in range(1, 15)]
64
+ future_df = pd.DataFrame(index=future_dates, columns=X.columns)
65
+ future_df = future_df.fillna(method='ffill')
66
+
67
+ future_X_scaled = scaler.transform(future_df)
68
+ lr_predictions = lr_model.predict(future_X_scaled)
69
+ rf_predictions = rf_model.predict(future_X_scaled)
70
+
71
+ # Combine predictions (average)
72
+ combined_predictions = (lr_predictions + rf_predictions) / 2
73
+
74
+ # Display predictions
75
+ predictions_df = pd.DataFrame({'Date': future_dates, 'Predicted Close': combined_predictions})
76
+ predictions_df.set_index('Date', inplace=True)
77
+ st.write("Future Bitcoin Price Predictions")
78
+ st.dataframe(predictions_df)
79
+
80
+ # Plot the predictions
81
+ st.line_chart(predictions_df['Predicted Close'])