Skip to content

Instantly share code, notes, and snippets.

@svigneau
Created April 30, 2021 01:29
Show Gist options
  • Save svigneau/13b6aa51acbcb7e8aa7fcd6f1b863e18 to your computer and use it in GitHub Desktop.
Save svigneau/13b6aa51acbcb7e8aa7fcd6f1b863e18 to your computer and use it in GitHub Desktop.
Sell all you RobinHood stocks at once.
# This script allows to sell all your stocks on RobinHood at once using the RobinHood API.
# Only use if you understand what it does!
# install.packages("RobinHood")
library(RobinHood)
my_username <- "username" # replace with your username (should not contain any R special characters)
my_password <- "password" # replace with your password (should not contain any R special characters)
# Two-factor authentication needs to be turned off in order to connect using your password.
# It is recommended to turn it back on as soon as you are done using the script.
RH = RobinHood(username = my_username, password = my_password) # connect to your RobinHood account
positions <- get_positions(RH, limit_output = TRUE) # get information about the stocks you own
# Sell each stock with the parameters listed below.
# Make sure to read the RobinHood package documentation before running the following command!
for (i in 1:nrow(positions)) {
place_order(RH,
symbol = positions$symbol[i],
type = "market",
time_in_force = "gfd",
trigger = "immediate",
price = floor(positions$current_value[i]), # price of the stock rounded down to the nearest integer dollar value, the minimum price you are willing to sell the stock for
stop_price = NA,
quantity = positions$quantity[i],
side = "sell")
print(paste0("Sold ", positions$symbol[i]))
Sys.sleep(5) # wait after selling each stock, as RobinHood throttles selling too many stocks at once
}
logout(RH) # close connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment