OPEN-SOURCE SCRIPT
Optimal F by Ralph Vince. Position Sizing and Risk Analysis

Even a profitable strategy can produce weak results when position sizing is poorly calibrated. A position that is too small limits capital growth, while excessive exposure can turn an ordinary losing streak into a severe drawdown. MM: Optimal F analyzes the actual trade results of your strategy and identifies the historical risk level that produced the highest geometric capital growth.
The script implements the Optimal F concept developed by Ralph Vince. The method searches for the fraction of capital that maximizes the compounded result of the complete trade sequence, represented by Terminal Wealth Relative, or `G(f)`. Optimal F is not designed to maximize the profit of an individual trade. Its purpose is to optimize the long-term compounded growth of the entire system.
How the Script Works
The strategy must first be tested using a fixed position size of one contract or one unit of the traded instrument.
For every closed trade, the script calculates the financial result per contract. It then identifies the largest historical losing trade: Max Loss per Contract
Every trade is normalized relative to this loss: R = Profit per Contract / Max Loss per Contract
The script then tests values of `f` from 0.00 to 1.00 and calculates the following result for each value: G(f) = Π(1 + f × R)
A curve showing the relationship between `f` and `G(f)` is displayed on the chart.
The script reports four main values:
The number of contracts is calculated using: Contracts = Equity × Selected F / Max Loss per Contract
Example:
Equity = 100,000
Selected F = 0.21
Max Loss per Contract = 9,775
Contracts = 100,000 × 0.21 / 9,775
Contracts = 2.15
For futures trading, the result is rounded down: 2 contracts
The table displays the value in the following format: 2 (2.15)
The first number is the whole-contract position size. The value in parentheses is the exact calculated position size before rounding.
What Optimal F Actually Means
A result such as: Best F = 0.21
does not mean that the strategy should enter a position using 21% of account equity as the nominal position value.
It means that the position size should be calculated so that a repetition of the largest historical loss per contract would represent approximately 21% of the current account equity.
For this reason, Optimal F cannot be replicated by simply setting:
Order size = 21% of equity
in the TradingView strategy settings.
The percentage of equity used as nominal position value and the percentage of equity exposed to the largest historical loss are different measurements.
Full F and Fractional F
Full Optimal F maximizes historical geometric growth, but it usually produces aggressive position sizing, large equity fluctuations, and deep drawdowns. It does not optimize equity-curve smoothness and does not impose a maximum drawdown limit.
Ralph Vince specifically discussed Half F, or 50% of the calculated Optimal F, as an attractive practical compromise. In his example, Half F reduced variability and expected drawdown by approximately half, although more trades were required to reach the same capital objective. He also noted that using a smaller fraction of Optimal F generally produces a smoother equity curve.
Example:
Best F = 0.21
Full F = 0.21
Half F = 0.105
Quarter F = 0.0525
Half F is calculated as:
Selected F = Best F × 0.50
This should not be treated as a universal rule. Half F is a more conservative starting point that can be compared with Full F and other fractional values.
How to Use the Script
1. Insert your own strategy code into the designated strategy section.
2. Disable: "Enable Optimal F Position Sizing"
In this mode, the strategy should be tested using one contract.
3. Verify that the following strategy properties are configured correctly:
These parameters directly affect trade results, the largest losing trade, and the calculated Optimal F.
4. Run the backtest and record:
5. Select the fraction of Optimal F that you intend to apply:
A more conservative starting point is: Selected F = Best F × 0.50
6. Enter the selected value into: Optimal F
7. Enter the calculated maximum loss per contract into: Max Loss
8. Enable: "Enable Optimal F Position Sizing"
The strategy will now dynamically recalculate its position size before every new trade using the current account equity.
9. For futures trading, enable: Rounds contracts number
The calculated position size will be rounded down to a whole number of contracts. When the calculated size is below one contract, the strategy will continue trading one contract.
Important Limitations
Optimal F is calculated from historical trades. A future loss may exceed the largest loss observed in the backtest.
Full Optimal F often creates excessive exposure. Results should therefore be evaluated using more than Net Profit alone. Important metrics include:
For a more objective evaluation, calculate Optimal F on one section of historical data and apply the fixed values to a separate out-of-sample period.
Insert your own strategy into MM: Optimal F, run the analysis using one contract, and determine the level of risk that is actually supported by the historical structure of your strategy’s profits and losses.
The script implements the Optimal F concept developed by Ralph Vince. The method searches for the fraction of capital that maximizes the compounded result of the complete trade sequence, represented by Terminal Wealth Relative, or `G(f)`. Optimal F is not designed to maximize the profit of an individual trade. Its purpose is to optimize the long-term compounded growth of the entire system.
How the Script Works
The strategy must first be tested using a fixed position size of one contract or one unit of the traded instrument.
For every closed trade, the script calculates the financial result per contract. It then identifies the largest historical losing trade: Max Loss per Contract
Every trade is normalized relative to this loss: R = Profit per Contract / Max Loss per Contract
The script then tests values of `f` from 0.00 to 1.00 and calculates the following result for each value: G(f) = Π(1 + f × R)
A curve showing the relationship between `f` and `G(f)` is displayed on the chart.
The script reports four main values:
- Best F — the value of `f` that produced the highest historical geometric growth.
- Best G(f) — the maximum theoretical growth multiplier for the analyzed trade sequence.
- Max Loss — the largest closed loss per contract.
- Recommended Contracts — the calculated position size based on the current account equity.
The number of contracts is calculated using: Contracts = Equity × Selected F / Max Loss per Contract
Example:
Equity = 100,000
Selected F = 0.21
Max Loss per Contract = 9,775
Contracts = 100,000 × 0.21 / 9,775
Contracts = 2.15
For futures trading, the result is rounded down: 2 contracts
The table displays the value in the following format: 2 (2.15)
The first number is the whole-contract position size. The value in parentheses is the exact calculated position size before rounding.
What Optimal F Actually Means
A result such as: Best F = 0.21
does not mean that the strategy should enter a position using 21% of account equity as the nominal position value.
It means that the position size should be calculated so that a repetition of the largest historical loss per contract would represent approximately 21% of the current account equity.
For this reason, Optimal F cannot be replicated by simply setting:
Order size = 21% of equity
in the TradingView strategy settings.
The percentage of equity used as nominal position value and the percentage of equity exposed to the largest historical loss are different measurements.
Full F and Fractional F
Full Optimal F maximizes historical geometric growth, but it usually produces aggressive position sizing, large equity fluctuations, and deep drawdowns. It does not optimize equity-curve smoothness and does not impose a maximum drawdown limit.
Ralph Vince specifically discussed Half F, or 50% of the calculated Optimal F, as an attractive practical compromise. In his example, Half F reduced variability and expected drawdown by approximately half, although more trades were required to reach the same capital objective. He also noted that using a smaller fraction of Optimal F generally produces a smoother equity curve.
Example:
Best F = 0.21
Full F = 0.21
Half F = 0.105
Quarter F = 0.0525
Half F is calculated as:
Selected F = Best F × 0.50
This should not be treated as a universal rule. Half F is a more conservative starting point that can be compared with Full F and other fractional values.
How to Use the Script
1. Insert your own strategy code into the designated strategy section.
2. Disable: "Enable Optimal F Position Sizing"
In this mode, the strategy should be tested using one contract.
3. Verify that the following strategy properties are configured correctly:
- Initial Capital
- Commission
- Slippage
- Margin
These parameters directly affect trade results, the largest losing trade, and the calculated Optimal F.
4. Run the backtest and record:
- Best F
- Max Loss per Contract
- Recommended Contracts
5. Select the fraction of Optimal F that you intend to apply:
- Full F
- Half F
- Quarter F
A more conservative starting point is: Selected F = Best F × 0.50
6. Enter the selected value into: Optimal F
7. Enter the calculated maximum loss per contract into: Max Loss
8. Enable: "Enable Optimal F Position Sizing"
The strategy will now dynamically recalculate its position size before every new trade using the current account equity.
9. For futures trading, enable: Rounds contracts number
The calculated position size will be rounded down to a whole number of contracts. When the calculated size is below one contract, the strategy will continue trading one contract.
Important Limitations
Optimal F is calculated from historical trades. A future loss may exceed the largest loss observed in the backtest.
Full Optimal F often creates excessive exposure. Results should therefore be evaluated using more than Net Profit alone. Important metrics include:
- Maximum Drawdown
- Recovery Factor
- Equity Curve Stability
- Largest Losing Trade
- Maximum Losing Streak
For a more objective evaluation, calculate Optimal F on one section of historical data and apply the fixed values to a separate out-of-sample period.
Insert your own strategy into MM: Optimal F, run the analysis using one contract, and determine the level of risk that is actually supported by the historical structure of your strategy’s profits and losses.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。