# tamilp/1placetrade architecture

> Architecture diagram of the GitHub repository tamilp/1placetrade, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tamilp/1placetrade
- Repository: https://github.com/tamilp/1placetrade

## Overview

#### Purpose and topology
A TypeScript monorepo for an options-trading platform: live market monitoring, broker-connected live execution, paper trading, strategy automation, historical backtesting, reporting, and AI assistance. It has a React/Vite frontend, a Node.js backend API/WebSocket process, and a shared contracts package.
- `backend/src/index.ts`
- `frontend/src/main.tsx`
- `shared/src/index.ts`

#### Application entry and client boundary
The backend process is the system integration boundary: it exposes feature routes and a WebSocket server. The SPA starts at `main.tsx`, renders `App.tsx`, calls HTTP through a centralized client, and consumes server-pushed market/trading updates through a WebSocket hook.
- `backend/src/index.ts`
- `backend/src/ws/wsServer.ts`
- `frontend/src/App.tsx`
- `frontend/src/api/client.ts`
- `frontend/src/api/useWebSocket.ts`

#### Identity, tenancy, and persistence
Authentication/authorization gates protected operations, including entitlement checks. Persistent domain state is abstracted behind `StorageAdapter`, with PostgreSQL as the material adapter and schema source; trade and journal stores support execution history and reporting. Shared types define broker, market, signal, and strategy contracts across tiers.
- `backend/src/auth/requireAuth.ts`
- `backend/src/auth/requireEntitlement.ts`
- `backend/src/storage/StorageAdapter.ts`
- `backend/src/storage/PgStorageAdapter.ts`
- `backend/src/storage/schema.sql`
- `shared/src/types/strategy-automation.ts`

#### Broker and market-data integration
`BrokerRegistry` and broker adapters isolate Angel One, Dhan, Fyers, Upstox, and Zerodha APIs behind normalized execution/feed interfaces. Instrument-master registries resolve broker-specific symbols; feeds, OI snapshots, candle aggregation, and signal engine transform live inputs into dashboard and automation-ready market state.
- `backend/src/brokers/BrokerRegistry.ts`
- `backend/src/brokers/MarketFeedClient.ts`
- `backend/src/brokers/instrumentMasterRegistry.ts`
- `backend/src/candle-aggregator/index.ts`
- `backend/src/signal-engine/index.ts`

#### Trading execution modes and safeguards
Paper trading simulates orders, positions, premiums, and brokerage independently of brokers. Live trading delegates orders through broker connections. Global exit and trading-status services provide cross-cutting controls; frontend pages separate paper and live workflows.
- `backend/src/paper-trading/PaperTradingService.ts`
- `backend/src/live-trading/LiveTradingService.ts`
- `backend/src/live-trading/liveOrderExecution.ts`
- `backend/src/services/globalExitService.ts`
- `frontend/src/features/paper-trading/PaperTradingPage.tsx`
- `frontend/src/features/live-trading/LiveTradingPage.tsx`

#### Strategy automation pipeline
Schedules invoke strategy execution; market-data services construct inputs, strategy implementations and engine rules generate/filter recommendations, then position adaptation and execution connect decisions to trading state. This is the core automated-trading control loop, including daily account caps and failure protection.
- `backend/src/strategy-automation/StrategyScheduleService.ts`
- `backend/src/strategy-automation/StrategyExecutionService.ts`
- `backend/src/strategy-automation/StrategyMarketDataService.ts`
- `backend/src/strategy-automation/StrategyRecommendationService.ts`
- `backend/src/strategy-automation/strategies/index.ts`
- `backend/src/strategy-automation/engineRules/failureProtection.ts`

#### Historical analysis and user-facing operations
Backtests run through an engine/runner with worker isolation and historical chain/market providers. Reports consume stored trade/journal data. AI assistance builds trading context, selects Anthropic/Gemini/OpenAI-compatible providers, rate-limits usage, and exposes dedicated routes/UI. Notification services integrate email, Telegram, web push, and webhooks.
- `backend/src/backtesting/backtestRunner.ts`
- `backend/src/backtesting/runBacktestInWorker.ts`
- `backend/src/backtesting/historicalMarketBuilder.ts`
- `backend/src/ai-assistant/ProviderManager.ts`
- `backend/src/ai-assistant/aiContextBuilder.ts`
- `backend/src/services/alertDispatch.ts`

Main entry points: [`backend/src/index.ts`](https://github.com/tamilp/1placetrade/blob/main/backend/src/index.ts) (Backend process), [`frontend/src/App.tsx`](https://github.com/tamilp/1placetrade/blob/main/frontend/src/App.tsx) (Application shell), [`backend/src/strategy-automation/StrategyExecutionService.ts`](https://github.com/tamilp/1placetrade/blob/main/backend/src/strategy-automation/StrategyExecutionService.ts) (Strategy execution), [`backend/src/live-trading/LiveTradingService.ts`](https://github.com/tamilp/1placetrade/blob/main/backend/src/live-trading/LiveTradingService.ts) (Live trading)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React SPA"]
  node_spa_entry["SPA entry<br/>React entry<br/>[main.tsx]"]
  node_app["Application shell<br/>React app<br/>[App.tsx]"]
  node_http_client["HTTP client<br/>API client<br/>[client.ts]"]
  node_websocket_hook["WebSocket consumer<br/>React hook<br/>[useWebSocket.ts]"]
  node_trading_pages["Paper &amp; live pages<br/>React workflows"]
end

subgraph group_api["Backend boundary"]
  node_backend{{"Backend process<br/>Node API<br/>[index.ts]"}}
  node_ws_server["WebSocket server<br/>push gateway<br/>[wsServer.ts]"]
  node_auth["Auth &amp; entitlements<br/>access control<br/>[requireAuth.ts]"]
  node_ai_assistant["AI assistant<br/>provider orchestration<br/>[ProviderManager.ts]"]
  node_alerts["Alert dispatch<br/>notifications<br/>[alertDispatch.ts]"]
end

subgraph group_trading["Trading &amp; automation"]
  node_paper_trading["Paper trading<br/>simulated execution"]
  node_live_trading["Live trading<br/>broker execution"]
  node_risk_controls["Global exit controls<br/>trading safeguard"]
  node_strategy_schedule["Strategy scheduler<br/>automation trigger"]
  node_strategy_execution["Strategy execution<br/>automation pipeline"]
  node_backtesting["Backtest runner<br/>historical analysis<br/>[backtestRunner.ts]"]
end

subgraph group_data["Data &amp; integrations"]
  node_storage[("PostgreSQL storage adapter<br/>persistence")]
  node_shared_contracts["Strategy contracts<br/>shared TypeScript types"]
  node_broker_registry["Broker registry<br/>broker adapters<br/>[BrokerRegistry.ts]"]
  node_market_feed["Market feed client<br/>live market input"]
  node_market_processing["Candles &amp; signals<br/>market processing<br/>[index.ts]"]
end

node_spa_entry -->|"renders"| node_app
node_app -->|"calls APIs"| node_http_client
node_app -->|"subscribes"| node_websocket_hook
node_app -->|"hosts workflows"| node_trading_pages
node_http_client -->|"HTTP"| node_backend
node_websocket_hook -->|"server push"| node_ws_server
node_backend -->|"protects operations"| node_auth
node_backend -->|"persists domain state"| node_storage
node_backend -->|"publishes updates"| node_ws_server
node_backend -->|"routes paper orders"| node_paper_trading
node_backend -->|"routes live orders"| node_live_trading
node_backend -->|"runs analyses"| node_backtesting
node_backend -->|"serves assistance"| node_ai_assistant
node_market_feed -->|"live inputs"| node_market_processing
node_broker_registry -->|"broker feeds"| node_market_feed
node_market_processing -->|"market updates"| node_ws_server
node_paper_trading -->|"stores trades"| node_storage
node_live_trading -->|"submits orders"| node_broker_registry
node_live_trading -->|"subject to exits"| node_risk_controls
node_strategy_schedule -->|"invokes"| node_strategy_execution
node_market_processing -->|"market state"| node_strategy_execution
node_strategy_execution -->|"automated execution"| node_live_trading
node_strategy_execution -->|"caps &amp; failure protection"| node_risk_controls
node_strategy_execution -->|"notifies outcomes"| node_alerts
node_backtesting -->|"reads history"| node_storage
node_ai_assistant -->|"builds trading context"| node_storage
node_shared_contracts -.->|"shared contracts"| node_app
node_shared_contracts -.->|"shared contracts"| node_backend

click node_spa_entry "https://github.com/tamilp/1placetrade/blob/main/frontend/src/main.tsx"
click node_app "https://github.com/tamilp/1placetrade/blob/main/frontend/src/App.tsx"
click node_http_client "https://github.com/tamilp/1placetrade/blob/main/frontend/src/api/client.ts"
click node_websocket_hook "https://github.com/tamilp/1placetrade/blob/main/frontend/src/api/useWebSocket.ts"
click node_trading_pages "https://github.com/tamilp/1placetrade/blob/main/frontend/src/features/live-trading/LiveTradingPage.tsx"
click node_backend "https://github.com/tamilp/1placetrade/blob/main/backend/src/index.ts"
click node_ws_server "https://github.com/tamilp/1placetrade/blob/main/backend/src/ws/wsServer.ts"
click node_auth "https://github.com/tamilp/1placetrade/blob/main/backend/src/auth/requireAuth.ts"
click node_storage "https://github.com/tamilp/1placetrade/blob/main/backend/src/storage/PgStorageAdapter.ts"
click node_shared_contracts "https://github.com/tamilp/1placetrade/blob/main/shared/src/types/strategy-automation.ts"
click node_broker_registry "https://github.com/tamilp/1placetrade/blob/main/backend/src/brokers/BrokerRegistry.ts"
click node_market_feed "https://github.com/tamilp/1placetrade/blob/main/backend/src/brokers/MarketFeedClient.ts"
click node_market_processing "https://github.com/tamilp/1placetrade/blob/main/backend/src/signal-engine/index.ts"
click node_paper_trading "https://github.com/tamilp/1placetrade/blob/main/backend/src/paper-trading/PaperTradingService.ts"
click node_live_trading "https://github.com/tamilp/1placetrade/blob/main/backend/src/live-trading/LiveTradingService.ts"
click node_risk_controls "https://github.com/tamilp/1placetrade/blob/main/backend/src/services/globalExitService.ts"
click node_strategy_schedule "https://github.com/tamilp/1placetrade/blob/main/backend/src/strategy-automation/StrategyScheduleService.ts"
click node_strategy_execution "https://github.com/tamilp/1placetrade/blob/main/backend/src/strategy-automation/StrategyExecutionService.ts"
click node_backtesting "https://github.com/tamilp/1placetrade/blob/main/backend/src/backtesting/backtestRunner.ts"
click node_ai_assistant "https://github.com/tamilp/1placetrade/blob/main/backend/src/ai-assistant/ProviderManager.ts"
click node_alerts "https://github.com/tamilp/1placetrade/blob/main/backend/src/services/alertDispatch.ts"

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,node_http_client,node_websocket_hook,node_trading_pages toneBlue
class node_backend,node_ws_server,node_auth,node_ai_assistant,node_alerts toneAmber
class node_paper_trading,node_live_trading,node_risk_controls,node_strategy_schedule,node_strategy_execution,node_backtesting toneMint
class node_storage,node_shared_contracts,node_broker_registry,node_market_feed,node_market_processing toneRose
```

## Components

### React SPA

- [SPA entry](https://github.com/tamilp/1placetrade/blob/main/frontend/src/main.tsx): React entry · `frontend/src/main.tsx`
- [Application shell](https://github.com/tamilp/1placetrade/blob/main/frontend/src/App.tsx): React app · `frontend/src/App.tsx`
- [HTTP client](https://github.com/tamilp/1placetrade/blob/main/frontend/src/api/client.ts): API client · `frontend/src/api/client.ts`
- [WebSocket consumer](https://github.com/tamilp/1placetrade/blob/main/frontend/src/api/useWebSocket.ts): React hook · `frontend/src/api/useWebSocket.ts`
- [Paper & live pages](https://github.com/tamilp/1placetrade/blob/main/frontend/src/features/live-trading/LiveTradingPage.tsx): React workflows · Separate user workflows for simulated and broker-connected trading. · `frontend/src/features/live-trading/LiveTradingPage.tsx`

### Backend boundary

- [Backend process](https://github.com/tamilp/1placetrade/blob/main/backend/src/index.ts): Node API · Feature-route and integration boundary. · `backend/src/index.ts`
- [WebSocket server](https://github.com/tamilp/1placetrade/blob/main/backend/src/ws/wsServer.ts): push gateway · `backend/src/ws/wsServer.ts`
- [Auth & entitlements](https://github.com/tamilp/1placetrade/blob/main/backend/src/auth/requireAuth.ts): access control · `backend/src/auth/requireAuth.ts`
- [AI assistant](https://github.com/tamilp/1placetrade/blob/main/backend/src/ai-assistant/ProviderManager.ts): provider orchestration · Builds trading context and selects compatible AI providers. · `backend/src/ai-assistant/ProviderManager.ts`
- [Alert dispatch](https://github.com/tamilp/1placetrade/blob/main/backend/src/services/alertDispatch.ts): notifications · Delivers email, Telegram, push, and webhook notifications. · `backend/src/services/alertDispatch.ts`

### Trading & automation

- [Paper trading](https://github.com/tamilp/1placetrade/blob/main/backend/src/paper-trading/PaperTradingService.ts): simulated execution · Simulates orders, positions, premiums, and brokerage. · `backend/src/paper-trading/PaperTradingService.ts`
- [Live trading](https://github.com/tamilp/1placetrade/blob/main/backend/src/live-trading/LiveTradingService.ts): broker execution · `backend/src/live-trading/LiveTradingService.ts`
- [Global exit controls](https://github.com/tamilp/1placetrade/blob/main/backend/src/services/globalExitService.ts): trading safeguard · `backend/src/services/globalExitService.ts`
- [Strategy scheduler](https://github.com/tamilp/1placetrade/blob/main/backend/src/strategy-automation/StrategyScheduleService.ts): automation trigger · `backend/src/strategy-automation/StrategyScheduleService.ts`
- [Strategy execution](https://github.com/tamilp/1placetrade/blob/main/backend/src/strategy-automation/StrategyExecutionService.ts): automation pipeline · Builds inputs, generates recommendations, and applies failure protection. · `backend/src/strategy-automation/StrategyExecutionService.ts`
- [Backtest runner](https://github.com/tamilp/1placetrade/blob/main/backend/src/backtesting/backtestRunner.ts): historical analysis · Runs isolated worker backtests with historical market inputs. · `backend/src/backtesting/backtestRunner.ts`

### Data & integrations

- [PostgreSQL storage adapter](https://github.com/tamilp/1placetrade/blob/main/backend/src/storage/PgStorageAdapter.ts): persistence · Domain-state abstraction backed by PostgreSQL. · `backend/src/storage/PgStorageAdapter.ts`
- [Strategy contracts](https://github.com/tamilp/1placetrade/blob/main/shared/src/types/strategy-automation.ts): shared TypeScript types · Cross-tier broker, market, signal, and strategy contracts. · `shared/src/types/strategy-automation.ts`
- [Broker registry](https://github.com/tamilp/1placetrade/blob/main/backend/src/brokers/BrokerRegistry.ts): broker adapters · Normalizes Angel One, Dhan, Fyers, Upstox, and Zerodha connections. · `backend/src/brokers/BrokerRegistry.ts`
- [Market feed client](https://github.com/tamilp/1placetrade/blob/main/backend/src/brokers/MarketFeedClient.ts): live market input · `backend/src/brokers/MarketFeedClient.ts`
- [Candles & signals](https://github.com/tamilp/1placetrade/blob/main/backend/src/signal-engine/index.ts): market processing · Aggregates live feeds into candles and actionable signal state. · `backend/src/signal-engine/index.ts`

## Connections

- SPA entry → Application shell: renders
- Application shell → HTTP client: calls APIs
- Application shell → WebSocket consumer: subscribes
- Application shell → Paper & live pages: hosts workflows
- HTTP client → Backend process: HTTP
- WebSocket consumer → WebSocket server: server push
- Backend process → Auth & entitlements: protects operations
- Backend process → PostgreSQL storage adapter: persists domain state
- Backend process → WebSocket server: publishes updates
- Backend process → Paper trading: routes paper orders
- Backend process → Live trading: routes live orders
- Backend process → Backtest runner: runs analyses
- Backend process → AI assistant: serves assistance
- Market feed client → Candles & signals: live inputs
- Broker registry → Market feed client: broker feeds
- Candles & signals → WebSocket server: market updates
- Paper trading → PostgreSQL storage adapter: stores trades
- Live trading → Broker registry: submits orders
- Live trading → Global exit controls: subject to exits
- Strategy scheduler → Strategy execution: invokes
- Candles & signals → Strategy execution: market state
- Strategy execution → Live trading: automated execution
- Strategy execution → Global exit controls: caps & failure protection
- Strategy execution → Alert dispatch: notifies outcomes
- Backtest runner → PostgreSQL storage adapter: reads history
- AI assistant → PostgreSQL storage adapter: builds trading context
- Strategy contracts → Application shell: shared contracts
- Strategy contracts → Backend process: shared contracts
