File size: 2,496 Bytes
a378f96 f39be00 eb8e5f2 cc89bfa 4bc3d12 cc89bfa a378f96 4bc3d12 990c40e 4bc3d12 a378f96 4bc3d12 a378f96 7270b1f a378f96 7270b1f a378f96 c276dc0 a378f96 eb8e5f2 a378f96 eb8e5f2 4bc3d12 a378f96 eb8e5f2 76979fd 4bc3d12 94b902a 5b1fa3d eb8e5f2 | 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 | 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) |