System Optimization With Expectancy

Expectancy Function

In the previous article I showed you how to compute both expectancy and the expectancy score. Here, I want to provide you an EasyLanguage function that will do it for you. This code can be used when building your own systems or when evaluating trading systems where you have access to the code. Basically, if you have access to the code you can use this function. The function is called,  _CE_Expectancy and it uses the formula discussed in the previous article to calculate an expectancy. This function provides a quick way to calculate both expectancy values easily.

Place this function at the very bottom of the strategy code and execute your strategy. The function will then perform two things. First, it will populate two variables (oExpectancyand oExpectancySctore) with the expectancy value and expectancy score value. Second, if the Display boolean is set to true, the two expectancy values will be displayed within the TradeStation output window. If you don’t have Display set to true, the two expectancy values are not displayed but are accessible within the two output variables, oExpectancy and oExpectancySctore.

Below are the function input parameters as they appear in the code:

float oExpectancy(NumericRef), // Output: Expectancy
float oExpectancyScore(NumericRef), // Output: Expectancy Score
bool Display(Truefalsesimple); // Display results or not

Below is an example of using the function within a strategy. The first two input parameters will be the expectancy values. You can use these variables to display the values or send them to a file. The last parameter tells the function not to display the expectancy values in the TradeStation print log:

RetrunVal = _CE_Expectancy( vExpectancy, vExpectancyScore, false );

Below is a video where I give a demonstration on using the expectancy function.

Expectancy Optimizer

In the previous article I hinted that you could use expectancy score as the key value to optimize a trading system against. Many people will use net profit or average profit per trade, but expectancy score might be useful as well. The expectancy score includes trading opportunity (as defined by the frequency of trading) its calculation. However, there is a problem. TradeStation provides no way to optimize over expectancy score. It must be done manually. To aid in this process I created a simple function called, _CE_Expectancy_Optimizer.

This function can be added to the bottom of any trading code and is designed to help you optimize a single parameter. For example, let’s say you wish to optimize the RSI look-back period for a particular strategy. After adding the function and configuring the required parameters, you perform your TradeStation optimization as you normally would. After each iteration of the TradeStation optimizer a line is written to an Excel file which contains the current RSI look-back period, expectancy and expectancy score. You can then use Excel’s sort feature to sort the different optimization iterations based upon the expectancy score.

There is nothing fancy about the Excel document generated. In fact, if you open up this file you will find  no headers or description. This is due to this function being called each time an optimization run is performed. I found no quick way to add headers only once.

Below are the function input parameters:

`iWriteFlag( TrueFalseSeries ), // Should we write to a file or not?`
`iFileName(string), // File name`
`iSystemName( string ), // Name of system to test`
`iSystemVersionNumber( string ), // System's version number`
`iOptimize(NumericSimple); // The strategy input value you are optimizing`

Here is an example of using the function within a strategy:

`vRetValue = _CE_Expectancy_Optimizer( iWriteToFile, vFileName, RegimeLookback );`

Below is a video demonstration of the _CE_Expectancy_Optimizer function.

Conclusion

With these two functions you will have two tools needed to calculate both the expectancy and expectancy score for the trading systems you are developing.

Jeff is the founder of EasyLanguage Mastery - a website and mission to empowering the EasyLanguage trader with the proper knowledge and tools to become a profitable trader. Join our EasyLanguage FaceBook group to interact with other EasyLanguage traders! Click the FaceBook icon to join.

• Xerox says:

” You just stick it at the end of your signal and start the optimizer. Every iteration of the optimizer will cause a line to be written to an Excel .csv file. Then all you do is load it into Excel, sort by the last column, and voila! The parameters for maximum expectancy score are right at the top.”