MakeVolumeBinIdx<-function(data1.volume,volBinSize){
### PURPOSE: Find the indexes for a given size of volume bin, that is
### Find the indexes of the data.frame where the sum of the volume is equal
### to the input volBinSize.
### INPUT: a vector of trades volume and the desired volume bin size
### OUTPUT: a vector indicating where each row belongs to which volume bin
#create index
volBin<-1
sumVol<-0
Volume<-data1.volume
volBinIdx <- numeric(length(Volume))
#create cutting for each volume bin
for(i in seq_len(length(Volume))){
sumVol<-sumVol + Volume[i]
if (sumVol<= volBinSize) {
volBinIdx[i] <- volBin
} else {
volBinIdx[i] <- volBin <- volBin + 1
sumVol <- Volume[i]
}
}
#clean environment
rm(Volume,i,sumVol,volBinSize,volBin)
return(volBinIdx)
}
##### put all functions neededin-memory
source("FT_functions_SO.R")
## read data in
data1<-read.table("XYZ_EE.txt",sep=",",stringsAsFactor=F,header=F,
colClasses=c("character","character","numeric","numeric"))
#Name Columns
colnames(data1)<-c("Date","Time","Price","Volume")
#Add columns for total amount traded
data1["TT"]<-data1[,"Price"]*data1[,"Volume"]
##### time volBinIdx5K
start.time.volBinIdx5k<-Sys.time()
volBinIdx5k<-MakeVolumeBinIdx(data1$Volume,5000)
## Purpose: find indexes where cumulative volume equal 5,000 shares
## Input: data1$Volume and the size of the volume bin
## Output: vector with indexes for each row signifying which row belong to which
## volume bin
##### time it took volBinIdx5k
end.time.volBinIdx5k<-Sys.time()
time.volBinIdx5k<-end.time.volBinIdx5k-start.time.volBinIdx5k
time.volBinIdx5k
##### time MakeBinCandles
start.time.MakeBindcandles.5k<-Sys.time()
data1.5k.dfm<-MakeBinCandles(data1,volBinIdx5k)
## Purpose: Create candles based on volume bins
## Input: data1
## Output: data.frame: Date,Time,OHLC,volume,HighIdx,LowIdx,MF,TT, VWAP
##### time it took MakeBinCandles for 5000 shares
end.time.MakeBinCandles.5k<-Sys.time()
time.MakeBinCandles.5k<-end.time.MakeBinCandles.5k-start.time.MakeBindcandles.5k
time.MakeBinCandles.5k
### FT_functions_EE.R
MakeVolumeBinIdx<-function(data1.volume,volBinSize){
### PURPOSE: Find the indexes for a given size of volume bin, that is
### Find the indexes of the data.frame where the sum of the volume is equal
### to the input volBinSize.
### INPUT: a vector of trades volume and the desired volume bin size
### OUTPUT: a vector indicating where each row belongs to which volume bin
#create index
volBin<-1
sumVol<-0
Volume<-data1.volume
volBinIdx <- numeric(length(Volume))
#create cutting for each volume bin
for(i in seq_len(length(Volume))){
sumVol<-sumVol + Volume[i]
if (sumVol<= volBinSize) {
volBinIdx[i] <- volBin
} else {
volBinIdx[i] <- volBin <- volBin + 1
sumVol <- Volume[i]
}
}
#clean environment
rm(Volume,i,sumVol,volBinSize,volBin)
return(volBinIdx)
}
MakeBinCandles<-function(data,volBinIdxk){
### PURPOSE: Create candles based on bins
### INPUT: a new data.frame containing only Date,Time, Price,Volume,TT, AND
### a vector containing the output of MakeVolumeBinIdx
### OUTPUT: a data.frame with Date,Time, OHLC, Volume,
### HighIdx,lowIdx, TT,VWAP (=volume weighted average price)
library(dplyr)
data.return<-data %>%
mutate(volBinIdxk=volBinIdxk) %>%
group_by(volBinIdxk) %>%
summarize(Date=head(Date,1),
Time=head(Time,1),
Open=head(Price,1),
High=max(Price),
Low=min(Price),
Close=tail(Price,1),
Volume=sum(Volume),
# HighIdx=which.max(Price),
# LowIdx=which.min(Price),
TT=sum(TT,na.rm=T),
VWAP=TT/Volume) %>%
select(-volBinIdxk) %>%
as.data.frame()
return(data.return)
}
MakeBinCandlesXts<-function(data){
### PURPOSE: Turn bin candles from data.frame into xts object
### INPUT: data frame outputed by MakeBinCandles()
### OUTPUT: xts object
library(xts)
data$Date<-strptime(paste(data$Date,data$Time),"%m/%d/%Y %H:%M:%S")
data<-data[,-2] # if I don't remove it, all columns become characters
data.xts<-xts(data[,-1],order.by=as.POSIXct(data[,1]))
return (data.xts)
}
XYZ-EE.txtIf you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Solving an equation | 8 | 63 | |
Volume Calculation | 14 | 62 | |
Percentage | 6 | 58 | |
Odds of picking games correctly | 4 | 90 |
Join the community of 500,000 technology professionals and ask your questions.