import streamlit as st import yfinance as yf from sklearn.model_selection import train_test_split from xgboost import XGBRegressor from sklearn.metrics import mean_squared_error from math import sqrt import numpy as np import pandas as pd def predict_crypto_prices(crypto_name): data = yf.download(crypto_name, period='max') # Create a new "lagged" dataframe for the model data['Previous_Open'] = data['Open'].shift(1) data['Previous_Close'] = data['Close'].shift(1) data['Previous_Low'] = data['Low'].shift(1) data['Previous_High'] = data['High'].shift(1) # Drop the missing values created by shifting data = data.dropna() # Define inputs and outputs for each prediction features = ['Previous_Open', 'Previous_Close', 'Previous_Low', 'Previous_High'] predictions = ['Open', 'Close', 'Low', 'High'] predicted_values = pd.DataFrame(columns=['Open', 'Close', 'Low', 'High', 'Value']) for feature, prediction in zip(features, predictions): X = data[feature].values.reshape(-1, 1) y = data[prediction].values # Split the data into training and testing datasets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Initialize XGBoost regressor model model = XGBRegressor(n_estimators=100, learning_rate=0.1, objective='reg:squarederror') # Train the model model.fit(X_train, y_train) # Make predictions test_predictions = model.predict(X_test) test_rmse = sqrt(mean_squared_error(y_test, test_predictions)) # Predict the future price using the most recent price latest_price = data[prediction].iloc[-1] # Reshape the input latest_price_reshaped = np.array(latest_price).reshape(1, -1) # Make prediction for the next day predicted_price = model.predict(latest_price_reshaped) predicted_values[prediction] = predicted_price # Calculate the predicted value (assuming an average of open, close, high, low as value) predicted_values['Value'] = predicted_values.mean(axis=1) return predicted_values st.title('Cryptocurrency Price Prediction') crypto_name = st.text_input('Enter the cryptocurrency symbol (e.g., BTC-USD)', value='BTC-USD') if st.button('Predict'): predicted_prices = predict_crypto_prices(crypto_name) st.write('Predicted Prices and Value:') st.write(predicted_prices)