R/q_quote.R

Defines functions top_btm_chart watchlist_data_table q_quotes

q_quotes <- function(tickers){
q_quote <- function(ticker){
q_quote_raw <- function(ticker){




  q <- getQuote(ticker) %>%
    as_tibble() %>%
    mutate(ticker = ticker) %>%
    select(ticker,everything())

  names(q) <- c("symbol","trade_time","last","chg","pct_chg","open","high","low","volume")

  q %>%
    mutate_if(is.numeric,funs(round(.,2))) %>%
    mutate(trade_time = format(trade_time,"%m/%d %H:%M:%S")) %>%
    mutate(pct_chg = pct_chg/100)
}

q_quote_safe <- safely(q_quote_raw)


q_quote_safe(ticker)$result

}

map_dfr(tickers,q_quote)

}

watchlist_data_table <- function(quote_df){

  quote_df %>%
    arrange(desc(pct_chg)) %>%
    mutate(green_red = as.numeric(ifelse(chg>0,1,0))) %>%
    datatable(rownames = F,
              selection = 'single',
              options = list(scrollX = TRUE,
                             columnDefs = list(list(targets = 9, visible = FALSE))
              )) %>%
    formatPercentage("pct_chg",2) %>%
    formatStyle(c('pct_chg'),'green_red',
                backgroundColor = styleEqual(c(1, 0), c('rgba(50, 171, 96, 0.5)', 'rgba(219, 64, 82, 0.5)')))

}

top_btm_chart <- function(quote_df,n = 5){

  q_quotes <- quote_df %>%
    mutate(rank = min_rank(desc(pct_chg))) %>% filter(rank <= n | rank >= (max(rank,na.rm=T)-n)) %>% select(-rank)

  q_quotes %>%
    arrange(pct_chg) %>%
    mutate(color = ifelse(pct_chg>0,'rgba(50, 171, 96, 0.7)', 'rgba(219, 64, 82, 0.7)')) %>%
    mutate(symbol = as_factor(symbol)) %>%
    plot_ly(x = ~pct_chg,
            y = ~symbol,
            type = 'bar',
            orientation = 'h',
            marker = list(color = ~color)) %>%
    add_annotations(x = q_quotes$pct_chg/2,
                    y = q_quotes$symbol,
                    text = q_quotes$symbol,
                    xref = "x",
                    yref = "y",
                    showarrow = F,
                    font = list(color = '#ffffff',
                                family = 'sans serif',
                                size = 20)) %>%
    plotly::layout(xaxis = list(title = "",
                        tickformat = ".2%"),
           yaxis = list(title = "",
                        zeroline = F,
                        showline = FALSE,
                        showticklabels = FALSE,
                        showgrid = FALSE),
           showlegend = F)

}
zac-garland/equityresearch documentation built on July 30, 2020, 2:29 p.m.