Skip to contents

Create a grid of ichimoku indicator conditions and next period returns. The grid facilitates the comparison of strategy returns and provides a basis for use in machine learning applications. Translates the visual representation of the relationship between cloud chart elements into a numerical format for further analysis.

Usage

mlgrid(
  x,
  y = c("logret", "ret", "none"),
  k = 1L,
  dir = c("long", "short"),
  type = c("boolean", "numeric", "z-score"),
  format = c("dataframe", "matrix"),
  unique = TRUE,
  expr = list()
)

Arguments

x

an ichimoku object.

y

[default 'logret'] choose target variable 'logret' (log returns), 'ret' (discrete returns), or 'none'.

k

[default 1L] number of periods time horizon over which to calculate target variable 'y'.

dir

[default 'long'] trade direction, either ‘long’ or ‘short’.

type

[default 'boolean'] choose 'boolean', 'numeric' or 'z-score'. 'boolean' creates a grid of dummy variables for ichimoku indicator conditions of the form 1 if c1 > c2, 0 otherwise. 'numeric' creates a grid of the numeric difference c1 - c2. 'z-score' standardises the numeric grid by the mean and standard deviation of each feature.

format

[default 'dataframe'] select either 'dataframe' or 'matrix' for the format of returned object.

unique

[default TRUE] to return only unique combinations of c1 and c2. Set to FALSE to return both c1 > c2 and c2 > c1.

expr

[default list()] (for advanced use only) a named list of quoted language objects or expressions, which are evaluated internally within the function, referencing intermediate objects such as: 'core', the coredata matrix of the ichimoku object, 'xlen' the number of observations, or any of the function parameters etc. Each evaluated expression must return a vector of length 'xlen' for inclusion in the grid.

Value

A data.frame or matrix in a tidy format with one observation per row and one feature per column with the target ‘y’ as the first column (unless set to ‘none’).

The ‘y’ and ‘k’ parameters, trade direction and grid type are set as attributes, with ‘means’ and ‘sdevs’ also populated for type ‘z-score’ to return the mean and standard deviation for each column. To view these, use look on the returned object.

Details

The date-time index corresponds to when the indicator condition is met at the close for that period. The return is the k-period return achieved by transacting at the immediately following opening price until the next opening price.

Only valid combinations are included. This excludes any combination involving ‘open’ as it is in effect a lagged indicator and not contemporaneous. The following trivial or highly-collinear pairs are also excluded: (high, close), (low, close), (low, high), (cloudTop, Senkou A), (cloudBase, senkou A), (cloudTop, senkouB), (cloudBase, senkouB), (cloudBase, cloudTop).

Further Details

Please refer to the strategies vignette by calling: vignette("strategies", package = "ichimoku")

See also

autostrat which uses mlgrid() to enumerate all valid return combinations.

relative which uses mlgrid() to relate the latest observed numeric representation to historical values.

Examples

cloud <- ichimoku(sample_ohlc_data, ticker = "TKR")
grid <- mlgrid(cloud, y = "ret", k = 2, dir = "short", type = "z-score")
str(grid)
#> 'data.frame':	152 obs. of  38 variables:
#>  $ y              : num  -0.00645 0.00242 -0.00321 -0.00242 -0.0016 ...
#>  $ chikou_close   : num  -1.086 -0.79 -0.79 -0.856 -0.757 ...
#>  $ chikou_high    : num  -0.964 -0.85 -0.768 -0.752 -0.719 ...
#>  $ chikou_low     : num  -1.061 -0.866 -0.85 -0.898 -0.833 ...
#>  $ chikou_tenkan  : num  -0.783 -0.67 -0.751 -0.945 -0.88 ...
#>  $ chikou_kijun   : num  -0.631 -0.514 -0.498 -0.531 -0.431 ...
#>  $ chikou_senkouA : num  -0.326 -0.218 -0.203 -0.241 -0.169 ...
#>  $ chikou_senkouB : num  -0.709 -0.58 -0.562 -0.599 -0.489 ...
#>  $ chikou_cloudT  : num  -0.345 -0.238 -0.222 -0.253 -0.161 ...
#>  $ chikou_cloudB  : num  -0.714 -0.581 -0.562 -0.609 -0.519 ...
#>  $ close_tenkan   : num  0.783 1.092 1.036 0.643 0.615 ...
#>  $ close_kijun    : num  -0.527 -0.313 -0.237 -0.298 -0.114 ...
#>  $ close_senkouA  : num  -0.724 -0.606 -0.64 -0.757 -0.673 ...
#>  $ close_senkouB  : num  -0.841 -0.711 -0.692 -0.729 -0.618 ...
#>  $ close_cloudT   : num  -0.62 -0.499 -0.534 -0.655 -0.568 ...
#>  $ close_cloudB   : num  -0.983 -0.852 -0.834 -0.871 -0.759 ...
#>  $ high_tenkan    : num  0.698 0.793 0.856 0.412 0.38 ...
#>  $ high_kijun     : num  -0.643 -0.548 -0.404 -0.468 -0.277 ...
#>  $ high_senkouA   : num  -0.784 -0.733 -0.733 -0.852 -0.767 ...
#>  $ high_senkouB   : num  -0.904 -0.847 -0.791 -0.829 -0.716 ...
#>  $ high_cloudT    : num  -0.682 -0.629 -0.629 -0.753 -0.664 ...
#>  $ high_cloudB    : num  -1.047 -0.99 -0.934 -0.971 -0.859 ...
#>  $ low_tenkan     : num  0.362 1.029 1.492 0.855 0.652 ...
#>  $ low_kijun      : num  -0.7564 -0.3619 -0.0129 -0.195 -0.1039 ...
#>  $ low_senkouA    : num  -0.846 -0.63 -0.513 -0.696 -0.663 ...
#>  $ low_senkouB    : num  -0.976 -0.736 -0.552 -0.662 -0.607 ...
#>  $ low_cloudT     : num  -0.751 -0.526 -0.405 -0.595 -0.56 ...
#>  $ low_cloudB     : num  -1.113 -0.873 -0.689 -0.799 -0.744 ...
#>  $ tenkan_kijun   : num  -1.283 -1.221 -1.077 -0.871 -0.604 ...
#>  $ tenkan_senkouA : num  -1.041 -1.014 -1.032 -1.032 -0.932 ...
#>  $ tenkan_senkouB : num  -1.145 -1.116 -1.078 -0.981 -0.855 ...
#>  $ tenkan_cloudT  : num  -0.948 -0.919 -0.938 -0.938 -0.833 ...
#>  $ tenkan_cloudB  : num  -1.29 -1.261 -1.222 -1.125 -0.999 ...
#>  $ kijun_senkouA  : num  -0.706 -0.706 -0.829 -0.965 -0.992 ...
#>  $ kijun_senkouB  : num  -0.666 -0.666 -0.702 -0.702 -0.702 ...
#>  $ kijun_cloudT   : num  -0.507 -0.507 -0.63 -0.767 -0.795 ...
#>  $ kijun_cloudB   : num  -0.909 -0.909 -0.948 -0.948 -0.948 ...
#>  $ senkouA_senkouB: num  -0.0681 -0.0681 0.0317 0.198 0.2312 ...
#>  - attr(*, "y")= chr "ret"
#>  - attr(*, "k")= int 2
#>  - attr(*, "direction")= chr "short"
#>  - attr(*, "type")= chr "z-score"
#>  - attr(*, "ticker")= chr "TKR"
#>  - attr(*, "means")= num [1:37] 1.414 0.691 2.222 1.646 2.241 ...
#>  - attr(*, "sdevs")= num [1:37] 6.09 6.11 6.15 6.19 6.01 ...

custom <- mlgrid(cloud, type = "numeric", expr = list(cd = quote(core[, "cd"])))
str(custom)
#> 'data.frame':	153 obs. of  39 variables:
#>  $ y              : num  0.007273 -0.000805 -0.001613 0.004831 -0.002413 ...
#>  $ chikou_close   : num  -5.2 -3.4 -3.4 -3.8 -3.2 ...
#>  $ chikou_high    : num  -5.2 -4.5 -4 -3.9 -3.7 ...
#>  $ chikou_low     : num  -4.3 -3.1 -3 -3.3 -2.9 ...
#>  $ chikou_tenkan  : num  -3.2 -2.5 -3 -4.2 -3.8 ...
#>  $ chikou_kijun   : num  -1.55 -0.85 -0.75 -0.95 -0.35 ...
#>  $ chikou_senkouA : num  1.3 2 2.1 1.85 2.33 ...
#>  $ chikou_senkouB : num  0.4 1.1 1.2 1 1.6 ...
#>  $ chikou_cloudT  : num  0.4 1.1 1.2 1 1.6 ...
#>  $ chikou_cloudB  : num  1.3 2 2.1 1.85 2.33 ...
#>  $ close_tenkan   : num  1.9 2.45 2.35 1.65 1.6 ...
#>  $ close_kijun    : num  -0.5 0.2 0.45 0.25 0.85 ...
#>  $ close_senkouA  : num  -2.38 -1.67 -1.88 -2.57 -2.08 ...
#>  $ close_senkouB  : num  -1.55 -0.85 -0.75 -0.95 -0.35 ...
#>  $ close_cloudT   : num  -2.38 -1.67 -1.88 -2.57 -2.08 ...
#>  $ close_cloudB   : num  -1.55 -0.85 -0.75 -0.95 -0.35 ...
#>  $ high_tenkan    : num  2.3 2.45 2.55 1.85 1.8 ...
#>  $ high_kijun     : num  -0.1 0.2 0.65 0.45 1.05 ...
#>  $ high_senkouA   : num  -1.97 -1.67 -1.67 -2.38 -1.88 ...
#>  $ high_senkouB   : num  -1.15 -0.85 -0.55 -0.75 -0.15 ...
#>  $ high_cloudT    : num  -1.97 -1.67 -1.67 -2.38 -1.88 ...
#>  $ high_cloudB    : num  -1.15 -0.85 -0.55 -0.75 -0.15 ...
#>  $ low_tenkan     : num  0.3 1.45 2.25 1.15 0.8 ...
#>  $ low_kijun      : num  -2.1 -0.8 0.35 -0.25 0.05 ...
#>  $ low_senkouA    : num  -3.97 -2.67 -1.97 -3.07 -2.88 ...
#>  $ low_senkouB    : num  -3.15 -1.85 -0.85 -1.45 -1.15 ...
#>  $ low_cloudT     : num  -3.97 -2.67 -1.97 -3.07 -2.88 ...
#>  $ low_cloudB     : num  -3.15 -1.85 -0.85 -1.45 -1.15 ...
#>  $ tenkan_kijun   : num  -2.4 -2.25 -1.9 -1.4 -0.75 ...
#>  $ tenkan_senkouA : num  -4.27 -4.12 -4.22 -4.22 -3.67 ...
#>  $ tenkan_senkouB : num  -3.45 -3.3 -3.1 -2.6 -1.95 ...
#>  $ tenkan_cloudT  : num  -4.27 -4.12 -4.22 -4.22 -3.67 ...
#>  $ tenkan_cloudB  : num  -3.45 -3.3 -3.1 -2.6 -1.95 ...
#>  $ kijun_senkouA  : num  -1.88 -1.88 -2.33 -2.83 -2.93 ...
#>  $ kijun_senkouB  : num  -1.05 -1.05 -1.2 -1.2 -1.2 ...
#>  $ kijun_cloudT   : num  -1.88 -1.88 -2.33 -2.83 -2.93 ...
#>  $ kijun_cloudB   : num  -1.05 -1.05 -1.2 -1.2 -1.2 ...
#>  $ senkouA_senkouB: num  0.825 0.825 1.125 1.625 1.725 ...
#>  $ cd             : num  1 1 -1 -1 1 -1 1 1 1 1 ...
#>  - attr(*, "y")= chr "logret"
#>  - attr(*, "k")= int 1
#>  - attr(*, "direction")= chr "long"
#>  - attr(*, "type")= chr "numeric"
#>  - attr(*, "ticker")= chr "TKR"
#>  - attr(*, "means")= logi NA
#>  - attr(*, "sdevs")= logi NA