OPEN-SOURCE SCRIPT
Kelly Optimal Leverage Indicator

The Kelly Optimal Leverage Indicator mathematically applies Kelly Criterion to determine optimal position sizing based on market conditions.
This indicator helps traders answer the critical question: "How much capital should I allocate to this trade?"
Note that "optimal position sizing" does not equal the position sizing that you should have. The Optima position sizing given by the indicator is based on historical data and cannot predict a crash, in which case, high leverage could be devastating.
Originally developed for gambling scenarios with known probabilities, the Kelly formula has been adapted here for financial markets to dynamically calculate the optimal leverage ratio that maximizes long-term capital growth while managing risk.
Key Features
How to Use
Mathematical Foundation
The indicator calculates the optimal leverage (f*) using the formula:
f* = μ/σ²
Where:
This approach balances potential gains against risk of ruin, offering a scientific framework for position sizing that maximizes long-term growth rate.
Notes
Enjoy the indicator! :)
P.S. If you are really geeky about the Kelly Criterion, I recommend the book The Kelly Capital Growth Investment Criterion by Edward O. Thorp and others.
This indicator helps traders answer the critical question: "How much capital should I allocate to this trade?"
Note that "optimal position sizing" does not equal the position sizing that you should have. The Optima position sizing given by the indicator is based on historical data and cannot predict a crash, in which case, high leverage could be devastating.
Originally developed for gambling scenarios with known probabilities, the Kelly formula has been adapted here for financial markets to dynamically calculate the optimal leverage ratio that maximizes long-term capital growth while managing risk.
Key Features
- Kelly Position Sizing: Uses historical returns and volatility to calculate mathematically optimal position sizes
- Multiple Risk Profiles: Displays Full Kelly (aggressive), 3/4 Kelly (moderate), 1/2 Kelly (conservative), and 1/4 Kelly (very conservative) leverage levels
- Volatility Adjustment: Automatically recommends appropriate Kelly fraction based on current market volatility
- Return Smoothing: Option to use log returns and smoothed calculations for more stable signals
- Comprehensive Table: Displays key metrics including annualized return, volatility, and recommended exposure levels
How to Use
- Interpret the Lines: Each colored line represents a different Kelly fraction (risk tolerance level). When above zero, positive exposure is suggested; when below zero, reduce exposure. Note that this is based on historical returns. I personally like to increase my exposure during market downturns, but this is hard to illustrate in the indicator.
- Monitor the Table: The information panel provides precise leverage recommendations and exposure guidance based on current market conditions.
- Follow Recommended Position: Use the "Recommended Position" guidance in the table to determine appropriate exposure level.
- Select Your Risk Profile: Conservative traders should follow the Half Kelly or Quarter Kelly lines, while more aggressive traders might consider the Three-Quarter or Full Kelly lines.
- Adjust with Volatility: During high volatility periods, consider using more conservative Kelly fractions as recommended by the indicator.
Mathematical Foundation
The indicator calculates the optimal leverage (f*) using the formula:
f* = μ/σ²
Where:
- μ is the annualized expected return
- σ² is the annualized variance of returns
This approach balances potential gains against risk of ruin, offering a scientific framework for position sizing that maximizes long-term growth rate.
Notes
- The Full Kelly is theoretically optimal for maximizing long-term growth but can experience significant drawdowns. You should almost never use full kelly.
- Most practitioners use fractional Kelly strategies (1/2 or 1/4 Kelly) to reduce volatility while capturing most of the growth benefits
- This indicator works best on daily timeframes but can be applied to any timeframe
- Negative Kelly values suggest reducing or eliminating market exposure
- The indicator should be used as part of a complete trading system, not in isolation
Enjoy the indicator! :)
P.S. If you are really geeky about the Kelly Criterion, I recommend the book The Kelly Capital Growth Investment Criterion by Edward O. Thorp and others.
Script open-source
In pieno spirito TradingView, il creatore di questo script lo ha reso open-source, in modo che i trader possano esaminarlo e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricorda che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.
Script open-source
In pieno spirito TradingView, il creatore di questo script lo ha reso open-source, in modo che i trader possano esaminarlo e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricorda che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.