Professional Documents
Culture Documents
1
2 ichimoku-package
R topics documented:
ichimoku-package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
autoplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
autostrat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
duplicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
hasStrat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
ichimoku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
iplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
is.ichimoku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
maxOver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
minOver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
mlgrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
plot.ichimoku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
print.ichimoku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
sample_ohlc_data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
strat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
stratcombine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
summary.ichimoku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
tradingDays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
trimdf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Index 21
ichimoku-package ichimoku: Visualization and Tools for Ichimoku Kinko Hyo Strategies
Description
An implementation of ’Ichimoku Kinko Hyo’, also commonly known as ’cloud charts’. Static
and interactive visualizations with tools for creating, backtesting and development of quantitative
’ichimoku’ strategies. As described in Sasaki (1996, ISBN:4925152009), the technique is a refine-
ment on candlestick charting originating from Japan, now in widespread use in technical analysis
worldwide. Translating as ’one-glance equilibrium chart’, it allows the price action and market
structure of financial securities to be determined ’at-a-glance’.
Strategy layer
ML layer
• mlgrid to generate a numeric representation of the relationship between ichimoku cloud chart
elements.
Author(s)
Charlie Gao <<charlie.gao@shikokuchuo.net>>
References
Package website: https://shikokuchuo.net/ichimoku/
The most recent version of the package may be found at https://github.com/shikokuchuo/
ichimoku/
autoplot autoplot.ichimoku
Description
Plot static Ichimoku Kinko Hyo cloud charts from ichimoku objects.
Usage
## S3 method for class 'ichimoku'
autoplot(
object,
window,
ticker,
theme = c("original", "dark", "solarized", "mono"),
gaps = FALSE,
strat = TRUE,
...
)
Arguments
object an object of class ’ichimoku’.
window (optional) a date-time window to subset the plot, in ISO-8601 compatible range
strings of the format used for ’xts’ objects, for example "2020-02-15/2020-08-
15" or "2020-02-15/", "/2020-08" or "2020-07".
ticker (optional) specify a ticker (or other text) to include in the chart heading. If not
set, the ticker saved within the ichimoku object will be used.
theme defaults to ’original’. This can also be set to ’dark’, ’solarized’ or ’mono’.
4 autostrat
gaps defaults to FALSE to remove weekend and holiday gaps. Set to TRUE for a
continuous timescale axis, but with gaps for non-trading days.
strat set to TRUE by default. If the ichimoku object contains a strategy, the periods
for which the strategy results in a position will be shaded. Set to FALSE to turn
off this behaviour.
... other arguments to be passed along.
Details
This function is an S3 method for the generic function autoplot() for class ’ichimoku’. It can be
invoked by calling autoplot(x) on an object ’x’ of class ’ichimoku’.
Value
Returns a ggplot2 object with S3 classes ’gg’ and ’ggplot’.
Further Details
Please refer to the reference vignette by running: vignette("reference",package = "ichimoku")
Examples
cloud <- ichimoku(sample_ohlc_data, ticker = "TKR")
autoplot(cloud)
autoplot(cloud, window = "2020-05-15/2020-10-30", theme = "dark")
autoplot(cloud, ticker = "TKR Co.", theme = "solarized", gaps = TRUE)
Description
Generate a list of the top performing ichimoku cloud strategies based on rule conditions of the form
’c1 > c2’ or level 2 strategies based on combined rule conditions of the form ’c1 > c2 & c3 > c4’.
Usage
autostrat(x, n = 8, dir = c("long", "short"), level = 1)
Arguments
x an ichimoku object.
n defaults to 8. Select top n number of strategies to return.
dir trade direction either ’long’ or ’short’ with a default of ’long’.
level defaults to 1. Set to 2 to also return combined strategies.
duplicate 5
Details
Ichimoku objects for each strategy are returned as a list. The cumulative log returns for all strategies
as well as the summaries for the ’n’ top strategies are saved as attributes to the list and may be
viewed by using attributes() on the returned list.
Each individual ichimoku object may be accessed via its position in the list e.g. [[1]] for the 1st
item.
Value
A list of ’n’ ichimoku objects containing strategies. The cumulative log returns for all strategies
as well as the summaries for the ’n’ top strategies are saved as attributes to the list. The strategy
summaries are printed to the console as a side effect.
Further Details
Please refer to the strategies vignette by running: vignette("strategies",package = "ichimoku")
Examples
cloud <- ichimoku(sample_ohlc_data, ticker = "TKR")
Description
Create a vector of element positions of duplicates in the output of expand.grid on 2 identical vectors.
A much faster method of creating combinations for 2 variabes vs. the combn() function from the
utils package.
Usage
duplicate(n, identical = FALSE)
Arguments
n the length of vector passed to expand.grid.
identical defaults to FALSE to not select the elements where the 2 items are identi-
cal. Set to TRUE to also include these. The output of expand.grid, subset
to remove duplicates with ’identical’ set to TRUE would be the equivalent of
utils::combn(n,2).
6 hasStrat
Value
A numeric vector.
Examples
n <- 3
expand.grid(1:n, 1:n)
expand.grid(1:n, 1:n)[-duplicate(n), ]
expand.grid(1:n, 1:n)[-duplicate(n, identical = TRUE), ]
hasStrat hasStrat
Description
A function for checking if an object contains a strategy.
Usage
hasStrat(x)
Arguments
x an object.
Details
Designed to be used by ichimoku functions that are either S3 methods for class ’ichimoku’ or after
validation that ’x’ is an ichimoku object, hence there is no check on the class of ’x’ within this
function.
Value
A logical value of TRUE if ’x’ has a ’strat’ attribute set that is not NULL, otherwise FALSE.
Examples
cloud <- ichimoku(sample_ohlc_data)
strat <- strat(cloud)
# TRUE:
hasStrat(strat)
# FALSE:
hasStrat(cloud)
ichimoku 7
ichimoku ichimoku
Description
Create an ichimoku object containing the values for all components of the Ichimoku Kinko Hyo
cloud chart, ready for plotting. The object includes a date-time index, OHLC pricing data, candle
direction, the cloud lines Tenkan-sen, Kijun-sen, Senkou span A, Senkou span B and Chikou span,
as well as values for the cloud top and base.
Usage
ichimoku(x, ...)
## Default S3 method:
ichimoku(x = NULL, ticker, periods = c(9, 26, 52), ...)
Arguments
x a data.frame or other compatible object, which includes xts, data.table, tibble,
and matrix.
... other arguments to be passed along. For instance, ’holidays’ may be passed to
the tradingDays function, used by ichimoku when calculating the future cloud.
ticker (optional) specify a ticker to identify the instrument, otherwise this is set to the
name of the input object ’x’.
periods a vector defining the length of periods used for the cloud, with a default of c(9,
26, 52). This parameter shoud not normally be changed as using other values
would be invalid in the context of traditional Ichimoku analysis.
Details
Calling an ichimoku object automatically invokes its print method, which will by default produce a
printout of the data to the console as well as a static plot of the cloud chart to the graphical device.
8 iplot
For further options, including interactive charting, use plot() on the returned ichimoku object to
pass further arguments for customising the chart.
Where an ichimoku object is passed to ichimoku(), the ichimoku object is re-calculated using the
OHLC pricing data contained within.
Value
An ichimoku object is returned with S3 classes of ’ichimoku’, ’xts’ and ’zoo’.
This object contains a date-time index, OHLC pricing data, candle direction, the computed ichimoku
cloud values, and cloud top and base values, with ticker, periods, and periodicity parameters set as
attributes.
Further Details
Please refer to the reference vignette by running: vignette("reference",package = "ichimoku")
Examples
cloud <- ichimoku(sample_ohlc_data)
ichimoku(sample_ohlc_data, ticker = "TKR", periods = c(9, 26, 52))
Description
Plot interactive Ichimoku Kinko Hyo cloud charts from ichimoku objects.
Usage
iplot(
x,
window,
ticker,
theme = c("original", "dark", "solarized", "mono"),
gaps = FALSE,
...
)
Arguments
x an object of class ’ichimoku’.
window (optional) a date-time window to subset the plot, in ISO-8601 compatible range
strings of the format used for ’xts’ objects, for example "2020-02-15/2020-08-
15" or "2020-02-15/", "/2020-08" or "2020-07".
is.ichimoku 9
ticker (optional) specify a ticker (or other text) to include in the chart heading. If not
set, the ticker saved within the ichimoku object will be used.
theme defaults to ’original’. This can also be set to ’dark’, ’solarized’ or ’mono’.
gaps defaults to FALSE to remove weekend and holiday gaps. Set to TRUE for a
continuous timescale axis, but with gaps for non-trading days.
... other arguments to be passed along.
Value
Returns a plotly object with classes ’plotly’ and ’htmlwidget’.
Further Details
Please refer to the reference vignette by running: vignette("reference",package = "ichimoku")
Examples
iplot(cloud)
iplot(cloud, window = "2020-05-15/2020-10-30", theme = "dark")
iplot(cloud, ticker = "TKR Co.", theme = "solarized", gaps = TRUE)
is.ichimoku is.ichimoku
Description
A function for checking if an object is an ichimoku object.
Usage
is.ichimoku(x)
Arguments
x an object.
Value
A logical value of TRUE if ’x’ is of class ’ichimoku’, otherwise FALSE.
10 maxOver
Examples
# TRUE:
is.ichimoku(cloud)
# FALSE:
is.ichimoku(sample_ohlc_data)
Description
Usage
maxOver(x, window)
Arguments
x a vector.
window size of the rolling window.
Details
Value
A vector of the same length as ’x’ with elements 1 to (length(window) - 1) containing NAs.
Examples
maxOver(sample_ohlc_data$close[1:10], 3L)
minOver 11
Description
Used by ichimoku to calculate the minimum over a rolling window.
Usage
minOver(x, window)
Arguments
x a vector.
window size of the rolling window.
Details
Fast implementation with no error checking or NA handling.
Value
A vector of the same length as ’x’ with elements 1 to (length(window) - 1) containing NAs.
Examples
minOver(sample_ohlc_data$close[1:10], 3L)
Description
Create a grid of ichimoku rule conditions and next period returns. The grid facilitates comparing
strategy returns or as a basis for further processing in machine learning applications. The purpose of
this function is to translate the visual representation of the relationship between cloud chart elements
into a numerical format for further analysis.
Usage
mlgrid(
x,
y = c("logret", "ret"),
dir = c("long", "short"),
type = c("boolean", "numeric"),
unique = TRUE
)
12 mlgrid
Arguments
x an ichimoku object.
y choose target variable ’logret’ (log returns) or ’ret’ (discrete returns), with a
default of ’logret’.
dir trade direction either ’long’ or ’short’ with a default of ’long’.
type either ’boolean’ or ’numeric’ with a default of ’boolean’. ’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.
unique defaults to TRUE to return only unique combinations of c1 and c2. Set to FALSE
to return both c1 > c2 and c2 > c1.
Details
The date-time index corresponds to when the condition is met at the close for that period. The return
is the single-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).
mlgrid is used by autostrat to enumerate the returns for all valid strategy combinations.
Value
A data.frame in a ’tidy’ format with one observation per row and one feature per column with the
target ’y’ as the first column.
The ’y’ parameter and trade direction are set as atrributes. To view these, use the attributes() func-
tion on the returned object.
Further Details
Examples
Description
Plot Ichimoku Kinko Hyo cloud charts from ichimoku objects.
Usage
## S3 method for class 'ichimoku'
plot(
x,
window,
ticker,
theme = c("original", "dark", "solarized", "mono"),
gaps = FALSE,
strat = TRUE,
...
)
Arguments
x an object of class ’ichimoku’.
window (optional) a date-time window to subset the plot, in ISO-8601 compatible range
strings of the format used for ’xts’ objects, for example "2020-02-15/2020-08-
15" or "2020-02-15/", "/2020-08" or "2020-07".
ticker (optional) specify a ticker (or other text) to include in the chart heading. If not
set, the ticker saved within the ichimoku object will be used.
theme defaults to ’original’. This can also be set to ’dark’, ’solarized’ or ’mono’.
gaps defaults to FALSE to remove weekend and holiday gaps. Set to TRUE for a
continuous timescale axis, but with gaps for non-trading days.
strat set to TRUE by default. If the ichimoku object contains a strategy, the periods
for which the strategy results in a position will be shaded. Set to FALSE to turn
off this behaviour.
... other arguments to be passed along.
Details
This function is an S3 method for the generic function plot() for class ’ichimoku’. It can be invoked
by calling plot(x) on an object ’x’ of class ’ichimoku’.
Value
Returns a ggplot2 object with classes ’gg’ and ’ggplot’.
14 print.ichimoku
Further Details
Examples
cloud <- ichimoku(sample_ohlc_data, ticker = "TKR")
plot(cloud)
plot(cloud, window = "2020-05-15/2020-10-30", theme = "mono")
Description
Usage
Arguments
Details
This function is an S3 method for the generic function print() for class ’ichimoku’. It can be invoked
by calling print(x) on an object ’x’ of class ’ichimoku’.
Value
Further Details
Examples
Description
Simulated prices for a hypothetical financial asset. Created for the purpose of demonstrating pack-
age functions in examples and vignettes only.
Usage
sample_ohlc_data
Format
Source
Examples
Description
Create custom ichimoku cloud strategies using the rule condition ’long (or short) while c1 > c2’.
Usage
strat(
x,
c1 = c("close", "chikou", "open", "high", "low", "tenkan", "kijun", "senkouA",
"senkouB", "cloudTop", "cloudBase"),
c2 = c("tenkan", "kijun", "senkouA", "senkouB", "cloudTop", "cloudBase", "chikou",
"close", "open", "high", "low"),
dir = c("long", "short")
)
Arguments
x an ichimoku object.
c1 column name specified as a string, with a default of ’close’.
c2 column name specified as a string, with a default of ’tenkan’.
dir trade direction either ’long’ or ’short’ with a default of ’long’.
Details
The following assumption applies to all strategies: confirmation of whether a condition is satisfied
is received at the ’close’ of a particular period, and a transaction is initiated at the next ’open’. All
transactions happen at the ’open’.
The original ichimoku object ’x’ is augmented with the following additional columns used to cal-
culate the strategy:
’cond’, a boolean vector if the rule condition is met, ’posn’, a boolean vector indicating if a position
is held, and ’txn’, a vector representing the transactions to implement the position.
logret’ is a column of log returns, ’slogret’ is a column of log returns for the strategy.
ret’ is a column of discrete returns, and ’sret’ is a column of discrete returns for the strategy.
The stategy summary is saved as an object attribute and may be accessed via the summary() func-
tion.
The periods in which the strategy results in a position is shaded by default on the ichimoku cloud
chart. To turn off this behaviour, pass the ’strat = FALSE’ argument to plot().
Value
An ichimoku object augmented with the strategy.
stratcombine 17
Further Details
Examples
cloud <- ichimoku(sample_ohlc_data, ticker = "TKR")
strat <- strat(cloud, c1 = "close", c2 = "tenkan")
summary(strat)
plot(strat)
Description
Create more complex strategies with a rule condition of the form ’c1 > c2 & c3 > c4’ by combining
existing strategies with rule conditions ’c1 > c2’ and ’c3 > c4’ respectively.
Usage
stratcombine(s1, s2)
Arguments
Details
The combined strategy ’s1 & s2’ means conditions in ’s1’ and ’s2’ have to be simulateneously met
for a trade position to be taken.
The boolean values showing whether these conditions are met are stored in the ’cond’ column.
The stategy summary may be accessed via the summary() function.
Value
Further Details
Examples
cloud <- ichimoku(sample_ohlc_data, ticker = "TKR")
strat1 <- strat(cloud, c1 = "close", c2 = "kijun")
strat2 <- strat(cloud, c1 = "cloudBase", c2 = "tenkan")
cstrat <- stratcombine(strat1, strat2)
summary(cstrat)
plot(cstrat)
Description
Custom summary method for ichimoku objects for viewing strategies.
Usage
## S3 method for class 'ichimoku'
summary(object, strat = TRUE, ...)
Arguments
object an object of class ’ichimoku’.
strat defaults to TRUE to show the strategy summary if present. Set to FALSE to
show the data summary instead.
... other arguments to be passed along.
Details
This function is an S3 method for the generic function summary() for class ’ichimoku’. It can be
invoked by calling summary(x) on an object ’x’ of class ’ichimoku’.
Value
A matrix containing the strategy summary if present, otherwise a table containing the data summary.
Further Details
Please refer to the strategies vignette by running: vignette("strategies",package = "ichimoku")
Examples
strat <- strat(ichimoku(sample_ohlc_data, ticker = "TKR"))
summary(strat)
tradingDays 19
Description
Used by ichimoku to subset a vector of dates to trading days.
Usage
tradingDays(x, holidays, ...)
Arguments
x a vector of POSIXct date objects.
holidays (optional) a vector, or function which outputs a vector, of dates defined as holi-
days. If not specified, New Year’s and Christmas day are defined as holidays by
default.
... other arguments to be passed along.
Value
A vector of logical values: TRUE if the corresponding element of ’x’ is a weekday and not a holiday,
FALSE otherwise.
Examples
dates <- seq(from = as.POSIXct("2020-01-01"), by = "1 day", length.out = 7)
tradingDays(dates)
Description
Trim NA values from a data.frame. A streamlined version of na.omit.data.frame.
Usage
trimdf(x)
Arguments
x the data.frame to trim.
20 trimdf
Value
The data.frame ’x’ with NA values removed.
Examples
data <- data.frame(c(1:4, NA), c(NA, 2:5))
trimdf(data)
Index
∗ datasets
sample_ohlc_data, 15
autoplot, 3
autostrat, 3, 4, 12
duplicate, 5
hasStrat, 6
maxOver, 10
minOver, 11
mlgrid, 3, 11
plot.ichimoku, 2, 13
print.ichimoku, 14
sample_ohlc_data, 15
strat, 2, 16
stratcombine, 2, 17
summary.ichimoku, 18
tradingDays, 7, 19
trimdf, 19
21