Optimizer Fitness - System Quality Number
Optimizer Fitness - System Quality Number
Hi y'all,
Does anybody know how to pull information from each trade w/in an optimizer run? I want to calculate the stdev of the trades, and to do that I need to get information from each trade. I don't think we can do this using a custom criteria.
My goal is to produce Van K Tharp's System Quality Number. The formula is:
SQN= Squareroot(N) * Average (Profit&Loss) / Stdev (Profit&Loss).
The ideas are these:
The large the N, the more trading opportunities you have.
The large the average(P&L), the better you are obviously.
The smaller the Stdev (P&L), the more regular are your results and the smaller are the drawdowns. This is crucial when we want to increase our position size.
Additionally, if anyone can share a custom fitness criteria that measures (or is a good proxy for) the volatility of individual trades w/in a run, then I would be very grateful!
Cheers.
wadams19
Does anybody know how to pull information from each trade w/in an optimizer run? I want to calculate the stdev of the trades, and to do that I need to get information from each trade. I don't think we can do this using a custom criteria.
My goal is to produce Van K Tharp's System Quality Number. The formula is:
SQN= Squareroot(N) * Average (Profit&Loss) / Stdev (Profit&Loss).
The ideas are these:
The large the N, the more trading opportunities you have.
The large the average(P&L), the better you are obviously.
The smaller the Stdev (P&L), the more regular are your results and the smaller are the drawdowns. This is crucial when we want to increase our position size.
Additionally, if anyone can share a custom fitness criteria that measures (or is a good proxy for) the volatility of individual trades w/in a run, then I would be very grateful!
Cheers.
wadams19
- Dave Masalov
- Posts: 1712
- Joined: 16 Apr 2010
- Has thanked: 51 times
- Been thanked: 489 times
Dear wadams19,
New keywords have been added in MC 6.0 to allow access to trade properties if pyramiding is turned on.
Numeric OpenEntriesCount- number of open entries
Numeric OpenEntryDate(Numeric entry_index=0) - date of entry order execution. entry_index - entry number.
Numeric OpenEntryTime(Numeric entry_index=0) - time of entry order execution. entry_index - entry number.
Numeric OpenEntryPrice(Numeric entry_index=0) - price of entry order execution. entry_index - entry number.
Numeric OpenEntryContracts(Numeric entry_index=0) - number of entry order contracts. entry_index - entry number.
Numeric OpenEntryProfit(Numeric entry_index=0) - current entry profit. entry_index - entry number.
Numeric OpenEntryMaxProfit(Numeric entry_index=0) - maximum potential entry profit which was achieved. entry_index - entry number.
Numeric OpenEntryMinProfit(Numeric entry_index=0) - minimum potential entry profit which was achieved. entry_index - entry number.
Numeric OpenEntryProfitPerContract(Numeric entry_index=0) - current entry profit per contract. entry_index - entry number.
Numeric OpenEntryMaxProfitPerContract(Numeric entry_index=0) - maximum potential entry profit per contract which was achieved. entry_index - entry number.
Numeric OpenEntryMinProfitPerContract(Numeric entry_index=0) - minimum potential entry profit per contract which was achieved. entry_index - entry number.
Numeric OpenEntryComission(Numeric entry_index=0) - value of entry comission. entry_index - entry number.
New keywords have been added in MC 6.0 to allow access to trade properties if pyramiding is turned on.
Numeric OpenEntriesCount- number of open entries
Numeric OpenEntryDate(Numeric entry_index=0) - date of entry order execution. entry_index - entry number.
Numeric OpenEntryTime(Numeric entry_index=0) - time of entry order execution. entry_index - entry number.
Numeric OpenEntryPrice(Numeric entry_index=0) - price of entry order execution. entry_index - entry number.
Numeric OpenEntryContracts(Numeric entry_index=0) - number of entry order contracts. entry_index - entry number.
Numeric OpenEntryProfit(Numeric entry_index=0) - current entry profit. entry_index - entry number.
Numeric OpenEntryMaxProfit(Numeric entry_index=0) - maximum potential entry profit which was achieved. entry_index - entry number.
Numeric OpenEntryMinProfit(Numeric entry_index=0) - minimum potential entry profit which was achieved. entry_index - entry number.
Numeric OpenEntryProfitPerContract(Numeric entry_index=0) - current entry profit per contract. entry_index - entry number.
Numeric OpenEntryMaxProfitPerContract(Numeric entry_index=0) - maximum potential entry profit per contract which was achieved. entry_index - entry number.
Numeric OpenEntryMinProfitPerContract(Numeric entry_index=0) - minimum potential entry profit per contract which was achieved. entry_index - entry number.
Numeric OpenEntryComission(Numeric entry_index=0) - value of entry comission. entry_index - entry number.
- Dave Masalov
- Posts: 1712
- Joined: 16 Apr 2010
- Has thanked: 51 times
- Been thanked: 489 times
Thanks for getting back, Dave.
Just to clarify, what I'm looking for are words that can be called within the "Fitness Function" window. The code for a fitness function looks more like JavaScript, not EasyLanuaage (What you posted look like they're going to be used in EL.) What I was looking for was something similar to the existing list of reserved words supported in Multicharts optimization. Those are:
* StrategyPerformance.NetProfit
* StrategyPerformance.GrossProfit
* StrategyPerformance.GrossLoss
...
...
Are the reserved words that you posted supposed to be used as part of a strategy's logic or within the custom fitness's code?
Just to clarify, what I'm looking for are words that can be called within the "Fitness Function" window. The code for a fitness function looks more like JavaScript, not EasyLanuaage (What you posted look like they're going to be used in EL.) What I was looking for was something similar to the existing list of reserved words supported in Multicharts optimization. Those are:
* StrategyPerformance.NetProfit
* StrategyPerformance.GrossProfit
* StrategyPerformance.GrossLoss
...
...
Are the reserved words that you posted supposed to be used as part of a strategy's logic or within the custom fitness's code?
- Dave Masalov
- Posts: 1712
- Joined: 16 Apr 2010
- Has thanked: 51 times
- Been thanked: 489 times
-
- Posts: 323
- Joined: 07 Jan 2007
- Location: Lahore, Pakistan
- Has thanked: 64 times
- Been thanked: 57 times
Re: Optimizer Fitness - System Quality Number
SQN is a great basis for position sizing.
Being a discreationary trader EXCEL suffices for me, an example of which is attached. (The first week of May has simply been a gift!)
As for programming, regretabbly, I do not have the skills; but this may help:
http://hightick.com/TS/Functi ... index.html
Being a discreationary trader EXCEL suffices for me, an example of which is attached. (The first week of May has simply been a gift!)
As for programming, regretabbly, I do not have the skills; but this may help:
http://hightick.com/TS/Functi ... index.html
- Attachments
-
- EURUSD_sqn_may2011.xls
- (26.5 KiB) Downloaded 627 times
-
- Posts: 323
- Joined: 07 Jan 2007
- Location: Lahore, Pakistan
- Has thanked: 64 times
- Been thanked: 57 times
Re: Optimizer Fitness - System Quality Number
Yesterday I was rather careless posting the hightick.com link.
http://hightick.com/TS/Functi ... index.html
describes as SQN = SquareRoot(TotalTrades) * AverageWins / ( StdDev of Wins )
This appears to be an older definition.
Van Tharp describes a system’s quality in terms of Ratio of Expectancy to Standard Deviation of Risk. This is R Multiples / Standard Deviation of R Multiples.
My understanding of R Multiple is Profit or Loss / Stop Size or Average of variable Stop Sizes or Average Loss of trades under review.
Therefore, SQN = SquareRoot (TotalTrades) * R Multiples / Standard Deviation of R Multiples
The comments-added EXCEL file is attached.
http://hightick.com/TS/Functi ... index.html
describes as SQN = SquareRoot(TotalTrades) * AverageWins / ( StdDev of Wins )
This appears to be an older definition.
Van Tharp describes a system’s quality in terms of Ratio of Expectancy to Standard Deviation of Risk. This is R Multiples / Standard Deviation of R Multiples.
My understanding of R Multiple is Profit or Loss / Stop Size or Average of variable Stop Sizes or Average Loss of trades under review.
Therefore, SQN = SquareRoot (TotalTrades) * R Multiples / Standard Deviation of R Multiples
The comments-added EXCEL file is attached.
- Attachments
-
- EURUSD_sqn_may2011[i].xls
- (28.5 KiB) Downloaded 687 times
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re:
Are these keywords, including the ones that are provided as example in the (default) Custom Criteria, all the available keywords? If not, could copy-paste the whole list? That would be incredibly helpful.
New keywords have been added in MC 6.0 to allow access to trade properties if pyramiding is turned on.
[...]
Regards,
Josh
- Dave Masalov
- Posts: 1712
- Joined: 16 Apr 2010
- Has thanked: 51 times
- Been thanked: 489 times
Re: Optimizer Fitness - System Quality Number
Dear Josh,
Yes, these keywords are all the available keywords.
Yes, these keywords are all the available keywords.
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Optimizer Fitness - System Quality Number
Thanks. Do you have any plans to add Strategy Performance keywords in the future, so that we can 'look inside' strategies and calculate performance metrics as standard deviations and length of draw downs? (Given the memory challenges with this, this would probably be something for 64 bit version?)Yes, these keywords are all the available keywords.
Regards,
Josh
- Stan Bokov
- Posts: 963
- Joined: 18 Dec 2009
- Has thanked: 367 times
- Been thanked: 302 times
Re: Optimizer Fitness - System Quality Number
It's possible we will add them in the future, but there are no exact plans as of this time.Thanks. Do you have any plans to add Strategy Performance keywords in the future, so that we can 'look inside' strategies and calculate performance metrics as standard deviations and length of draw downs? (Given the memory challenges with this, this would probably be something for 64 bit version?)Yes, these keywords are all the available keywords.
Regards,
Josh
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Optimizer Fitness - System Quality Number
Thanks Stan. Can the parameter settings be accessed in a Custom Criteria function?It's possible we will add them in the future, but there are no exact plans as of this time.
If not, and if these could be relatively easily added (because, unlike the Strategy Position keywords, these are already known to the Optimization Report), can you consider adding them?
Regards,
Josh
- Stan Bokov
- Posts: 963
- Joined: 18 Dec 2009
- Has thanked: 367 times
- Been thanked: 302 times
Re: Optimizer Fitness - System Quality Number
Dear Josh,
We will consider adding them in the future.
We will consider adding them in the future.
- furytrader
- Posts: 354
- Joined: 30 Jul 2010
- Location: Chicago, IL
- Has thanked: 155 times
- Been thanked: 217 times
Re: Optimizer Fitness - System Quality Number
One workaround for this issue (which, admittedly, would take a little time), would be to code the indicator you're looking for into the actual code of the trading strategy you're optimizing.
Then, when you run your optimization, you can have the model print out to the output window each of the inputs you're optimizing, along with all of the statistics you want to track (including net profit, total trades, the SQN, etc). Every time MultiCharts ran another optimization trial, you would see another line in the output window. You could then copy and paste this data from the output window into a spreadsheet for review (in the code below, I separate the data by comma).
The key to this approach is that you would have to use functions associated with the ELCollections.dll, which is a public domain DLL that allows you to build lists from within an EasyLanguage strategy. Using the ELCollections.dll, we'd create a list with the P&L for each trade generated by a particular optimization, and then, using the built-in statistical functions from the ELCollections.dll, we can generate information like the standard deviation for the trade.
Of course, you'd first have to download and install the ELCollections.dll.
You can find that here: viewtopic.php?t=2483.
It's free, easy to install, and the associated documentation for these software routines (included in the .zip file) is actually very good. If you're interested in doing advanced programming in EasyLanguage, it's a must-have.
Okay, for example, let's say you wanted to generate Van Tharp's system quality number and you're optimizing three parameters: iParam1, iParam2, iParam3. You would write something like this:
The downside to this strategy is that you'd have to code this solution into every model you're going to optimize. Also, if the strategy ends the optimization period with an open trade, the information about that open trade will not be included in the SQN statistics. Finally, I'd recommend running this a couple of times and verifying the results manually to make sure they match up.
Good luck!
Then, when you run your optimization, you can have the model print out to the output window each of the inputs you're optimizing, along with all of the statistics you want to track (including net profit, total trades, the SQN, etc). Every time MultiCharts ran another optimization trial, you would see another line in the output window. You could then copy and paste this data from the output window into a spreadsheet for review (in the code below, I separate the data by comma).
The key to this approach is that you would have to use functions associated with the ELCollections.dll, which is a public domain DLL that allows you to build lists from within an EasyLanguage strategy. Using the ELCollections.dll, we'd create a list with the P&L for each trade generated by a particular optimization, and then, using the built-in statistical functions from the ELCollections.dll, we can generate information like the standard deviation for the trade.
Of course, you'd first have to download and install the ELCollections.dll.
You can find that here: viewtopic.php?t=2483.
It's free, easy to install, and the associated documentation for these software routines (included in the .zip file) is actually very good. If you're interested in doing advanced programming in EasyLanguage, it's a must-have.
Okay, for example, let's say you wanted to generate Van Tharp's system quality number and you're optimizing three parameters: iParam1, iParam2, iParam3. You would write something like this:
Code: Select all
Inputs: iParam1(0),
iParam2(0),
iParam3(0);
Vars: vSQN(0), // This is the variable used to calculate the System Quality Number
vNTrades(0), // We use this to keep track of how many trades have happened
vTradeList(ListN.New); // This is the ELCollections list we use to track our trades
// <--- Insert Trade Entry and Exit Rules Here --->
// In this code block, we save the trade result for any new trades into our list
If TotalTrades > vNTrades Then Begin
value10 = TotalTrades - vNtrades;
for value11 = 1 to value10 Begin
value1 = ListN.Pushback(vTradeList,PositionProfit(value11));
vNTrades = vNTrades + 1;
End;
End;
// When we reach the last bar on the chart, we calculate the SQN for all closed trades
// that have happened, and then output all of our results to the output window
If LastBarOnChart = True and TotalTrades > 0 Then Begin
value1 = ListN.Count(vTradeList); // The number of trades we've seen
value2 = ListN.StdDev(vTradeList,1,value1); // The standard deviation of these trades
value3 = ListN.Average(vTradeList,1,value1); // The average trade P&L
value4 = SquareRoot(value1); // The square root of the number of trades
vSQN = value4 * value3 / value2; // The SQN
// In the following line, we output relevant data for this particular trading trial
Print(NumToStr(iParam1,2) + "," + NumToStr(iParam2,2) + "," + NumToStr(iParam3,2) + "," + NumToStr(NetProfit,2) + "," + NumToStr(TotalTrades,2) + "," + NumToStr(vSQN,2));
End;
Good luck!
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Optimizer Fitness - System Quality Number
Thanks Furytrader! This is very, very helpful and will look into it this weekend. Thanks a lot for your code example, this is very helpful regarding the use of ELCollections.
Added bonus: this can also be used with Exhaustive optimization!
Thanks again,
Regards,
Josh
Added bonus: this can also be used with Exhaustive optimization!
Thanks again,
Regards,
Josh
Re: Optimizer Fitness - System Quality Number
Not to bring up an old thread but in order to make a custom optimization method for SQN MC would need to add 2 new reserved words,
a function that allows you to compute a square root of any existing reserved word
a function that allows you to calculate the std dev of any existing reserved word
MC should do this anyway, not just for SQN bc it opens the doors to lots of other possibilities when optimizing.
The only way to accomplish SQN for now is to export the metric to excel as mentioned above. a variation of the listed method above is to create a function called SQN w/ the following code:
then in your strategy code (you dont need a separate indicator) you can do the following:
You can add whatever other metrics you want in the TradeStr1 variable and change the output location as needed.
***It should be noted
the code everyone posts as SQN is not actually the real SQN as defined by van tharp, its just a t-statistic.
Tharp does something different:
SQN = (expectancy/stdev(R))*strq(# of trades)
expectancy = net profit / total trades
R is the ratio of reward to risk of the trade so you're looking at the stdev of the trade profit vs how much risk is associated w/ that trade ie trade profit$$/stop loss $$
in reality the whole shebang is something like
SQN = ((netprofit/total trades)/(stdev(tradeprofit/stoploss))*sqrt(# of trades)
can read more about that here http://www.blueowlpress.com/WordPress/a ... m-quality/
Along the lines of van tharp, you can optimize for expectancy in MC (even tho expectancy is not unique to van tharp nor did he create it even though he tried to patent the idea) which is
Expectancy = (Probability of Win * Average Win) – (Probability of Loss * Average Loss)
This reduces to netprofit /# of trades which you can shove into the custom fitness function:
in reality the above formula should return a notional dollar amount ie 15000/25 = 600 however it returns values between 0 and 2 for some reason so i'll keep messing around with it.
a function that allows you to compute a square root of any existing reserved word
a function that allows you to calculate the std dev of any existing reserved word
MC should do this anyway, not just for SQN bc it opens the doors to lots of other possibilities when optimizing.
The only way to accomplish SQN for now is to export the metric to excel as mentioned above. a variation of the listed method above is to create a function called SQN w/ the following code:
Code: Select all
// SQN - function
var:
j ( 0 ) ,
LB ( false ) ,
TT ( 0 ) ,
sqrtTT ( 0 ) ,
AverageTrade ( 0 ) ,
stdevTrades ( 0 ) ;
array: SD[10000](0); // must be >= total number of trades
LB = ( Date = JulianToDate(LastCalcJDate) and LastCalcMMTime = 60 * IntPortion(Time * 0.01) + 100 * FracPortion(Time * 0.01) ) ;
TT = TotalTrades ;
if TT > TT[1] then
begin
if TT > 1 then
for j = TT downto 2 begin
SD[j] = SD[j-1] ;
end ;
SD[1] = PositionProfit(1) ;
end ;
if LB then
begin
if TotalTrades > 0 then
if totaltrades <> 0 then begin
AverageTrade = NetProfit / TotalTrades ;
end;
stdevTrades = StandardDevArray( SD, TT, 1 ) ;
sqrtTT = SquareRoot( TotalTrades ) ;
if stdevTrades <> 0 then begin
SQN = sqrtTT * (AverageTrade / stdevTrades) ;
end;
end ;
Code: Select all
inputs: writetofile(false), filename("C:/output.csv");
vars: TradeStr1("");
if LastBarOnChart and writetofile then begin
TradeStr1=NumtoStr(sqn,2)+newline;
FileAppend(filename,TradeStr1);
end;
You can add whatever other metrics you want in the TradeStr1 variable and change the output location as needed.
***It should be noted
the code everyone posts as SQN is not actually the real SQN as defined by van tharp, its just a t-statistic.
Tharp does something different:
SQN = (expectancy/stdev(R))*strq(# of trades)
expectancy = net profit / total trades
R is the ratio of reward to risk of the trade so you're looking at the stdev of the trade profit vs how much risk is associated w/ that trade ie trade profit$$/stop loss $$
in reality the whole shebang is something like
SQN = ((netprofit/total trades)/(stdev(tradeprofit/stoploss))*sqrt(# of trades)
can read more about that here http://www.blueowlpress.com/WordPress/a ... m-quality/
Along the lines of van tharp, you can optimize for expectancy in MC (even tho expectancy is not unique to van tharp nor did he create it even though he tried to patent the idea) which is
Expectancy = (Probability of Win * Average Win) – (Probability of Loss * Average Loss)
This reduces to netprofit /# of trades which you can shove into the custom fitness function:
Code: Select all
//expectancy
if (StrategyPerformance.TotalTrades != 0) {
return StrategyPerformance.NetProfit / StrategyPerformance.TotalTrades;
}
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: Optimizer Fitness - System Quality Number
Hello Ulich05,
You can use Math.sqrt reserved word for custom fitness function.a function that allows you to compute a square root of any existing reserved word
You can calculate the value you need in PowerLanguage code and transfer it to custom fitness function using CustomFitnessValuea function that allows you to calculate the std dev of any existing reserved word
Re: Optimizer Fitness - System Quality Number
Has anything further been done with respect to using the SQN as a Custom Fitness Value since the last post in this thread? How did the previously posted code work for you?
-
- Posts: 323
- Joined: 07 Jan 2007
- Location: Lahore, Pakistan
- Has thanked: 64 times
- Been thanked: 57 times
Re: Optimizer Fitness - System Quality Number
Actually, MultiCharts has had the ability to calculate System Quality Number® for years, if not from the very first version! I took me some time to figure out:Has anything further been done with respect to using the SQN as a Custom Fitness Value since the last post in this thread? How did the previously posted code work for you?
SQN® = (Avg Trade / 1 Std. Deviation of Avg. Trade) * 10 if the number of trades is 100, or higher. If the number of trades is less than 100, then the square root of the number of trades replaces 10.
Avg Trade (win/loss) and 1 Std. Deviation of Avg. Trade are both available in Total Trade Analysis part of any Back-Testing Strategy Performance Report. I think a little bit of effort would allow users to apply SQN® for optimization, or whatever.
Khalid
-
- Posts: 323
- Joined: 07 Jan 2007
- Location: Lahore, Pakistan
- Has thanked: 64 times
- Been thanked: 57 times
Re: Optimizer Fitness - System Quality Number
SQN® is, indeed, a very valuable metric but, because it punishes upside volatility, its appeal is limited in many instances. Another metric with many of the same advantages, but which does not punish upside volatility is R-Multiple: Average Trade / Average Losing Trade in cases where average risk is unknown. Attached is a screenshot of R-Multiple Fitness Function.
Because MultiCharts calculates Average Losing Trade as a negative value, the best return will be the biggest negative number; please enable the optimisation report to display the bottom most values.
Khalid
Because MultiCharts calculates Average Losing Trade as a negative value, the best return will be the biggest negative number; please enable the optimisation report to display the bottom most values.
Khalid
- Attachments
-
- Optimise by R-Multiple.jpg
- (253.09 KiB) Downloaded 2555 times