Advertisement
Guest User

When A high is made in a Low day/oppisit for up day

a guest
Feb 12th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. library(R.utils)
  2. library(lubridate)
  3.  
  4. BlockOutput <- function() {
  5. sink("test.log", type="output")
  6. }
  7.  
  8. TestHighLowofDay <- function(data,timeshift=5) {
  9.   Dates <- unique(data[,1]) #List of dates in data
  10.   BlockOutput()
  11.   temp   <- data.frame(
  12.     str(10),
  13.     numeric(),
  14.     numeric(),
  15.     numeric(),
  16.     numeric(),
  17.     str(5),
  18.     str(5),
  19.     stringsAsFactors=FALSE)
  20.     sink()
  21.     x   <- 1
  22.     cat("  Collecting information\n")
  23.     pb  <- txtProgressBar(min = 0, max = length(Dates), style = 3)
  24.    
  25.     for (x in 1:length(Dates)) {
  26.       cur_day <- data[data[,1] == Dates[x],]
  27.          
  28.       nHigh = max(cur_day[,4])
  29.       nLow = min(cur_day[,5])
  30.            
  31.       nHighAtnr = which.max(cur_day[,4])
  32.       nLowAtnr  = which.min(cur_day[,5])
  33.  
  34.       nHighAt = cur_day[nHighAtnr,2]
  35.       nLowAt = cur_day[nLowAtnr,2]
  36.      
  37.       temp[x,1] = as.character(Dates[x])
  38.       temp[x,2] = cur_day[1,3]
  39.       temp[x,3] = nHigh
  40.       temp[x,4] = nLow
  41.       temp[x,5] = cur_day[length(cur_day),6]
  42.       temp[x,6] = nHighAt
  43.       temp[x,7] = nLowAt
  44.       setTxtProgressBar(pb,x )
  45.     }
  46.     cat("\n")
  47.     colnames(temp) <- c('Date','Open','High','Low','Close','HighAt','LowAt')
  48.    
  49.     cat("   Removeing Saturday and Sunday candles\n")
  50.     temp <- temp[!(wday(as.Date(temp$Date,"%Y.%m.%d"))==7 || wday(as.Date(temp$Date,"%Y.%m.%d"))==1 ),]
  51.    
  52.     cat("  Creating time list\n")
  53.     BlockOutput()
  54.     TimeList <- data.frame(str(5))
  55.     sink()
  56.     x = 1
  57.     for (x in 1:length(temp$Date)) {
  58.      
  59.     if (is.na(temp$Open[x]) | is.na(temp$Close[x])) {
  60.         #skip
  61.  
  62.     } else if (temp$Open[x] <= temp$Close[x]) {
  63.         #UP DAY
  64.         TimeList[x,1] <- lapply(temp$LowAt[x], as.character)
  65.  
  66.       }else {
  67.         #Down Day
  68.         TimeList[x,1] <- lapply(temp$HighAt[x], as.character)
  69.  
  70.       }
  71.     }
  72.     TimeList <- TimeList[!is.na(TimeList)]
  73.     TimeList_unique <- unique(TimeList)
  74.     CountList <- 1
  75.    
  76.     cat("  Creating Count list\n")
  77.     x   <-1
  78.        
  79.     for (x in 1:length(TimeList_unique)) {
  80.       CountList[x] = match(TimeList_unique[x], TimeList)
  81.     }
  82.    
  83.     cat("  creating return list\n")
  84.     TimeListPOSIX <- as.POSIXlt(TimeList_unique,format="%H:%M")
  85.     ret <- data.frame(Time = TimeListPOSIX, Count = CountList)
  86.     ret$Time <- as.POSIXlt(ret$Time)
  87.     ret$Time$hour <- ret$Time$hour+timeshift
  88.     ret_time <- strftime(ret$Time,format = "%H:%M")
  89.     ret$Time <- as.POSIXlt(ret_time,format="%H:%M")
  90.     ret <- ret[order(ret$Time),]
  91.    
  92.     cat("  Done.\n")  
  93.     cat("\n")
  94.     return(ret)
  95. }
  96. ############################################################################
  97.  
  98. eu    <- read.csv(file.choose(), header=F)
  99. data  <- TestHighLowofDay(eu)
  100.  
  101. plot(data,type="h")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement