Hi guys,
I've been trying to code up these fitness functions myself but I'm stuck. Have you guys come across how to calculate these? They have been referenced in a few books I've read recently as good fitness functions particularly for smoothing out equity curves. The principal is to create an idealized equity curve and calculate either the percent difference or correlation between the idealized trades and the actual trades. I've tried a dozen versions but can't seem to get it right. I was wondering if you have any insights into how they are calculated?
I've been trying to code up these fitness functions myself but I'm stuck. Have you guys come across how to calculate these? They have been referenced in a few books I've read recently as good fitness functions particularly for smoothing out equity curves. The principal is to create an idealized equity curve and calculate either the percent difference or correlation between the idealized trades and the actual trades. I've tried a dozen versions but can't seem to get it right. I was wondering if you have any insights into how they are calculated?
Rename
QUOTE:
The principal is to create an idealized equity curve and ...
You're looking for a "regularized" statistical fit. WL has one regularized fitting indicator, regEMA, for smoothing your price data. Have you tried it?
I know you're going to ask what statistical assumptions is its regularization based upon? I don't know; I haven't read the TASC paper. But I use regEMA all the time on WL with good success.
Another option would be to use a spline fit. These aren't statistical, but are based on first and second derivative properties. Math.Net has several spline functions including about 5 cubic spline functions. ScottPlot also has some "loose fitting" spline options. Check them out here. https://scottplot.net/cookbook/4.1/category/misc/#spline-interpolation-types
But finding a regularized fitting algorithm tailored to your specific application would be best choice, of course. A macro economics journal on numerical methods might have some regularized fitting algorithms published we could adapt to WL.
I did find this:
“ Perfect Profit Percentage: This assumes perfect forward information. Find the absolute BEST equity curve, then score strategies based upon this ideal (unattainable) equity curve.”
So it looks like it would need to simulate buying every low and selling every high, then get the percent return of that idealized strategy, and finally determine what percentage the actual strategy’s return is compared to this ideal?
Is the idea here to add these fitness functions to a new ScoreCard?
“ Perfect Profit Percentage: This assumes perfect forward information. Find the absolute BEST equity curve, then score strategies based upon this ideal (unattainable) equity curve.”
So it looks like it would need to simulate buying every low and selling every high, then get the percent return of that idealized strategy, and finally determine what percentage the actual strategy’s return is compared to this ideal?
Is the idea here to add these fitness functions to a new ScoreCard?
QUOTE:
Is the idea here to add these fitness functions to a new ScoreCard?
You may need to pass tuning parameters into the ScoreCard implementation to adjust the fit. Would that be possible to do?
What I typically do is create a Backtester extension method that works up the data and plots it out with ScottPlot. And I can pass parameters on that extension method.
What kind of parameter would be needed for this Perfect Profit Percent? It seems like it’s pretty cut and dried. And no there’s no current mechanism to pass a parameter to a ScoreCard metric.
QUOTE:
What kind of parameter would be needed for this Perfect Profit Percent?
I don't know. But the regularized EMA indicator, regEMA, takes two parameters.
The problem with the equity curve is that it has very nonlinear behavior (since it's event driven), so trying to model it with a linear method is hard. I have thought about using a nonlinear method (a point system scoring the trades), but I never settled on a way to do that. If you know of a paper doing something like that, please post the reference.
Thanks for all of your comments. Yes I was trying to add it to a scorecard. It would also be useful for the genetic strategy builder. Unlike other standard fitness functions it is comparing the captured profit from the potential profit or rather another way to look at efficiency.
From what I have read it doesn't take any parameters in most implementations. I would rather just make some generalized assumptions instead of going the parameter route. What I tried was to utilize MAE/MFE time series for each trade to approximate optimal equity curve (create logic that each trade exited optimally). Then calculate the correlation between the optimal equity curve and the actual equity curve. This still didn't seem to work ideally.
I can try to mock this up later today or tomorrow, I have a good idea on an approach.
Awesome! Let me know if I can help in any way.
QUOTE:
I tried was to utilize MAE/MFE time series for each trade to approximate optimal equity curve
I like the idea.
QUOTE:
I can try to mock this up later today or tomorrow,
It's probably going to take a ScottPlot to debug the implementation. That's what it took to debug the polynomial fit of the equity curve I did.
What would be nice is a ScottPlot of the MAE, MFE, and the equity curve segments to see how far apart they are. I may be able to help with the ScottPlotting part.
I was thinking about the Profit Percent and I came to the conclusion that it doesn't provide much value. I most certainly could be wrong, since I'm not a mathematician and don't read scientific papers, so my understanding might be incorrect, but hear me out.
The first step is to determine the profit of THE ideal strategy. This is easy enough to do if you're trading one symbol. You could just run a strategy that peeks ahead and uses limit orders to buy every swing low and sell/short every swing high.
It gets more complicated when you're dealing with a portfolio. You could run the ideal strategy on every symbol and take the one that resulted in the highest profit. It will be the symbol that has the largest swings. But there's most likely a more ideal strategy that would have to buy/short in and out of different symbols along the timeline. It becomes near-impossible to calculate the very best ideal strategy switching symbol at various times.
But say we are trading one symbol and can get the ideal profit, say it's $200,000. If our strategy profit is $100,000 then the Perfect Profit is 50%. If our strategy generates $150,000 then Perfect Profit becomes 75%. When our profit increases, Perfect Profit increases. When our profit decreases, Perfect Profit decreases.
So does it matter what number the ideal profit is? And does this provide any value over just using profit as a fitness function?
Looking forward to learning what I missed in all this and how Perfect Profit actually does add some value :)
The first step is to determine the profit of THE ideal strategy. This is easy enough to do if you're trading one symbol. You could just run a strategy that peeks ahead and uses limit orders to buy every swing low and sell/short every swing high.
It gets more complicated when you're dealing with a portfolio. You could run the ideal strategy on every symbol and take the one that resulted in the highest profit. It will be the symbol that has the largest swings. But there's most likely a more ideal strategy that would have to buy/short in and out of different symbols along the timeline. It becomes near-impossible to calculate the very best ideal strategy switching symbol at various times.
But say we are trading one symbol and can get the ideal profit, say it's $200,000. If our strategy profit is $100,000 then the Perfect Profit is 50%. If our strategy generates $150,000 then Perfect Profit becomes 75%. When our profit increases, Perfect Profit increases. When our profit decreases, Perfect Profit decreases.
So does it matter what number the ideal profit is? And does this provide any value over just using profit as a fitness function?
Looking forward to learning what I missed in all this and how Perfect Profit actually does add some value :)
QUOTE:
The first step is to determine the profit of THE ideal strategy.
I haven't read the article you're referring to, but you might post a link. But I wouldn't call it the "ideal strategy". I would call it the ideal time to sell so as to get the best MAE and MFE action.
QUOTE:
This is easy enough to do if you're trading one symbol.
And that's the problem. This is about judging the merit of a strategy's sell algorithm for one particular stock. Is the strategy and stock a good fit for each other as far as the sell algorithm is concerned? This ScoreCard metric is designed exclusively for use by Strategy Rankings Window (tool) and nothing else. And the goal is to match up individual incoming stocks (from a screener) to a given strategy.
You could use it to improve the sell code of strategy itself, but Analysis Series can also model MAE and MFE for that purpose now. What's interesting about Analysis Series is that it handles MAE/MFE modeling on a dataset of stocks rather than just individual stocks. That's a nice trick.
For my own polynomial fitting equity curve ScoreCard, which I called ScreenerCard, it only displays the ScottPlot of the fit and analysis for single stocks, although it can work on datasets to display the latest equity gain behavior (using a crippled algorithm). But its real purpose is to work with the Strategy Rankings Window to help place/match individual stocks to individual strategies. That's why it's called ScreenerCard.
The current ScoreCard paradigm allows the author to declare if a metric should be included as a choice for the optimizer. That paradigm could be expanded so the author can flag a metric as a choice for individual stocks, not datasets, as well. Such metrics would be targeting the Strategy Rankings Window and would only be displayed in the Metrics Report (or Data Panel) if they were for individual stocks.
How about using the equity curve and the equity mae/mfe and treat it like a single asset? Do you store the portfolio aggregate mae and mfe?
a) Step through the equity curve for each time step
b) Assume a change in equity is a single trade
c) Use the aggregate mae and mfe to determine the optimal equity curve (there are obviously some assumptions being made on timing of the mae mfe).
d) calculate the percent difference / correlation to the optimal equity curve.
Even though it isn't perfect, I still think it would be a good fitness function as it is giving you a good ranking of efficiency. Does that make sense to you guys?
a) Step through the equity curve for each time step
b) Assume a change in equity is a single trade
c) Use the aggregate mae and mfe to determine the optimal equity curve (there are obviously some assumptions being made on timing of the mae mfe).
d) calculate the percent difference / correlation to the optimal equity curve.
Even though it isn't perfect, I still think it would be a good fitness function as it is giving you a good ranking of efficiency. Does that make sense to you guys?
I do know that it is a common practice for creating strategy portfolios to just use the aggregate equity curve to calculate correlations by extracting out theoretical trades as a means of efficiency. Especially if you are running millions of scenarios to generate optimal portfolio's of strategies as it would be impractical to do it any other way.
We're talking about Perfect Profit Correlation now, which I think has more value than Perfect Profit Percent.
Regarding using the equity curve to calculate an idealized curve:
It would have to be done by writing some code to re-construct the equity curve trade by trade, but exiting each position at its MFE. It's a bit complicated if you want to also factor in commissions, dividends, and cash & margin interest like WL8's backtester already does.
You'd then get the correlation of the idealized equity curve and the actual equity curve.
It's interesting to note that TradeStation uses the approach I mentioned in Post #11 to get it's ideal equity curve. It's based on an ideal strategy that buys every bottom and sells every top. I'd say it's possible for only single security backtesting. Their PPC is described at the bottom of this page:
https://help.tradestation.com/09_01/tradestationhelp/optimize/sor_report_fitness_function_fields.htm
Regarding using the equity curve to calculate an idealized curve:
It would have to be done by writing some code to re-construct the equity curve trade by trade, but exiting each position at its MFE. It's a bit complicated if you want to also factor in commissions, dividends, and cash & margin interest like WL8's backtester already does.
You'd then get the correlation of the idealized equity curve and the actual equity curve.
It's interesting to note that TradeStation uses the approach I mentioned in Post #11 to get it's ideal equity curve. It's based on an ideal strategy that buys every bottom and sells every top. I'd say it's possible for only single security backtesting. Their PPC is described at the bottom of this page:
https://help.tradestation.com/09_01/tradestationhelp/optimize/sor_report_fitness_function_fields.htm
Here's some code to generate a "perfect equity curve" based on the MFEs of the positions. It doesn't consider cash/margin interest or dividends.
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using System.Windows.Documents; using ScottPlot.Plottable; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { } //a close crosses SMA system public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { if (bars.Close[idx] > SMA.Value(idx, bars.Close, 10)) PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { if (bars.Close[idx] < SMA.Value(idx, bars.Close, 10)) PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } //declare private variables below public override void BacktestComplete() { double currentEquity = Backtester.PositionSize.StartingCapital; double currentCash = currentEquity; //create TimeSeries for perfect equity curve TimeSeries perfectEquity = new TimeSeries(Backtester.EquityCurve.DateTimes); double equity = Backtester.PositionSize.StartingCapital; //create a list of all positions, sorted by entry date List<Position> positions = new List<Position>(Backtester.Positions); positions.Sort((a, b) => a.EntryDate.CompareTo(b.EntryDate)); //this list will hold open positions List<Position> openPositions = new List<Position>(); //process each bar of the equity curve for (int n = 0; n < perfectEquity.Count; n++) { DateTime dt = perfectEquity.DateTimes[n]; //add positions that are open on this date while(positions.Count > 0 && positions[0].EntryDate <= dt) { openPositions.Add(positions[0]); Position openPos = positions[0]; double entryValue = openPos.Quantity * openPos.EntryPrice; currentCash -= entryValue; positions.RemoveAt(0); } //process all open positions for this bar currentEquity = currentCash; for (int p = openPositions.Count - 1; p >= 0; p--) { Position openPos = openPositions[p]; //is it at its MFE this bar? if (openPos.MFE == openPos.MFEAsOf(n)) { SetBackgroundColor(BacktestData[0], n, WLColor.Gray.MakeTransparent(32)); //yes, add MFE currentEquity += openPos.MFE; currentCash += openPos.MFE; double entryValue = openPos.Quantity * openPos.EntryPrice; currentEquity += entryValue; currentCash += entryValue; //and remove it openPositions.RemoveAt(p); } else { //no, add its value as of this bar currentEquity += openPos.ValueAsOf(n, false); } } //save this equity value perfectEquity[n] = currentEquity; } //plot equity, perfect equity PlotTimeSeries(Backtester.EquityCurve, "Equity", "Equity", WLColor.Green); PlotTimeSeries(perfectEquity, "Perfect Equity", "Equity", WLColor.Lime); } } }
Thanks Dion. Let me digest this overnight. I appreciate your help!
Here is a perfect profit strategy I built last year using building blocks. It's a simple stop & reverse strategy using the ZigZag HL indicator that peaks ahead 1 day. In my testing I think this is only useful for curve fitting/machine learning algorithms like the famous "Trading System Lab". Since these algos need a target to curve fit, perfect profit strategies are ideal.
Your Response
Post
Edit Post
Login is required