TraderT Posted November 28, 2009 Report Posted November 28, 2009 Ich möchte meine Oanda Trades mit R analysieren. Leider hat Das Datum von Oanda ein Format wie im Beispiel. Date Balance 1 January 1 16:00:00 2009 25980.34 2 January 2 16:00:00 2009 25480.22 3 January 3 16:00:00 2009 25580.24 4 January 4 16:00:00 2009 25680.31 Frage: Wie bekomme ich die Zeit aus der Date Variablen in eine neue Spalte und das Date als Datum Objekt um die Equity zu Ploten ? Danke im Voraus.
Technix Posted January 19, 2011 Report Posted January 19, 2011 # --- configurable data -------------------------------------------------------- inputFileName <- 't:\\Oanda.in' # input file name of data file # --- function definitions ----------------------------------------------------- readAndConvert <- function(inputFileName) # read data from input file and split DateTime to Date and a string # representing time { # read data from file, assuming a tab separation, which perhaps wasn't # displayed well in the HTML output of the data sample inputData <- read.table(inputFileName, header = TRUE, sep = '\t') # extract DateTime and Balance values using english locale time settings locale <- Sys.getlocale(category = "LC_TIME") Sys.setlocale(category = 'LC_TIME', locale = 'en') dateTimes <- as.POSIXct(as.character(inputData$Date), format = '%B %d %H:%M:%S %Y') Sys.setlocale("LC_TIME", locale) equity <- NULL # split DateTime into Date (R Date data type) and Time (a simple string) equity$Date <- as.Date(dateTimes) equity$Time <- format(dateTimes, format = '%H:%M:%S') # character equity, R in basic package configuration has no special equity type # for time without date only, for date and time together exist the two types # 'POSIXct' and 'POSIXlt' and for Date only exists type 'Date' equity$Balance <- inputData$Balance # coerce to data frame equity <- data.frame(equity) rownames(equity) <- dateTimes return(equity) } filterLastQuoteOfTheDay <- function(equity) # extract the data lines which represent the last quote of a day # assumes equity already sorted by DateTime { isEOD <- vector() for ( i in 1 : nrow(equity) ) isEOD[i] <- equity$Date[i] != equity$Date[i + 1] isEOD[nrow(equity)] <- TRUE return(equity[isEOD,]) } filterLastQuoteOfTheDay.alternativeSolution <- function(equity) # same result as above but with more R tricks equity[tapply(rownames(equity), factor(equity$Date), max),] plotEquity <- function(equity, plotType = 'o') # plot the equity against the date { yMinMax <- c(min(equity$Balance) * 0.95, max(equity$Balance) * 1.05) par(bg = '#C0C0C0', mar = c(8, 6, 3, 1), mgp = c(5, 1, 0)) plot(equity$Date, equity$Balance , type = plotType, pch = '-' , ylim = yMinMax, log = 'y' , axes = FALSE , main = 'Equity', xlab = 'Date', ylab = 'Equity' , cex.main = 1.05, cex.lab = 0.85 ) box() axis(1, las = 3, at = equity$Date, labels = equity$Date) axis(2, las = 1, padj = 0.35, hadj = 0.9) } # --- run the functions -------------------------------------------------------- equity <- readAndConvert(inputFileName) equityEOD <- filterLastQuoteOfTheDay(equity) # equityEOD <- filterLastQuoteOfTheDay.alternativeSolution(equity) # plotEquity(equity) plotEquity(equityEOD, 'l') 2
Technix Posted January 19, 2011 Report Posted January 19, 2011 Ich merke korrigierend und entschuldigend an, daß in der Funktion readAndConvert() in einer Kommentarzeile # character data, R in basic package configuration has no special data typestehen sollte, wobei ohne die getestete Funktionalität zu mindern, bei irgendeinem Suchen/Ersetzen aus "data" ungewollt an zwei Stellen "equity" geworden ist. Habe es hier noch hinzugefügt, weil sich der Kommentar gerade auf die Kern-Frage bezog und so nicht sehr sinnreich war.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now