' Program: Reg-ProgIII_OF-FD.prg (FOR TABLE 4 IN THE ARTICLE)
' Program that generates tables where OF (or any
' other variable if you add {%Htext} to dep-var)
' is dependent and FD is the independent variable.
' Single equation procedures.

' Set path
' Program is supposed to reside in top-directory of UIP-project
%path = @runpath
cd %path

' MAIN ROUTINE:
' Arguments: End of file-name (%file), Table-name (%TNum) and Dependent variable (%Flow and %Vol)
subroutine regvarfdII(string %file, string %TNum, string %Flow, string %Vol)
' Open data-file
' Use relative path (relative to top-directory of UIP-project)
wfopen monthly\workfiles\month-dataiii_{%file}
pageselect data

smpl @first 2007m04

' Creates one table to hold results. Includes R2.
table tab{%TNum}_Reg_{%Flow}{%Vol}FD
table tab{%TNum}_stat{%Flow}{%Vol}FD

tab{%TNum}_Reg_{%Flow}{%Vol}FD(1,3) = "Flow: "+@upper(%Flow)+","+@upper(%Vol)
tab{%TNum}_Reg_{%Flow}{%Vol}FD(2,1) = "Currency"
tab{%TNum}_Reg_{%Flow}{%Vol}FD(2,2) = "Horizon"
tab{%TNum}_Reg_{%Flow}{%Vol}FD(2,3) = "beta_o^k"
tab{%TNum}_Reg_{%Flow}{%Vol}FD(2,4) = "t-stat"
tab{%TNum}_Reg_{%Flow}{%Vol}FD(2,5) = "R^2"

' Declare equation object
equation tempeq

table names
names(1,1) = "EUR"
names(1,2) = "JPY"
names(1,3) = "GBP"
names(2,1) = "USD/EUR"
names(2,2) = "USD/JPY"
names(2,3) = "USD/GBP"
names(3,1) = "01"
names(3,2) = "03"
names(3,3) = "06"
names(3,3) = "12"

' Currencies
for !i = 1 to 3

	' Create macro-variable to identify currency
    %Ccy = names(1,!i)
    %Ccyname = names(2,!i)

		' Maturities
		for !j = 1 to 4
		' Identify maturity	 H=horizon
		%Htext = names(3,!j)
		!L = @val(%Htext)

' Definitions of Order Flow: Independent variable
' Edit below here to select how OF enters:
' -----------------------------------------------
' Vol at time t-k
series temp_of = @movsum((of{%Flow}{%Ccy}/@stdev(of{%Flow}{%Ccy})),!L)*({%Vol}{%Ccy}{%Htext}(-!L)^2/@mean({%Vol}{%Ccy}{%Htext}^2))/100

' Vol
series temp_vol{%Ccy}{%Htext} = ({%Vol}{%Ccy}{%Htext}(-!L)^2/@mean({%Vol}{%Ccy}{%Htext}^2))/100

' Main equation for this program
		tempeq.gmm(b=a) temp_of c fd{%Ccy}{%Htext}(-!L)*100 @ c fd{%Ccy}{%Htext}(-!L)*100 
' End		

		' Tab-index is row, column
		tab{%TNum}_Reg_{%Flow}{%Vol}FD(!i*3+!i-1,1) = @upper(%Ccyname)		' Column-text: EUR etc.
		tab{%TNum}_Reg_{%Flow}{%Vol}FD(!i*3+!i-1+!j-1,2) = @str(!L)			' Row-text: 1M etc.
		tab{%TNum}_Reg_{%Flow}{%Vol}FD(!i*3+!i-1+!j-1,3) = tempeq.@coefs(2)	' FD-coeff
		tab{%TNum}_Reg_{%Flow}{%Vol}FD(!i*3+!i-1+!j-1,4) = tempeq.@tstats(2)	' t-stat
		tab{%TNum}_Reg_{%Flow}{%Vol}FD(!i*3+!i-1+!j-1,5) = tempeq.@rbar2		' Rbar2

tempeq.makeresid tempres
tempres.uroot(kpss,save=tempurb)	'select 3
tempres.uroot(adf,save=tempur)		'select 4

		' Tab-index is row, column
		tab{%TNum}_stat{%Flow}{%Vol}FD(1,!j+1) = @str(!L)+" Month"	' Column-text: 1M etc.
		tab{%TNum}_stat{%Flow}{%Vol}FD(2*!i,1) = @upper(%Ccyname)		' Row-text: EUR etc.
		tab{%TNum}_stat{%Flow}{%Vol}FD(2*!i,!j+1) = tempeq.@rbar2	' Coeff on var: e.g. beta_fd
		tab{%TNum}_stat{%Flow}{%Vol}FD(2*!i+1,!j+1) = @str(tempurb(3),"g.3")+"/"+@str(tempur(4),"g.3")	' std.err below coeff.

		next  ' End maturities
next	' End currencies

' Copy tables to Table-page, show them, and clean up temporary variables
delete temp*
copy(overwrite) Data\tab{%TNum}_reg_{%Flow}{%Vol}FD Tables\
copy(overwrite) Data\tab{%TNum}_stat{%Flow}{%Vol}FD Tables\
delete tab{%TNum}_reg_{%Flow}{%Vol}FD tab{%TNum}_stat{%Flow}{%Vol}FD
pageselect tables
'show tab{%TNum}_reg_{%Flow}{%Vol}FD

pageselect data

delete temp*

smpl @all
wfsave monthly\workfiles\month-dataiii_{%file}
close month-dataiii_{%file}
endsub

' !!!!!!!!!!  EXECUTION !!!!!!!!!!
' Notice that sample ends in april and is set at top
'smpl @first 2007m04

' 2) Call procedure! First parameter is selecting file for estimation.

' Order flow on FD
' File=ExNew
call regvarfdII("ExNew","_dtmp","n","iv")
