Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-139): get_positions_options: Column updated_at doesn't exist #140

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
---
title: "NEWS"

# RobinHood 1.6.2

## Bug Fixes
- [(GH-139)](https://github.com/JestonBlu/RobinHood/issues/139) `get_positions_options()`, `get_contracts()`: fixed api change (@fouslim, @SwingBotScripts)


-------------------------------------------------------------------------------

# RobinHood 1.6.1

## New Features
Expand Down
4 changes: 2 additions & 2 deletions R/api_contracts.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
api_contracts <- function(RH, chain_symbol, type) {

# URL and token
url <- paste(api_endpoints(endpoint = "option_instruments"),
url <- paste(RobinHood::api_endpoints(endpoint = "option_instruments"),
"?state=active",
"&type=", type,
"&chain_symbol=", chain_symbol, sep = "")
Expand All @@ -22,7 +22,7 @@ api_contracts <- function(RH, chain_symbol, type) {
"Authorization" = token))

# format return
dta <- mod_json(dta, "fromJSON")
dta <- RobinHood::mod_json(dta, "fromJSON")
dta <- as.data.frame(dta$results)

# If returns no rows, no options exist
Expand Down
8 changes: 4 additions & 4 deletions R/api_instruments_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @param expiration_date (string) expiration date ("YYYY-MM-DD")
#' @import httr magrittr
#' @export
api_instruments_options <- function(RH, method = "url", option_instrument_url = NULL,
api_instruments_options <- function(RH, method = "url", option_instrument_url = NULL, # nolint
chain_symbol = NULL, type = NULL, state = NULL,
strike_price = NULL, tradability = NULL,
expiration_date = NULL) {
Expand All @@ -31,7 +31,7 @@ api_instruments_options <- function(RH, method = "url", option_instrument_url =
"Authorization" = token))

# Format return
dta <- mod_json(dta, "fromJSON")
dta <- RobinHood::mod_json(dta, "fromJSON")
dta <- as.data.frame(dta)

# Format output
Expand Down Expand Up @@ -64,7 +64,7 @@ api_instruments_options <- function(RH, method = "url", option_instrument_url =
"Authorization" = token))

# Format return
dta <- mod_json(dta, "fromJSON")
dta <- RobinHood::mod_json(dta, "fromJSON")
output <- as.data.frame(dta$results)

output <- cbind(output[, names(output) != "min_ticks"],
Expand All @@ -83,7 +83,7 @@ api_instruments_options <- function(RH, method = "url", option_instrument_url =
"Authorization" = token))

# Format return
dta <- mod_json(dta, "fromJSON")
dta <- RobinHood::mod_json(dta, "fromJSON")

dta2 <- dta$results

Expand Down
22 changes: 13 additions & 9 deletions R/api_marketdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ api_marketdata <- function(RH, instrument, type = "instrument_id") {

# Reformat columns
dta <- dta %>%
dplyr::mutate_at(c("adjusted_mark_price", "ask_price", "bid_price",
"break_even_price", "high_price", "last_trade_price",
"low_price", "mark_price", "previous_close_price",
"high_fill_rate_buy_price", "high_fill_rate_sell_price",
"low_fill_rate_buy_price", "low_fill_rate_sell_price",
"chance_of_profit_long", "chance_of_profit_short", "delta",
"gamma", "implied_volatility", "rho", "theta", "vega"),
function(x) as.numeric(as.character(x))) %>%
dplyr::mutate_at("previous_close_date", lubridate::ymd)
dplyr::mutate_at(c("adjusted_mark_price", "adjusted_mark_price_round_down",
"ask_price", "bid_price", "break_even_price",
"high_price", "last_trade_price", "low_price",
"mark_price", "previous_close_price",
"high_fill_rate_buy_price", "high_fill_rate_sell_price",
"low_fill_rate_buy_price", "low_fill_rate_sell_price",
"chance_of_profit_long", "chance_of_profit_short",
"delta", "gamma", "implied_volatility", "rho", "theta",
"vega"),
function(x) as.numeric(as.character(x))) %>%
dplyr::mutate_at(c("previous_close_date"), lubridate::ymd) %>%
dplyr::mutate_at(c("updated_at"), lubridate::ymd_hms)


return(dta)

Expand Down
6 changes: 3 additions & 3 deletions R/api_positions_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#' @param RH object of class RobinHood
#' @import httr magrittr
#' @export
api_positions_options <- function(RH) {
api_positions_options <- function(RH) { # nolint

# URL and token
url <- api_endpoints("option_positions")
url <- RobinHood::api_endpoints("option_positions")
token <- paste("Bearer", RH$tokens.access_token)

# GET call
Expand All @@ -18,7 +18,7 @@ api_positions_options <- function(RH) {
"Authorization" = token))

# format return
dta <- mod_json(dta, "fromJSON")
dta <- RobinHood::mod_json(dta, "fromJSON")
dta <- as.data.frame(dta$results)

dta <- dta %>%
Expand Down
25 changes: 8 additions & 17 deletions R/get_contracts.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
get_contracts <- function(RH, chain_symbol, type, detail = FALSE) {

# Check if RH is valid
check_rh(RH)
RobinHood::check_rh(RH)

# Get last price
contracts <- api_contracts(RH, chain_symbol, type)
contracts <- RobinHood::api_contracts(RH, chain_symbol, type)

# Trim output
contracts <- contracts[, !names(contracts) %in% c("instrument")]
# Remove conflicting updated_at
contracts <- contracts %>% dplyr::select(-c("updated_at"))

# Get market data on contracts, split up calls into 50 instruments at a time
market_data <- data.frame()
Expand All @@ -39,7 +39,7 @@ get_contracts <- function(RH, chain_symbol, type, detail = FALSE) {
instruments <- as.character(stats::na.omit(contracts$url[x]))
instruments <- paste(instruments, collapse = ",")

intr_market_data <- api_marketdata(RH, instruments, type = "insturment_url")
intr_market_data <- RobinHood::api_marketdata(RH, instruments, type = "insturment_url")

market_data <- rbind(market_data, intr_market_data)

Expand All @@ -54,22 +54,13 @@ get_contracts <- function(RH, chain_symbol, type, detail = FALSE) {
# Select columns
if (detail == TRUE) {

contracts <- contracts %>%
dplyr::select(c("expiration_date", "type", "strike_price", "last_trade_price", "last_trade_size",
"break_even_price", "chance_of_profit_short", "chance_of_profit_long", "ask_price",
"ask_size", "bid_price", "bid_size", "low_price", "high_price", "mark_price",
"adjusted_mark_price", "below_tick", "above_tick", "cutoff_price", "rhs_tradability",
"tradability", "created_at", "updated_at", "previous_close_date", "previous_close_price",
"volume", "delta", "gamma", "implied_volatility", "rho", "theta", "vega", "high_fill_rate_buy_price",
"high_fill_rate_sell_price", "low_fill_rate_buy_price", "low_fill_rate_sell_price", "id"))
return(contracts)

} else {

contracts <- contracts %>%
dplyr::select(c("expiration_date", "type", "strike_price", "last_trade_price", "last_trade_size", "break_even_price",
"chance_of_profit_short", "chance_of_profit_long", "ask_price", "ask_size", "bid_price", "bid_size",
"low_price", "high_price", "mark_price", "adjusted_mark_price", "below_tick", "above_tick",
"cutoff_price", "rhs_tradability", "tradability", "created_at", "updated_at", "id"))
dplyr::select(-c("chain_id", "id", "url", "long_strategy_code",
"short_strategy_code", "instrument_id"))

}

Expand Down
13 changes: 7 additions & 6 deletions R/get_positions_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
#'
#' get_positions_options(RH)
#'}
get_positions_options <- function(RH, trim_pending = TRUE) {
get_positions_options <- function(RH, trim_pending = TRUE) { # nolint

# Check if RH is valid
check_rh(RH)
RobinHood::check_rh(RH)

# Options postions
options <- api_positions_options(RH)
options <- RobinHood::api_positions_options(RH)

# Get option type, loop through URL
x <- unique(options$option)
Expand All @@ -26,7 +26,7 @@ get_positions_options <- function(RH, trim_pending = TRUE) {

# Loop through option instruments to pull additional columns
for (i in x) {
y <- api_instruments_options(RH, i)
y <- RobinHood::api_instruments_options(RH, method = "url", option_instrument_url = i)

y <- y %>%
dplyr::select(c("url", "type", "state", "strike_price", "rhs_tradability", "tradability")) %>%
Expand All @@ -39,7 +39,8 @@ get_positions_options <- function(RH, trim_pending = TRUE) {
# Join with option positions
options$option <- as.character(options$option)
options_instruments$option <- as.character(options_instruments$option)
options <- dplyr::inner_join(options, options_instruments, by = "option")
options <- dplyr::inner_join(options, options_instruments, by = "option") %>%
dplyr::select(-c("updated_at"))

if (nrow(options) == 0) stop("You dont have any open positions")

Expand All @@ -52,7 +53,7 @@ get_positions_options <- function(RH, trim_pending = TRUE) {
option_market_data <- data.frame()

for (i in x) {
y <- api_marketdata(RH, i)
y <- RobinHood::api_marketdata(RH, i)

option_market_data <- rbind(option_market_data, y)

Expand Down