Skip to content

Commit

Permalink
Updated charting
Browse files Browse the repository at this point in the history
Added examples, and refined the conversion between DF and XTS
  • Loading branch information
serkor1 committed Oct 25, 2023
1 parent c9d2b00 commit 8d748cc
Show file tree
Hide file tree
Showing 13 changed files with 393 additions and 155 deletions.
4 changes: 1 addition & 3 deletions R/ATOMUSDT.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
#' This xts object is primarily for the unit
#' tests, and serves no other purpose than this.
#'
#' A xts object with daily OHLC of USDT denominated ATOM with 28 rows and 7 columns, from 2023-01-01 to 2023-01-07:
#' A xts object with 15m OHLC of USDT denominated ATOM with 97 rows and 4 columns, from 2023-01-01 to 2023-01-02:
#' \describe{
#' \item{Open}{Opening price}
#' \item{High}{Highest price}
#' \item{Low}{Lowest price}
#' \item{Close}{Closing price}
#' \item{market}{Determines wether its Futures (0) or Spot (1) markets.}
#' \item{exchange}{Determines the source of the data, ie. the excange. Binance (1), Kucoin (2), Kraken (3)}
#' }
"ATOMUSDT"

Expand Down
115 changes: 62 additions & 53 deletions R/chart.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ kline <- function(
# 2) Create candlestick
# plot
plot <- plotly::plot_ly(
data = quoteDF,showlegend = FALSE,
data = quoteDF,
showlegend = FALSE,
name = attributes(quote)$interval,
yaxis = 'y',
x = ~Index,
Expand Down Expand Up @@ -68,7 +69,7 @@ kline <- function(
main = plot
)

attributes(plot_list)$quote <- quoteDF
attributes(plot_list)$quote <- quote
attributes(plot_list)$deficiency <- deficiency

return(
Expand Down Expand Up @@ -96,6 +97,7 @@ ohlc <- function(
# plot
plot <- plotly::plot_ly(
data = quoteDF,
showlegend = FALSE,
name = attributes(quote)$interval,
yaxis = 'y',
x = ~Index,
Expand Down Expand Up @@ -129,7 +131,7 @@ ohlc <- function(
main = plot
)

attributes(plot_list)$quote <- quoteDF
attributes(plot_list)$quote <- quote
attributes(plot_list)$deficiency <- deficiency

return(
Expand All @@ -154,73 +156,80 @@ chart <- function(
chart,
slider = TRUE
) {
suppressWarnings(
{
# 0) calculate number of
# charts
has_subplot <- length(chart) > 1

# 0) calculate number of
# charts
has_subplot <- length(chart) > 1
if (has_subplot) {

if (has_subplot) {
heights <- c(
0.5,
rep(
x = (1-0.5)/ (length(chart) - 1),
length.out = length(chart) - 1
)
)

heights <- c(
0.5,
rep(
x = (1-0.5)/ (length(chart) - 1),
length.out = length(chart) - 1
)
)
} else {

} else {
heights <- 1

heights <- 1

}

}

quoteDF <- attributes(chart)$quote
quote <- attributes(chart)$quote

quoteDF <- attributes(chart)$quote
quote <- attributes(chart)$quote



# 1) Main Chart
chart <- plotly::subplot(
chart,
nrows = length(chart),
shareX = TRUE,
titleY = TRUE,
heights = heights
)

# 1) Main Chart
chart <- plotly::subplot(
chart,
nrows = length(chart),
shareX = TRUE,
titleY = TRUE,
heights = heights
)

chart <- chart %>% plotly::layout(
yaxis = list(
title = 'Price'
),
xaxis = list(
rangeslider = list(visible = slider)
),
showlegend = TRUE,
legend = list(orientation = 'h', x = 0, y = 1),
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
) %>% plotly::add_annotations(
x= 0,
y= 1,
xref = "paper",
yref = "paper",
text = paste(
'<b>Ticker:</b>',
attributes(quote)$tickerInfo$ticker,
'<b>Interval:</b>', attributes(quote)$tickerInfo$interval, paste0('(', attributes(quote)$tickerInfo$market,')'),
'<b>Exchange:</b>', attributes(quote)$tickerInfo$source),
showarrow = FALSE,
font = list(
size = 18
)
chart <- chart %>% plotly::layout(
yaxis = list(
title = 'Price'
),
xaxis = list(
rangeslider = list(
visible = slider
)
),
showlegend = TRUE,
legend = list(orientation = 'h', x = 0, y = 1),
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
) %>% plotly::add_annotations(
x= 0,
y= 1,
xref = "paper",
yref = "paper",
text = paste(
'<b>Ticker:</b>',
attributes(quote)$tickerInfo$ticker,
'<b>Interval:</b>', attributes(quote)$tickerInfo$interval, paste0('(', attributes(quote)$tickerInfo$market,')'),
'<b>Exchange:</b>', attributes(quote)$tickerInfo$source),
showarrow = FALSE,
font = list(
size = 18
)

)

return(chart)
}
)

return(chart)
}


Expand Down
Loading

0 comments on commit 8d748cc

Please sign in to comment.