step_bbands: Extract Bollinger Bands features

Description Usage Arguments Details Value Examples

View source: R/step_bbands.R

Description

step_bbands creates a specification of a recipe step that will extract Bollinger Bands features from an asset price historical data.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
step_bbands(recipe, ..., ma_fun = TTR::SMA, n = 20, sd_mult = 2,
  weights = NULL, ma_options = list(), state = FALSE,
  previous = TRUE, thresholds = bbands_thresholds(),
  prefix = "bbands", h = NULL, l = NULL, c = NULL, type = NULL,
  role = "predictor", trained = FALSE, skip = FALSE,
  id = rand_id("bbands"))

bbands_thresholds(high = 1, medhigh = 0.75, medlow = 0.25, low = 0)

## S3 method for class 'step_bbands'
tidy(x, info = "terms", ...)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

Either three or one (unquoted) column name(s). If three columns are given, it will represent the "high", "low", and "close" prices, respectively. Otherwise, if only one column name is given, it will treated as "close" price.

ma_fun

A function to extract moving average, or a character vector of length one which specify a moving average function. Defaults to TTR::SMA.

n

A numeric vector of length one which specify the moving average window.

sd_mult

A numeric vector of length one which specify standard deviation multiplier. The default is 2.

weights

A character vector of length one that specify a column name, or a numeric vector for ma_fun that has wts or volume argument. See details for more information.

ma_options

A list of additional argument(s) that would be passed to ma_fun function.

state

An option to specify whether to return the current states of the Bollinger Bands. Defaults to FALSE.

previous

An option to specify whether to return the summary of previous states. Defaults to TRUE and only works if state = TRUE.

thresholds

A list of threshold that would be used as state determination. See details for further information.

prefix

A character vector of length one that would be used as a prefix to the created columns.

h

A container for the names of "high". Leave to NULL as it will be populated by prep() function.

l

A container for the names of "low". Leave to NULL as it will be populated by prep() function.

c

A container for the names of "close". Leave to NULL as it will be populated by prep() function.

type

A container for the final series type that would be used ("hlc" or "c"). Leave to NULL as it will be populated by prep() function.

role

For model terms created by this step, what analysis role should they be assigned? By default, the function assumes that the created columns will be used as "predictors" in a model.

trained

A logical to indicate if the necessary informations for preprocessing have been estimated.

skip

A logical. Should the step be skipped when the recipe is baked by bake()? While all operations are baked when prep() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations

id

A character string that is unique to this step to identify it.

high

Threshold for "high" state; see details sections.

medhigh

Threshold for "medhigh" state; see details sections.

medlow

Threshold for "medlow" state; see details sections.

low

Threshold for "low" state; see details sections.

x

A step_bbands object.

info

Options for tidy() method; whether to return tidied information for used "terms" or "params"

Details

The output from this step are several new columns which contains the extracted Bollinger Bands features.

For basic output, this step will produces:

If state argument is TRUE, it will also produces:

These states are determined using four different threshold, which listed in thresholds arguments. These are the default threshold values:

Note: that the rest values would be categorized as "medium".

Those default values are produced from bbands_thresholds() helper functions; any modification to the threshold could be made through this helper functions. See examples for some demonstrations.

Additionally, if previous argument is TRUE, it will also provides some summary regarding previous Bollinger Bands states:

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# import libs
library(quantrecipes)

# basic usage
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_bbands(high, low, close) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

# using state argument
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_bbands(high, low, close, state = TRUE) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

# modify the threshold
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_bbands(high, low, close, state = TRUE, thresholds = bbands_thresholds(
    high = 0.9, medhigh = 0.65, medlow = 0.35, low = 0.1
  )) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

bagasbgy/quantrecipes documentation built on Dec. 25, 2019, 7:54 a.m.