getPotencialYearGain | R Documentation |
getPotencialYearGain(ds, year)
ds |
|
year |
##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (ds, year)
{
from <- paste(year, "-01-01", sep = "")
to <- paste(year, "-12-31", sep = "")
lucro <- 0
ds <- ds[as.character(ds$Date) >= from & as.character(ds$Date) <=
to, ]
if (nrow(ds) > 0) {
first <- head(ds, 1)
tendency <- first$Tendency
buy <- ifelse(tendency == "up", first$Ticket, 0)
sell <- 0
for (idx in 1:nrow(ds)) {
day <- ds[idx, ]
if (day$Tendency != tendency) {
tendency <- day$Tendency
if (day$Tendency == "up") {
buy <- day$Ticket
}
else {
if (buy != 0) {
sell <- day$Ticket
lucro <- lucro + ((sell/buy) - 1) * 100
buy <- 0
}
}
}
}
if (buy != 0) {
sell <- day$Ticket
lucro <- lucro + ((sell/buy) - 1) * 100
}
}
return(lucro)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.