' set window size
!window =800

' get size of workfile
!length = @obsrange

' declare equation for estimation
equation eq01
%cmnd = _this.@command 'EDIT: get the equation specification

' set step size
!step = 1

'calculate number of rolls
!nrolls = @floor((!length-!window)/!step) 

'matrix to store coefficient estimates
matrix(4,!nrolls) coefmat 'where the number of coefficients is 4. 
series fcast  'series to store forecast estimates
series fcastse '
series fcastvar

%start = "@first" 
%end = "@last"



'variable keeping track of how many rolls we've done
!j = 0

' move sample !step obs at a time
for !i = 1 to !length-!window+1-!step step !step
   !j = !j +1
   %first = @otod(@dtoo(%start)+!i-1)
   %last = @otod(@dtoo(%start)+!i+!window-2)
   smpl {%first} {%last}

   'estimate equation - Error catching , any errors mean we'll keep going.

   !maxerr = @maxerrcount
   setmaxerrs !maxerr+1   
   _this.{%cmnd}   'estimate equation
   if @lasterrnum>0 then
      clearerrs
   else
      colplace(coefmat,eq01.@coefs,!j) 'store coefficients 
 
  ' 5-period-ahead forecast
      %pers = @otod(@dtoo(%start)+!i+!window-1)   'start point
      %pere = @otod(@dtoo(%start)+!i+!window+4)   'end point 

       if {%end} < {%5pere} then   'check whether the forecast end point is greater than the workfile end point
          return
       endif
       
       ' set smpl for forecasting period
       smpl {%5pers} {%5pere}   
      
      eq01.fit(f=na) r_f1 r_se r_var     
         
      ' store forecasts vars
      fcast = r_f1
      fcastse =  r_se
      fcastvar = r_var 
   endif
next

smpl @all

