getPotencialYearGain: Get potential profit gain in a year.

View source: R/functions.R

getPotencialYearGainR Documentation

Get potential profit gain in a year.

Usage

getPotencialYearGain(ds, year)

Arguments

ds
year

Examples

##---- 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)
  }

danielfhenrique789/profitmaximization documentation built on April 5, 2025, 4:05 p.m.