OPEN-SOURCE SCRIPT

Sigmoid Risk Allocator

112
The Sigmoid Risk Allocator is a dynamic position sizing indicator that tells you how much of your capital to allocate based on current market conditions. Unlike simple "risk-on/risk-off" signals, this indicator gives you smooth, gradual transitions based on a sigmoid function.

Why a Sigmoid Curve?
Most position sizing approaches use fixed thresholds: "If drawdown > 20%, buy. Otherwise, don't." This creates all-or-nothing decisions.

Using the sigmoid (S-curve) makes this decision different. It creates a smooth transition where:
  • Small drawdowns → Stay near your baseline allocation
  • Moderate drawdowns → Gradually increase exposure
  • Large drawdowns → Approach maximum allocation

The sigmoid curve naturally "saturates" at the extremes, preventing you from going all-in too early or panicking out too fast. This is very useful to meek traders psychology and risk management in check.

What's a Sigmoid Function?
The sigmoid function is a mathematical S-curve defined as:
σ(x) = 1 / (1 + e^(-x))

This formula takes any input value and smoothly maps it to a number between 0 and 1. The curve has three key properties that make it ideal for position sizing in investing:
  1. Smooth transitions: No sudden jumps. Allocation changes gradually.
  2. Saturation at extremes: The curve flattens near 0 and 1, preventing overreaction and overexposure.
  3. Sensitive in the middle: Most of the action happens around the midpoint.

To convert this into an allocation percentage, the indicator uses:
Pine Script®
Allocation = α_min + (α_max - α_min) × σ(k × (Risk - Midpoint))

Where:
- `α_min` = Your minimum allocation (default 50%)
- `α_max` = Your maximum allocation (default 100%)
- `Risk` = Current risk metric (drawdown %, volatility, or Kelly %)
- `Midpoint` = The risk level where allocation sits halfway between min and max (default 15%)
- `k` = Steepness—how quickly allocation changes around the midpoint

Example: With defaults, if drawdown hits 15% (the midpoint), your allocation will be 75% (halfway between 50% and 100%). As the drawdown increases beyond 15%, the allocation curves toward 100%. As it decreases toward 0%, allocation curves toward 50%.
Cool, isn't it?

Asymmetric Response: Fast In, Slow Out
The indicator uses different steepness values for scaling in vs. scaling out. This is great to increase trend following. This is something I'm proud of too in this indicator.
  • k_increase = 30 (steep curve): When drawdowns appear, allocation ramps up quickly to catch the opportunity
  • k_decrease = 5 (slower curve): When conditions normalize, allocation decreases slowly to avoid selling the rebound

This asymmetry reflects how markets behave—drawdowns often overshoot fundamentals (rewarding quick entries), while recoveries tend to be more orderly (rewarding patience on exits).

Three Risk Metrics
You can choose what drives your allocation:
Drawdown (Default)
Volatility - Scales your position inversely to current market volatility.
Kelly Criterion - Automatically calculates optimal position size. The indicator applies a conservative "half Kelly" by default.

Use Cases
  • Position sizing for swing trading or trend following
  • Risk management overlay for any existing strategy
  • Drawdown-based DCA (dollar cost averaging) decisions
  • Volatility-adjusted exposure management

Feel free to provide feedback and share your thoughts!
- Henrique Centieiro

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.