File size: 7,612 Bytes
0dfdfb5 9e68217 70363ab 0dfdfb5 70363ab f0f6430 9e7913b 70363ab 9e68217 9e7913b 70363ab 9e68217 70363ab 9e68217 70363ab 9e68217 70363ab 9e68217 70363ab 0dfdfb5 70363ab 0dfdfb5 70363ab 0dfdfb5 70363ab 9e68217 70363ab 9e68217 70363ab 9e68217 70363ab 9e68217 70363ab 971d03b |
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
import yfinance as yf
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
import gradio as gr
from datetime import datetime, timedelta
# Ensure matplotlib does not require a display environment
import matplotlib
matplotlib.use('Agg')
# Define the stock tickers
STOCK_TICKERS = [
'AAPL', 'GOOGL', 'MSFT', 'AMZN', 'TSLA',
'FB', 'NVDA', 'JPM', 'V', 'DIS'
]
def fetch_data(ticker, start_date, end_date):
"""
Fetch historical stock data from yfinance.
Args:
ticker (str): Stock ticker symbol.
start_date (str): Start date in 'YYYY-MM-DD'.
end_date (str): End date in 'YYYY-MM-DD'.
Returns:
pd.DataFrame: Historical stock data.
"""
data = yf.download(ticker, start=start_date, end=end_date)
return data
def preprocess_data(data):
"""
Preprocess the stock data for model training.
Args:
data (pd.DataFrame): Raw stock data.
Returns:
np.ndarray, np.ndarray: Features and labels.
"""
# Calculate moving averages
data['MA10'] = data['Close'].rolling(window=10).mean()
data['MA20'] = data['Close'].rolling(window=20).mean()
# Drop NaN values
data = data.dropna()
# Features: Close, MA10, MA20
features = data[['Close', 'MA10', 'MA20']].values
# Labels: 1 if next day's Close > today's Close, else 0
data['Target'] = np.where(data['Close'].shift(-1) > data['Close'], 1, 0)
labels = data['Target'].values[:-1]
features = features[:-1]
return features, labels
def build_model(input_shape):
"""
Build and compile the TensorFlow model.
Args:
input_shape (int): Number of features.
Returns:
tf.keras.Model: Compiled model.
"""
model = models.Sequential([
layers.Dense(64, activation='relu', input_shape=(input_shape,)),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid') # Binary classification
])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
return model
# Train the model for each stock ticker and store in a dictionary
models_dict = {}
for ticker in STOCK_TICKERS:
# Fetch data for the past 5 years
end = datetime.today()
start = end - timedelta(days=5*365)
data = fetch_data(ticker, start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))
if data.empty:
print(f"No data found for {ticker}. Skipping...")
continue
features, labels = preprocess_data(data)
model = build_model(features.shape[1])
model.fit(features, labels, epochs=10, batch_size=32, verbose=0)
models_dict[ticker] = model
print(f"Model trained for {ticker}")
def predict_stock(ticker, start_date, end_date):
"""
Predict whether to Buy or Sell the stock based on user input.
Args:
ticker (str): Selected stock ticker.
start_date (str): Training start date.
end_date (str): Training end date.
Returns:
dict: Prediction results and graph.
"""
# Fetch data
data = fetch_data(ticker, start_date, end_date)
if data.empty:
return {
"Percentage Change": "No data available for the selected dates.",
"Highest Price": "N/A",
"Lowest Price": "N/A",
"Prediction": "N/A",
"Graph": None
}
# Preprocess data
features, labels = preprocess_data(data)
if features.size == 0:
return {
"Percentage Change": "Insufficient data after preprocessing.",
"Highest Price": "N/A",
"Lowest Price": "N/A",
"Prediction": "N/A",
"Graph": None
}
# Get the latest features for prediction
latest_data = data[['Close', 'MA10', 'MA20']].values[-1].reshape(1, -1)
# Predict using the trained model
model = models_dict.get(ticker)
if not model:
return {
"Percentage Change": "Model not found for the selected ticker.",
"Highest Price": "N/A",
"Lowest Price": "N/A",
"Prediction": "N/A",
"Graph": None
}
prediction = model.predict(latest_data)
prediction_label = "Buy" if prediction[0][0] > 0.5 else "Sell"
# Calculate percentage change
start_close = data['Close'].iloc[0]
latest_close = data['Close'].iloc[-1]
percent_change = ((latest_close - start_close) / start_close) * 100
# Highest and Lowest values
highest = data['Close'].max()
lowest = data['Close'].min()
# Plot historical data
plt.figure(figsize=(10,5))
plt.plot(data.index, data['Close'], label='Historical Close')
# Predict future 3 months (approx 63 trading days)
future_days = 63
# For simplicity, we'll extend the latest close with random walk
future_prices = [latest_close]
for _ in range(future_days):
change_percent = np.random.uniform(-0.02, 0.02) # Simulate small changes
new_price = future_prices[-1] * (1 + change_percent)
future_prices.append(new_price)
future_dates = pd.date_range(data.index[-1] + timedelta(days=1), periods=future_days+1, freq='B')
plt.plot(future_dates, future_prices[1:], label='Predicted Close')
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(f'{ticker} Historical and Predicted Performance')
plt.legend()
plt.tight_layout()
plt.savefig('performance.png')
plt.close()
# Prepare the result
result = {
"Percentage Change": f"{percent_change:.2f}%",
"Highest Price": f"${highest:.2f}",
"Lowest Price": f"${lowest:.2f}",
"Prediction": prediction_label,
"Graph": 'performance.png'
}
return result
# Define Gradio Interface
iface = gr.Interface(
fn=predict_stock,
inputs=[
gr.Dropdown(choices=STOCK_TICKERS, label="Select Stock Ticker"),
gr.DatePicker(label="Start Date"),
gr.DatePicker(label="End Date")
],
outputs=[
gr.Textbox(label="Percentage Change"),
gr.Textbox(label="Highest Price"),
gr.Textbox(label="Lowest Price"),
gr.Textbox(label="Buy/Sell Prediction"),
gr.Image(label="Performance Graph")
],
title="π Stock Buy/Sell Prediction App",
description=(
"Select a stock ticker and a date range to predict whether to **Buy** or **Sell** the stock. "
"View the percentage change, highest and lowest prices, and a performance graph."
)
)
# Launch the app
iface.launch()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input
def create_model(input_shape):
model = Sequential()
model.add(Input(shape=(input_shape,))) # Explicitly define input shape
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
data.loc[:, 'Target'] = np.where(data['Close'].shift(-1) > data['Close'], 1, 0)
import gradio as gr
def predict_stock(ticker, start_date, end_date):
# Your prediction logic
return "Prediction result"
interface = gr.Interface(
fn=predict_stock,
inputs=[
gr.inputs.Dropdown(['AAPL', 'MSFT', 'GOOG', 'AMZN', 'TSLA'], label="Stock Ticker"),
gr.inputs.Date(label="Start Date"), # Replace DatePicker with Date
gr.inputs.Date(label="End Date")
],
outputs="text"
)
interface.launch()
|