Hodrick-Prescott indicator with standard error bands

Category: Indicators By: MCG Created: March 26, 2024, 9:22 AM
March 26, 2024, 9:22 AM
Indicators
0 Comments

This is an extension of Nicolas’s great indicator that implements the Hodrick-Prescott smoother. My contribution just consists of calculating the standard error bands (with respect to the close) around the HP line.

defparam drawonlastbaronly=true

//PRC_Hodrick-Prescott filter | indicator
//28.09.2020
//Nicolas @ www.prorealcode.com
//Sharing ProRealTime knowledge

// Added standard error
// 25.03.2024
// Manuel Cabedo @ www.prorealcode.com
// Sharing ProRealTime knowledge

// --- settings
nobs   =100 //Number of bars to smooth
lambda =30 //Higher lambda leads to the smoother data
// --- end of settings

if islastbarupdate and barindex > max(nobs,lambda) THEN

// IsLastBarUpdate  duvuelve un 1 si estamos tratando la última barra PERO no nos dice su numero.
// (eso lo hace la instrucción BarIndex)

for i=0 to nobs-1
$output[i]=Close[i]
$Cierre[i]=Close[i]
next

$a[0]=1.0+lambda
$b[0]=-2.0*lambda
$c[0]=lambda
for i=1 to nobs-3 do
$a[i]=6.0*lambda+1.0
$b[i]=-4.0*lambda
$c[i]=lambda
next
 
$a[1]=5.0*lambda+1
$a[nobs-1]=1.0+lambda
$a[nobs-2]=5.0*lambda+1.0
$b[nobs-2]=-2.0*lambda
$b[nobs-1]=0.0
$c[nobs-2]=0.0
$c[nobs-1]=0.0

//Forward
for i=0 to nobs-1 do
Z=$a[i]-H4*H1-HH5*HH2
HB=$b[i]
HH1=H1
H1=(HB-H4*H2)/Z
$b[i]=H1
HC=$c[i]
HH2=H2
H2=HC/Z
$c[i]=H2
$a[i]=($output[i]-HH3*HH5-H3*H4)/Z
HH3=H3
H3=$a[i]
H4=HB-H5*HH1
HH5=H5
H5=HC
next

//Backward
H2=0
H1=$a[nobs-1]
$output[nobs-1]=H1
for i = nobs-2 downto 0 do
$output[i]=$a[i]-$b[i]*H1-$c[i]*H2  // $output es el valor del indicador
H2=H1
H1=$output[i]
drawsegment(barindex[i],$output[i],barindex[i+1],$output[i+1]) coloured(0,0,0) style(line,3)
next

// Calculate StdErr of values of the Hodrick-Prescott value.
Sum = 0.0
Ubound = LastSet($output)
for k = Ubound downto 0
Sum = Sum + square($output[k] - $Cierre[k])
next
StdErr = sqrt(Sum/(Ubound-1))

// Draw the bands around the Hodrick-Prescott value
for i=nobs-1 downto 0 do
drawsegment(barindex[i],$output[i] + 2 * StdErr, barindex[i+1], $output[i+1] + 2 * StdErr) coloured(128, 128, 128) style(dottedline, 3)
drawsegment(barindex[i],$output[i] - 2 * StdErr, barindex[i+1], $output[i+1] - 2 * StdErr) coloured(128, 128, 128) style(dottedline, 3)
NEXT

endif

Return

Download
Filename: Hodrick-Prescott-SE-Bands.itf
Downloads: 128
MCG New
This author is like an anonymous function, present but not directly identifiable. More details on this code architect as soon as they exit 'incognito' mode.
Author’s Profile

Comments

Logo Logo
Loading...