In a recent article called Rank Your Trading System With Expectancy Score I discussed the concept of expectancy and expectancy score and how they can be used to help evaluate and compare the profitability of trading systems. In this article I’m going to provide some EasyLanguage code that will help you compute these values for a trading system and how to optimize a trading system based on expectancy. This article will be heavily geared towards TradeSation’s EasyLanguage, however the concepts can be applied to any programming language.
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 with then perform two things. First, it will populate two variables (oExpectancy and 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:
Below is a video where I give a demonstration on using the expectancy function.
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:
Jeff is the founder of System Trader Success – a website and mission to empowering the retail trader with the proper knowledge and tools to become a profitable trader the world of quantitative/automated trading.