I am using this code in Rstudio and I am trying to implement my dataset AAPL which I have already imported into Rstudio from excel:
> library(ggplot2 )
> library(quantmo d)
> draw_candles <- function(df, title_param, alpha_param = 1){
+ df$change <- ifelse(df$Close > df$Open, "up", ifelse(df$Close < df$Open, "down", "flat"))
+
+ # originally the width of the bars was calculated by FXQuantTrader with use of
'periodicity()' , which
+ # seems to work ok only with: ‘minute’,‘hourl y’, ‘daily’,‘weekly ’, ‘monthly’,
+ # ‘quarterly’, and ‘yearly’, but can not do 1 sec bars while we want arbitrary bar size support!-)
+ # df$width <- as.numeric(peri odicity(df)[1])
+ # So let us instead find delta (seconds) between 1st and 2nd row and just
+ # use it for all other rows. We check 1st 3 rows to avoid larger "weekend gaps"
+ width_candidate s <- c(as.numeric(di fftime(df$Date[2], df$Date[1]), units = "secs"),
+ as.numeric(diff time(df$Date[3], df$Date[2]), units = "secs"),
+ as.numeric(diff time(df$Date[4], df$Date[3]), units = "secs"))
+
+ df$width_s = min(width_candi dates) # one (same) candle width (in seconds) for all the bars
+
+ # define the vector of candle colours either by name or by rgb()
+ #candle_colors = c("down" = "red", "up" = "green", "flat" = "blue")
+ candle_colors = c("down" = rgb(192,0,0,alp ha=255,maxColor Value=255), "up" = rgb(0,192,0,alp ha=255,maxColor Value=255), "flat" = rgb(0,0,192,alp ha=255,maxColor Value=255))
+
+ # Candle chart:
+ g <- ggplot(AAPL, aes(x=Date))+
+ geom_linerange( aes(ymin=Low, ymax=High, colour = change), alpha = alpha_param) + # candle whiskerss (vertical thin lines:)
+ theme_bw() +
+ labs(title=titl e_param) +
+ geom_rect(aes(x min = Date - width_s/2 * 0.9, xmax = Date + width_s/2 * 0.9, ymin = pmin(Open, Close), ymax = pmax(Open, Close), fill = change), alpha = alpha_param) +
# cabdke body
+ guides(fill = FALSE, colour = FALSE) +
+ scale_color_man ual(values = candle_colors) + # color for line
+ scale_fill_manu al(values = candle_colors) # color for candle fill
+
+ # Handle special cases: flat bar and Open == close:
+ if (any(AAPL$chang e == "flat")) g <- g + geom_segment(da ta = df[df$change == "flat",], aes(x = Date - width_s / 2 * 0.9, y = Close, yend = Close, xend = Date + width_s / 2 * 0.9, colour = change), alpha = alpha_param)
+
+ #print(g)
+ g
+ }
> print(draw_cand les(g,AAPL))
My problem is that every time I try to print out the graph, I get this error:
Error in ifelse(df$Close > df$Open, "up", ifelse(df$Close < df$Open, "down", : object 'g' not found
And I have no idea how to fix it// what I am doing wrong. Can anyone help?
> library(ggplot2 )
> library(quantmo d)
> draw_candles <- function(df, title_param, alpha_param = 1){
+ df$change <- ifelse(df$Close > df$Open, "up", ifelse(df$Close < df$Open, "down", "flat"))
+
+ # originally the width of the bars was calculated by FXQuantTrader with use of
'periodicity()' , which
+ # seems to work ok only with: ‘minute’,‘hourl y’, ‘daily’,‘weekly ’, ‘monthly’,
+ # ‘quarterly’, and ‘yearly’, but can not do 1 sec bars while we want arbitrary bar size support!-)
+ # df$width <- as.numeric(peri odicity(df)[1])
+ # So let us instead find delta (seconds) between 1st and 2nd row and just
+ # use it for all other rows. We check 1st 3 rows to avoid larger "weekend gaps"
+ width_candidate s <- c(as.numeric(di fftime(df$Date[2], df$Date[1]), units = "secs"),
+ as.numeric(diff time(df$Date[3], df$Date[2]), units = "secs"),
+ as.numeric(diff time(df$Date[4], df$Date[3]), units = "secs"))
+
+ df$width_s = min(width_candi dates) # one (same) candle width (in seconds) for all the bars
+
+ # define the vector of candle colours either by name or by rgb()
+ #candle_colors = c("down" = "red", "up" = "green", "flat" = "blue")
+ candle_colors = c("down" = rgb(192,0,0,alp ha=255,maxColor Value=255), "up" = rgb(0,192,0,alp ha=255,maxColor Value=255), "flat" = rgb(0,0,192,alp ha=255,maxColor Value=255))
+
+ # Candle chart:
+ g <- ggplot(AAPL, aes(x=Date))+
+ geom_linerange( aes(ymin=Low, ymax=High, colour = change), alpha = alpha_param) + # candle whiskerss (vertical thin lines:)
+ theme_bw() +
+ labs(title=titl e_param) +
+ geom_rect(aes(x min = Date - width_s/2 * 0.9, xmax = Date + width_s/2 * 0.9, ymin = pmin(Open, Close), ymax = pmax(Open, Close), fill = change), alpha = alpha_param) +
# cabdke body
+ guides(fill = FALSE, colour = FALSE) +
+ scale_color_man ual(values = candle_colors) + # color for line
+ scale_fill_manu al(values = candle_colors) # color for candle fill
+
+ # Handle special cases: flat bar and Open == close:
+ if (any(AAPL$chang e == "flat")) g <- g + geom_segment(da ta = df[df$change == "flat",], aes(x = Date - width_s / 2 * 0.9, y = Close, yend = Close, xend = Date + width_s / 2 * 0.9, colour = change), alpha = alpha_param)
+
+ #print(g)
+ g
+ }
> print(draw_cand les(g,AAPL))
My problem is that every time I try to print out the graph, I get this error:
Error in ifelse(df$Close > df$Open, "up", ifelse(df$Close < df$Open, "down", : object 'g' not found
And I have no idea how to fix it// what I am doing wrong. Can anyone help?