require(xts) require(PerformanceAnalytics) require(pdfetch) require(gdata) ### The index track the performance of 3 simple moving average on crosses that explain most fo the turnover in the Foreign exchange market. FX_Crosses <- c("EUR-USD","USD-JPY","GBP-USD","USD-CHF","EUR-GBP","EUR-JPY","EUR-CHF") IR_rates <- c("USD1MTD156N","EUR1MTD156N","JPY1MTD156N","GBP1MTD156N","CHF1MTD156N") ############### The spot rates and 1-month interest rates are sourced from the BOE website and the Libor used for estimating the carry effect sourced from the Federal Reserve Bank of St Louis. Spot_rates <- pdfetch_BOE(c("XUDLERD","XUDLJYD","XUDLGBD","XUDLSFD"),"1970-01-01") colnames(Spot_rates) <- FX_Crosses[1:4] LIBOR_rates <- pdfetch_FRED(IR_rates) colnames(LIBOR_rates) <-substr(IR_rates,1,3) temp_mat <- na.omit(na.locf(cbind(Spot_rates,LIBOR_rates))) Spot_rates <- temp_mat[,1:4] LIBOR_rates <- temp_mat[,5:9] ##############Weighst in the AFX are estimated true the turnover published in the BIS Triennial Survey 1998 to 2016. The survey is publihed on the BIS Website and the turnover are taken from the table: for thee currency pair in the index BIS_turnovers <- c(365.0, 372, 541, 892, 1099, 1292, 1172, 266.6, 250, 328, 438, 567, 980, 901, 117.7, 129, 259, 384, 360, 473, 470, 78.6, 59, 83, 151, 166, 184, 180, 30.7, 27, 47, 69, 109, 102, 100, 24.2, 36, 61, 86, 111, 148, 79, 18.4, 13, 30, 62, 71, 71, 44) BIS_turnovers <-as.xts(t(matrix(BIS_turnovers,nrow = 7,ncol=7,byrow = TRUE,dimnames = list(c("EUR-USD","USD-JPY","GBP-USD","USD-CHF","EUR-GBP","EUR-JPY","EUR-CHF"), paste(as.numeric(c("1998","2001","2004","2007","2010","2013","2016"))+1,"-01-01",sep=""))))) AFX_weights <- na.locf(cbind(temp_mat[,0],as.xts(apply(BIS_turnovers,2,function(x) x/apply(BIS_turnovers,1,sum))))) #Calculate the total returns for the exchange rates included in the AFX Spot_rates <- cbind(1/Spot_rates[,1],Spot_rates[,2],1/Spot_rates[,3],Spot_rates[,4],Spot_rates[,3]/Spot_rates[,1],Spot_rates[,2]/Spot_rates[,1],Spot_rates[,4]/Spot_rates[,1]) colnames(Spot_rates) <- FX_Crosses carry_diff <- (LIBOR_rates[,substr(colnames(Spot_rates),1,3)]-LIBOR_rates[,substr(colnames(Spot_rates),5,7)])*as.vector((c(diff(index(LIBOR_rates)),0)/36500)) Total_returns <- diff(log(Spot_rates))+lag(carry_diff,1) MA_signals <- function(x) {sign(sum(x*(1:length(x))))} ma_basket_signals <- (rollapply(Total_returns,width=31,by.column=TRUE,MA_signals)+rollapply(Total_returns,width=61,by.column=TRUE,MA_signals)+rollapply(Total_returns,width=116,by.column=TRUE,MA_signals))/3 Returns_per_crosses <- na.omit(lag(ma_basket_signals,1)*Total_returns) AFX <- Returns_per_crosses*AFX_weights AFX_M <- as.xts(to.period(cumsum(apply(AFX,1,sum)),period="months",OHLC = FALSE))