-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinSignal_Interface.py
More file actions
139 lines (102 loc) · 4.85 KB
/
CoinSignal_Interface.py
File metadata and controls
139 lines (102 loc) · 4.85 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
from joblib import load
from data_fetcher import Dataset
from datetime import datetime
# Load the saved model and scaler
regressor = load('joblib_model1.joblib')
scaler = load('joblib_scaler1.joblib')
def predict_smoothed(regressor, df, scaler, symbol_mapping, prediction_days=30):
# Prepare the dataset for prediction
df_copy = df.copy()
df_copy = df_copy.reset_index()
df_copy['symbol'] = df_copy['symbol'].map(symbol_mapping)
df_scaled = scaler.transform(df_copy[['open', 'high', 'low', 'volume', 'symbol']])
# Make the prediction
predicted_values = regressor.predict(df_scaled)
# Add the predicted values to the dataframe
df_copy[['t1', 't7', 't30']] = predicted_values
# Extract the prediction for the specified period
if prediction_days == 1:
predicted_value = df_copy['t1'].iloc[-1]
elif prediction_days == 7:
predicted_value = df_copy['t7'].iloc[-1]
elif prediction_days == 30:
predicted_value = df_copy['t30'].iloc[-1]
return predicted_value, df_copy
# Load the symbols
symbols = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT', 'ADAUSDT', 'DOGEUSDT', 'DOTUSDT', 'XRPUSDT', 'LINKUSDT', 'LTCUSDT', 'BCHUSDT', 'UNIUSDT', 'MATICUSDT', 'SOLUSDT', 'VETUSDT', 'ETCUSDT', 'FILUSDT', 'THETAUSDT', 'XLMUSDT', 'TRXUSDT', 'EOSUSDT']
# Set Streamlit parameters
st.set_page_config(page_title='Crypto Forecast', layout='wide')
# title of the app
st.title("Crypto Forecast")
# Add a selectbox to the sidebar:
coin = st.sidebar.selectbox(
'Select a cryptocurrency',
symbols
)
# Add a selectbox for the prediction period:
period = st.sidebar.selectbox(
'Select a prediction period',
['1 day', '7 days', '30 days']
)
# Get a fresh dataset
df = Dataset().get_data(ticker=coin, days=(datetime.now() - datetime(datetime.now().year - 1, 1, 1)).days, ts='1d')
df = pd.DataFrame(df)
df['symbol'] = coin
# Create a symbol_mapping dictionary
symbol_mapping = {symbol: i for i, symbol in enumerate(symbols)}
# Get prediction
prediction, df_pred = predict_smoothed(regressor, df, scaler, symbol_mapping, int(period.split()[0]))
# Display prediction
st.write(f"The predicted price for {coin} for the next {period} is ${prediction:.2f}")
# Create a Plotly figure
fig = go.Figure()
# Add the historical price line
fig.add_trace(go.Scatter(x=df_pred['time'], y=df_pred['close'], mode='lines', name='Historical Price'))
# Add the predicted price point
fig.add_trace(go.Scatter(x=[df_pred['time'].iloc[-1]], y=[prediction], mode='markers', name='Predicted Price', marker=dict(color='red', size=10)))
# Set the figure layout
fig.update_layout(title=coin, xaxis_title='Date', yaxis_title='Close Price USD ($)', showlegend=True)
# Display the figure
st.plotly_chart(fig)
def predict_smoothed(regressor, df, scaler, symbol_mapping, prediction_days=30):
# Prepare the dataset for prediction
df_copy = df.copy()
df_copy = df_copy.reset_index() # Since 'time' is used as index in your dataset
df_copy['symbol'] = df_copy['symbol'].map(symbol_mapping) # Convert the 'symbol' to integer
df_scaled = scaler.transform(df_copy[['open', 'high', 'low', 'volume', 'symbol']])
# Make the prediction
predicted_values = regressor.predict(df_scaled)
# Add the predicted values to the dataframe
df_copy[['t1', 't7', 't30']] = predicted_values
# Extract the prediction for the specified period
if prediction_days == 1:
predicted_value = df_copy['t1'].iloc[-1]
elif prediction_days == 7:
predicted_value = df_copy['t7'].iloc[-1]
elif prediction_days == 30:
predicted_value = df_copy['t30'].iloc[-1]
return predicted_value, df_copy
def predict_smoothed(regressor, df, scaler, symbol_mapping, prediction_days=30):
# Prepare the dataset for prediction
df_copy = df.copy()
df_copy = df_copy.reset_index() # Since 'time' is used as index in your dataset
df_copy['symbol'] = df_copy['symbol'].map(symbol_mapping) # Convert the 'symbol' to integer
df_scaled = scaler.transform(df_copy[['open', 'high', 'low', 'volume', 'symbol']])
# Make the prediction
predicted_values = regressor.predict(df_scaled)
# Add the predicted values to the dataframe
df_copy[['t1', 't7', 't30']] = predicted_values
# Extract the prediction for the specified period
if prediction_days == 1:
predicted_value = df_copy['t1'].iloc[-1]
elif prediction_days == 7:
predicted_value = df_copy['t7'].iloc[-1]
elif prediction_days == 30:
predicted_value = df_copy['t30'].iloc[-1]
return predicted_value, df_copy