R/d1.md.R

Defines functions d1.md.buildUniverse d1.md.getUniverse d1.md.addRt

d1.md.buildUniverse <- function(universe=NULL,DS,from,to){

	# Description: 
	# Function to fetch market prices, and collect them in one unique table

	d1.debug.info("Getting market prices...")
	# get universe composition 
	if (is.null(universe)){
		universe = d1.md.getUniverse(from)
	}




	## fecth prices
	getSymbols(
			   Symbols = universe,
			   src = "yahoo",
			   index.class = "POSIXct",
			   from = from,
			   to = to,
			   adjust = T,
			   env = .GlobalEnv
			   )



	# melt data
	if (!hasArg(DS)){
		DS = data.frame(date=as.POSIXct(character()),
							ticker=character(),
							op=double(),
							hp=double(),
							lp=double(),
							cp=double(),
							ts=double(),
							adjcp=double())
	}
	for (i in universe){
		activestock = get(i)
		tempComb=data.table(date=index(activestock),
							ticker=i,
							op = drop(coredata(activestock[,1])),
							hp = drop(coredata(activestock[,2])),
							lp = drop(coredata(activestock[,3])),
							cp = drop(coredata(activestock[,4])),
							ts = drop(coredata(activestock[,5])),
							adjcp = drop(coredata(activestock[,6]))
							)
		DS = rbind(DS,tempComb)
	}

	d1.backtest.cacheSymbols(securities)
	d1.debug.info("Done. Fetched",nrow(DS),"rows")
	return(DS)
}


d1.md.getUniverse <- function(d){
	# Get index composition for date d
	# ...
}

d1.md.addRt <- function(DT){
# Description ------------------------------------------------------------
# function to add returns column to dataset. DT must contain: adjcp,ticker

	d1.debug.info("Computing ex-post returns T, T+1, ..., T+5")
	DT[,rt:=log(adjcp) - shift(log(adjcp)),by=ticker]
	for (t in 1:5)
		DT[,(paste("postRt",t,sep="")):=shift(log(adjcp),t,type="lead") - log(adjcp),by=ticker] 

}
overhuman/d1r documentation built on May 24, 2019, 5:55 p.m.