File size: 3,572 Bytes
3f42539 bcca0d1 787014a 3f42539 b0f32ad e95c170 b0f32ad e95c170 49d8288 b0f32ad 3f42539 b0f32ad 3f42539 e940d32 4315a6e 77a0854 e940d32 3f42539 1bf4d1f 4315a6e 77a0854 4315a6e 1bf4d1f eb86549 1bf4d1f 3f42539 | 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 | from sqlalchemy import create_engine, Column, Integer, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime
import streamlit as st
import pandas as pd
import time
import telebot
import yfinance as yf
import pandas as pd
import numpy as np
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from math import sqrt
# Database connection
engine = create_engine('sqlite:///date_time.db', echo=True)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
def give_integer_data():
data = yf.download('BTC-USD', period='max')#start="2019-01-01", end="2023-12-28")
# Select Open and Close price
data = data[['Open', 'Close']]
#print(data)
# Create a new "lagged" dataframe for the model
data['Previous_Close'] = data['Close'].shift(1)
# 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))
#print(f"Train RMSE: {train_rmse}")
test_predictions = model.predict(X_test)
test_rmse = sqrt(mean_squared_error(y_test, test_predictions))
#print(f"Test RMSE: {test_rmse}")
# Predict tomorrow's close price using the most recent 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)
return predicted_close[0]
# Define the table using the Declarative approach
class DateTimeData(Base):
__tablename__ = 'date_table'
id = Column(Integer, primary_key=True)
date_time = Column(DateTime, default=datetime.datetime.now)
integer_data = Column(Integer, default=give_integer_data) # Add a new column for integer data
# Create the table in the database
Base.metadata.create_all(engine)
# Streamlit app
st.title('Date and Time Saver')
show_data_button = st.button('Show Saved Data')
if show_data_button:
query = session.query(DateTimeData).all()
data = {'Date and Time': [entry.date_time for entry in query],
'Integer Data': [float(entry.integer_data) for entry in query]}
df = pd.DataFrame(data)
st.write(df)
# Button to save date and time to the database
save_button = st.button('Save Date and Time')
if save_button:
st.write('Data saving started')
while True:
integer_data = give_integer_data()
if isinstance(integer_data, np.float32):
integer_data = float(integer_data) # Convert numpy.float32 to standard float
new_data = DateTimeData(integer_data=integer_data) # Assign the integer value from the function
session.add(new_data)
session.commit()
st.write('Data saved')
time.sleep(3600)
#new_data = DateTimeData()
#session.add(new_data)
#session.commit()
# Button to show entire saved data
|