DYNAMIC-POSITION-SIZER-ATR-CALCULATOR(1)

NAME

dynamic-position-sizer-atr-calculatorA utility that automatically calculates the exact lot size for a trade based on a fixed 1% account risk and current…

SYNOPSIS

$pip install -r

INFO

35 stars
0 views

DESCRIPTION

A utility that automatically calculates the exact lot size for a trade based on a fixed 1% account risk and current market volatility (ATR).

README

Dynamic Position Sizer ATR Calculator

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.

Implemented Features

  • Fixed-risk sizing (risk_amount = balance * risk_percent)
  • ATR-based stop distance (stop_distance = ATR * atr_multiplier)
  • Optional explicit stop from entry_price and stop_price
  • Contract-aware lot sizing with contract_size and tick_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

Project Structure

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

Core Formula

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)

Installation

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Usage

1) Single Calculation (CLI args)

python 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 paper

2) Single Calculation (Config file)

python main.py --config examples/config.yaml --format json --mode paper

3) Watchlist Batch Sizing

python main.py \
  --watchlist examples/watchlist.json \
  --portfolio-risk-cap 0.02 \
  --format json \
  --journal-output output/journal.json \
  --mode paper

4) Export Journal as CSV

python main.py \
  --watchlist examples/watchlist.json \
  --journal-output output/journal.csv

Notes on Risk Controls

  • Use rounding_mode=down as the safest default to avoid oversizing.
  • Use portfolio-risk-cap to 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.

Testing

pytest -q

Tests currently validate:

  • Correct ATR sizing behavior
  • Rejection logic for min_lot
  • Portfolio cap behavior across sequential trades
  • CLI success path and failure path

Roadmap (Next Improvements)

  • 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

Contact

GitHub: @leionion

Disclaimer

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.

SEE ALSO

clihub4/16/2026DYNAMIC-POSITION-SIZER-ATR-CALCULATOR(1)