ichimoku: Auxiliary Functions

library(ichimoku)

Introduction

This vignette is dedicated to the auxiliary functions exported by the ichimoku package.

Note that all of the auxiliary functions are programmed for performance and are hence stripped of all validation and error-checking code. If they are used outside of their intended scopes then errors can be expected. In particular, the input types must match exactly.

Core Auxiliary Functions

tradingDays()

Used to subset a vector of dates to trading days. Note, if the argument ‘holidays’ is passed to ichimoku(), this is passed through to this function when calculating the dates that the future cloud will fall on.

Takes 2 arguments:

  • x a vector of POSIXct date objects.
  • holidays (optional) a vector, or function which outputs a vector, of dates defined as holidays. If not specified, New Year’s and Christmas day are defined as holidays by default.
dates <- seq(from = as.POSIXct("2020-01-01"), by = "1 day", length.out = 7)
dates
#> [1] "2020-01-01 GMT" "2020-01-02 GMT" "2020-01-03 GMT" "2020-01-04 GMT"
#> [5] "2020-01-05 GMT" "2020-01-06 GMT" "2020-01-07 GMT"
tradingDays(dates)
#> [1] FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE
tradingDays(dates, holidays = c("2020-01-01", "2020-01-02"))
#> [1] FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE

grid_dup()

Create a vector of element positions of duplicates in the output of expand.grid on 2 identical vectors. A faster method of creating combinations for 2 variabes than utils::combn().

Takes 2 arguments:

  • n the length of vector passed to expand.grid().
  • omit.id [default FALSE] to not select the elements where the 2 items are identical. Set to TRUE to also select these. The output of expand.grid, subset to remove duplicates with ‘omit.id’ set to TRUE would be the equivalent of utils::combn(n, 2).
n <- 3
expand.grid(1:n, 1:n)
#>   Var1 Var2
#> 1    1    1
#> 2    2    1
#> 3    3    1
#> 4    1    2
#> 5    2    2
#> 6    3    2
#> 7    1    3
#> 8    2    3
#> 9    3    3
expand.grid(1:n, 1:n)[-grid_dup(n), ]
#>   Var1 Var2
#> 1    1    1
#> 2    2    1
#> 3    3    1
#> 5    2    2
#> 6    3    2
#> 9    3    3
expand.grid(1:n, 1:n)[-grid_dup(n, omit.id = TRUE), ]
#>   Var1 Var2
#> 2    2    1
#> 3    3    1
#> 6    3    2

Performant Dataframe Constructors

xts_df()

Convert an ‘xts’ object to ‘data.frame’. This function can be an order of magnitude faster than as.data.frame for an ‘xts’ object.

Takes a single argument:

  • x the ‘xts’ object to convert to ‘data.frame’.
cloud <- ichimoku(sample_ohlc_data)
df <- xts_df(cloud)
str(df)
#> 'data.frame':    260 obs. of  13 variables:
#>  $ index    : POSIXct, format: "2020-01-02 00:00:00" "2020-01-03 00:00:00" ...
#>  $ open     : num  123 123 123 123 124 ...
#>  $ high     : num  123 123 123 124 125 ...
#>  $ low      : num  122 123 122 123 124 ...
#>  $ close    : num  123 123 123 124 125 ...
#>  $ cd       : num  -1 1 1 1 1 1 -1 0 -1 -1 ...
#>  $ tenkan   : num  NA NA NA NA NA ...
#>  $ kijun    : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ senkouA  : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ senkouB  : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ chikou   : num  123 123 124 124 122 ...
#>  $ cloudTop : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ cloudBase: num  NA NA NA NA NA NA NA NA NA NA ...

matrix_df()

Convert a matrix to ‘data.frame’. This function is slightly faster than as.data.frame() for a matrix.

Takes a single argument:

  • x the matrix to convert to ‘data.frame’.
cloud <- ichimoku(sample_ohlc_data)
mcloud <- as.matrix(cloud)
df <- matrix_df(mcloud)
str(df)
#> 'data.frame':    260 obs. of  12 variables:
#>  $ open     : num  123 123 123 123 124 ...
#>  $ high     : num  123 123 123 124 125 ...
#>  $ low      : num  122 123 122 123 124 ...
#>  $ close    : num  123 123 123 124 125 ...
#>  $ cd       : num  -1 1 1 1 1 1 -1 0 -1 -1 ...
#>  $ tenkan   : num  NA NA NA NA NA ...
#>  $ kijun    : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ senkouA  : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ senkouB  : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ chikou   : num  123 123 124 124 122 ...
#>  $ cloudTop : num  NA NA NA NA NA NA NA NA NA NA ...
#>  $ cloudBase: num  NA NA NA NA NA NA NA NA NA NA ...
str(row.names(df))
#>  chr [1:260] "2020-01-02 00:00:00" "2020-01-03 00:00:00" ...

Dataframe Manipulation Functions

df_trim()

Trim rows containing NA values from a ‘data.frame’ object. This is a faster version of stats:::na.omit().

Takes a single argument:

  • x the data.frame to trim.
data <- data.frame(c(1:4, NA), c(NA, 2:5))
data
#>   c.1.4..NA. c.NA..2.5.
#> 1          1         NA
#> 2          2          2
#> 3          3          3
#> 4          4          4
#> 5         NA          5
df_trim(data)
#>   c.1.4..NA. c.NA..2.5.
#> 2          2          2
#> 3          3          3
#> 4          4          4

df_merge()

Full join on an arbitrary number of ‘data.frame’ objects passed as arguments, preserving all unique entries. Can be used to combine historical time series data where each observation is indexed by a unique timestamp and all periods are complete.

Takes an arbitrary number of arguments:

  • ... data.frame objects to combine.

Can be used to join price dataframes retrieved by oanda(). The function is designed to join complete historical data. If the data to be merged contains data with incomplete periods, all entries are preserved rather than updated. If incomplete periods are detected within the data, a warning is issued, and the resulting dataframe should be manually inspected in case it contains unwanted duplicates. Use df_append() for updating dataframes with new values.

data1 <- sample_ohlc_data[1:6, ]
data1
#>         time  open  high   low close
#> 1 2020-01-02 123.0 123.1 122.5 122.7
#> 2 2020-01-03 122.7 122.8 122.6 122.8
#> 3 2020-01-05 122.8 123.4 122.4 123.3
#> 4 2020-01-06 123.3 124.3 123.3 124.1
#> 5 2020-01-07 124.1 124.8 124.0 124.8
#> 6 2020-01-08 124.8 125.4 124.5 125.3
data2 <- sample_ohlc_data[4:10, ]
data2
#>          time  open  high   low close
#> 4  2020-01-06 123.3 124.3 123.3 124.1
#> 5  2020-01-07 124.1 124.8 124.0 124.8
#> 6  2020-01-08 124.8 125.4 124.5 125.3
#> 7  2020-01-09 125.3 125.3 124.8 125.2
#> 8  2020-01-10 125.2 125.3 125.1 125.2
#> 9  2020-01-12 125.2 125.2 124.3 124.4
#> 10 2020-01-13 124.4 124.5 123.7 123.9
df_merge(data1, data2)
#>          time  open  high   low close
#> 1  2020-01-02 123.0 123.1 122.5 122.7
#> 2  2020-01-03 122.7 122.8 122.6 122.8
#> 3  2020-01-05 122.8 123.4 122.4 123.3
#> 4  2020-01-06 123.3 124.3 123.3 124.1
#> 5  2020-01-07 124.1 124.8 124.0 124.8
#> 6  2020-01-08 124.8 125.4 124.5 125.3
#> 7  2020-01-09 125.3 125.3 124.8 125.2
#> 8  2020-01-10 125.2 125.3 125.1 125.2
#> 9  2020-01-12 125.2 125.2 124.3 124.4
#> 10 2020-01-13 124.4 124.5 123.7 123.9

df_append()

Update a ‘data.frame’ object with new data. Can be used to append new updated time series data to an existing dataframe, where each observation is indexed by a unique timestamp.

Takes 2 arguments:

  • new data.frame object containing new data.
  • old data.frame object containing existing data.

Can be used to update price dataframes retrieved by oanda(). The function is designed to update existing data with new values as they become available. As opposed to df_merge(), the data in ‘new’ will overwrite the data in ‘old’ rather than create duplicates.

data1 <- sample_ohlc_data[7:10, ]
data1
#>          time  open  high   low close
#> 7  2020-01-09 125.3 125.3 124.8 125.2
#> 8  2020-01-10 125.2 125.3 125.1 125.2
#> 9  2020-01-12 125.2 125.2 124.3 124.4
#> 10 2020-01-13 124.4 124.5 123.7 123.9
data2 <- sample_ohlc_data[1:8, ]
data2
#>         time  open  high   low close
#> 1 2020-01-02 123.0 123.1 122.5 122.7
#> 2 2020-01-03 122.7 122.8 122.6 122.8
#> 3 2020-01-05 122.8 123.4 122.4 123.3
#> 4 2020-01-06 123.3 124.3 123.3 124.1
#> 5 2020-01-07 124.1 124.8 124.0 124.8
#> 6 2020-01-08 124.8 125.4 124.5 125.3
#> 7 2020-01-09 125.3 125.3 124.8 125.2
#> 8 2020-01-10 125.2 125.3 125.1 125.2
df_append(data1, data2)
#>          time  open  high   low close
#> 1  2020-01-02 123.0 123.1 122.5 122.7
#> 2  2020-01-03 122.7 122.8 122.6 122.8
#> 3  2020-01-05 122.8 123.4 122.4 123.3
#> 4  2020-01-06 123.3 124.3 123.3 124.1
#> 5  2020-01-07 124.1 124.8 124.0 124.8
#> 6  2020-01-08 124.8 125.4 124.5 125.3
#> 7  2020-01-09 125.3 125.3 124.8 125.2
#> 8  2020-01-10 125.2 125.3 125.1 125.2
#> 9  2020-01-12 125.2 125.2 124.3 124.4
#> 10 2020-01-13 124.4 124.5 123.7 123.9

Further Resources

Package website: https://shikokuchuo.net/ichimoku/

The most recent version of the package may be found at https://github.com/shikokuchuo/ichimoku/