# sainikhil573/crypto-agent architecture

> Architecture diagram of the GitHub repository sainikhil573/crypto-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sainikhil573/crypto-agent
- Repository: https://github.com/sainikhil573/crypto-agent

## Overview

#### Purpose and Entry Points
CryptoAgent is a Node.js crypto-futures signal scanner and browser dashboard. It scans roughly the top 30 instruments, evaluates technical signals, persists trade candidates and outcomes, and can alert a human operator via Telegram. The runtime starts at `server.js`; the self-contained UI is `index.html`; REST routes are defined in `backend/api.js`.

#### HTTP Server, Dashboard, and Proxy Boundary
`server.js` hosts the static dashboard, exposes health checking, serves API routes, proxies OKX data to avoid browser CORS restrictions, and fetches/caches crypto RSS news. The browser is intentionally thin and has no build pipeline:
- `server.js`
- `index.html`
- `backend/api.js`

External-facing boundaries are the dashboard/API consumer, OKX v5 market-data API, RSS sources, and deployment health endpoint `/api/health`.

#### Scheduled Signal Pipeline
The scanner is the primary control loop: a five-minute cron job obtains futures market data, runs analysis, records qualifying signals, sends alerts, and monitors signal performance. It is the orchestration point between acquisition, analysis, persistence, and notification:
- `backend/scanner.js`
- `backend/fetcher.js`
- `backend/analysis.js`

A human remains responsible for making trades and later marking outcomes; the system does not execute orders.

#### Market Data Acquisition
`backend/fetcher.js` encapsulates retrieval of OKX v5 exchange data used by scanning and analysis. `server.js` also acts as an OKX proxy for browser-originated requests. This separates exchange-specific network handling from signal logic:
- `backend/fetcher.js`
- `server.js`

The exchange dependency is OKX v5, selected in part for availability from the stated deployment geography.

#### Signal Analysis Engine
`backend/analysis.js` implements the ten-layer technical-analysis engine that turns fetched market data into candidate futures signals. Its results feed scanner qualification/alerting and stored signal records:
- `backend/analysis.js`
- `backend/scanner.js`

Optional Claude/Anthropic macro analysis is enabled through `ANTHROPIC_API_KEY`; core operation can run without it.

#### Persistence, Outcomes, and Validation
SQLite is the system of record for signals, manually supplied WIN/LOSS/BREAKEVEN outcomes, and backtest results. The database uses WAL mode and is accessed through a dedicated data module:
- `backend/db.js`
- `backend/backtester.js`
- `backend/api.js`

The backtester evaluates historical performance and identifies disabled or caution patterns. API endpoints expose active/history views, aggregate stats, manual outcome updates, and explicit backtest execution.

#### Notifications and Deployment
Telegram is an optional outbound alert channel invoked by the scanner, controlled by `TG_BOT_TOKEN` and `TG_CHAT_ID`:
- `backend/scanner.js`
- `.env.example`

The application is containerized and deployable as a single web service on Railway or Render. Persistent SQLite storage must be considered when selecting platform storage behavior:
- `Dockerfile`
- `railway.toml`
- `render.yaml`

Main entry points: [`server.js`](https://github.com/sainikhil573/crypto-agent/blob/main/server.js) (HTTP Server), [`backend/scanner.js`](https://github.com/sainikhil573/crypto-agent/blob/main/backend/scanner.js) (Five-Minute Scanner), [`backend/api.js`](https://github.com/sainikhil573/crypto-agent/blob/main/backend/api.js) (REST API), [`backend/db.js`](https://github.com/sainikhil573/crypto-agent/blob/main/backend/db.js) (Data Module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web &amp; API"]
  node_dashboard["Browser Dashboard<br/>static UI<br/>[index.html]"]
  node_server["HTTP Server<br/>Node.js server<br/>[server.js]"]
  node_api["REST API<br/>route layer<br/>[api.js]"]
  node_health(("Health Endpoint<br/>deployment probe<br/>[server.js]"))
end

subgraph group_pipeline["Signal Pipeline"]
  node_scanner["Five-Minute Scanner<br/>cron orchestrator<br/>[scanner.js]"]
  node_fetcher["Market Fetcher<br/>OKX client<br/>[fetcher.js]"]
  node_analysis{{"Technical Analysis<br/>signal engine<br/>[analysis.js]"}}
end

subgraph group_data["Records &amp; Evaluation"]
  node_backtester["Backtester<br/>historical evaluator<br/>[backtester.js]"]
  node_dbmodule["Data Module<br/>SQLite access layer<br/>[db.js]"]
  node_sqlite[("SQLite Records<br/>system of record")]
end

subgraph group_external["External Services"]
  node_okx["OKX v5<br/>market-data API"]
  node_rss["RSS Sources<br/>news feeds"]
  node_telegram["Telegram<br/>alert channel"]
  node_anthropic["Anthropic Claude<br/>optional macro analysis"]
end

node_operator(("Human Operator<br/>trade decision maker"))
node_container["Container Deployment<br/>single web service"]

node_dashboard -->|"loads UI and calls API"| node_server
node_server -->|"routes requests"| node_api
node_server -->|"serves"| node_health
node_server -->|"browser CORS proxy"| node_okx
node_server -->|"fetches and caches news"| node_rss
node_api -->|"reads and updates records"| node_dbmodule
node_api -->|"runs on request"| node_backtester
node_scanner -->|"gets futures data"| node_fetcher
node_fetcher -->|"retrieves market data"| node_okx
node_scanner -->|"evaluates instruments"| node_analysis
node_analysis -.->|"optional macro context"| node_anthropic
node_scanner -->|"records candidates and performance"| node_dbmodule
node_scanner -.->|"sends qualifying alerts"| node_telegram
node_dbmodule -->|"persists"| node_sqlite
node_backtester -->|"uses history and saves results"| node_dbmodule
node_operator -->|"reviews signals"| node_dashboard
node_operator -->|"marks WIN/LOSS/BREAKEVEN"| node_api
node_container -->|"runs"| node_server

click node_dashboard "https://github.com/sainikhil573/crypto-agent/blob/main/index.html"
click node_server "https://github.com/sainikhil573/crypto-agent/blob/main/server.js"
click node_api "https://github.com/sainikhil573/crypto-agent/blob/main/backend/api.js"
click node_health "https://github.com/sainikhil573/crypto-agent/blob/main/server.js"
click node_scanner "https://github.com/sainikhil573/crypto-agent/blob/main/backend/scanner.js"
click node_fetcher "https://github.com/sainikhil573/crypto-agent/blob/main/backend/fetcher.js"
click node_analysis "https://github.com/sainikhil573/crypto-agent/blob/main/backend/analysis.js"
click node_backtester "https://github.com/sainikhil573/crypto-agent/blob/main/backend/backtester.js"
click node_dbmodule "https://github.com/sainikhil573/crypto-agent/blob/main/backend/db.js"
click node_container "https://github.com/sainikhil573/crypto-agent/blob/main/Dockerfile"

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_dashboard,node_server,node_api,node_health toneBlue
class node_scanner,node_fetcher,node_analysis toneAmber
class node_backtester,node_dbmodule,node_sqlite toneMint
class node_okx,node_rss,node_telegram,node_anthropic toneRose
class node_operator,node_container toneNeutral
```

## Components

### Web & API

- [Browser Dashboard](https://github.com/sainikhil573/crypto-agent/blob/main/index.html): static UI · Thin self-contained operator dashboard. · `index.html`
- [HTTP Server](https://github.com/sainikhil573/crypto-agent/blob/main/server.js): Node.js server · Serves UI, health, API, OKX proxy, and cached news. · `server.js`
- [REST API](https://github.com/sainikhil573/crypto-agent/blob/main/backend/api.js): route layer · Provides signals, stats, outcomes, and backtest endpoints. · `backend/api.js`
- [Health Endpoint](https://github.com/sainikhil573/crypto-agent/blob/main/server.js): deployment probe · `server.js`

### Signal Pipeline

- [Five-Minute Scanner](https://github.com/sainikhil573/crypto-agent/blob/main/backend/scanner.js): cron orchestrator · Qualifies signals, alerts operators, and monitors performance. · `backend/scanner.js`
- [Market Fetcher](https://github.com/sainikhil573/crypto-agent/blob/main/backend/fetcher.js): OKX client · `backend/fetcher.js`
- [Technical Analysis](https://github.com/sainikhil573/crypto-agent/blob/main/backend/analysis.js): signal engine · Ten-layer engine producing futures candidates. · `backend/analysis.js`

### Records & Evaluation

- [Backtester](https://github.com/sainikhil573/crypto-agent/blob/main/backend/backtester.js): historical evaluator · Identifies disabled and caution patterns. · `backend/backtester.js`
- [Data Module](https://github.com/sainikhil573/crypto-agent/blob/main/backend/db.js): SQLite access layer · Dedicated WAL-mode data access. · `backend/db.js`
- **SQLite Records**: system of record · Stores signals, outcomes, and backtest results.

### External Services

- **OKX v5**: market-data API
- **RSS Sources**: news feeds
- **Telegram**: alert channel · Optional outbound signal alerts.
- **Anthropic Claude**: optional macro analysis · Enabled only when an API key is configured.

### Other components

- **Human Operator**: trade decision maker · Makes trades and manually records outcomes.
- [Container Deployment](https://github.com/sainikhil573/crypto-agent/blob/main/Dockerfile): single web service · Deployable to Railway or Render; persistent SQLite storage is required. · `Dockerfile`

## Connections

- Browser Dashboard → HTTP Server: loads UI and calls API
- HTTP Server → REST API: routes requests
- HTTP Server → Health Endpoint: serves
- HTTP Server → OKX v5: browser CORS proxy
- HTTP Server → RSS Sources: fetches and caches news
- REST API → Data Module: reads and updates records
- REST API → Backtester: runs on request
- Five-Minute Scanner → Market Fetcher: gets futures data
- Market Fetcher → OKX v5: retrieves market data
- Five-Minute Scanner → Technical Analysis: evaluates instruments
- Technical Analysis → Anthropic Claude: optional macro context
- Five-Minute Scanner → Data Module: records candidates and performance
- Five-Minute Scanner → Telegram: sends qualifying alerts
- Data Module → SQLite Records: persists
- Backtester → Data Module: uses history and saves results
- Human Operator → Browser Dashboard: reviews signals
- Human Operator → REST API: marks WIN/LOSS/BREAKEVEN
- Container Deployment → HTTP Server: runs
