' set window size
!window = 500

' set step size
!step = 1

' get size of workfile
!length = @obsrange

' declare equation for estimation
'equation eq1

'calculate number of rolls
!nrolls = (!length-!window)/!step

'matrix to store coefficient estimates
vector(!nrolls) coefmat 

'series to store forecast estimates
series gfc1m

'redundant series for catching start and end points
series ser = 1   
'%start = @otod(@ifirst(ser))   
'%end = @otod(@ilast(ser))
   
'variable keeping track of how many rolls we've done
!j=0

' move sample !step obs at a time
for !i = 500  to  !length-!window+1-!step step !step
   !j=!j+1
      
   ' set sample for estimation period         
   %first = @otod(@dtoo(%start)+!i-1)
   %last = @otod(@dtoo(%start)+!i+!window-2)
   smpl {%first} {%last}      

   ' estimate GARCH (1,1) equation for log aud returns 
  equation eq1.arch audr c
coefmat(!1)=eq1. 'store
  
   ' store coefficients
   colplace(coefmat,eq1.@coefs,!j)
   
   ' 21-period-ahead forecast
  %21pers = @otod(@dtoo(%start)+!i+!window-1)      'start point
  %21pere = @otod(@dtoo(%start)+!i+!window+19)   'end point: %21pere - %21pers +1 = 21
  if {%end} < {%21pere} then   'check whether the forecast end point is greater than the workfile end point
     return
  endif
   
   ' set smpl for forecasting period
   smpl {%21pers} {%21pere}   
   
   ' forecast with command *forecast* (see also *fit*)
  eq1.forecast audgf [audr_se audr_var]      
   
   ' set sampl to obtain the 21 periods observation
  smpl {%21pere} {%21pere}   
   
   ' store forecasts
   gfcast = audgf

next

smpl @all
show coefmat
'show fcast.line

d(noerr) ser

