import streamlit as st import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from xgboost import XGBRegressor from sklearn.metrics import mean_squared_error from math import sqrt import schedule import time import warnings import yfinance as yf # Suppress FutureWarnings from yfinance warnings.simplefilter(action='ignore', category=FutureWarning) l = [] text_input = 'BTC-USD' # Replace with your desired cryptocurrency symbol data = yf.download(text_input, period='max') # Fetch historical data for BTC-USD def predict_and_save_close_price(): #data = pd.DataFrame() # Initialize data as an empty DataFrame # Fetch historical data #try: #data = yf.download(text_input, period='max') #except: #pass #if data.empty: #return # Select Open and Close price data = data[['Open', 'Close']].copy() # Use .copy() to avoid the SettingWithCopyWarning # Create a new "lagged" dataframe for the model data.loc[:, 'Previous_Close'] = data['Close'].shift(1) # Use .loc to modify the DataFrame safely # Drop the missing values created by shifting data = data.dropna() # Define inputs and outputs X = data['Previous_Close'].values.reshape(-1, 1) y = data['Close'].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) # Evaluate the model train_predictions = model.predict(X_train) train_rmse = sqrt(mean_squared_error(y_train, train_predictions)) test_predictions = model.predict(X_test) test_rmse = sqrt(mean_squared_error(y_test, test_predictions)) # Fetch latest close price latest_close = data['Close'].iloc[-1] # Reshape the input latest_close_reshaped = np.array(latest_close).reshape(1, -1) # Make prediction predicted_close = model.predict(latest_close_reshaped) l.append(predicted_close) # Schedule the function to run every minute #schedule.every(1).minutes.do(predict_and_save_close_price) # Run the scheduling loop #if st.button('pezh'): #while True: #schedule.run_pending() #time.sleep(1) if st.button('pezho'): predict_and_save_close_price st.write(l)