# collab-with-sudhar/stock-market-decision-optimizer architecture

> Architecture diagram of the GitHub repository collab-with-sudhar/stock-market-decision-optimizer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/collab-with-sudhar/stock-market-decision-optimizer
- Repository: https://github.com/collab-with-sudhar/stock-market-decision-optimizer

## Overview

#### Purpose and System Shape
An end-to-end trading decision platform: reinforcement-learning models generate BUY/HOLD/SELL signals from market data; a Node backend exposes authenticated trading/monitoring APIs; a React client visualizes portfolio, orders, decisions, and live updates. Python services support model training, inference, and market-data streaming.

#### Client Application and Entry
The Vite/React SPA starts at `client/src/main.jsx` and composes routing/application state in `client/src/App.jsx`. Dashboard-oriented pages cover landing, authentication, portfolio, orders, and operational views under `client/src/pages/`. `client/src/components/DashboardLayout.jsx` forms the authenticated workspace shell.

#### Client State, API, and Live Updates
Redux slices partition client state for auth, orders, portfolio, trades, signals, charts, and WebSocket status in `client/src/redux/store.js`, `client/src/redux/signalsSlice.js`, and `client/src/redux/websocketSlice.js`. HTTP calls are centralized through Axios/API wrappers (`client/src/api/axiosInstance.js`, `client/src/api/api.js`), while socket lifecycle and event consumption are handled by `client/src/services/socket.js` and `client/src/hooks/useWebSocket.js`. Protected navigation is enforced client-side by `client/src/components/ProtectedRoute.jsx`.

#### Backend API and Trading Boundary
The Node server enters at `server/index.js`; configuration is established in `server/config.js`. Route groups separate authentication, market access, signals, monitoring, and trading: `server/routes/auth.js`, `server/routes/market.js`, and `server/routes/trading.js`. Controllers implement request-level orchestration (`server/controllers/userController.js`, `server/controllers/marketController.js`, `server/controllers/tradingController.js`), with JWT middleware at `server/middlewares/auth.js`.

#### Execution, Decisions, and Persistence
Trading orchestration crosses two critical service boundaries: model prediction via `server/services/modelClient.js`, and broker/order execution through `server/services/executionAdapter.js`. Persistent domain records represent users/accounts and trading lifecycle state: `server/models/userModel.js`, `server/models/Order.js`, `server/models/Position.js`, `server/models/Trade.js`, and `server/models/Decision.js`. This is the audit boundary between generated signals, submitted orders, fills/positions, and user-visible portfolio metrics.

#### Model Serving and RL Lifecycle
A Python model-serving process is rooted at `model_server/app.py`; it loads artifacts from `models/`, including NIFTY PPO and DQN models such as `models/ppo_nifty_finetuned.zip` and `models/dqn_nifty.zip`. The ML pipeline defines trading environments in `src/envs/gym_trading_env.py` and `src/envs/trading_env_improved.py`, with training/inference/evaluation entry scripts in `src/train_ppo.py`, `src/model_inference.py`, and `src/evaluate_ppo.py`. Prepared and raw NIFTY data live in `data/processed/NIFTY_15m_enriched.csv` and `data/raw/^NSEI.csv`.

#### Real-Time Market Feed
The realtime subsystem integrates SmartAPI-style market access: `realtime/smartapi_streamer.py` receives ticks, `realtime/tick_to_candle.py` converts them into candle data, and `realtime/market_schedule.py` gates activity around market hours. Its output is the likely operational input to signal inference and backend market/monitoring endpoints. Relevant external dependencies are the broker/SmartAPI service, the Python model server, database infrastructure implied by server models, and Vercel client deployment configuration at `client/vercel.json`.

Main entry points: [`client/src/components/DashboardLayout.jsx`](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/components/DashboardLayout.jsx) (Dashboard workspace), [`server/routes/trading.js`](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/routes/trading.js) (Trading API), [`server/middlewares/auth.js`](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/middlewares/auth.js) (Auth boundary), [`server/index.js`](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/index.js) (API server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React Client"]
  node_spa_entry["SPA entry<br/>Vite React<br/>[main.jsx]"]
  node_app_router["App routing<br/>React application<br/>[App.jsx]"]
  node_dashboard["Dashboard workspace<br/>authenticated UI"]
  node_client_state["Client state<br/>Redux store<br/>[store.js]"]
  node_http_client["HTTP API client<br/>Axios wrapper<br/>[axiosInstance.js]"]
  node_live_client["Live updates<br/>WebSocket client<br/>[socket.js]"]
end

subgraph group_backend["Node Backend"]
  node_node_server{{"API server<br/>Node server<br/>[index.js]"}}
  node_auth_boundary["Auth boundary<br/>JWT middleware and routes<br/>[auth.js]"]
  node_trading_api["Trading API<br/>routes and controller<br/>[trading.js]"]
  node_market_api["Market API<br/>routes and controller<br/>[market.js]"]
  node_model_client["Model client<br/>prediction adapter<br/>[modelClient.js]"]
  node_execution_adapter["Execution adapter<br/>broker adapter"]
  node_trading_records[("Trading records<br/>domain models<br/>[Decision.js]")]
end

subgraph group_ml["RL Model Pipeline"]
  node_model_server{{"Model serving<br/>Python inference service<br/>[app.py]"}}
  node_rl_environment["Trading environment<br/>RL environment<br/>[gym_trading_env.py]"]
  node_training_pipeline["PPO training<br/>training script<br/>[train_ppo.py]"]
  node_model_artifacts["RL model artifacts<br/>PPO/DQN models"]
end

subgraph group_realtime["Market Data Stream"]
  node_market_stream["Tick stream<br/>SmartAPI streamer"]
  node_candle_builder["Candle builder<br/>tick transformation<br/>[tick_to_candle.py]"]
end

node_broker(("Broker / SmartAPI"))
node_database[("Database<br/>external persistence")]

node_spa_entry -->|"mounts"| node_app_router
node_app_router -->|"renders protected workspace"| node_dashboard
node_dashboard -->|"reads and updates"| node_client_state
node_dashboard -->|"requests data"| node_http_client
node_dashboard -->|"subscribes"| node_live_client
node_http_client -->|"HTTPS API"| node_node_server
node_live_client -->|"live events"| node_node_server
node_node_server -->|"authenticates requests"| node_auth_boundary
node_auth_boundary -->|"authorizes"| node_trading_api
node_auth_boundary -->|"authorizes"| node_market_api
node_trading_api -->|"requests signal"| node_model_client
node_model_client -->|"prediction request"| node_model_server
node_trading_api -->|"submits order"| node_execution_adapter
node_execution_adapter -->|"executes orders"| node_broker
node_trading_api -->|"records decisions and lifecycle"| node_trading_records
node_market_api -->|"reads portfolio state"| node_trading_records
node_trading_records -->|"persists"| node_database
node_broker -->|"market ticks"| node_market_stream
node_market_stream -->|"ticks"| node_candle_builder
node_candle_builder -->|"inference input"| node_model_server
node_rl_environment -->|"simulates trading"| node_training_pipeline
node_training_pipeline -->|"produces"| node_model_artifacts
node_model_artifacts -->|"loaded by"| node_model_server

click node_spa_entry "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/main.jsx"
click node_app_router "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/App.jsx"
click node_dashboard "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/components/DashboardLayout.jsx"
click node_client_state "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/redux/store.js"
click node_http_client "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/api/axiosInstance.js"
click node_live_client "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/services/socket.js"
click node_node_server "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/index.js"
click node_auth_boundary "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/middlewares/auth.js"
click node_trading_api "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/routes/trading.js"
click node_market_api "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/routes/market.js"
click node_model_client "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/services/modelClient.js"
click node_execution_adapter "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/services/executionAdapter.js"
click node_trading_records "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/models/Decision.js"
click node_model_server "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/model_server/app.py"
click node_rl_environment "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/src/envs/gym_trading_env.py"
click node_training_pipeline "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/src/train_ppo.py"
click node_model_artifacts "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/models/ppo_nifty_finetuned.zip"
click node_market_stream "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/realtime/smartapi_streamer.py"
click node_candle_builder "https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/realtime/tick_to_candle.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spa_entry,node_app_router,node_dashboard,node_client_state,node_http_client,node_live_client toneBlue
class node_node_server,node_auth_boundary,node_trading_api,node_market_api,node_model_client,node_execution_adapter,node_trading_records toneAmber
class node_model_server,node_rl_environment,node_training_pipeline,node_model_artifacts toneMint
class node_market_stream,node_candle_builder toneRose
class node_broker,node_database toneNeutral
```

## Components

### React Client

- [SPA entry](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/main.jsx): Vite React · `client/src/main.jsx`
- [App routing](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/App.jsx): React application · `client/src/App.jsx`
- [Dashboard workspace](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/components/DashboardLayout.jsx): authenticated UI · Portfolio, orders, decisions, and operational views. · `client/src/components/DashboardLayout.jsx`
- [Client state](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/redux/store.js): Redux store · Auth, portfolio, orders, trades, signals, charts, and socket state. · `client/src/redux/store.js`
- [HTTP API client](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/api/axiosInstance.js): Axios wrapper · `client/src/api/axiosInstance.js`
- [Live updates](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/client/src/services/socket.js): WebSocket client · `client/src/services/socket.js`

### Node Backend

- [API server](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/index.js): Node server · `server/index.js`
- [Auth boundary](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/middlewares/auth.js): JWT middleware and routes · `server/middlewares/auth.js`
- [Trading API](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/routes/trading.js): routes and controller · Orchestrates order and decision requests. · `server/routes/trading.js`
- [Market API](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/routes/market.js): routes and controller · Serves market and monitoring access. · `server/routes/market.js`
- [Model client](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/services/modelClient.js): prediction adapter · `server/services/modelClient.js`
- [Execution adapter](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/services/executionAdapter.js): broker adapter · `server/services/executionAdapter.js`
- [Trading records](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/server/models/Decision.js): domain models · Users, orders, positions, trades, and decisions form the audit trail. · `server/models/Decision.js`

### RL Model Pipeline

- [Model serving](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/model_server/app.py): Python inference service · Loads trained RL artifacts and returns trading predictions. · `model_server/app.py`
- [Trading environment](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/src/envs/gym_trading_env.py): RL environment · `src/envs/gym_trading_env.py`
- [PPO training](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/src/train_ppo.py): training script · `src/train_ppo.py`
- [RL model artifacts](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/models/ppo_nifty_finetuned.zip): PPO/DQN models · `models/ppo_nifty_finetuned.zip`

### Market Data Stream

- [Tick stream](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/realtime/smartapi_streamer.py): SmartAPI streamer · `realtime/smartapi_streamer.py`
- [Candle builder](https://github.com/collab-with-sudhar/stock-market-decision-optimizer/blob/main/realtime/tick_to_candle.py): tick transformation · Converts scheduled ticks into candle data. · `realtime/tick_to_candle.py`

### Other components

- **Broker / SmartAPI**: external market and execution service
- **Database**: external persistence

## Connections

- SPA entry → App routing: mounts
- App routing → Dashboard workspace: renders protected workspace
- Dashboard workspace → Client state: reads and updates
- Dashboard workspace → HTTP API client: requests data
- Dashboard workspace → Live updates: subscribes
- HTTP API client → API server: HTTPS API
- Live updates → API server: live events
- API server → Auth boundary: authenticates requests
- Auth boundary → Trading API: authorizes
- Auth boundary → Market API: authorizes
- Trading API → Model client: requests signal
- Model client → Model serving: prediction request
- Trading API → Execution adapter: submits order
- Execution adapter → Broker / SmartAPI: executes orders
- Trading API → Trading records: records decisions and lifecycle
- Market API → Trading records: reads portfolio state
- Trading records → Database: persists
- Broker / SmartAPI → Tick stream: market ticks
- Tick stream → Candle builder: ticks
- Candle builder → Model serving: inference input
- Trading environment → PPO training: simulates trading
- PPO training → RL model artifacts: produces
- RL model artifacts → Model serving: loaded by
