# iamdeepakhr/stock-price-prediction-using-machine-learning architecture

> Architecture diagram of the GitHub repository iamdeepakhr/stock-price-prediction-using-machine-learning, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iamdeepakhr/stock-price-prediction-using-machine-learning
- Repository: https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning

## Overview

##### Main Components
- Frontend (User Interface):
- Represented by the HTML template (templates/index.html) and static files (static/script.js, static/style.css).
- UI components, including interactive graph elements, are powered by Chart.js.
- Backend Web Server:
- Managed by Flask (app.py), which handles API requests from the client.
- Acts as the controller to process user inputs, fetch data, and serve web pages.
- Machine Learning Models:
- The trained LSTM model (model/stock_lstm_model.keras) and scaler (model/scaler.pkl) are used to predict stock prices.
- The model training and retraining are handled by train_model.py.
- External Data Source:
- Integration with Yahoo Finance API for real-time stock data acquisition.
- Data Flow & Processing:
- Python libraries like yFinance and TensorFlow/Keras that bring data processing and model inference into the flow.

##### Relationships and Interactions
- Users interact with the Frontend via their web browsers.
- The Frontend sends requests (e.g., input stock symbols, trigger retraining) to the Flask backend.
- The Flask backend receives requests and either:
- Contacts the Yahoo Finance API to fetch current and historical stock data.
- Uses the pre-trained LSTM model and scaler to process the data and generate predictions.
- Or triggers retraining by running the train_model.py script to update the model with new data.
- Once processed, the data (actual stock prices and predictions) are sent back to the Frontend where they are visualized (using Chart.js).

##### Architectural Patterns and Design Principles
- Separation of Concerns: The frontend is strictly for presentation while the backend serves the application logic and machine learning inference.
- Modularity: Each component (UI, backend logic, ML model, external API interaction) is independently manageable.
- Dynamic Data Fetching: Real-time integration with Yahoo Finance to ensure up-to-date predictions.
- Scalability & Maintainability: Code is organized to allow for easy tweaks to data handling, UI update, and model retraining.

## Diagram (Mermaid)

```mermaid
flowchart TD
    User["User/Browser"]:::user
    Frontend["Frontend (HTML/CSS/JS + Chart.js)"]:::frontend
    Backend["Backend (Flask Application)"]:::backend
    ML["Machine Learning Module (LSTM Model & scaler)"]:::ml
    Training["Model Training Module (train_model.py)"]:::training
    Yahoo["Yahoo Finance API"]:::external

    User -->|"requests"| Frontend
    Frontend -->|"API_request"| Backend
    Frontend -->|"retraining_request"| Backend
    Backend -->|"fetch_data"| Yahoo
    Backend -->|"invoke_prediction"| ML
    ML -->|"return_prediction"| Backend
    Backend -->|"serve_response"| Frontend
    Frontend -->|"display_data"| User
    Backend -->|"trigger_retraining"| Training
    Training -->|"update_model"| ML

    click Frontend "https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning/blob/main/templates/index.html"
    click Frontend "https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning/blob/main/static/script.js"
    click Frontend "https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning/blob/main/static/style.css"
    click Backend "https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning/blob/main/app.py"
    click ML "https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning/blob/main/model/stock_lstm_model.keras"
    click ML "https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning/blob/main/model/scaler.pkl"
    click Training "https://github.com/iamdeepakhr/stock-price-prediction-using-machine-learning/blob/main/train_model.py"

    classDef user fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef frontend fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef backend fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef ml fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef training fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef external fill:#D6EAF8,stroke:#2471A3,stroke-width:2px;
```
