Nicolas

Hodrick-Prescott filter (HP)

Category: Indicators By: Nicolas Created: September 28, 2020, 1:21 PM
September 28, 2020, 1:21 PM
Indicators
12 Comments
Hodrick-Prescott filter (HP)

The Hodrick–Prescott filter (also known as Hodrick–Prescott decomposition) is a mathematical tool used in macroeconomics, especially in real business cycle theory, to remove the cyclical component of a time series from raw data. It is used to obtain a smoothed-curve representation of a time series, one that is more sensitive to long-term than to short-term fluctuations. (source Wikipedia: https://en.wikipedia.org/wiki/Hodrick%E2%80%93Prescott_filter)

Be aware that this indicator is continuously rechecking the complete history to redraw the curve to be perfectly in line with the data serie. So it is repainting the past and should not be used “as is” for live trading.

Compatible only with PRT v11 and onwards.

defparam drawonlastbaronly=true

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

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

if islastbarupdate and barindex>max(nobs,lambda) then 
 for i=0 to nobs-1
  $output[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
  H2=H1
  H1=$output[i]
 //drawpoint(barindex[i],$output[i],1) coloured(255,255,0)
 drawsegment(barindex[i],$output[i],barindex[i+1],$output[i+1]) coloured(255,255,0) style(line,3)
 next
endif

return

 

Download
Filename: PRC_Hodrick-Prescott-filter.itf
Downloads: 148
Nicolas
Nicolas Legend
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Denis
4 years ago
#

Bonjour Nicolas et RicLg, Merci Nicolas pour cet indicateur intéressant et merci RicLg pour la suggestion. Toutefois, même en insérant une instruction suggérée par RicLg, je n'arrive pas à faire renvoyer toutes les valeurs qui dessinent le segment, ce qui empêche qu'une alerte se déclenche. Auriez-vous une idée de ce qui bloque le renvoi de ces valeurs ? Merci par avance de votre aide

Italo Fabian
6 years ago
#

Nicolas, Ces matrices sont-elles des vecteurs unidimensionnels ou multidimensionnels? Depuis maintenant, Reconnaissant!

RicLg
6 years ago
#

Merci Nicolas pour le retour. En effet on récupère "the Last value" du dernier Point de traçage, après la création d'une variable, AllValuPoint=$output[i] entre les ligne 63 et 64 Est il possible de récupérer Toutes les valeurs du traçage de ce drawsegment? Merci Nicolas

Nicolas
6 years ago
#

c'est exactement ce que je fais entre les lignes 58 et 64 pour pouvoir tracer la ligne complète

RicLg
6 years ago
#

Hi Nicolas, Can we get the Value of this drawsegment? i do not understand how this code works Thank you .

Nicolas
6 years ago
#

The last value is $output[i], so return $output[i] Be aware that the indicator is repainting continuously.

Fabian
6 years ago
#

Hi Nicolas, do you plan to release this indicator in version 10.3 as well or is this not practicable due to the usage of arrays?

Nicolas
6 years ago
#

No because it needs arrays! :)

willi
6 years ago
#

Ich habe meinen Fehler gefunden: Das lambda war für meine eingestellte Zeiteinheit zu groß gewählt, mit einem kleineren Wert bekomme ich eine Anzeige. Danke Willi

willi
6 years ago
#

Guten Morgen, ich habe den Indikator importiert, bekomme aber leider nichts angezeigt. Liegt es vielleicht daran, das ich nur die Demoversion 11.1 benutze? Danke Willi

Nicolas
6 years ago
#

Nein, es sollte genauso funktionieren mit Demo oder Real Account. Haben Sie es auf die Preisliste angewendet?

juanj
6 years ago
#

Could be useful in identifying Elliott Waves

ProRealCode ProRealCode
Loading...