I had a reader of the blog ask how to use Optimal F. That was really a great question. A few posts back I provided the OptimalFGeo function but didn’t demonstrate on how to use it for allocation purposes. In this post, I will do just that.
I Have Optimal F – Now What?
From Ralph Vince’s book, “Portfolio Management Formulas”, he states: “Once the highest f is found, it can readily be turned into a dollar amount by dividing the biggest loss by the negative optimal f. For example, if our biggest loss is $100 and our optimal f is 0.25, then -$100/ 0.25 = $400. In other words, we should bet 1 unit for every $400 we have in our stake.”
Convert Optimal F to dollars and then to number of shares
In my example strategy, I start out with an initial capital of $50,000 and allow reinvestment of profit or loss. The protective stop is set as 3 X ATR(10). A fixed $2000 profit objective is also utilized. The conversion form Optimal F to position size is illustrated by the following lines of code:
//keep track of biggest loss biggestLoss = minList(positionProfit(1),biggestLoss); //calculate the Optimal F with last 10 trades. OptF = OptimalFGeo(10); //reinvest profit or loss risk$ = initCapital$ + netProfit; //convert Optimal F to $$$ if OptF <> 0 then numShares = risk$ / (biggestLoss / (-1*OptF));
Code snippet - Optimal F to Position Size
Keep track of biggest loss
Calculate optimal F with OptimalFGeo function – minimum 10 trades
Calculate Risk$ by adding InitCapital to current NetProfit (Easylanguage keyword)
Calculate position size by dividing Risk$ by the quotient of biggest loss and (-1) Optimal F
I applied the Optimal F position sizing to a simple mean reversion algorithm where you buy on a break out in the direction of the 50-day moving average after a lower low occurs.
//keep track of biggest loss biggestLoss = minList(positionProfit(1),biggestLoss); //calculate the Optimal F with last 10 trades. OptF = OptimalFGeo(10); //reinvest profit or loss risk$ = initCapital$ + netProfit; //convert Optimal F to $$$ if OptF <> 0 then numShares = risk$ / (biggestLoss / (-1*OptF)); numShares = maxList(1,numShares); //if Optf <> 0 then print(d," ",t," ",risk$ / (biggestLoss / (-1*OptF))," ",biggestLoss," ",optF);
if c > average(c,50) and low < low[1] then Buy numShares shares next bar at open + .25* range stop;
setStopPosition; setProfitTarget(2000);
setStopLoss(3*avgTrueRange(10)*bigPointValue);
Strategy Using Optimal F
I have included the results below. At one time during the testing the number of contracts jumped up to 23. That is 23 mini Nasdaq futures ($20 * 7,300) * 23. That’s a lot of leverage and risk. Optimal doesn’t always mean the best risk mitigation. Please let me know if you find any errors in the code or in the logic.
Here is the ELD that incorporates the Strategy and the Function.USINGOPTIMALF
hi
my English is bad.But,I want to say that there\’s something wrong in the logic. In the book of Ralph Vince’s Book, the Optimal f means tracing the all the passed trades ,by looping from 1 to 0.01 the value f to find the
highest TWR.
to use the optimal f ,the program must first Simulate lots of trades. after that , we might use optimal f to Calculate the best share number into the following trades under an Hypothetical condition Winning probability is fixed.
Hi Sam – thanks for commenting on the post. Take a look at the function that does the actual calculation. https://georgepruitt.com/easylanguage-code-for-optimal-f-multi-charts-and-vba-too/
In the code one of the comments reads: //calculate the Optimal F with last 10 trades.
But it should read //calculate the Optimal F with at least 10 trades.
To test we have to build the Optimal F with the trades we have at that point in time. The more trades the better. However, you have to use what you have to enable a back-test.
Let me know what you think.
George
Backtesting with [Trade Station,Python,AmiBroker, Excel]. Intended for informational and educational purposes only!
Get All Five Books in the Easing Into EasyLanguage Series - The Trend Following Edition is now Available!
Announcement – A Trend Following edition has been added to my Easing into EasyLanguage Series! This edition will be the fifth and final installment and will utilize concepts discussed in the Foundation editions. I will pay respect to the legends of Trend Following by replicating the essence of their algorithms. Learn about the most prominent form of algorithmic trading. But get geared up for it by reading the first four editions in the series now. Get your favorite QUANT the books they need!
This series includes five editions that covers the full spectrum of the EasyLanguage programming language. Fully compliant with TradeStation and mostly compliant with MultiCharts. Start out with the Foundation Edition. It is designed for the new user of EasyLanguage or for those you would like to have a refresher course. There are 13 tutorials ranging from creating Strategies to PaintBars. Learn how to create your own functions or apply stops and profit objectives. Ever wanted to know how to find an inside day that is also a Narrow Range 7 (NR7?) Now you can, and the best part is you get over 4 HOURS OF VIDEO INSTRUCTION – one for each tutorial.
This book is ideal for those who have completed the Foundation Edition or have some experience with EasyLanguage, especially if you’re ready to take your programming skills to the next level. The Hi-Res Edition is designed for programmers who want to build intraday trading systems, incorporating trade management techniques like profit targets and stop losses. This edition bridges the gap between daily and intraday bar programming, making it easier to handle challenges like tracking the sequence of high and low prices within the trading day. Plus, enjoy 5 hours of video instruction to guide you through each tutorial.
The Advanced Topics Edition delves into essential programming concepts within EasyLanguage, offering a focused approach to complex topics. This book covers arrays and fixed-length buffers, including methods for element management, extraction, and sorting. Explore finite state machines using the switch-case construct, text graphic manipulation to retrieve precise X and Y coordinates, and gain insights into seasonality with the Ruggiero/Barna Universal Seasonal and Sheldon Knight Seasonal methods. Additionally, learn to build EasyLanguage projects, integrate fundamental data like Commitment of Traders, and create multi-timeframe indicators for comprehensive analysis.
The Day Trading Edition complements the other books in the series, diving into the popular approach of day trading, where overnight risk is avoided (though daytime risk still applies!). Programming on high-resolution data, such as five- or one-minute bars, can be challenging, and this book provides guidance without claiming to be a “Holy Grail.” It’s not for ultra-high-frequency trading but rather for those interested in techniques like volatility-based breakouts, pyramiding, scaling out, and zone-based trading. Ideal for readers of the Foundation and Hi-Res editions or those with EasyLanguage experience, this book offers insights into algorithms that shaped the day trading industry.
For thirty-one years as the Director of Research at Futures Truth Magazine, I had the privilege of collaborating with renowned experts in technical analysis, including Fitschen, Stuckey, Ruggiero, Fox, and Waite. I gained invaluable insights as I watched their trend-following methods reach impressive peaks, face sharp declines, and ultimately rebound. From late 2014 to early 2020, I witnessed a dramatic downturn across the trend-following industry. Iconic systems like Aberration, CatScan, Andromeda, and Super Turtle—once thriving on robust trends of the 1990s through early 2010s—began to falter long before the pandemic. Since 2020 we have seen the familiar trends return. Get six hours of video instruction with this edition.
Pick up your copies today – e-Book or paperback format – at Amazon.com
I enjoyed this George. Any chance you\’ll take a stab at Mr. Vince\’s Leverage Space Portfolio next? If so, how about in Visual Basic?
Hi Steve – I might do this in Python. Wouldn’t be difficult to convert to VB I wouldn’t think.
hi
my English is bad.But,I want to say that there\’s something wrong in the logic. In the book of Ralph Vince’s Book, the Optimal f means tracing the all the passed trades ,by looping from 1 to 0.01 the value f to find the
highest TWR.
to use the optimal f ,the program must first Simulate lots of trades. after that , we might use optimal f to Calculate the best share number into the following trades under an Hypothetical condition Winning probability is fixed.
Hi Sam – thanks for commenting on the post. Take a look at the function that does the actual calculation.
https://georgepruitt.com/easylanguage-code-for-optimal-f-multi-charts-and-vba-too/
In the code one of the comments reads: //calculate the Optimal F with last 10 trades.
But it should read //calculate the Optimal F with at least 10 trades.
To test we have to build the Optimal F with the trades we have at that point in time. The more trades the better. However, you have to use what you have to enable a back-test.
Let me know what you think.
George