Zum Inhalt springen
View in the app

A better way to browse. Learn more.

#T/N/X/T

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Oanda Trades mit R analysieren

Geschrieben

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.

Featured Replies

Geschrieben
# --- 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')

Geschrieben

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 type

stehen 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.

Dein Kommentar

Du kannst jetzt schreiben und Dich später registrieren. Wenn Du ein Konto hast, melde Dich jetzt an, um unter Deinem Benutzernamen zu schreiben.

Gast
Auf dieses Thema antworten...

Account

Navigation

Suche

Suche

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.