Optimal F in EasyLanguage for TradeStation and MultiCharts
Here is the code for the Optimal F calculation. For a really good explanation of Optimal f, I refer you to Ralph Vince’s Book Portfolio Management FORMULAS. We had programmed this years ago for our Excalibur software and I was surprised the EasyLanguage code was not really all that accessible on the internet. Finding the Optimal f is found through an iterative process or in programmers terms a loop. The code is really quite simple and I put it into a Function. I decided to create this function because I wanted to demonstrate the ideas from my last post on how a function can store variable and array data. Plus this code should be readily available somewhere out there.
//OptimalFGeo by George Pruitt //My interpretation Sept. 2018 //www.georgepruitt.com //georgeppruitt@gmail.com
totalTradesCount = totalTrades; If totalTradesCount > totalTradesCount[1] then begin tradeCnt = tradeCnt + 1; tradesArray[tradeCnt] = positionProfit(1); end;
// Taken from my Fortran library - GPP and Vince Book PMF
optF = 0.0; gMean = 1.00; gat = 0.00; //Only calculate if new trade IF(tradeCnt>minNumTrades and totalTradesCount > totalTradesCount[1]) then Begin biggestLoser = 0; grandTot = 0; For iCnt = 1 to tradeCnt //get the biggest loser begin grandTot = grandTot + tradesArray[iCnt]; IF(tradesArray[iCnt]<biggestLoser) then biggestLoser = tradesArray[iCnt]; end; // print(grandTot," ",biggestLoser); IF({grandTot > 0 and} biggestLoser <0) then begin // print("Inside TWR Calculations"); highI = 0; hiTWR = 0.0; for iCnt = 1 to 100 begin fVal = .01 * iCnt; TWR = 1.0; for jCnt = 1 to tradeCnt // calculate the Terminal Wealth Relative begin HPR = 1. + (fVal * (-1*tradesArray[jCnt]) / biggestLoser); TWR = TWR * HPR; // print(fVal," ",iCnt," " ,jCnt," Trades ",tradesArray[jCnt]," HPR ",HPR:6:4," TWR : ",TWR:6:4," hiTWR",hiTWR:6:4," bl ",biggestLoser); end; // print(iCnt," ",TWR," ",hiTWR); IF(TWR>hiTWR) THEN begin hiTWR = TWR; optF = fVal; // assign optF to fVal in case its the correct one end else break; //highest f found - stop looping end; If (TWR <= hiTWR or optF >= 0.999999) then begin TWR = hiTWR; OptimalFGeo = optF; //assign optF to the name of the function end; gmean = power(TWR,(1.0 / tradeCnt));
if(optF<>0) then GAT = (gMean - 1.0) * (biggestLoser / -(optF)); print(d," gmean ",gmean:6:4," ",GAT:6:4); // I calculate the GMEAN and GeoAvgTrade end; end;
Optimal F Calculation by Ralph Vince code by George Pruitt
VBA version of Optimal F
For those of you who have a list of trades and want to see how this works in Excel here is the VBA code:
Sub OptimalF()
Dim tradesArray(1000) As Double i = 0 biggestLoser = 0# Do While (Cells(3 + i, 1) <> "") tradesArray(i) = Cells(3 + i, 1) If tradesArray(i) < bigLoser Then biggestLoser = tradesArray(i) i = i + 1 Loop tradeCnt = i - 1 highI = 0 hiTWR = 0# rc = 3 For fVal = 0.01 To 1 Step 0.01 TWR = 1# For jCnt = 0 To tradeCnt HPR = 1# + (fVal * (-1 * tradesArray(jCnt)) / biggestLoser) TWR = TWR * HPR Cells(rc, 5) = jCnt Cells(rc, 6) = tradesArray(jCnt) Cells(rc, 7) = HPR Cells(rc, 8) = TWR rc = rc + 1 Next jCnt Cells(rc, 9) = fVal Cells(rc, 10) = TWR rc = rc + 1
If (TWR > hiTWR) Then hiTWR = TWR optF = fVal Else Exit For End If
Next fVal If (TWR <= hiTWR Or optF >= 0.999999) Then TWR = hiTWR OptimalFGeo = optF End If Cells(rc, 8) = "Opt f" Cells(rc, 9) = optF rc = rc + 1 gMean = TWR ^ (1# / (tradeCnt + 1)) If (optF <> 0) Then GAT = (gMean - 1#) * (biggestLoser / -(optF)) Cells(rc, 8) = "Geo Mean" Cells(rc, 9) = gMean rc = rc + 1 Cells(rc, 8) = "Geo Avg Trade" Cells(rc, 9) = GAT
End Sub
VBA code for Optimal F
I will attach the eld and .xlsm file a little later.
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 believe there is an error in line 8 of the VBA code. There is a variable named \”bigLoser\” which is not referred to anywhere else in the code.
Hi Pepe – righto! I will double check.
Thanks for the input,
George