System Performance – Helping you Master EasyLanguage https://easylanguagemastery.com Helping you Master EasyLanguage Tue, 26 Apr 2022 03:14:18 +0000 en-US hourly 1 https://wordpress.org/?v=7.0 https://easylanguagemastery.com/wp-content/uploads/2019/02/cropped-logo_size_icon_invert.jpg System Performance – Helping you Master EasyLanguage https://easylanguagemastery.com 32 32 System Performance and Confidence Interval https://easylanguagemastery.com/building-strategies/system-performance-and-confidence-interval/?utm_source=rss&utm_medium=rss&utm_campaign=system-performance-and-confidence-interval https://easylanguagemastery.com/building-strategies/system-performance-and-confidence-interval/#comments Mon, 15 Oct 2018 10:00:25 +0000 http://systemtradersuccess.com/?p=3748

When you review the performance of a trading model, how do you know it’s worth trading for? How do you know it’s the right system for you? How confident are you that it will continue to profit in the future? When it comes to evaluating your trading model there are many factors to take into account. Some of them are obvious such as Net Profit and Risk-Per-Trade. Others may be a bit more unfamiliar such as Sharpe ratio or Profit Factor. This article is going to be the first article in several where I highlight a method or idea that can help you gauge the quality of a given trading model. In this article I would like to highlight a statistical-based metric that can be used to help indicate the likelihood that a given system will continue to generate profits in the future.

Many people simply look at the net profit of a trading model assuming a system with more profit must be the better system. This is often far from a good idea. More profit may also mean more risk, deeper drawdown, or other compromises to achieve those higher results. When testing trading models during the development process or reviewing a commercially available system before making a purchase, it is advisable to have a few metrics on hand that will allow you to make a wise choice. There is no one single score that will give you the definitive answer. Furthermore, everyone has unique risk tolerances and expectations on what is considered tradable. Yet, we can make smarter choices than simply looking at net profit. Here is one method you should be aware of.

Confidence Interval

It’s easy to find a trading model that has a positive average profit of $100 and then conclude it could be profitable into the future. But is there a metric we can use to help us predict what might happen into the future? A complicated approach would be to use the Monte Carlo method, but not everyone has access to this however, we all have access to a simple calculator. By visiting a topic in statistics called Confidence Intervals (CI) we can obtain a hint at what’s possible and perhaps find weaknesses in our seemingly profitable trading model.

The average net profit of a trading model is simply the historical P&L for each trade over a given time period. Let’s imagine a trading model that has produced 60 trades. Some of the trades are winners and some are losers. We add the total P&L together for each individual trade and divide it by the number of trades – 60 in this case – and we get $100. Clearly this is well above zero so in the long run this system appears profitable.

However, we also know that individual trades can be very different from our average profit per trade. Some trades produce much larger winning trades while others produce smaller winning trades. Still, other trades produce a range of losing trades. If we graph each trade’s P&L and then draw a line representing our average P&L we would see each individual trade falls around our mean value of $100. In other words, the P&L for any trade will vary around this mean value. We can measure this variation and use it to estimate the likelihood the system might remain profitable.

Statistically speaking, a trading model that exhibits a large standard deviation of profit per trade will have an increased chance of failing in the future. This is true even if the average mean is currently profitable. But what makes a standard deviation too large? This is explained below when we attempt to use our confidence interval to estimate a likely range of average P&L values into the future.

The green line is the average P&L. This line is plotted for each trade and we can see it’s angled downward showing a falling P&L as time goes by. Each blue plot is a single trade’s net P&L.

What we wish to do with our confidence interval is estimate with 95% confidence, if our system will probably produce a negative average P&L into the future. In other words, is it likely our seemingly profitable trading model is based upon chance? We can estimate this with our CI formula.

CI = t * SD / squareroot( N )
CI = Confidence Interval
t = T-score (we estimate value to be 2 and the reasoning behind this is beyond this article)
SD = P&L Standard Deviation for all trades
N = number of trades in our sample

With our imaginary trading model we have a $100 average net profit and 60 trades in our sample. Please note that in order for this method to work, you must have a minimum of 60 trades in your sample. Let’s also state the standard deviation for all trades is $400. With this information we can compute our 95% confidence interval.

CI = 2 * $400 / squareroot( 60 )
CI = $800 / 7.746
CI = $103.28

For simplicity let’s round the confidence interval to the nearest dollar which is $103. What do we do with this value? We create a range or band around our average net profit value of $100 by both adding and subtracting the confidence interval value.

upper band = Average Net Profit + CI = $203
lower band = Average Net profit - CI = -$3

We have now created a range of -$3 to $203 for our average net profit. What does this mean? Based on our calculation we have estimated with 95% confidence that our trading model’s average net profit could be as low as -$3 or as high as $203. The important number is the lower band because this represents a worse case situation. In our example, we have a negative value which indicates a losing model. Or at least, a potential losing scenario.

In short, our hypothetical trading model’s average net profit could be based upon chance and in the future could produce a negative P&L. Suddenly what seems like a solid system appears more shaky. Does this mean our trading model should be abandoned? Not necessarily.

In the case of the confidence interval there are two critical factors at play. Those values are the number of trades (N) and the value of the standard deviation between trades. Modification of the standard deviation can be achieved by altering the trading model logic. Modifying stops, targets, and other trading rules will change the standard deviation value. The goal would be to tighten the variation of each trade to reduce the standard deviation. This in turn, would create a smaller confidence interval. However, if you don’t want to modify the system or if you are unable to modify the system there is another way.

Our example system was based on 60 trades. This is really not a lot of trades. Let’s say we find more data to test our system and we get up to 100 trades. Let’s also pretend all the other performance factors stay the same. If we recalculate our confidence interval value we now get a value of $80. This gives us a range of $20 and $180. In this case, we have a system which produces positive value for the lower band. So maybe before we make a judgment on a system that appears borderline we should collect more trades first.

I should also point out that our imaginary trading model we are looking at has $30 deduction for each trade to account for slippage and commissions. So this negative effect is already factored into our confidence interval calculation. If we did not take into account slippage and commissions during our back-testing we would have to deduct this from our final range which would give us -$10 and $150. The impact of commissions and slippage just puts us back into negative territory gain. But we have them accounted for in our back-tested results.

As you can see having enough data points (trades) can have a significant impact on the confidence interval calculation. For system trading there are many reasons for having a large number of trades. Of course continuing to add more and more trades is not going to turn a losing system into a good system. The point here is sometimes you need to have more data before making an informed decision. If you have what you believe is a good system, yet you only have a few data points, the confidence interval calculation may be warning you to get more trades in the test sample.

What Confidence Interval Does Not Tell Us and How To Use It

Our confidence interval calculation makes some assumptions about the data. That is, the data points (trades) have a normal distribution. This of course is not necessarily correct. In many cases our trade distribution looks somewhat normal with fat tails in our distribution. Thus, we must take our confidence interval results with a grain of salt.

A bigger issue, I think is the confidence interval calculation does not indicate if this system has been curve fitted. If we have a killer system with 1,000 trades with a confidence interval range of $100 – $200 that’s great. However, it’s pointless if the system is curve fitted to the historical data and there is no way our confidence interval calculation can tell us. So, what do we do?

Ideally you would validate your new trading model on out-of-sample data to help detect if your model is over-fit to the historical data. Furthermore, you would also perform the confidence interval calculation on the out-of-sample data of your trading model. By doing this, you’ll have more meaningful results. This step will help reduce the possibility of trading an over-fit trading model and give more meaning to your confidence interval test.

But even if we have a solid system that is not curve fitted to the historical data, our confidence interval calculations are no guarantee of success in the future. The markets are dynamic and changing and it’s possible that the distribution of trades will change thus altering our average trade and standard deviation. In the end, even if our system looks great on paper, validates on the out-of-sample data, and our confidence interval looks fantastic, our trading model could fail as soon as we trade it live. If this is the case, what is the point of all these testing and is it worth doing? The short answer is, yes.

In trading there is no guarantee for future results – ever. The point of testing a system is not to prove how much money it will make in the future. The goal is to find reasons why not to trade it. The purpose of any validating test is to find weaknesses so we can address those weaknesses now before we have money on the line. Our job as professional system traders is to manage risk which means eliminating risky actions. There is no certainty. This is one of the reasons this field is so psychologically difficult.

As a final point, by using confidence interval we have another tool to find weaknesses and ultimately gives us more confidence that a particular trading model will likely bring us success into the future.

]]>
https://easylanguagemastery.com/building-strategies/system-performance-and-confidence-interval/feed/ 12
System Optimization With Expectancy https://easylanguagemastery.com/building-strategies/easylanguage-expectancy-functions/?utm_source=rss&utm_medium=rss&utm_campaign=easylanguage-expectancy-functions https://easylanguagemastery.com/building-strategies/easylanguage-expectancy-functions/#comments Mon, 01 Oct 2012 10:00:20 +0000 http://systemtradersuccess.com/?p=3807

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.

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.

]]>
https://easylanguagemastery.com/building-strategies/easylanguage-expectancy-functions/feed/ 4
Rank Your Trading System With Expectancy Score https://easylanguagemastery.com/rank-your-trading-system-with-expectancy-score/?utm_source=rss&utm_medium=rss&utm_campaign=rank-your-trading-system-with-expectancy-score https://easylanguagemastery.com/rank-your-trading-system-with-expectancy-score/#comments Mon, 24 Sep 2012 10:00:41 +0000 http://systemtradersuccess.com/?p=3786

In a previous article called, “System Performance and Confidence Interval“, I showed how a statistical method could be used to analyze historical trading results to give us an idea if the system would likely fail in the future. In this article I would like to introduce a mathematical formula which can be applied to any trading system and used as an objective score to compare and rank different trading systems.

When it comes to trading systems, no two are alike. There is a vast number of different trading styles that cover the range from simple tick scalping to multi year investment models. Of course, there is a huge number of different instruments and markets to trade. How would one determine if two different trading systems that trade different markets with different trading styles make an informed choice on which system was more profitable? How do you compare two trading systems’ performances? If two trading systems are both profitable and both seem like good systems, is there a single metric that can be used to compare the profitability between each?

Expectancy

Expectancy is a concept that was described in Van Tharp’s book, Trade Your Way To Financial Freedom. Expectancy tells you on average how much you expect to make per dollar at risk. For example, if you have a trading system that has a .50 Expectancy that means for every dollar you risk the trading system returns $.50.

There are two ways to compute Expectancy. Both methods are simple but one requires a little more explanation but does give a more conservative answer. The second method is a bit more straightforward to explain but only give an approximation of Expectancy. However, this approximation is good enough for what we are attempting to accomplish here.

Often you will find that expectancy is computed with the following formula:

Expectancy = (AW * PW + AL * PL ) / | AL |  
Where:
AW = Average winning trade in dollars
PW = Probability of winning trades
| AL | = Absolute value of the average losing trade in dollars
PL= Probability of losing trades

But if you look carefully at the calculation within the parenthesis you will see the value is nothing more than the trading system’s average net profit per trade. Substituting this value gives us the simplified formula of:

Expectancy = Average Net Profit Per Trade / | AL |

The two values you plug into the Expectancy formula can be found on most (if not all) strategy performance reports generated by backtesting. This is certainly true for TradeStation‘s strategy reports.

Now that we have our trading system’s Expectancy value are we ready to use this value to compare to other trading systems? Not yet. There is another step we must first take. Our Expectancy value simply tells us our historical profit per dollar risked for each trade. But we are missing something. Let’s imagine we have two trading systems that have two different Expectancy values:

Trading System #1 has an Expectancy of .25
Trading System #2 has an Expectancy of .50

Based on what we know it appears Trading System #2 produces more profit per dollar risked on each trade. In fact, it produces twice as much profit per dollar risked. Thus, if we risked $500 on each trade, Trading System 1 would generate $125 dollars while Trading System #2 would generate $250. But this is not the complete picture. We are missing the frequency at which each trading system operates. For example, maybe Trading System #1 trades once per day while Trading System #2 trades once per week. We need to take into account the number of times the trading system trades over the number of days the system was tested. In Van Tharp’s book he described that as Expectancy multiplied by opportunity. Opportunity is nothing more than how often does a given trading system trade. Opportunity times Expectancy leads us to our final calculation for Expectancy Score.

Expectancy Score

This value is an annualized Expectancy value which produces an objective number that can be used in comparing various trading systems. In essence the Expectancy Score factors in a trading system’s trade frequency. The higher the Expectancy Score the more profitable the system. This final score allows you to compare very different trading systems.

Expectancy Score = Expectancy * Number of Trades * 365 / Days in historical test

The Number of strategy trading days is nothing more than the number of days your backtesting was performed.

Conclusion

With the Expectancy Score in hand we have a metric to aid us in comparing different trading systems. Other uses for Expectancy Score might also include using this value as a target for optimization. Often optimization is performed on net profit, profit factor, Sharpe ratio or other metrics. Using Expectancy may also be something worth pursuing. But how would you do that using TradeStation? Unfortunately, there is no easy way to do it with TradeStation. However, I’m working on some EasyLanguage code that will help with a manual process. More on that in a future issue.

]]>
https://easylanguagemastery.com/rank-your-trading-system-with-expectancy-score/feed/ 10