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.
Function Variable Data Survives from One Call to the Next – A Pretty Nifty Tool in EasyLanguage!
Creating a function that can store data and then have that data survive on successive function calls without having to pass information back and forth is really a cool and powerful tool in EasyLanguage. In most programming languages, the variables defined in a function are local to that particular bit of code and once program execution exits the function, then the data is destroyed. There are two exceptions (in other languages) that come to mind – if the variable is passed back and forth via their addresses, then the data can be maintained or if the variable is global in scope to the function and the calling program. EasyLanguage prevents you from having to do this and this can definitely save on headaches. I wrote a function that defines an array that will hold a list of trades. Once the number of trades reaches a certain level, I then calculate a moving average of the last 10 trades. The average is then passed back to the calling strategy. Here is the simple code to the function.
{Function Name: StoreTradesFunc by George Pruitt} {Function to Calculate the average trade for past N trades. ---------------------------------------------------------- Function remembers the current trade count in tradeCnt. It also remembers the values in the array tradesArray. It does this between function calls. Values - simple and array - undoubtedly are global to the function}
totalTradesCount = totalTrades; If totalTradesCount > totalTradesCount[1] then begin tradeCnt = tradeCnt + 1; tradesArray[tradeCnt] = positionProfit(1); // print("Storing data ",tradesArray[tradeCnt]," ",tradeCnt); end;
If totalTrades > avgTradeCalcLen then begin Value2 = 0; For value1 = totalTrades downTo totalTrades - avgTradeCalcLen begin Value2 = value2 + tradesArray[value1]; end; print("Sum of last 10 Trades: ",value2); StoreTradesFunc = value2/avgTradeCalcLen; end;
Store A List of Trades in a Function
I call this function on every bar (daily would be best but you could do it on intra-day basis) and it polls the function/keyword totalTrades to see if a new trade has occurred. If one has, then the variable tradeCnt is incremented and the trade result is inserted into the tradesArray array by using the tradeCnt as the array index. When you come back into the function from the next bar of data tradeCnt and tradesArray are still there for you and most importantly still intact. In other words there values are held static until you change them and remembered. This really comes in handy when you want to store all the trades in an array and then do some type of calculation on the trades and then have that information readily available for use in the strategy. My example just provides the average trade for the past ten trades. But you could do some really exotic things like Optimal F. The thing to remember is once you define a variable or an array in a function and start dumping stuff in them, the stuff will be remembered throughout the life of the simulation. The function data and variables are encapsulated to just the function scope – meaning I can’t access tradesArray outside of the function. One last note – notice how I was able to determine a new trade had occurred. I assigned the result of totalTrades to my own variable totalTradesCount and then compared the value to the prior bar’s value. If the values were different than I knew a new trade had just completed.
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