Training towards DD or SR instead of PF

Posted By: nanotir

Training towards DD or SR instead of PF - 04/23/16 14:34

Hi

Someone knows how to change the training optimization towards a low DD or high SR instead of a stable PF?
Posted By: Sphin

Re: Training towards DD or SR instead of PF - 04/23/16 17:52

I think

http://www.zorro-trader.com/manual/en/optimize.htm

Quote:
The performance is calculated in the objective function in include\default.c. This function can be replaced by any user provided function in the script that is named objective. This way, other performance values can be used for optimizing parameters, for instance the profit/drawdown ratio, or the gross profit, or the Sharpe ratio.
Posted By: nanotir

Re: Training towards DD or SR instead of PF - 04/23/16 23:47

Problem is I am not sure how to adapt it towards high SR or low DD tongue.... Or at least, if someone knows already, then I do not need to burn my brain on it tongue
Posted By: Mithrandir77

Re: Training towards DD or SR instead of PF - 04/24/16 16:51

Here jcl described how to override the default objective function that is declared in include\default.h -by the way, there it is commented that it optimizes based on the Pessimistic Return Ratio not the Profit Factor-

Click to reveal..

Code:
// optimizing objective based on PRR
var objective()
{
	if(!NumWinTotal && !NumLossTotal) return 0.;
	var wFac = 1./sqrt(1.+NumWinTotal); 
	var lFac = 1./sqrt(1.+NumLossTotal);
	var win = WinTotal, loss = LossTotal;
// remove single outliers
	if(NumWinTotal > 2) win -= (NumWinTotal-2)*WinMaxTotal/NumWinTotal;
	if(NumLossTotal > 2) loss -= (NumLossTotal-2)*LossMaxTotal/NumLossTotal;
// return PRR
	return (1.-wFac)/(1.+lFac)*(1.+win)/(1.+loss);
}




Concerning optimizing for SR, I think you can use the data in the PERFORMANCE struct inside the GLOBALS struct, both are defined in include\trading.h, inside the GLOBALS struct there is a PERFORMANCE * w variable

Also as you can see in include\default.c there is this line:

Code:
extern GLOBALS* g; // global variables struct



which means that whatever script that is including default.c will have a variable g to access the global variables. Since Sharpe Ratio in Zorro is simply Mean Profit / Std of Profits I think you can do something like

Code:
var optimize(){
    return g->w->vMean / g->w->vStd;
}



I hope this helps, I explained it a bit step by step to help others that are reading this thread and are not introduced to coding.
Posted By: Mithrandir77

Re: Training towards DD or SR instead of PF - 04/24/16 21:43

Correction, although at http://www.zorro-trader.com/manual/en/functions.htm it is defined as objective()

It seems that the correct way to override the objective function in my later example according to the help file of Zorro 1.42 would be

Code:
var objective(PERFORMANCE * perf){
    return perf->vMean / perf->vStd;
}



even though the former should also work
Posted By: nanotir

Re: Training towards DD or SR instead of PF - 04/24/16 22:11

Thanks a lot for the step by step. It helps me to understand the process and other low skilled users as well.
Posted By: Mithrandir77

Re: Training towards DD or SR instead of PF - 04/24/16 23:43

You are welcome! I myself got enthusiastic with your idea, I have been thinking about optimizing more than one trade statistic. First one clarification about optimizing, generally if you want to minimize/maximize a value you can just return it or the negative of it, ie if you want to minimize the ulcer index alone that would be
Code:
return -1 * perf->vUlcer;


since you want it to be as small as possible. But since Zorro does not search for the max but for the 'broader peak' in this case if there are negative values in the result I don't know if it is going to pick the 'broader valley', so it is better to return the reciprocal,
Code:
if (perf->vUlcer == 0)
    return 9999999999;// just return the max float or var value
else
    return 1 / perf->vUlcer;


If you want to optimize more than one and the one you want to minimize is always equal or greater than 0 -you don't want (-1)*(-something) become positive laugh - you can still substract it, like:
Code:
//optimize sharpe ratio and ulcer index
var sharpe_ratio = perf->vMean / perf->vStd;
return sharpe_ratio - perf->vUlcer;


but again I doubt Zorro will find the broader peak, for instance if you have this Sharpe Ratios and Drawdowns (invented)
Code:
SR	DD
0.7	7000
0.5	1500
0.7	1000
1.2	2000
1.5	3500
1	500
2	1000
3.5	7000
2.5	689
3	700
2.9	110
-0.8	2500


Then all values in the performance charts will be negative, on the other hand if objective is:
Code:
//optimize sharpe ratio and drawdown
var sharpe_ratio = perf->vMean / perf->vStd;
if (perf->vDrawDown == 0)
    return 9999999999;
else
    return sharpe_ratio / perf->vDrawDown;


Then most of the values will be positive and Zorro will search for the broader peak as it is documented.

PS: I remember a post about modifying the optimize/objective function so that it returned the highest peak instead of the broader one, I have been searching the forum to no avail maybe someone remembers it.
Posted By: boatman

Re: Training towards DD or SR instead of PF - 04/25/16 05:00

Mithrandir has covered the nuts and bolts of how to optimize on different parameters, but I thought I'd share some thoughts as well:

I don't think I'd optimize on low drawdown specifically, since the value of the worst drawdown is essentially random.

Consider two systems that each take 40 trades and produce the exact same profit and loss figures overall, but the first system does so while incurring 20 consecutive losers followed by 20 consecutive winners. The second system has no more than 3 losers in a row.

Same overall finishing equity, different max drawdown. And since the outcome of any particular trade within a trading system of a certain expectancy is random, even an otherwise brilliant system could end up with a string of losses that result in a large max drawdown purely by chance alone.

If you wanted to optimize on max drawdown, I would recommend optimizing on percentage of winning trades, which would be a proxy with some relationship to the system's maximum drawdown without optimizing on random events.
Posted By: jcl

Re: Training towards DD or SR instead of PF - 04/25/16 13:05

When you define an objective function this way:

var objective(PERFORMANCE * perf)

it does NOT override the default objective since C makes a difference between functions of the same name, but different parameter types. It's better this way:

var objective()
{
PERFORMANCE* perf = &g->w;
...
}

Passing PERFORMANCE as a parameter is not really needed since it's also stored in the g struct.
Posted By: Mithrandir77

Re: Training towards DD or SR instead of PF - 04/25/16 13:51

Originally Posted By: jcl
When you define an objective function this way:

var objective(PERFORMANCE * perf)

it does NOT override the default objective since C makes a difference between functions of the same name, but different parameter types. It's better this way:

var objective()
{
PERFORMANCE* perf = &g->w;
...
}

Passing PERFORMANCE as a parameter is not really needed since it's also stored in the g struct.


Ups yes, I have the word carried from the previous explanation. Jcl, so what does Zorro do when you declare the user defined function

var objective(PERFORMANCE * perf)

?

Does Zorro use it and not the one in default.c? I ask you because in User Defined Functions in the help file of Zorro 1.42 it is defined as var objective(PERFORMANCE * perf)

Concerning what boatman said, I agree and I add that optimizing drawdown with sharpe ratio would be kinda redundant since the drawdowns would be correlated to the std of the returns I think. I have thougth of this super optimization metric,

return sharpe_ratio * (g->w->numWin + g->w->numLoss) / g->w->vUlcer

To optimize sharpe ratio and minimize ulcer and also penalize low trades, I am not sure if g->w->numWin is the number of CLOSED won trades or there can be open trades counted in that number, same with g->w->numLoss...
Posted By: jcl

Re: Training towards DD or SR instead of PF - 04/25/16 14:15

I had just recently that case. Zorro does not call your function in that case, but still the default function since it was declared first and not overridden by the other one. For this reason I've changed also the description in the manual.

For accessing PERFORMANCE elements directly in the g struct, it's g->w.numWin, not g->w->numWin. w is the content, not a pointer.
Posted By: Mithrandir77

Re: Training towards DD or SR instead of PF - 04/25/16 16:34

Many thanks Jcl for the clarification, and what does numwin represent? The number of open + closed winning trades or just the closed ones? In the comment in trading.h it says just 'number of winning trades'
Posted By: jcl

Re: Training towards DD or SR instead of PF - 04/25/16 16:54

The closed ones. You can find the description of most parameters in the manual under "Trade statistics".
Posted By: Mithrandir77

Re: Training towards DD or SR instead of PF - 04/25/16 17:31

Originally Posted By: jcl
The closed ones. You can find the description of most parameters in the manual under "Trade statistics".


Thank you again! I wasn't sure that variables in the PERFORMANCE struct mapped directly to the one described in the manual in Trade statistics.
© 2024 lite-C Forums