Please, note that the code could also be adapted for other univariate filters like the Band-Pass, HP one sided, Kalman Filter, etc. Moreover, a further step could be to adapt it for several groups of variables.
Code: Select all
'**********
' HP Filter Rolling Window
'**********
close @objects
smpl @all
' ------------------------------------------------------------------
' Set your order here
string tohp = "x1" ' Set here the name of your variable
string inidate = "1995q1" ' Sample (and rolling window) start date
string windate = "2005q4" ' Rolling window end date
string enddate = "2019q1" ' Sample end date
!step = 1 ' Set the step size (1, 2, 3, 4, …)
!lambda = 1601 ' Set the lambda
' ---> Now you may run the code <---
' ------------------------------------------------------------------
' Do not modify this block from here on
smpl {inidate} {enddate}
!tspan = @obssmpl
smpl {inidate} {windate}
!win = @obssmpl
!length= !tspan - !win
smpl {inidate}-1 {windate}-1
delete hptt* hpcy*
group _g {tohp}
_g.line
for !i = 0 to @round(@abs(!length+!step-1)) step !step
for !v =1 to _g.@count
%y = _g.@seriesname(!v)
{%y}.hpf(lambda=!lambda) {%y}_hp @ {%y}_hpc
series hptt{!i} = {%y}_hp
series hpcy{!i} = {%y}_hpc
{%y}_hp.line
{%y}_hpc.line
delete {%y}_hp {%y}_hpc
next
smpl {inidate}+!i {windate}+!i
next
' ------------------------------------------------------------------
'Get your results here
group _g hptt* ' Trends
_g.line
group _gc hpcy* ' Cycles
_gc.line
' ------------------------------------------------------------------