From f367668a4ea765ac951904d9a6c458df369c9a1d Mon Sep 17 00:00:00 2001 From: Gabs <76870170+privacyrespected@users.noreply.github.com> Date: Tue, 21 Feb 2023 13:34:06 +0000 Subject: [PATCH] Update main.py --- main.py | 79 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 3f5ba7e..e375f99 100644 --- a/main.py +++ b/main.py @@ -1,28 +1,61 @@ -import sys -import time -import pyfiglet -def slowprint(s): - for c in s + '\n': - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(1./1000) -def ascii(input): - result= pyfiglet.figlet_format(input) - return result -### formatting functions -results = ascii("Omega") -print(results) -slowprint("Version 1.0 Alpha") -########################################################## import pandas as pd +from sklearn.linear_model import LinearRegression +from sklearn.model_selection import train_test_split import yfinance as yf -import matplotlib.pyplot as plt -import ta -import pandas_ta as pta -from finta import TA +from datetime import datetime, timedelta +# Set the ticker and the start and end dates +ticker = 'TSLA' +start_date = '2010-02-09' +end_date = '2023-02-17' +# Use the Ticker object to download the data +ticker_data = yf.Ticker(ticker) +df = ticker_data.history(start=start_date, end=end_date) -df = yf.download("AAPL", start="2020-01-01", end="2020-12-31") +# Save the DataFrame to a CSV file +df.to_csv('stock_data.csv', index=True) + +# Load the data into a Pandas DataFrame +df = pd.read_csv('stock_data.csv') + +# Create a new DataFrame with only the closing prices +prices = df[['Close']] + +# Shift the prices so that the first row corresponds to today's price and the second row corresponds to yesterday's price +prices_shifted = prices.shift(1) + +# Add a column with tomorrow's closing price +prices_shifted['Tomorrow'] = prices['Close'] + +# Drop the first row, which has a null value for yesterday's price +prices_shifted = prices_shifted.dropna() + +# Separate the target variable (tomorrow's closing price) from the input features (yesterday's closing price) +X = prices_shifted[['Close']] +y = prices_shifted['Tomorrow'] + +# Split the data into training and test sets +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Create a Linear Regression model +model = LinearRegression() + +# Train the model on the training data +model.fit(X_train, y_train) + +# Make predictions on the test data +y_pred = model.predict(X_test) + +# Calculate the test score +test_score = model.score(X_test, y_test) +print(f'Test score: {test_score:.2f}') +n=0 +# Use the model to predict tomorrow's closing price based on today's closing price +today_close = df['Close'].iloc[-1] +tomorrow_close_prediction=today_close +for i in range(1,31): + tomorrow_close_prediction = model.predict([[tomorrow_close_prediction]])[n] + print(tomorrow_close_prediction) + +print('#'*80) -df.plot() -plt.show()