Skip to content
SlashClone
Made by@qtrader

Position sizer

@qtrader/position-sizer

Work out how many shares to buy before you click. Fixed-fractional, ATR (volatility) based, and capped fractional-Kelly sizing, plus a portfolio heat check that totals the risk across open positions and tells you if a new trade fits. Not financial advice.

SkillTradingv1.0.0 (latest)Tested
Activity
New: be the first to try it
  1. 1# position-sizer
  2. 2
  3. 3Know your size before you click buy. This kit turns your account size, risk per trade, and stop into a share count, with fixed-fractional, ATR-based, and capped Kelly methods, and checks your total open risk ("portfolio heat") before you add a new position.
  4. 4
  5. 5**Who it is for:** discretionary and systematic traders who want a consistent, written-down sizing rule instead of a gut feel.
  6. 6
  7. 7Not financial advice. It is a calculator; it never tells you whether to take a trade.
  8. 8
  9. 9## Example
  10. 11```bash
  11. 12node scripts/size.mjs heat --equity 100000 --positions examples/positions.csv --add FFF,long,120,25,23
  12. 13```
  13. 15```text
  14. 16Portfolio heat: 5.80% of equity at risk ($5,800.00) | limit 6.00% | OK
  15. 17Symbol Side Qty Ref Stop Risk $ Risk % Flags
  16. 18AAA long 100 52 46 600.00 0.60%
  17. 19BBB long 200 35 31 800.00 0.80% stop locks in profit vs entry
  18. 20CCC short 50 80 84 200.00 0.20%
  19. 21DDD long 150 20 - 3,000.00 3.00% NO STOP: full value at risk; over 2% single-trade limit
  20. 22EEE long 300 41 37 1,200.00 1.20%
  21. 23Proposed FFF x120: risk $240.00 (0.24%), heat after 6.04%. Does not fit. Max that fits: 100 shares.
  22. 24```
  23. 26## Hand check (verify it yourself)
  24. 28- **Fixed fractional:** equity 50,000, risk 1%, entry 100, stop 95. Risk budget 500, per-share risk 5, so 100 shares, a 10,000 position (20% of equity). With a 15% max position: floor(7,500 / 100) = 75 shares.
  25. 29- **ATR(3)** on `examples/prices.csv`. True ranges: 1.0 (first bar, high - low), 0.8, 1.1, 0.8, 1.7 (12 - 10.3 beats 12 - 10.5). Seed = (1.0 + 0.8 + 1.1) / 3 = 0.966667. Then (0.966667 x 2 + 0.8) / 3 = 0.911111, then (0.911111 x 2 + 1.7) / 3 = 1.174074. With 2 x ATR, stop = 11.8 - 2.348148 = 9.451852. With 10,000 equity at 1%: floor(100 / 2.348148) = 42 shares.
  26. 30- **Kelly:** win rate 0.55, payoff 1.5. f* = 0.55 - 0.45 / 1.5 = 0.25. Half Kelly = 12.5% of equity at risk, which the default 5% cap cuts to 5%. From `examples/trades.csv` (3 wins averaging 216.67, 3 losses averaging 83.33): p = 0.5, b = 2.6, f* = 0.5 - 0.5 / 2.6 = 0.3077.
  27. 31- **Heat:** AAA 100 x (52 - 46) = 600. BBB 200 x (35 - 31) = 800. CCC short 50 x (84 - 80) = 200. DDD has no stop: 150 x 20 = 3,000. EEE 300 x (41 - 37) = 1,200. Total 5,800 = 5.8% of 100,000. Room left 0.2% = 200, so FFF with a 2-point stop fits at most 100 shares.
  28. 33`node scripts/selftest.mjs` checks every number above.
  29. 35## Opinionated defaults
  30. 37- Kelly is half Kelly capped at 5% of equity at risk. Full Kelly assumes you know your edge exactly, and you do not.
  31. 38- Heat limit 6% of equity, single-trade limit 2%. Change them with `--max-heat-pct` and `--max-risk-pct`.
  32. 39- Shares always round down.
  33. 41## What it can access
  34. 43- Reads CSVs you point it at in `data/`, writes JSON to `out/` when you pass `--out`.
  35. 44- Runs only its own scripts with Node. No network, no keys, no broker access.
  36. 46## Setup
  37. 48Node 22 or newer. Nothing to install.