'This program spins through the TS trend series contained in forecasting_series::TS_Seriesin:
'	1. The number of series and the specific series to be treated as constant series (random walks around a mean) need to be adjusted
'	 according to the series being forecast and the outcomes of the Tramoseats long term trend decomposition.
'	2. Change the autoarma specification, for both the with and without constant versions for specific results:
'		a. Most general: ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=4, maxsar=1, maxsma=1, forclen=20, etable) ser{!i}_F +/- c
'		b. AR only: ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=0, maxsar=1, maxsma=0, forclen=20, etable) ser{!i}_F +/- c
'		c. MA only: ser{!i}.autoarma(tform=auto, diff=2, maxar=0, maxma=4, maxsar=0, maxsma=1, forclen=20, etable) ser{!i}_F +/- c
'	3. Forecast length ( forclen=..) in the spec may need to be adjusted according to the circumstances.


'make forecasts for each each constant series, most are 'flat zero' trend equal 0.1 throughout
subroutine flatZERO
	series ser{!i}_F
' to the last observation if producing in sample forecasts, to the end point of the forecast period if out of sample forecasts
	For !j=1 to 89
	ser{!i}_F(!j,1)=@mean(ser{!i})
	next
	ARIMA1.add ser{!i}_F
endsub 

mode quiet
logmode logmsg
cd "G:\OSAS\12 Projections\2017 Projections - five years to November 2021\02 ANZSIC\02 Creating Forecasts\Eviews"

wfopen forecasting_series
wfselect forecasting_series::TS_Seriesin

'for in sample forecasts use
pagestruct(end=8/01/2016)
'for out of sample forecasts use
'pagestruct(end=11/01/2021)


group ARIMA1
'group ARIMA for real forecasts

for !i= 1 to 441
	wfselect forecasting_series::TS_Seriesin
	statusline Up to: ser{!i}

'series with mean around 0.1
if !i = 11 or !i= 22 or !i= 23 or !i= 39 or !i= 57 or !i= 68 or !i= 76 or !i=87 or !i=95 or !i=126 or !i=189 or !i=217 or !i=221 or !i=222 or !i=225 or !i=229 or !i=239 or !i=245  or !i=257  or !i=259  or !i=271 or !i=275 or !i=278 or !i=279 or !i=293 or !i=300 or !i=302 or !i=309 or !i=310 or !i=328 or !i=333 or !i=336 or !i=337 or !i=349 or !i=356 or !i=386  or !i=402  or !i=407 or !i=415 or !i=416 then 

	call flatZERO

		else
'change sample to @FIRST @LAST-21 if forecast is to November and last observation is August
	smpl @FIRST @LAST-20
!errcount = @errorcount
ser{!i}.sheet
freeze(output{!i}) ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=4, maxsar=1, maxsma=1, forclen=20, etable) ser{!i}_F c
	Output{!i}.extract(tab{!i}) eqoutput
	scalar constant_pvalue = @val(tab{!i}(11,5))	
if constant_pvalue > 0.05 then
	delete  Output{!i}
	delete tab{!i}
freeze(output{!i}) ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=4, maxsar=1, maxsma=1, forclen=20, etable) ser{!i}_F
	Output{!i}.extract(tab{!i}) eqoutput
	endif
	output{!i}.extract(sum{!i}) summary
	'string dep_var = @val(sum{!i}(2,2)) - should be able to extract the selected dependent variable
	
	Output{!i}.save(t=csv) arma{!i}
	copy Output{!i} forecasting_series::Models\arma{!i}

close SER{!i}
close SUM{!i}
close TAB{!i}

!errcount2 = @errorcount
if !errcount2>!errcount then
  	logmsg !i
	copy errormsg forecasting_series::Errors\errmsg{!i}
endif

	smpl @FIRST @LAST
	ARIMA1.add ser{!i}_F
endif
next
'	wfselect forecasting_series::TS_Seriesin

'pagesave(type=excelxml, mode=update) InSampleForecasts.xlsm byrow range="ARIMA!A1" @keep ARIMA1

 '	wfselect forecasting_series::Errors
'	wfsave(2) models

'pagesave(type=excelxml, mode=update) InSampleModels.xlsm range="ARIMA!A1" @keep ARMA*
'pagesave(type=excelxml, mode=update) InSampleModels.xlsm range="ESWDT!A1" @keep model*
	'wfclose forecasting_series


