Hello. I'm using equity straightness metric that looks like this: profit *
correlation(linear regression(equity),equity)
My implementation is very slow and clumsy and may not even be completely
correct (it appears to be working well though). I'm hoping someone will be able
to suggest a way to speed up the script below or suggest a better method of
getting the same results. Thanks in advance.
cash = 10000;
bi = BarIndex();
dt = DateTime();
SetCustomBacktestProc("");
enBar = 0;
exBar = 0;
if( Status("action") == actionPortfolio )
{
firstBar = False;
UPI = 0;
bo = GetBacktesterObject();
bo.PreProcess();
for( bar = 0; bar < BarCount; bar++ )
{
EQ[bar] = bo.Equity-cash;
bo.ProcessTradeSignals( bar );
}
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
exBar = trade.ExitDateTime;
if (firstBar == False)
{
enBar = trade.EntryDateTime;
firstBar = True;
}
}
st = bo.GetPerformanceStats(0);
bo.PostProcess();
if (exBar != 0 AND enBar != 0)
{
enBar_ = LastValue(ValueWhen(dt == enBar,bi));
exBar_ = LastValue(ValueWhen(dt == exBar,bi));
al = LastValue(LinRegSlope( EQ, exBar_- enBar_ )) ;
bl = LastValue(LinRegIntercept( EQ, exBar_ - enBar_ ));
j=0;
for( bar = enBar_; bar <= exBar_; bar++ )
{
Lr[j] = al * ( j) + bl;
EQ2[j] = EQ[bar];
j++;
}
Cor = Correlation( EQ2, Lr , j-1);
MW = LastValue(Cor) * st.GetValue("NetProfitPercent");
}
else
{
newMeetric = -999999;
}
bo.AddCustomMetric( "EQstreighness", newMeetric );
}