[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Plotting historical portfolio backtest metrics in INDICATOR



PureBytes Links

Trading Reference Links

TJ,

the answer is yes, yes and yes. The strange thing is that yesterday only 
DD% and Max DD% were displayed, but today - with the same IS and OOS 
data and code unchanged - all metrics were displayed. A caching 
problem?

The same problem occurred when I performed a WF test for the DAX from 
01/01/1996 (IS perod 1 year, step 3 months). A applied the code added 
below - all metrics were displayed with the exception of CECPP which 
showed an {EMPTY} value.

In the next step I changed the WF start period to 01/01/1990 (IS again 1 
year, step 3 months) - and this time CECPP value was diplayed but I got 
{EMPTY}values for CAR(%), ME(%) and WFE(%) instead! 

I also applied your code and got {EMPTY}values for Profit%, CAR and 
CAR/MDD.

I'm completely confused ...

Best regards,

Thomas 

Here's my code:

PlotForeign("~~~ISEQUITY","In-Sample Equity", colorRed, styleLine); 
PlotForeign("~~~OSEQUITY","Out-Of-Sample Equity", colorGreen, 
styleLine); 
RelPerfI=Foreign("~~~ISEQUITY","c")/C;
RelPerfO=Foreign("~~~OSEQUITY","c")/C;
if(ParamToggle("Show Rel.Perf Equity", "No|Yes",0)) 
Plot(RelPerfI, "Rel.Perf. InSample", colorRed,styleDots|styleThick|
styleOwnScale) AND Plot(RelPerfO, "Rel.Perf. OutSample", 
colorGreen,styleDots|styleThick|styleOwnScale);

iseq=Foreign("~~~ISEQUITY", "C");
oseq=Foreign("~~~OSEQUITY", "C");
//x=LastValue(Cum(1));
x = LastValue(BarIndex());


function FirstBarIndex(Condition)
{
     TotalBarsIndex = LastValue(BarIndex());
     a = 0;
     Counter = 0;
     for (a = 0 ;a < TotalBarsIndex; a++)
     {
       Counter = Counter+1;
       if (
           IsTrue(Condition[a])
          )
       a = TotalBarsIndex;
       }
     result = Counter-1;

     return result;
}

isfirst=FirstBarIndex(iseq);
osfirst=FirstBarIndex(oseq);

isTotalBars=x-isfirst;
osTotalBars=x-Osfirst;

isSlope = LinRegSlope(iseq,istotalbars);
osSlope = LinRegSlope(oseq,ostotalbars);

lastbar = BarIndex();
//lastbar = bar; //LastValue( ValueWhen( islastbar, bar ) );


//isfirstbar = LastValue( ValueWhen( isfirst, bar ) );
al = LastValue( ValueWhen( lastbar, LinRegSlope( iseq, Lastbar - isfirst 
+ 1 ) ) );
bl = LastValue( ValueWhen( lastbar, LinRegIntercept( iseq, Lastbar - 
isfirst + 1 ) ) );
isLr = al * ( BarIndex() - isfirst ) + bl;
//isLr = IIf( lastbar >= isfirst AND lastbar <= lastbar , isLr, Null );

//firstbar = LastValue( ValueWhen( isfirst, bar ) );

a2 = LastValue( ValueWhen( lastbar, LinRegSlope( oseq, Lastbar - osfirst 
+ 1 ) ) );
b2 = LastValue( ValueWhen( lastbar, LinRegIntercept( oseq, Lastbar - 
osfirst + 1 ) ) );
osLr = a2 * ( BarIndex() - osfirst ) + b2;
//osLr = IIf( lastbar >= osfirst AND lastbar <= lastbar , osLr, Null );



if( ParamToggle("Show lin. reg.", "No|Yes", 0 ) )
Plot( isLr , "Linear Reg", ParamColor( "ISLinReg Color",colorCycle), 
styleThick ) 
AND Plot( osLr , "Linear Reg", ParamColor( "OOSLinReg Color", 
colorCycle), styleThick );

//K-Ratio
iskratio=10000*isSlope/(StdErr(iseq,istotalbars)*istotalbars);
oskratio=10000*osSlope/(StdErr(oseq,ostotalbars)*ostotalbars);

//CAR
isCAR=100*((LastValue(iseq)/iseq[0])^(252/isTotalBars) -1);
osCAR=100*((LastValue(oseq)/Oseq[0])^(252/osTotalBars) -1);

//Drawdown
isdr=100*(LastValue(iseq)/Highest(iseq)-1);
osdr=100*(LastValue(oseq)/Highest(oseq)-1);
ismaxdr=Lowest(isdr);
osmaxdr=Lowest(Osdr);


//Walk-Forward Efficiency
WFE=100*OsCAR/isCAR;

//Perfect Profit
//PP=Cum(abs(C-Ref(C,-1)));

if(ParamToggle("Long AND SHORT?", "No|Yes",0))
//PP=Cum(IIf(C-Ref(C,-1)>0,C-Ref(C,-1),0));
//else
//PP=Cum(abs(C-Ref(C,-1)));
PP=Cum(Max(abs(H-Ref(L,-1)),abs(L-Ref(H,-1))));
else
PP=Cum(IIf(H-Ref(L,-1)>0,H-Ref(L,-1),0));
InitialEquity = GetOption("InitialEquity");
PE=PP+InitialEquity;
if( ParamToggle("Show Perfect Profit", "No|Yes", 0 ) )
Plot(PE,"Perfect Profit",colorBlue);

isCECPP=Correlation(iseq,PP,isTotalBars);
osCECPP=Correlation(oseq,PP,osTotalBars);

//Model Efficiency
isProfit=LastValue(iseq)-iseq[0];
OsProfit=LastValue(Oseq)-oseq[0];
isME=100*isProfit/PP;
OsME=100*OsProfit/PP;


Title = "{{NAME}} - {{INTERVAL}} {{DATE}} {{VALUES}}"
+"\n IS      Slope:   " + WriteVal(isSlope,1.1)+"   
K-Ratio: "+WriteVal(iskratio)+"   CAR(%):  "+WriteVal(isCAR,1.1)
+"   Max DD(%):  "+WriteVal(ismaxdr,1.1)+"   
ME(%):  "+WriteVal(isME,1.1)+"   
CECPP:  "+WriteVal(isCECPP) //+"Days"+WriteVal(istotalbars)
+"\n OOS Slope:   " + WriteVal(OsSlope,1.1)+"   
K-Ratio: "+WriteVal(oskratio)+"   CAR(%):  "+WriteVal(osCAR,1.1)
+"   Max DD(%):  "+WriteVal(osmaxdr,1.1)+"   
ME(%):  "+WriteVal(osME,1.1)+"   
CECPP:  "+WriteVal(osCECPP) //+"Days"+WriteVal(ostotalbars);
+"\n WFE(%):   "+WriteVal(WFE,1.1);
//+"\n PP:  "+WriteVal(PP)
//+"\n isProfit  "+WriteVal(isprofit)
//+"\n osProfit  "+WriteVal(osprofit);

> Did you run WF test? Do you have ~~~OSEQUITY ticker ? The code will
> work only AFTER WF. Did you change selected symbol to ~~~OSEQUITY?
> It won't work without that.
> If you want to use it on regular equity, you need to change the first
> line of the code to Symbol = "~~~EQUITY";
>
> Best regards,
> Tomasz Janeczko
> amibroker.com
> ----- Original Message -----
> From: "Thomas Ludwig" <Thomas.Ludwig@xxxxxx>
> To: <amibroker@xxxxxxxxxxxxxxx>
> Sent: Monday, May 19, 2008 7:40 PM
> Subject: Re: [amibroker] Plotting historical portfolio backtest
> metrics in INDICATOR
>
> > TJ,
> >
> > I don't get any values for Profit, CAR and CAR/MDD. Any idea why?
> >
> > Best regards,
> >
> > Thomas
> >
> >> Hello,
> >>
> >> As far as concatenated OOS is considered, things depend on which
> >> metrics do you really want.
> >> That is so because some metrics accumulate nicely so
> >> you can simply add them up (either using AddToComposite or
> >> just adding multiple Foreign() calls).
> >>
> >> Some metrics are derived directly from equity (Drawdown, sharpe
> >> ratio, UlcerIndex) and can be calculated directly using plain AFL
> >> formula working on IS or OOS equity.
> >>
> >> For example CAR/MDD for out-of-sample test OOS:
> >>
> >> Symbol = "~~~OSEQUITY"; // change the ticker to your preference
> >>
> >> eq = Foreign( Symbol, "C" );
> >>
> >> if( Name() != Symbol ) Title = "You should change symbol to " +
> >> Symbol;
> >>
> >> function TotalDays()
> >> {
> >>  yy = Year();
> >>  dy = DayOfYear();
> >>  LastLeapYear = (yy % 4) == 1 && yy != 2001;
> >>  YearChg = yy != Ref(yy, -1);
> >>  YearChg = IIf(IsNull(YearChg), False, YearChg);
> >>  YearLen = IIf(YearChg, IIf(LastLeapYear, 366, 365), 0);
> >>  return Cum(YearLen) + dy - dy[0];
> >> }
> >>
> >> dr = 100 * ( eq/Highest(eq) - 1);
> >> profit = 100 * ( eq/eq[0] - 1 );
> >>
> >> td = TotalDays();
> >> Days = td[ BarCount - 1 ] - td[ 0 ];
> >>
> >> Car = 100 * ( ( eq / eq[ 0 ] ) ^ ( 365 / Days ) - 1 );
> >>
> >> //Plot( dr, "DD%", colorRed );
> >> //Plot( Lowest(dr), "Max DD%", colorBlue );
> >> //Plot( profit, "Profit %", colorGreen );
> >> //Plot( Car, "CAR", colorDarkGreen );
> >>
> >> Plot( Car/Highest( -dr ), "CAR/MDD", colorOrange );
> >>
> >> Best regards,
> >> Tomasz Janeczko
> >> amibroker.com
> >> ----- Original Message -----
> >> From: "Thomas Ludwig" <Thomas.Ludwig@xxxxxx>
> >> To: <amibroker@xxxxxxxxxxxxxxx>
> >> Sent: Monday, May 19, 2008 12:02 PM
> >> Subject: Re: [amibroker] Plotting historical portfolio backtest
> >> metrics in INDICATOR
> >>
> >> > TJ,
> >> >
> >> > thanks a lot - very helpful!
> >> >
> >> > However, I'd be happy if you could also present a solution how
> >> > do do something similar for the concatenated IS and OOS equity
> >> > curves in a WF test - see my posts #123921 and 123945. I don't
> >> > think this can be done with the custom backtester.
> >> >
> >> > Best regards,
> >> >
> >> > Thomas
> >> >
> >> >> Hello,
> >> >>
> >> >> To show you how easy it is to actually PLOT ANY portfolio
> >> >> backtest metric as a historical series in INDICATOR, without
> >> >> using script, OLE - just PURE AFL I prepared this:
> >> >>
> >> >> http://www.amibroker.com/kb/2008/05/19/historical-portfolio-bac
> >> >>kte st- metrics/
> >> >>
> >> >> This addresses the following goals:
> >> >> a) having ANY portfolio backtest metric available as historical
> >> >> series for use ANYWHERE (in indicator or wherever) b) plotting
> >> >> backtest metrics in INDICATORS and doing this FAST (without
> >> >> constant recalculation) c) using only pure AFL for that
> >> >>
> >> >> Best regards,
> >> >> Tomasz Janeczko
> >> >> amibroker.com
> >> >>
> >> >> ------------------------------------
> >> >>
> >> >> Please note that this group is for discussion between users
> >> >> only.
> >> >>
> >> >> To get support from AmiBroker please send an e-mail directly to
> >> >> SUPPORT {at} amibroker.com
> >> >>
> >> >> For NEW RELEASE ANNOUNCEMENTS and other news always check
> >> >> DEVLOG: http://www.amibroker.com/devlog/
> >> >>
> >> >> For other support material please check also:
> >> >> http://www.amibroker.com/support.html
> >> >> Yahoo! Groups Links
> >> >
> >> > ------------------------------------
> >> >
> >> > Please note that this group is for discussion between users
> >> > only.
> >> >
> >> > To get support from AmiBroker please send an e-mail directly to
> >> > SUPPORT {at} amibroker.com
> >> >
> >> > For NEW RELEASE ANNOUNCEMENTS and other news always check
> >> > DEVLOG: http://www.amibroker.com/devlog/
> >> >
> >> > For other support material please check also:
> >> > http://www.amibroker.com/support.html
> >> > Yahoo! Groups Links
> >
> > ------------------------------------
> >
> > Please note that this group is for discussion between users only.
> >
> > To get support from AmiBroker please send an e-mail directly to
> > SUPPORT {at} amibroker.com
> >
> > For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> > http://www.amibroker.com/devlog/
> >
> > For other support material please check also:
> > http://www.amibroker.com/support.html
> > Yahoo! Groups Links
>
> ------------------------------------
>
> Please note that this group is for discussion between users only.
>
> To get support from AmiBroker please send an e-mail directly to
> SUPPORT {at} amibroker.com
>
> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> http://www.amibroker.com/devlog/
>
> For other support material please check also:
> http://www.amibroker.com/support.html
> Yahoo! Groups Links
>
>
>


------------------------------------

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/