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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
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 |
Share this
No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.
ProRealTime ITF files and other attachments :
Filename : download the ITF files
How to import ITF files into ProRealTime platform?
PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials