How dinu1763/capital-api-trading works
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 Trading CLI · client.py Capital API client · exceptions.py API exceptions · __init__.py capital_api package
Components 14
CLI & Trading Logic
Local operator interface and strategy policy.
- Trading CLI · Interactive command parsing, validation, output, and dispatch.
trader.py - Manual operations · Account, market, position, order, and transaction actions.
trader.py - Risk conversion rules · Converts currency risk to price levels and enforces 1:2 reward-to-risk.
trader.py - Martingale grid trader · Timed round-robin columns adjust size after wins or losses.
trader.py
Broker Integration
Capital.com protocol boundary.
- Capital API client · Authentication, session tokens, and broker request execution.
capital_api/client.py - Session authentication · Uses externally supplied credentials and demo/live base URL.
capital_api/client.py - Broker endpoint calls · Account, markets, positions, orders, and transactions requests.
capital_api/client.py - API exceptions · Separates broker/request failures from normal command flow.
capital_api/exceptions.py - capital_api package
capital_api/__init__.py
External Runtime
Supplied configuration and remote broker.
- Environment credentials · Local .env supplies API key, login, password, and endpoint options.
- Capital.com REST API · Demo or live trading service.
- Python dependencies · Python 3.7+ dependency declaration.
requirements.txt
Other components
- Operator
- Usage documentation
README.md
Connections 17
- 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