# dinu1763/capital-api-trading architecture

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

- Interactive diagram: https://gitdiagram.com/dinu1763/capital-api-trading
- Repository: https://github.com/dinu1763/capital-api-trading

## Overview

#### Purpose and Entry Point
This is a local Python CLI for Capital.com demo or live trading. It supports manual account, market-data, position, and order operations plus an automated Martingale grid strategy. The executable entry point is `trader.py`; reusable broker integration is in `capital_api/client.py`.

#### CLI Command Layer
`trader.py` owns the interactive `trader>` session: command parsing, user-facing output, validation, and dispatch for account lookup, instrument search/pricing, order lifecycle actions, transaction history, and strategy execution. It is the boundary between operator input and API calls.

Paths: `trader.py`, `README.md`

#### API Client and Authentication
`capital_api/client.py` encapsulates Capital.com REST authentication, session/token handling, and request execution against a configurable API base URL. It separates broker protocol details from CLI and strategy logic. Credentials and demo/live endpoint selection are supplied externally through environment variables loaded from a local `.env` file, not committed source.

Paths: `capital_api/client.py`, `capital_api/__init__.py`

#### Broker Error Boundary
Custom API failures are represented separately so CLI/automation can distinguish broker or request failures from normal command flow and report them safely rather than embedding protocol-specific error handling throughout the application.

Paths: `capital_api/exceptions.py`, `capital_api/client.py`

#### Manual Trading Flow
A command flows from `trader.py` validation to client calls for Capital.com account, market, positions, orders, and transaction endpoints. Order creation supports market direction, size, and stop/take-profit levels. Update, close, and cancel operations use broker deal identifiers; position queries provide live state needed for management actions.

Paths: `trader.py`, `capital_api/client.py`

#### Risk Amount Conversion and Update Rules
The CLI translates currency-denominated stop/profit inputs into price levels using a position’s `openLevel` anchor. It enforces a minimum 1:2 reward-to-risk floor. This logic sits above the API client because it is trading-policy behavior rather than a broker transport concern.

Paths: `trader.py`

#### Automated Grid Strategy
The grid trader in `trader.py` runs a timed, round-robin set of Martingale columns. Each column tracks its loss progression: winning trades reset to base size, while losses increase size up to configured row depth. After a fill, it waits, resolves a live deal through positions, waits again, then updates protective levels; it falls back to the broker-confirmed deal ID if resolution fails. `Ctrl+C` stops automation without automatically closing positions.

Paths: `trader.py`, `capital_api/client.py`

#### Runtime and External Dependencies
The runtime is Python 3.7+ with dependencies declared in `requirements.txt`. It communicates with Capital.com’s HTTPS REST API at demo or live URLs. Required external inputs are API key, login, password, and optional encrypted-password/base-URL settings.

Paths: `requirements.txt`, `README.md`, `capital_api/client.py`

Main entry points: [`trader.py`](https://github.com/dinu1763/capital-api-trading/blob/main/trader.py) (Trading CLI), [`capital_api/client.py`](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/client.py) (Capital API client), [`capital_api/exceptions.py`](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/exceptions.py) (API exceptions), [`capital_api/__init__.py`](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/__init__.py) (capital_api package)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI &amp; Trading Logic"]
  node_trader["Trading CLI<br/>Python CLI<br/>[trader.py]"]
  node_manual["Manual operations<br/>command flow<br/>[trader.py]"]
  node_risk["Risk conversion rules<br/>trading policy<br/>[trader.py]"]
  node_grid["Martingale grid trader<br/>automation<br/>[trader.py]"]
end

subgraph group_broker["Broker Integration"]
  node_client["Capital API client<br/>REST client<br/>[client.py]"]
  node_auth["Session authentication<br/>client layer<br/>[client.py]"]
  node_endpoints["Broker endpoint calls<br/>client layer<br/>[client.py]"]
  node_errors["API exceptions<br/>error boundary<br/>[exceptions.py]"]
  node_package["capital_api package<br/>Python package<br/>[__init__.py]"]
end

subgraph group_external["External Runtime"]
  node_environment["Environment credentials<br/>runtime input"]
  node_capital{{"Capital.com REST API<br/>external HTTPS API"}}
  node_requirements["Python dependencies<br/>runtime manifest<br/>[requirements.txt]"]
end

node_operator(("Operator<br/>user"))
node_readme["Usage documentation<br/>[README.md]"]

node_operator -->|"interactive commands"| node_trader
node_readme -.->|"documents usage"| node_trader
node_requirements -.->|"runtime dependencies"| node_trader
node_trader -->|"dispatches"| node_manual
node_trader -->|"applies"| node_risk
node_trader -->|"runs"| node_grid
node_manual -->|"broker operations"| node_client
node_risk -->|"submits price levels"| node_client
node_grid -->|"places and manages deals"| node_client
node_client -->|"authenticates through"| node_auth
node_client -->|"executes through"| node_endpoints
node_client -->|"raises"| node_errors
node_trader -.->|"reports safely"| node_errors
node_environment -->|"credentials and endpoint"| node_auth
node_auth -->|"creates session"| node_capital
node_endpoints -->|"HTTPS requests"| node_capital
node_package -.->|"exports"| node_client

click node_trader "https://github.com/dinu1763/capital-api-trading/blob/main/trader.py"
click node_manual "https://github.com/dinu1763/capital-api-trading/blob/main/trader.py"
click node_risk "https://github.com/dinu1763/capital-api-trading/blob/main/trader.py"
click node_grid "https://github.com/dinu1763/capital-api-trading/blob/main/trader.py"
click node_client "https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/client.py"
click node_auth "https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/client.py"
click node_endpoints "https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/client.py"
click node_errors "https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/exceptions.py"
click node_package "https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/__init__.py"
click node_requirements "https://github.com/dinu1763/capital-api-trading/blob/main/requirements.txt"
click node_readme "https://github.com/dinu1763/capital-api-trading/blob/main/README.md"

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_trader,node_manual,node_risk,node_grid toneBlue
class node_client,node_auth,node_endpoints,node_errors,node_package toneAmber
class node_environment,node_capital,node_requirements toneMint
class node_operator,node_readme toneNeutral
```

## Components

### CLI & Trading Logic

Local operator interface and strategy policy.

- [Trading CLI](https://github.com/dinu1763/capital-api-trading/blob/main/trader.py): Python CLI · Interactive command parsing, validation, output, and dispatch. · `trader.py`
- [Manual operations](https://github.com/dinu1763/capital-api-trading/blob/main/trader.py): command flow · Account, market, position, order, and transaction actions. · `trader.py`
- [Risk conversion rules](https://github.com/dinu1763/capital-api-trading/blob/main/trader.py): trading policy · Converts currency risk to price levels and enforces 1:2 reward-to-risk. · `trader.py`
- [Martingale grid trader](https://github.com/dinu1763/capital-api-trading/blob/main/trader.py): automation · Timed round-robin columns adjust size after wins or losses. · `trader.py`

### Broker Integration

Capital.com protocol boundary.

- [Capital API client](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/client.py): REST client · Authentication, session tokens, and broker request execution. · `capital_api/client.py`
- [Session authentication](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/client.py): client layer · Uses externally supplied credentials and demo/live base URL. · `capital_api/client.py`
- [Broker endpoint calls](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/client.py): client layer · Account, markets, positions, orders, and transactions requests. · `capital_api/client.py`
- [API exceptions](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/exceptions.py): error boundary · Separates broker/request failures from normal command flow. · `capital_api/exceptions.py`
- [capital_api package](https://github.com/dinu1763/capital-api-trading/blob/main/capital_api/__init__.py): Python package · `capital_api/__init__.py`

### External Runtime

Supplied configuration and remote broker.

- **Environment credentials**: runtime input · Local .env supplies API key, login, password, and endpoint options.
- **Capital.com REST API**: external HTTPS API · Demo or live trading service.
- [Python dependencies](https://github.com/dinu1763/capital-api-trading/blob/main/requirements.txt): runtime manifest · Python 3.7+ dependency declaration. · `requirements.txt`

### Other components

- **Operator**: user
- [Usage documentation](https://github.com/dinu1763/capital-api-trading/blob/main/README.md): documentation · `README.md`

## Connections

- Operator → Trading CLI: interactive commands
- Usage documentation → Trading CLI: documents usage
- Python dependencies → Trading CLI: runtime dependencies
- Trading CLI → Manual operations: dispatches
- Trading CLI → Risk conversion rules: applies
- Trading CLI → Martingale grid trader: runs
- Manual operations → Capital API client: broker operations
- Risk conversion rules → Capital API client: submits price levels
- Martingale grid trader → Capital API client: places and manages deals
- Capital API client → Session authentication: authenticates through
- Capital API client → Broker endpoint calls: executes through
- Capital API client → API exceptions: raises
- Trading CLI → API exceptions: reports safely
- Environment credentials → Session authentication: credentials and endpoint
- Session authentication → Capital.com REST API: creates session
- Broker endpoint calls → Capital.com REST API: HTTPS requests
- capital_api package → Capital API client: exports
