1 registered members (Quad),
454
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Linear Regression in objective function
#469447
11/16/17 16:33
11/16/17 16:33
|
Joined: Aug 2017
Posts: 102 Spain
Brax
OP
Member
|
OP
Member
Joined: Aug 2017
Posts: 102
Spain
|
Hi, I am currently doing some research on optimization criteria and i´ve got stuck trying to apply LinearRegSlope*R2 to optimize parameters. This code gives me an error regarding i need more lookback period:
var objective()
{
return LinearRegSlope(ResultsDaily,Bar-StartBar)*R2;
}
I´ve also tried with Day instead of Bar-StarBar, and even putting directly a number lower than lookback for the period gets a crush. ¿How could i achieve this? The alternative is using (WinTotal-LossTotal)*R2, but this is not what i really want.
|
|
|
Re: Linear Regression in objective function
[Re: Brax]
#469558
11/21/17 13:13
11/21/17 13:13
|
Joined: Jul 2000
Posts: 27,982 Frankfurt
jcl
Chief Engineer
|
Chief Engineer
Joined: Jul 2000
Posts: 27,982
Frankfurt
|
Ok. I cannot tell why "it crushes" because I have not yet used an indicator on something else than a data series. But here is how to look into such a problem.
First, check if ResultsDaily is really nonzero in the objective function. I believe it is, but otherwise the problem is clear.
Next, call LinearRegSlope with something simple, like an array { 1, 2, 3 } of length 3, and check if it still "crushes". If so, then LinearRegSlope only works with a real data series for some reason, and you need the more common polyFit function. When you're at this point, post again and I'll help.
|
|
|
Re: Linear Regression in objective function
[Re: jcl]
#469563
11/21/17 19:32
11/21/17 19:32
|
Joined: Aug 2017
Posts: 102 Spain
Brax
OP
Member
|
OP
Member
Joined: Aug 2017
Posts: 102
Spain
|
Hi jcl.
ResultsDaily is nonzero and it´s reversed as you stated.
LinearRegSlope(ResultsDaily,Day) with LookBack = 0 doesn't seem to work properly. It always returns 1, maybe there is an issue with the reversed order of the series.
polyfit(0,ResultsDaily,Day,1,1) letting LookBack with its original value produces different results in training, but in test mode the system results aren't profitable when other criteria do.
I think i need to reverse the series or apply the slope over the differencies, need some help.
Thanks.
Last edited by brax; 11/21/17 19:38.
|
|
|
Re: Linear Regression in objective function
[Re: jcl]
#469722
12/04/17 20:34
12/04/17 20:34
|
Joined: Aug 2017
Posts: 102 Spain
Brax
OP
Member
|
OP
Member
Joined: Aug 2017
Posts: 102
Spain
|
Hi. Sorry for not writing anything last days, i´ve been kind of busy lately... Coming back to the point discussed here, i must say i haven´t been able to make LinearRegSlope work properly in objective function. Something strange happen with this function as far as i´ve tested. I am using polyfit instead, but i think something is not working or maybe i am not getting the results i was expecting. Let´s take this piece of code:
static int criteria = 6;
static int minTrade = 0;
var objective()
{
if(NumWinTotal+NumLossTotal <= minTrade) {
return 0;
}
else {
switch(criteria){
case 0: //PRR
var wFactor = 1./sqrt(1.+NumWinTotal);
var lFactor = 1./sqrt(1.+NumLossTotal);
var win = WinTotal, loss = LossTotal;
if(NumWinTotal > 2) win -= (NumWinTotal-2)*WinMaxTotal/NumWinTotal;
if(NumLossTotal > 2) loss -= (NumLossTotal-2)*LossMaxTotal/NumLossTotal;
return (1.-wFactor)/(1.+lFactor)*(1.+win)/(1.+loss);
case 1: //PF
return WinTotal/max(1.,LossTotal);
case 2: //WinRate
return (var) NumWinTotal/max(1.,NumWinTotal+NumLossTotal);
case 3: //CAR
return WinTotal-LossTotal;
case 4: //Calmar
return (WinTotal-LossTotal)/max(1.,DrawDownMax);
case 5: //Sharpe
return ReturnMean/ReturnStdDev;
case 6: //K-Ratio
var coeff[2];
LookBack = Bar-StartBar;
polyfit(coeff,ResultsDaily,Bar-StartBar,1,1);
return coeff[1]*R2;
// LookBack = 0;
// return LinearRegSlope(ResultsDaily, Day)*R2;
}
}
}
function run()
{
set(LOGFILE+PARAMETERS);
BarPeriod = 4*60;
LookBack = 500;
StartDate = 2005;
EndDate = 2015;
asset("EUR/USD");
vars Price = series(price());
vars Filtered = series(BandPass(Price,optimize(30,20,40),0.5));
vars Signal = series(FisherN(Filtered,500));
var Threshold = optimize(1,0.5,1.5,0.1);
Stop = optimize(4,2,10) * ATR(100);
Trail = 4*ATR(100);
if(crossUnder(Signal,-Threshold))
enterLong();
else if(crossOver(Signal,Threshold))
enterShort();
plot("Filtered",Filtered,NEW,BLUE);
plot("Signal",Signal,NEW,RED);
plot("Threshold1",1,0,BLACK);
plot("Threshold2",-1,0,BLACK);
PlotWidth = 1024;
PlotHeight1 = 400;
}
Optimizing this system with almost any criteria produces an annual return ranging from 80% to 100%. However with K-Ratio it barely achieves an 60%. Not sure if K-Ratio isn´t as good as i thought or if i´ve implemented it wrongly (most probably). The manual says something about polyfit having a cap of 1000 in TimePeriod parameter, so maybe here is the issue. I am also setting lookback to the whole number of bars in the simulation as this seems to work better. This is everything i´ve figured out. Any suggestion or advice will be welcomed.
|
|
|
|