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.
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:
I will attach the eld and .xlsm file a little later.
Click Here for OptimalF Spreadsheet.
Please follow and like us:
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.
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.
Please follow and like us: