Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (VoroneTZ), 1,233 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Training towards DD or SR instead of PF #459124
04/23/16 14:34
04/23/16 14:34
Joined: Mar 2015
Posts: 336
Rogaland
N
nanotir Offline OP
Senior Member
nanotir  Offline OP
Senior Member
N

Joined: Mar 2015
Posts: 336
Rogaland
Hi

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

Re: Training towards DD or SR instead of PF [Re: nanotir] #459128
04/23/16 17:52
04/23/16 17:52
Joined: Dec 2013
Posts: 568
Fuerth, DE
Sphin Offline
User
Sphin  Offline
User

Joined: Dec 2013
Posts: 568
Fuerth, DE
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.

Re: Training towards DD or SR instead of PF [Re: Sphin] #459129
04/23/16 23:47
04/23/16 23:47
Joined: Mar 2015
Posts: 336
Rogaland
N
nanotir Offline OP
Senior Member
nanotir  Offline OP
Senior Member
N

Joined: Mar 2015
Posts: 336
Rogaland
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

Re: Training towards DD or SR instead of PF [Re: nanotir] #459137
04/24/16 16:51
04/24/16 16:51
Joined: Nov 2013
Posts: 123
Mithrandir77 Offline
Member
Mithrandir77  Offline
Member

Joined: Nov 2013
Posts: 123
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.

Re: Training towards DD or SR instead of PF [Re: nanotir] #459142
04/24/16 21:43
04/24/16 21:43
Joined: Nov 2013
Posts: 123
Mithrandir77 Offline
Member
Mithrandir77  Offline
Member

Joined: Nov 2013
Posts: 123
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

Last edited by Mithrandir77; 04/24/16 21:43.
Re: Training towards DD or SR instead of PF [Re: Mithrandir77] #459143
04/24/16 22:11
04/24/16 22:11
Joined: Mar 2015
Posts: 336
Rogaland
N
nanotir Offline OP
Senior Member
nanotir  Offline OP
Senior Member
N

Joined: Mar 2015
Posts: 336
Rogaland
Thanks a lot for the step by step. It helps me to understand the process and other low skilled users as well.

Re: Training towards DD or SR instead of PF [Re: nanotir] #459144
04/24/16 23:43
04/24/16 23:43
Joined: Nov 2013
Posts: 123
Mithrandir77 Offline
Member
Mithrandir77  Offline
Member

Joined: Nov 2013
Posts: 123
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.

Last edited by Mithrandir77; 04/25/16 02:54. Reason: not sharpe_ratio / perf->vDrawDown == 0 but perf->vDrawDown == 0
Re: Training towards DD or SR instead of PF [Re: Mithrandir77] #459151
04/25/16 05:00
04/25/16 05:00
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline
Senior Member
boatman  Offline
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
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.

Re: Training towards DD or SR instead of PF [Re: boatman] #459161
04/25/16 13:05
04/25/16 13:05
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
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.

Re: Training towards DD or SR instead of PF [Re: jcl] #459164
04/25/16 13:51
04/25/16 13:51
Joined: Nov 2013
Posts: 123
Mithrandir77 Offline
Member
Mithrandir77  Offline
Member

Joined: Nov 2013
Posts: 123
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...

Page 1 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1