Production-ready ATR-based risk sizing engine for traders and trading systems.
It calculates position size from account risk, volatility, and instrument contract rules so risk stays controlled across market regimes.
- Fixed-risk sizing (
risk_amount = balance * risk_percent) - ATR-based stop distance (
stop_distance = ATR * atr_multiplier) - Optional explicit stop from
entry_priceandstop_price - Contract-aware lot sizing with
contract_sizeandtick_value - Broker-safe lot rounding:
down,nearest,up - Min/max lot constraints with reject/clip behavior
- Portfolio-level risk cap across batch calculations
- Multi-asset support:
forex,crypto,stock,futures,cfd - YAML/JSON config loading for single position and watchlists
- Journal export to JSON or CSV
- CLI with human-readable and JSON output formats
- Automated tests for engine logic and CLI behavior
dynamic-position-sizer-atr-calculator/
├── dynamic_position_sizer/
│ ├── __init__.py
│ ├── calculator.py
│ ├── cli.py
│ ├── io_utils.py
│ └── models.py
├── examples/
│ ├── config.yaml
│ └── watchlist.json
├── tests/
│ ├── test_calculator.py
│ └── test_cli.py
├── main.py
└── requirements.txt
risk_amount = account_balance * risk_percent
stop_distance = atr * atr_multiplier
risk_per_unit = stop_distance * tick_value
raw_units = risk_amount / risk_per_unit
lot_size = round_to_step(raw_units / contract_size, lot_step)python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtpython main.py \
--symbol EURUSD \
--account-balance 10000 \
--risk-percent 0.01 \
--atr 0.0085 \
--atr-multiplier 1.5 \
--asset-type forex \
--contract-size 100000 \
--tick-value 1 \
--lot-step 0.0001 \
--rounding-mode down \
--format json \
--mode paperpython main.py --config examples/config.yaml --format json --mode paperpython main.py \
--watchlist examples/watchlist.json \
--portfolio-risk-cap 0.02 \
--format json \
--journal-output output/journal.json \
--mode paperpython main.py \
--watchlist examples/watchlist.json \
--journal-output output/journal.csv- Use
rounding_mode=downas the safest default to avoid oversizing. - Use
portfolio-risk-capto control aggregate risk in multi-trade flows. - If computed lot is below
min_lot, trade is rejected (status=REJECTED). - Always validate in paper mode before wiring to live execution.
pytest -qTests currently validate:
- Correct ATR sizing behavior
- Rejection logic for
min_lot - Portfolio cap behavior across sequential trades
- CLI success path and failure path
- Symbol-level preset registry (contract/tick defaults)
- Fee/slippage-aware effective risk model
- Time-stamped trade IDs and richer journaling fields
- Optional REST microservice wrapper for bot integration
- CI workflow for automated test and lint checks
GitHub: @leionion
This software is trading infrastructure, not financial advice. Incorrect inputs (ATR, tick value, contract size, broker constraints) can cause incorrect sizing and real losses. Validate all calculations in paper mode before live usage.