Optimizer Fitness - System Quality Number

Questions about MultiCharts and user contributed studies.
wadams19
Posts: 4
Joined: 30 May 2010

Optimizer Fitness - System Quality Number

Postby wadams19 » 05 Jul 2010

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

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Postby Dave Masalov » 14 Jul 2010

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.

tcat
Posts: 175
Joined: 02 Feb 2008
Location: Lausanne, Switzerland
Has thanked: 9 times
Been thanked: 5 times

Postby tcat » 14 Jul 2010

Dave, from where do those come from?

Have those items been disclosed before?

Thierry

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Postby Dave Masalov » 15 Jul 2010

Dear Thierry,

These new keywords have been added in MC 6.0 beta 2.

wadams19
Posts: 4
Joined: 30 May 2010

Postby wadams19 » 15 Jul 2010

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?

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Postby Dave Masalov » 16 Jul 2010

Dear wadams19,

The words that I have posted are supposed to be used within the custom fitness's code.

bibulous
Posts: 56
Joined: 16 Apr 2010
Has thanked: 17 times
Been thanked: 2 times

Re: Optimizer Fitness - System Quality Number

Postby bibulous » 20 Apr 2011

Is an implementation of the SQN as fitness's code available somewhere?

khalaad
Posts: 323
Joined: 07 Jan 2007
Location: Lahore, Pakistan
Has thanked: 64 times
Been thanked: 57 times

Re: Optimizer Fitness - System Quality Number

Postby khalaad » 07 May 2011

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
Attachments
EURUSD_sqn_may2011.xls
(26.5 KiB) Downloaded 582 times

khalaad
Posts: 323
Joined: 07 Jan 2007
Location: Lahore, Pakistan
Has thanked: 64 times
Been thanked: 57 times

Re: Optimizer Fitness - System Quality Number

Postby khalaad » 08 May 2011

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.
Attachments
EURUSD_sqn_may2011[i].xls
(28.5 KiB) Downloaded 658 times

bibulous
Posts: 56
Joined: 16 Apr 2010
Has thanked: 17 times
Been thanked: 2 times

Re: Optimizer Fitness - System Quality Number

Postby bibulous » 16 May 2011

Thanks!

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re:

Postby JoshM » 28 Jun 2011


New keywords have been added in MC 6.0 to allow access to trade properties if pyramiding is turned on.

[...]
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. :)

Regards,
Josh

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Re: Optimizer Fitness - System Quality Number

Postby Dave Masalov » 30 Jun 2011

Dear Josh,

Yes, these keywords are all the available keywords.

User avatar
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

Postby JoshM » 30 Jun 2011

Yes, these keywords are all the available keywords.
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?)

Regards,
Josh

User avatar
Stan Bokov
Posts: 963
Joined: 18 Dec 2009
Has thanked: 367 times
Been thanked: 302 times

Re: Optimizer Fitness - System Quality Number

Postby Stan Bokov » 30 Jun 2011

Yes, these keywords are all the available keywords.
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?)

Regards,
Josh
It's possible we will add them in the future, but there are no exact plans as of this time.

User avatar
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

Postby JoshM » 01 Jul 2011

It's possible we will add them in the future, but there are no exact plans as of this time.
Thanks Stan. Can the parameter settings be accessed in a Custom Criteria function?

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

User avatar
Stan Bokov
Posts: 963
Joined: 18 Dec 2009
Has thanked: 367 times
Been thanked: 302 times

Re: Optimizer Fitness - System Quality Number

Postby Stan Bokov » 01 Jul 2011

Dear Josh,

We will consider adding them in the future.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Optimizer Fitness - System Quality Number

Postby furytrader » 01 Jul 2011

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:

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;
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!

User avatar
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

Postby JoshM » 01 Jul 2011

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

Ulich05
Posts: 26
Joined: 02 Aug 2011
Has thanked: 2 times
Been thanked: 10 times

Re: Optimizer Fitness - System Quality Number

Postby Ulich05 » 25 Oct 2012

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:

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 ;
then in your strategy code (you dont need a separate indicator) you can do the following:

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;
}
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.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Optimizer Fitness - System Quality Number

Postby Henry MultiСharts » 31 Oct 2012

Hello Ulich05,
a function that allows you to compute a square root of any existing reserved word
You can use Math.sqrt reserved word for custom fitness function.
a function that allows you to calculate the std dev of any existing reserved word
You can calculate the value you need in PowerLanguage code and transfer it to custom fitness function using CustomFitnessValue

bwelthe
Posts: 3
Joined: 10 Feb 2014

Re: Optimizer Fitness - System Quality Number

Postby bwelthe » 22 Jul 2014

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?

khalaad
Posts: 323
Joined: 07 Jan 2007
Location: Lahore, Pakistan
Has thanked: 64 times
Been thanked: 57 times

Re: Optimizer Fitness - System Quality Number

Postby khalaad » 15 Sep 2017

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?
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:

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

khalaad
Posts: 323
Joined: 07 Jan 2007
Location: Lahore, Pakistan
Has thanked: 64 times
Been thanked: 57 times

Re: Optimizer Fitness - System Quality Number

Postby khalaad » 25 Sep 2017

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
Attachments
Optimise by R-Multiple.jpg
(253.09 KiB) Downloaded 2555 times


Return to “MultiCharts”