Quantmod Heikin-Ashi plotting unavailable
Recently I have been working with the quantmod package of R. The idea is to use shiny package to design the user interface of the web app while using quantmod functions to plot graphs of stock data for users.
Nonetheless, from the documentation of source code, there seems to be no pre-written functions (such as chartSeries) for plotting Heikin-Ashi graphs.
Is there alternative solution to plot Heikin-Ashi graphs without building the plotting function from scratch.
Any help is appreciated.
r plot shiny package quantmod
add a comment |
Recently I have been working with the quantmod package of R. The idea is to use shiny package to design the user interface of the web app while using quantmod functions to plot graphs of stock data for users.
Nonetheless, from the documentation of source code, there seems to be no pre-written functions (such as chartSeries) for plotting Heikin-Ashi graphs.
Is there alternative solution to plot Heikin-Ashi graphs without building the plotting function from scratch.
Any help is appreciated.
r plot shiny package quantmod
add a comment |
Recently I have been working with the quantmod package of R. The idea is to use shiny package to design the user interface of the web app while using quantmod functions to plot graphs of stock data for users.
Nonetheless, from the documentation of source code, there seems to be no pre-written functions (such as chartSeries) for plotting Heikin-Ashi graphs.
Is there alternative solution to plot Heikin-Ashi graphs without building the plotting function from scratch.
Any help is appreciated.
r plot shiny package quantmod
Recently I have been working with the quantmod package of R. The idea is to use shiny package to design the user interface of the web app while using quantmod functions to plot graphs of stock data for users.
Nonetheless, from the documentation of source code, there seems to be no pre-written functions (such as chartSeries) for plotting Heikin-Ashi graphs.
Is there alternative solution to plot Heikin-Ashi graphs without building the plotting function from scratch.
Any help is appreciated.
r plot shiny package quantmod
r plot shiny package quantmod
asked Dec 27 '18 at 7:08
Mr.BensonMr.Benson
1244
1244
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In some legacy / not working code of quantmod there is some Heikin-Ashi functionality hidden. But the formula used is incorrect and can produce errors. But a Heikin-Ashi chart is just a candlestick chart but with different open high low and close numbers. But these are just based on the standard ohlc data. Knowing the formula you can calculate the numbers yourself and pass these on to a candlestick chart.
I created this function in my own package on github. But if you use this function you are set to go. I tested it on chartSeries and chart_Series from quantmod, rtsplot from rtsplot, and geom_candlestick from tidyquant to see if all of them work correctly with the data. Comparing with broker data and platforms that show Heikin-Ashi charts the results are the same.
Basically:
library(quantmod)
ADM <- getSymbols("ADM", from = "2018-10-01", auto.assign = FALSE)
ha_ADM <- heikin_ashi(AMD)
chartSeries(ha_ADM) # or chart_Series or rtsplot or geom_candlestick
heikin_ashi function:
heikin_ashi <- function(data) {
if(!quantmod::is.OHLC(data)) stop("data must contain OHLC columns")
heikin_close <- xts::xts(Matrix::rowMeans(quantmod::OHLC(data)), order.by = index(data))
heikin_open <- quantmod::Op(data)
# need a loop: heiki ashi open is dependent on the previous value
for(i in 2:nrow(data)) {
heikin_open[i] <- (heikin_open[i-1] + heikin_close[i-1]) / 2
}
heikin_high <- xts::xts(apply(cbind(quantmod::Hi(data), heikin_open, heikin_close), 1, max), order.by = index(data))
heikin_low <- xts::xts(apply(cbind(quantmod::Lo(data), heikin_open, heikin_close), 1, min), order.by = index(data))
out <- merge(heikin_open, heikin_high, heikin_low, heikin_close)
out <- setNames(out, c("Open", "High", "Low", "Close"))
}
add a comment |
An Rcpp version if you need the speed. This is the type of scenario where Rcpp really shines:
Rcpp::cppFunction('NumericMatrix RawHeikinAshi(NumericMatrix x, CharacterVector n) {
// assumes OHLC matrix input
int nrow = x.nrow(), ncol = 4, Op=0, Hi=1, Lo=2, Cl=3;
NumericMatrix ha(nrow,ncol);
for (int i = 0; i < nrow; i++) {
ha(i, Cl) = (x(i,Op) + x(i,Hi) + x(i,Lo) + x(i,Cl)) / 4.0;
ha(i, Op) = (i > 0) ? ((ha(i - 1, Op) + ha(i - 1, Cl)) / 2.0) : x(i, Op);
ha(i, Hi) = std::max(x(i, Hi), std::max(ha(i, Op), ha(i, Cl)));
ha(i, Lo) = std::min(x(i, Lo), std::min(ha(i, Op), ha(i, Cl)));
}
colnames(ha) = n;
return ha;
}')
HAOHLC <- function(x) {
x <- OHLC(try.xts(x))
r <- RawHeikinAshi(x, paste0("ha.", colnames(x)))
return(reclass(r, x))
}
Benchmarks.
> p <- getSymbols("SPY")
> microbenchmark(HAOHLC(p), heikin_ashi(p), times = 100L, unit = "ms")
Unit: milliseconds
expr min lq mean median uq max neval
HAOHLC(p) 0.36409 0.4275205 0.5198086 0.502614 0.552392 1.378134 100
heikin_ashi(p) 563.33925 582.6144955 609.0082902 591.338550 620.179235 802.885348 100
Nice. Upvoted. I just heard you say that this is coming soon as an Rcpp Gallery post, right? ;-)
– Dirk Eddelbuettel
Jan 15 at 15:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53941102%2fquantmod-heikin-ashi-plotting-unavailable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In some legacy / not working code of quantmod there is some Heikin-Ashi functionality hidden. But the formula used is incorrect and can produce errors. But a Heikin-Ashi chart is just a candlestick chart but with different open high low and close numbers. But these are just based on the standard ohlc data. Knowing the formula you can calculate the numbers yourself and pass these on to a candlestick chart.
I created this function in my own package on github. But if you use this function you are set to go. I tested it on chartSeries and chart_Series from quantmod, rtsplot from rtsplot, and geom_candlestick from tidyquant to see if all of them work correctly with the data. Comparing with broker data and platforms that show Heikin-Ashi charts the results are the same.
Basically:
library(quantmod)
ADM <- getSymbols("ADM", from = "2018-10-01", auto.assign = FALSE)
ha_ADM <- heikin_ashi(AMD)
chartSeries(ha_ADM) # or chart_Series or rtsplot or geom_candlestick
heikin_ashi function:
heikin_ashi <- function(data) {
if(!quantmod::is.OHLC(data)) stop("data must contain OHLC columns")
heikin_close <- xts::xts(Matrix::rowMeans(quantmod::OHLC(data)), order.by = index(data))
heikin_open <- quantmod::Op(data)
# need a loop: heiki ashi open is dependent on the previous value
for(i in 2:nrow(data)) {
heikin_open[i] <- (heikin_open[i-1] + heikin_close[i-1]) / 2
}
heikin_high <- xts::xts(apply(cbind(quantmod::Hi(data), heikin_open, heikin_close), 1, max), order.by = index(data))
heikin_low <- xts::xts(apply(cbind(quantmod::Lo(data), heikin_open, heikin_close), 1, min), order.by = index(data))
out <- merge(heikin_open, heikin_high, heikin_low, heikin_close)
out <- setNames(out, c("Open", "High", "Low", "Close"))
}
add a comment |
In some legacy / not working code of quantmod there is some Heikin-Ashi functionality hidden. But the formula used is incorrect and can produce errors. But a Heikin-Ashi chart is just a candlestick chart but with different open high low and close numbers. But these are just based on the standard ohlc data. Knowing the formula you can calculate the numbers yourself and pass these on to a candlestick chart.
I created this function in my own package on github. But if you use this function you are set to go. I tested it on chartSeries and chart_Series from quantmod, rtsplot from rtsplot, and geom_candlestick from tidyquant to see if all of them work correctly with the data. Comparing with broker data and platforms that show Heikin-Ashi charts the results are the same.
Basically:
library(quantmod)
ADM <- getSymbols("ADM", from = "2018-10-01", auto.assign = FALSE)
ha_ADM <- heikin_ashi(AMD)
chartSeries(ha_ADM) # or chart_Series or rtsplot or geom_candlestick
heikin_ashi function:
heikin_ashi <- function(data) {
if(!quantmod::is.OHLC(data)) stop("data must contain OHLC columns")
heikin_close <- xts::xts(Matrix::rowMeans(quantmod::OHLC(data)), order.by = index(data))
heikin_open <- quantmod::Op(data)
# need a loop: heiki ashi open is dependent on the previous value
for(i in 2:nrow(data)) {
heikin_open[i] <- (heikin_open[i-1] + heikin_close[i-1]) / 2
}
heikin_high <- xts::xts(apply(cbind(quantmod::Hi(data), heikin_open, heikin_close), 1, max), order.by = index(data))
heikin_low <- xts::xts(apply(cbind(quantmod::Lo(data), heikin_open, heikin_close), 1, min), order.by = index(data))
out <- merge(heikin_open, heikin_high, heikin_low, heikin_close)
out <- setNames(out, c("Open", "High", "Low", "Close"))
}
add a comment |
In some legacy / not working code of quantmod there is some Heikin-Ashi functionality hidden. But the formula used is incorrect and can produce errors. But a Heikin-Ashi chart is just a candlestick chart but with different open high low and close numbers. But these are just based on the standard ohlc data. Knowing the formula you can calculate the numbers yourself and pass these on to a candlestick chart.
I created this function in my own package on github. But if you use this function you are set to go. I tested it on chartSeries and chart_Series from quantmod, rtsplot from rtsplot, and geom_candlestick from tidyquant to see if all of them work correctly with the data. Comparing with broker data and platforms that show Heikin-Ashi charts the results are the same.
Basically:
library(quantmod)
ADM <- getSymbols("ADM", from = "2018-10-01", auto.assign = FALSE)
ha_ADM <- heikin_ashi(AMD)
chartSeries(ha_ADM) # or chart_Series or rtsplot or geom_candlestick
heikin_ashi function:
heikin_ashi <- function(data) {
if(!quantmod::is.OHLC(data)) stop("data must contain OHLC columns")
heikin_close <- xts::xts(Matrix::rowMeans(quantmod::OHLC(data)), order.by = index(data))
heikin_open <- quantmod::Op(data)
# need a loop: heiki ashi open is dependent on the previous value
for(i in 2:nrow(data)) {
heikin_open[i] <- (heikin_open[i-1] + heikin_close[i-1]) / 2
}
heikin_high <- xts::xts(apply(cbind(quantmod::Hi(data), heikin_open, heikin_close), 1, max), order.by = index(data))
heikin_low <- xts::xts(apply(cbind(quantmod::Lo(data), heikin_open, heikin_close), 1, min), order.by = index(data))
out <- merge(heikin_open, heikin_high, heikin_low, heikin_close)
out <- setNames(out, c("Open", "High", "Low", "Close"))
}
In some legacy / not working code of quantmod there is some Heikin-Ashi functionality hidden. But the formula used is incorrect and can produce errors. But a Heikin-Ashi chart is just a candlestick chart but with different open high low and close numbers. But these are just based on the standard ohlc data. Knowing the formula you can calculate the numbers yourself and pass these on to a candlestick chart.
I created this function in my own package on github. But if you use this function you are set to go. I tested it on chartSeries and chart_Series from quantmod, rtsplot from rtsplot, and geom_candlestick from tidyquant to see if all of them work correctly with the data. Comparing with broker data and platforms that show Heikin-Ashi charts the results are the same.
Basically:
library(quantmod)
ADM <- getSymbols("ADM", from = "2018-10-01", auto.assign = FALSE)
ha_ADM <- heikin_ashi(AMD)
chartSeries(ha_ADM) # or chart_Series or rtsplot or geom_candlestick
heikin_ashi function:
heikin_ashi <- function(data) {
if(!quantmod::is.OHLC(data)) stop("data must contain OHLC columns")
heikin_close <- xts::xts(Matrix::rowMeans(quantmod::OHLC(data)), order.by = index(data))
heikin_open <- quantmod::Op(data)
# need a loop: heiki ashi open is dependent on the previous value
for(i in 2:nrow(data)) {
heikin_open[i] <- (heikin_open[i-1] + heikin_close[i-1]) / 2
}
heikin_high <- xts::xts(apply(cbind(quantmod::Hi(data), heikin_open, heikin_close), 1, max), order.by = index(data))
heikin_low <- xts::xts(apply(cbind(quantmod::Lo(data), heikin_open, heikin_close), 1, min), order.by = index(data))
out <- merge(heikin_open, heikin_high, heikin_low, heikin_close)
out <- setNames(out, c("Open", "High", "Low", "Close"))
}
answered Dec 29 '18 at 16:02
phiverphiver
13.1k92734
13.1k92734
add a comment |
add a comment |
An Rcpp version if you need the speed. This is the type of scenario where Rcpp really shines:
Rcpp::cppFunction('NumericMatrix RawHeikinAshi(NumericMatrix x, CharacterVector n) {
// assumes OHLC matrix input
int nrow = x.nrow(), ncol = 4, Op=0, Hi=1, Lo=2, Cl=3;
NumericMatrix ha(nrow,ncol);
for (int i = 0; i < nrow; i++) {
ha(i, Cl) = (x(i,Op) + x(i,Hi) + x(i,Lo) + x(i,Cl)) / 4.0;
ha(i, Op) = (i > 0) ? ((ha(i - 1, Op) + ha(i - 1, Cl)) / 2.0) : x(i, Op);
ha(i, Hi) = std::max(x(i, Hi), std::max(ha(i, Op), ha(i, Cl)));
ha(i, Lo) = std::min(x(i, Lo), std::min(ha(i, Op), ha(i, Cl)));
}
colnames(ha) = n;
return ha;
}')
HAOHLC <- function(x) {
x <- OHLC(try.xts(x))
r <- RawHeikinAshi(x, paste0("ha.", colnames(x)))
return(reclass(r, x))
}
Benchmarks.
> p <- getSymbols("SPY")
> microbenchmark(HAOHLC(p), heikin_ashi(p), times = 100L, unit = "ms")
Unit: milliseconds
expr min lq mean median uq max neval
HAOHLC(p) 0.36409 0.4275205 0.5198086 0.502614 0.552392 1.378134 100
heikin_ashi(p) 563.33925 582.6144955 609.0082902 591.338550 620.179235 802.885348 100
Nice. Upvoted. I just heard you say that this is coming soon as an Rcpp Gallery post, right? ;-)
– Dirk Eddelbuettel
Jan 15 at 15:00
add a comment |
An Rcpp version if you need the speed. This is the type of scenario where Rcpp really shines:
Rcpp::cppFunction('NumericMatrix RawHeikinAshi(NumericMatrix x, CharacterVector n) {
// assumes OHLC matrix input
int nrow = x.nrow(), ncol = 4, Op=0, Hi=1, Lo=2, Cl=3;
NumericMatrix ha(nrow,ncol);
for (int i = 0; i < nrow; i++) {
ha(i, Cl) = (x(i,Op) + x(i,Hi) + x(i,Lo) + x(i,Cl)) / 4.0;
ha(i, Op) = (i > 0) ? ((ha(i - 1, Op) + ha(i - 1, Cl)) / 2.0) : x(i, Op);
ha(i, Hi) = std::max(x(i, Hi), std::max(ha(i, Op), ha(i, Cl)));
ha(i, Lo) = std::min(x(i, Lo), std::min(ha(i, Op), ha(i, Cl)));
}
colnames(ha) = n;
return ha;
}')
HAOHLC <- function(x) {
x <- OHLC(try.xts(x))
r <- RawHeikinAshi(x, paste0("ha.", colnames(x)))
return(reclass(r, x))
}
Benchmarks.
> p <- getSymbols("SPY")
> microbenchmark(HAOHLC(p), heikin_ashi(p), times = 100L, unit = "ms")
Unit: milliseconds
expr min lq mean median uq max neval
HAOHLC(p) 0.36409 0.4275205 0.5198086 0.502614 0.552392 1.378134 100
heikin_ashi(p) 563.33925 582.6144955 609.0082902 591.338550 620.179235 802.885348 100
Nice. Upvoted. I just heard you say that this is coming soon as an Rcpp Gallery post, right? ;-)
– Dirk Eddelbuettel
Jan 15 at 15:00
add a comment |
An Rcpp version if you need the speed. This is the type of scenario where Rcpp really shines:
Rcpp::cppFunction('NumericMatrix RawHeikinAshi(NumericMatrix x, CharacterVector n) {
// assumes OHLC matrix input
int nrow = x.nrow(), ncol = 4, Op=0, Hi=1, Lo=2, Cl=3;
NumericMatrix ha(nrow,ncol);
for (int i = 0; i < nrow; i++) {
ha(i, Cl) = (x(i,Op) + x(i,Hi) + x(i,Lo) + x(i,Cl)) / 4.0;
ha(i, Op) = (i > 0) ? ((ha(i - 1, Op) + ha(i - 1, Cl)) / 2.0) : x(i, Op);
ha(i, Hi) = std::max(x(i, Hi), std::max(ha(i, Op), ha(i, Cl)));
ha(i, Lo) = std::min(x(i, Lo), std::min(ha(i, Op), ha(i, Cl)));
}
colnames(ha) = n;
return ha;
}')
HAOHLC <- function(x) {
x <- OHLC(try.xts(x))
r <- RawHeikinAshi(x, paste0("ha.", colnames(x)))
return(reclass(r, x))
}
Benchmarks.
> p <- getSymbols("SPY")
> microbenchmark(HAOHLC(p), heikin_ashi(p), times = 100L, unit = "ms")
Unit: milliseconds
expr min lq mean median uq max neval
HAOHLC(p) 0.36409 0.4275205 0.5198086 0.502614 0.552392 1.378134 100
heikin_ashi(p) 563.33925 582.6144955 609.0082902 591.338550 620.179235 802.885348 100
An Rcpp version if you need the speed. This is the type of scenario where Rcpp really shines:
Rcpp::cppFunction('NumericMatrix RawHeikinAshi(NumericMatrix x, CharacterVector n) {
// assumes OHLC matrix input
int nrow = x.nrow(), ncol = 4, Op=0, Hi=1, Lo=2, Cl=3;
NumericMatrix ha(nrow,ncol);
for (int i = 0; i < nrow; i++) {
ha(i, Cl) = (x(i,Op) + x(i,Hi) + x(i,Lo) + x(i,Cl)) / 4.0;
ha(i, Op) = (i > 0) ? ((ha(i - 1, Op) + ha(i - 1, Cl)) / 2.0) : x(i, Op);
ha(i, Hi) = std::max(x(i, Hi), std::max(ha(i, Op), ha(i, Cl)));
ha(i, Lo) = std::min(x(i, Lo), std::min(ha(i, Op), ha(i, Cl)));
}
colnames(ha) = n;
return ha;
}')
HAOHLC <- function(x) {
x <- OHLC(try.xts(x))
r <- RawHeikinAshi(x, paste0("ha.", colnames(x)))
return(reclass(r, x))
}
Benchmarks.
> p <- getSymbols("SPY")
> microbenchmark(HAOHLC(p), heikin_ashi(p), times = 100L, unit = "ms")
Unit: milliseconds
expr min lq mean median uq max neval
HAOHLC(p) 0.36409 0.4275205 0.5198086 0.502614 0.552392 1.378134 100
heikin_ashi(p) 563.33925 582.6144955 609.0082902 591.338550 620.179235 802.885348 100
edited Jan 15 at 23:41
answered Jan 15 at 14:57
EthanEthan
1489
1489
Nice. Upvoted. I just heard you say that this is coming soon as an Rcpp Gallery post, right? ;-)
– Dirk Eddelbuettel
Jan 15 at 15:00
add a comment |
Nice. Upvoted. I just heard you say that this is coming soon as an Rcpp Gallery post, right? ;-)
– Dirk Eddelbuettel
Jan 15 at 15:00
Nice. Upvoted. I just heard you say that this is coming soon as an Rcpp Gallery post, right? ;-)
– Dirk Eddelbuettel
Jan 15 at 15:00
Nice. Upvoted. I just heard you say that this is coming soon as an Rcpp Gallery post, right? ;-)
– Dirk Eddelbuettel
Jan 15 at 15:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53941102%2fquantmod-heikin-ashi-plotting-unavailable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown