Skip to content

Commit

Permalink
stock_price_predict_basic_v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
quanta-guy committed Aug 25, 2024
0 parents commit 0acb1c7
Show file tree
Hide file tree
Showing 141 changed files with 5,661 additions and 0 deletions.
128 changes: 128 additions & 0 deletions README .md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@

# Real-Time Stock Market Data Prediction API and Flutter App

## Overview
This project combines a real-time stock market data prediction API built using FastAPI and an accompanying Flutter frontend application. The API processes stock ticker data to predict future stock prices using an LSTM (Long Short-Term Memory) neural network, and the Flutter app provides an interface for users to interact with the API.

## Prerequisites

### Backend (FastAPI)
- Python 3.7 or higher
- Conda or virtualenv for environment management
- FastAPI
- Uvicorn
- TensorFlow
- Scikit-learn
- yfinance

### Frontend (Flutter)
- Flutter SDK
- A text editor or IDE (e.g., Visual Studio Code, Android Studio)

## Setup Instructions

### Backend Setup

1. **Clone the repository:**
```bash
git clone <repository_url>
cd <repository_name>
```

2. **Create a virtual environment:**
- Using Conda:
```bash
conda create --name stock-prediction python=3.8
conda activate stock-prediction
```
- Or using virtualenv:
```bash
python3 -m venv env
source env/bin/activate # On Windows use `env\Scriptsctivate`
```

3. **Install dependencies:**
```bash
pip install -r requirements.txt
```

4. **Run the API server:**
```bash
uvicorn main:app --reload
```

The API will be available at `http://127.0.0.1:8000/`.

### Frontend Setup

1. **Install Flutter SDK:**
Follow the instructions on the official [Flutter installation guide](https://flutter.dev/docs/get-started/install) to set up Flutter on your system.

2. **Create a new Flutter project:**
```bash
flutter create stock_prediction_app
cd stock_prediction_app
```

3. **Replace the `main.dart` and `pubspec.yaml`:**
Replace the `lib/main.dart` file in your Flutter project with the `main.dart` provided in this repository. Similarly, replace the `pubspec.yaml` file with the one provided.

4. **Install Flutter dependencies:**
In the root of your Flutter project directory, run:
```bash
flutter pub get
```

5. **Run the Flutter app:**
Connect a device or start an emulator, then run:
```bash
flutter run
```

The Flutter app will connect to the FastAPI backend to display real-time stock predictions.

## Project Structure

### Backend
- **`main.py`**: The FastAPI application that handles incoming requests and routes them to the stock prediction function.
- **`predictor.py`**: Contains the logic for data preprocessing, model training, and stock price prediction.
- **`requirements.txt`**: Lists the dependencies required for the backend.

### Frontend
- **`lib/main.dart`**: The main Dart file for the Flutter app, responsible for the UI and API integration.
- **`pubspec.yaml`**: Specifies the dependencies required for the Flutter app.

## Detailed Functionality

### API Endpoint
- **POST `/stock/{ticker}`**: Predicts future stock prices for a given ticker symbol over a specified period. The prediction includes:
- Current stock price
- Predicted prices for the forecasting period
- Historical prices
- Mean Squared Error (MSE) and Root Mean Squared Error (RMSE)
- A prediction label (`CALL` or `SELL`)

### Prediction Methodology
- The `predictor.py` script fetches historical stock data using the `yfinance` library.
- Data is preprocessed using scaling techniques to normalize the stock prices.
- The LSTM model is trained on the historical data to learn patterns.
- The model predicts future prices based on the last known data.
- Predictions are scaled back to the original price range, and the results are evaluated for accuracy.

### Model Architecture
- The LSTM network is designed with two LSTM layers followed by dropout layers to prevent overfitting.
- The final Dense layer outputs the predicted stock price.

## Example Usage

Once both the API server and the Flutter app are running, you can interact with the app to select a stock ticker and view the predicted prices.

## Future Scope

- **Attention Mechanism**: Implementing an attention mechanism to improve the accuracy of predictions by focusing on more critical time steps.
- **Extended UI**: Enhancing the Flutter frontend to provide more interactive features and better visualizations.
- **Additional Models**: Experimenting with other machine learning models to compare performance and accuracy.

## Conclusion

This project demonstrates the application of LSTM neural networks in stock market prediction, providing users with valuable insights into potential future stock prices through an intuitive Flutter frontend. The API is built to be scalable and easy to use, with future improvements planned to enhance prediction accuracy and user experience.
Binary file added backend/__pycache__/main.cpython-39.pyc
Binary file not shown.
Empty file added backend/dict
Empty file.
14 changes: 14 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from fastapi import FastAPI, Query
from stock_predictor.predictor import get_stock_prediction

app = FastAPI()

@app.post("/stock/{ticker}")
async def stock_prediction(ticker: str, forecasting_period: int = Query(30)):
# Call the synchronous function directly
result = get_stock_prediction(ticker, forecasting_period)
return result

if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
6 changes: 6 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
yfinance
numpy
pandas
matplotlib
scikit-learn
tensorflow
Empty file.
Binary file not shown.
Binary file not shown.
86 changes: 86 additions & 0 deletions backend/stock_predictor/predictor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import numpy as np
import pandas as pd
import yfinance as yf
from sklearn.preprocessing import MinMaxScaler, RobustScaler
from sklearn.metrics import mean_squared_error
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, Input
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau

def get_stock_prediction(ticker: str, forecasting_period: int):
data = yf.download(ticker, start='2010-01-01')

if data.empty:
raise ValueError(f"No data found for ticker {ticker}")

data = data[['Close']]
data = data.fillna(method='ffill')

if data.isnull().values.any():
raise ValueError("Data contains NaN values even after forward fill")

training_data_len = int(np.ceil(len(data) * 0.8))
train_data = data[:training_data_len]
test_data = data[training_data_len:]

scaler = RobustScaler()
scaled_train_data = scaler.fit_transform(train_data)
scaled_test_data = scaler.transform(test_data)

def create_sequences(data, seq_length):
xs, ys = [], []
for i in range(len(data) - seq_length):
x = data[i:i + seq_length]
y = data[i + seq_length]
xs.append(x)
ys.append(y)
return np.array(xs), np.array(ys)

SEQ_LENGTH = 60
X_train, y_train = create_sequences(scaled_train_data, SEQ_LENGTH)
X_test, y_test = create_sequences(scaled_test_data, SEQ_LENGTH)

model = Sequential([
Input(shape=(SEQ_LENGTH, 1)),
LSTM(50, return_sequences=True),
Dropout(0.2),
LSTM(50),
Dropout(0.2),
Dense(1)
])

model.compile(optimizer='adam', loss='mean_squared_error')

early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=0.0001)

model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.1, verbose=1,
callbacks=[early_stopping, reduce_lr])

predictions = []
last_sequence = X_test[-1]

for _ in range(forecasting_period):
prediction = model.predict(last_sequence.reshape(1, SEQ_LENGTH, 1))
predictions.append(prediction[0][0])
last_sequence = np.append(last_sequence[1:], prediction, axis=0)

predictions = np.array(predictions).reshape(-1, 1)
predictions = scaler.inverse_transform(predictions)
current_price = scaler.inverse_transform(y_test[-1].reshape(-1, 1))[0][0]

mse = mean_squared_error(y_test, model.predict(X_test))
rmse = np.sqrt(mse)

predictions = predictions.flatten().tolist()
mse = float(mse)
rmse = float(rmse)

return {
'current_price': current_price,
'predicted_prices': predictions,
'mse': mse,
'rmse': rmse,
'historical_prices': data['Close'].values[-SEQ_LENGTH:].tolist(),
'prediction': 'CALL' if predictions[-1] > current_price else 'SELL'
}
Binary file added documents/quanta_guys_techathon_2024.pptx
Binary file not shown.
43 changes: 43 additions & 0 deletions frontend_complete/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
45 changes: 45 additions & 0 deletions frontend_complete/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "a14f74ff3a1cbd521163c5f03d68113d50af93d3"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
- platform: android
create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
- platform: ios
create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
- platform: linux
create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
- platform: macos
create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
- platform: web
create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
- platform: windows
create_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3
base_revision: a14f74ff3a1cbd521163c5f03d68113d50af93d3

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
16 changes: 16 additions & 0 deletions frontend_complete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# app

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
28 changes: 28 additions & 0 deletions frontend_complete/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at https://dart.dev/lints.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
13 changes: 13 additions & 0 deletions frontend_complete/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java

# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
key.properties
**/*.keystore
**/*.jks
Loading

0 comments on commit 0acb1c7

Please sign in to comment.