-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker2w.py
34 lines (24 loc) · 1.03 KB
/
ticker2w.py
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
# Import libraries
import pandas as pd
import yfinance as yf
import numpy as np
from sklearn.linear_model import LinearRegression
# Get the ticker symbol from the user
symbol = input("Enter the ticker symbol: ")
# Download the stock data from Yahoo Finance
data = yf.Ticker(symbol).history(period="5y")
# Extract the input features and output as numpy arrays
X = data[["Open", "High", "Low", "Close", "Volume"]].values
y = data[["High", "Low"]].values
# Create a linear regression model
model = LinearRegression()
# Fit the model to the data
model.fit(X, y)
# Predict the stock high and low prices for the next two weeks
predictions = model.predict(np.array([data.iloc[-1][["Open", "High", "Low", "Close", "Volume"]].values] * 7))
# Calculate the minimum and maximum predicted prices
min_price = min(predictions[:, 1])
max_price = max(predictions[:, 0])
# Print the predicted price range
print(f"The latest close is ${data['Close'].iloc[-1]:.2f}")
print(f"The predicted price range for the next two weeks is ${min_price:.2f} to ${max_price:.2f}")