Create python
Browse files
python
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
def fetch_data(ticker, start_date, end_date):
|
| 3 |
+
# Fetch data using yfinance
|
| 4 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
| 5 |
+
return data
|
| 6 |
+
def preprocess_data(data):
|
| 7 |
+
# Select 'Close' prices for prediction
|
| 8 |
+
prices = data['Close'].values.reshape(-1, 1)
|
| 9 |
+
# Normalize the data
|
| 10 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 11 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 12 |
+
scaled_prices = scaler.fit_transform(prices)
|
| 13 |
+
|
| 14 |
+
# Create dataset with features and labels
|
| 15 |
+
x_data, y_data = [], []
|
| 16 |
+
for i in range(60, len(scaled_prices)):
|
| 17 |
+
x_data.append(scaled_prices[i-60:i, 0])
|
| 18 |
+
y_data.append(scaled_prices[i, 0])
|
| 19 |
+
return np.array(x_data), np.array(y_data), scaler
|
| 20 |
+
def build_model():
|
| 21 |
+
model = Sequential()
|
| 22 |
+
model.add(LSTM(units=50, return_sequences=True, input_shape=(60, 1)))
|
| 23 |
+
model.add(Dropout(0.2))
|
| 24 |
+
model.add(LSTM(units=50, return_sequences=False))
|
| 25 |
+
model.add(Dropout(0.2))
|
| 26 |
+
model.add(Dense(units=1)) # Prediction for the next price
|
| 27 |
+
model.compile(optimizer='adam', loss='mean_squared_error')
|
| 28 |
+
return model
|
| 29 |
+
def train_model(x_data, y_data):
|
| 30 |
+
# Reshape data for LSTM
|
| 31 |
+
x_data = np.reshape(x_data, (x_data.shape[0], x_data.shape[1], 1))
|
| 32 |
+
model = build_model()
|
| 33 |
+
model.fit(x_data, y_data, epochs=50, batch_size=32)
|
| 34 |
+
return model
|
| 35 |
+
def make_prediction(model, data, scaler):
|
| 36 |
+
# Prepare the last 60 days of data
|
| 37 |
+
last_data = data['Close'].values[-60:].reshape(-1, 1)
|
| 38 |
+
last_data_scaled = scaler.transform(last_data)
|
| 39 |
+
|
| 40 |
+
x_test = []
|
| 41 |
+
x_test.append(last_data_scaled)
|
| 42 |
+
x_test = np.array(x_test)
|
| 43 |
+
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
|
| 44 |
+
|
| 45 |
+
predicted_price = model.predict(x_test)
|
| 46 |
+
predicted_price = scaler.inverse_transform(predicted_price) # Convert back to original scale
|
| 47 |
+
return predicted_price[0][0]
|
| 48 |
+
def stock_prediction(ticker, start_date, end_date):
|
| 49 |
+
data = fetch_data(ticker, start_date, end_date)
|
| 50 |
+
x_data, y_data, scaler = preprocess_data(data)
|
| 51 |
+
model = train_model(x_data, y_data)
|
| 52 |
+
predicted_price = make_prediction(model, data, scaler)
|
| 53 |
+
|
| 54 |
+
# Calculate percentage change, highest and lowest values
|
| 55 |
+
percentage_change = ((predicted_price - data['Close'].iloc[-1]) / data['Close'].iloc[-1]) * 100
|
| 56 |
+
highest_price = data['Close'].max()
|
| 57 |
+
lowest_price = data['Close'].min()
|
| 58 |
+
|
| 59 |
+
# Visualization
|
| 60 |
+
plt.figure(figsize=(14, 5))
|
| 61 |
+
plt.plot(data['Close'], label='Historical Prices')
|
| 62 |
+
plt.axhline(y=predicted_price, color='r', linestyle='--', label='Predicted Price')
|
| 63 |
+
plt.title(f'{ticker} Price Prediction')
|
| 64 |
+
plt.xlabel('Date')
|
| 65 |
+
plt.ylabel('Price')
|
| 66 |
+
plt.legend()
|
| 67 |
+
plt.show()
|
| 68 |
+
|
| 69 |
+
return {
|
| 70 |
+
'Predicted Price': predicted_price,
|
| 71 |
+
'Percentage Change': percentage_change,
|
| 72 |
+
'Highest Price': highest_price,
|
| 73 |
+
'Lowest Price': lowest_price
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
# Gradio Interface
|
| 77 |
+
ticker_options = ['AAPL', 'GOOGL', 'AMZN', 'MSFT', 'TSLA', 'FB', 'NFLX', 'NVDA', 'BRK.B', 'DIS']
|
| 78 |
+
iface = gr.Interface(
|
| 79 |
+
fn=stock_prediction,
|
| 80 |
+
inputs=[
|
| 81 |
+
gr.inputs.Dropdown(choices=ticker_options, label='Stock Ticker'),
|
| 82 |
+
gr.inputs.Date(label='Start Date'),
|
| 83 |
+
gr.inputs.Date(label='End Date')
|
| 84 |
+
],
|
| 85 |
+
outputs='json'
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
iface.launch()
|