Created
October 4, 2012 17:34
-
-
Save nkabir/3835160 to your computer and use it in GitHub Desktop.
Time Series Cleaner in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################################################################################################### | |
# Name : DATACLEANING S&P | |
# Author : Nikos Rachmanis | |
# Version : 1.0 | |
# Copyright : Your copyright notice | |
# Description : Data cleaning and merging | |
#################################################################################################################### | |
#################################################################################################################### | |
### Libraries + Settings ########################################################################################### | |
#################################################################################################################### | |
#clear memory | |
rm(list=ls()) | |
#Increase display buffer (only when it is used with display opearations) | |
options(max.print=5E8) | |
#load Libraries ###### | |
library(xts) | |
#################################################################################################################### | |
### File Import + Data Object creation ############################################################################# | |
#################################################################################################################### | |
#Read the files | |
ES1=read.csv("K:\\000.ES1.csv", sep=",", header=TRUE) | |
SP1=read.csv("K:\\000.SP1.csv", sep=",", header=TRUE) | |
SPX=read.csv("K:\\000.SPX.csv", sep=",", header=TRUE) | |
#Create the xts using the constructor | |
#the "%m/%d/%Y" defines the way it is registered in the file | |
#[,-1] selects everything expect the first one | |
sp1<-xts(SP1[,-1],as.Date(SP1[,1],"%m/%d/%Y")) | |
es1<-xts(ES1[,-1],as.Date(ES1[,1],"%m/%d/%Y")) | |
spx<-xts(SPX[,-1],as.Date(SPX[,1],"%m/%d/%Y")) | |
#merge the 2 datasets | |
ohlc<-merge.xts(sp1,spx,fill=NA,join="left") | |
ohlc<-merge.xts(ohlc,es1,fill=NA,join="left") | |
#################################################################################################################### | |
### Clean the Data ################################################################################################# | |
#################################################################################################################### | |
#ES1 ############################################################################################################### | |
#Replace the volume with 0 wherever NA | |
#ohlc[,10]<-replace(ohlc[,10],is.na(ohlc[,10]),0) | |
ohlc[is.na(ohlc[,15]),15]<-0 | |
#Replace CLOSE[x] with CLOSE[x-1] | |
ohlc[,14]<-na.locf(ohlc[,14],maxgap=1) | |
#Replace LOW[x] with CLOSE[x] | |
ohlc[,12]<-ifelse(is.na(ohlc[,11]), ohlc[,14], ohlc[,12]) | |
#Replace HIGH[x] with CLOSE[x] | |
ohlc[,13]<-ifelse(is.na(ohlc[,11]), ohlc[,14], ohlc[,13]) | |
#Replace OPEN[x] with CLOSE[x] | |
ohlc[,11]<-ifelse(is.na(ohlc[,11]), ohlc[,14], ohlc[,11]) | |
#SPX ############################################################################################################### | |
#Replace the volume with 0 wherever NA | |
#ohlc[,10]<-replace(ohlc[,10],is.na(ohlc[,10]),0) | |
ohlc[is.na(ohlc[,10]),10]<-0 | |
#Replace CLOSE[x] with CLOSE[x-1] | |
ohlc[,9]<-na.locf(ohlc[,9],maxgap=1) | |
#Replace LOW[x] with CLOSE[x] | |
ohlc[,7]<-ifelse(is.na(ohlc[,6]), ohlc[,9], ohlc[,7]) | |
#Replace HIGH[x] with CLOSE[x] | |
ohlc[,8]<-ifelse(is.na(ohlc[,6]), ohlc[,9], ohlc[,8]) | |
#Replace OPEN[x] with CLOSE[x] | |
ohlc[,6]<-ifelse(is.na(ohlc[,6]), ohlc[,9], ohlc[,6]) | |
#SP1 ############################################################################################################### | |
#Replace the volume with 0 wherever NA | |
#ohlc[,10]<-replace(ohlc[,10],is.na(ohlc[,10]),0) | |
ohlc[is.na(ohlc[,5]),5]<-0 | |
#Replace CLOSE[x] with CLOSE[x-1] | |
ohlc[,4]<-na.locf(ohlc[,4],maxgap=1) | |
#Replace LOW[x] with CLOSE[x] | |
ohlc[,2]<-ifelse(is.na(ohlc[,1]), ohlc[,4], ohlc[,2]) | |
#Replace HIGH[x] with CLOSE[x] | |
ohlc[,3]<-ifelse(is.na(ohlc[,1]), ohlc[,4], ohlc[,3]) | |
#Replace OPEN[x] with CLOSE[x] | |
ohlc[,1]<-ifelse(is.na(ohlc[,1]), ohlc[,4], ohlc[,1]) | |
#################################################################################################################### | |
### Write to file ################################################################################################## | |
#################################################################################################################### | |
#write to file | |
#write.zoo(data, file="D:\\11._Dev_Shared\\05_Strat\\Commitment_Of_Traders_Report\\03.ESFutures\\output.csv",sep=",") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment