Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(R.utils)
- library(lubridate)
- BlockOutput <- function() {
- sink("test.log", type="output")
- }
- TestHighLowofDay <- function(data,timeshift=5) {
- Dates <- unique(data[,1]) #List of dates in data
- BlockOutput()
- temp <- data.frame(
- str(10),
- numeric(),
- numeric(),
- numeric(),
- numeric(),
- str(5),
- str(5),
- stringsAsFactors=FALSE)
- sink()
- x <- 1
- cat(" Collecting information\n")
- pb <- txtProgressBar(min = 0, max = length(Dates), style = 3)
- for (x in 1:length(Dates)) {
- cur_day <- data[data[,1] == Dates[x],]
- nHigh = max(cur_day[,4])
- nLow = min(cur_day[,5])
- nHighAtnr = which.max(cur_day[,4])
- nLowAtnr = which.min(cur_day[,5])
- nHighAt = cur_day[nHighAtnr,2]
- nLowAt = cur_day[nLowAtnr,2]
- temp[x,1] = as.character(Dates[x])
- temp[x,2] = cur_day[1,3]
- temp[x,3] = nHigh
- temp[x,4] = nLow
- temp[x,5] = cur_day[length(cur_day),6]
- temp[x,6] = nHighAt
- temp[x,7] = nLowAt
- setTxtProgressBar(pb,x )
- }
- cat("\n")
- colnames(temp) <- c('Date','Open','High','Low','Close','HighAt','LowAt')
- cat(" Removeing Saturday and Sunday candles\n")
- temp <- temp[!(wday(as.Date(temp$Date,"%Y.%m.%d"))==7 || wday(as.Date(temp$Date,"%Y.%m.%d"))==1 ),]
- cat(" Creating time list\n")
- BlockOutput()
- TimeList <- data.frame(str(5))
- sink()
- x = 1
- for (x in 1:length(temp$Date)) {
- if (is.na(temp$Open[x]) | is.na(temp$Close[x])) {
- #skip
- } else if (temp$Open[x] <= temp$Close[x]) {
- #UP DAY
- TimeList[x,1] <- lapply(temp$LowAt[x], as.character)
- }else {
- #Down Day
- TimeList[x,1] <- lapply(temp$HighAt[x], as.character)
- }
- }
- TimeList <- TimeList[!is.na(TimeList)]
- TimeList_unique <- unique(TimeList)
- CountList <- 1
- cat(" Creating Count list\n")
- x <-1
- for (x in 1:length(TimeList_unique)) {
- CountList[x] = match(TimeList_unique[x], TimeList)
- }
- cat(" creating return list\n")
- TimeListPOSIX <- as.POSIXlt(TimeList_unique,format="%H:%M")
- ret <- data.frame(Time = TimeListPOSIX, Count = CountList)
- ret$Time <- as.POSIXlt(ret$Time)
- ret$Time$hour <- ret$Time$hour+timeshift
- ret_time <- strftime(ret$Time,format = "%H:%M")
- ret$Time <- as.POSIXlt(ret_time,format="%H:%M")
- ret <- ret[order(ret$Time),]
- cat(" Done.\n")
- cat("\n")
- return(ret)
- }
- ############################################################################
- eu <- read.csv(file.choose(), header=F)
- data <- TestHighLowofDay(eu)
- plot(data,type="h")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement