• Invest: 1-855-880-2559
  • Open 24/7

Ally Invest API

R

In this R example make sure to get/install the latest ROAuth library version manually (instead of from CRAN).

# Install 0.9.2 of ROAuth
install.packages("link_to_file", repos = NULL, type="source")
# Call the required packages
library(ROAuth)
library(RJSONIO)
# Set your application keys
cKey <- '################'
cSecret <- '#################'
oKey <- '################'
oSecret <- '################'
# Set the API endpoint
tkURL <- "https://api.tradeking.com/v1/market/ext/quotes.json"
# Create the OAuth connection - this is straight from the ROAuth documentation on CRAN
credentials <- OAuthFactory$new(consumerKey=cKey,
                                consumerSecret=cSecret,
                                oauthKey = oKey, 
                                oauthSecret = oSecret,
                                needsVerifier=FALSE,
                                signMethod='HMAC')
# Update the connection so the handshake is TRUE
credentials$handshakeComplete <- TRUE
# Create the parameters to be passed with the GET request
query <- list()
query[[ "symbols" ]] <- "aapl, ibm, goog"
query[[ "fids" ]] <- "last,bid,ask"
# Make the GET request, passing along the parameters, storing the response, then parsing the response
response <- credentials$OAuthRequest(tkURL, query)
fromJSON(response)
# Here is what the response will look like
$response$quotes$quote[[1]]
       ask        bid       exch       last     symbol 
"209.8500" "209.8100"     "NYSE" "209.8100"      "IBM" 
$response$quotes$quote[[2]]
       ask        bid       exch       last     symbol 
"755.8900" "755.6400"     "NASD" "755.8400"     "GOOG" 
$response$quotes$quote[[3]]
       ask        bid       exch       last     symbol 
"639.7100" "639.5900"     "NASD" "639.5900"     "AAPL" 
Disclaimer:

Any mention of actual symbols are to be used for coding purposes only and do not imply a recommendation or solicitation to buy or sell a particular security or to engage in any particular investment strategy.

At the time of publication and in the preceding month, Ally Invest did not have ownership greater than 1% in any stocks mentioned here and does not have any other actual, material conflict of interest known at the time of publication.

Ally Invest did not receive compensation from a public offering or from investment banking services related to any companies mentioned here within the past 12 months, or expects to receive any in the next 3 months. Ally Invest did not engage in market making in the securities mentioned here.

Ally Invest Securities' background can be found at FINRA's BrokerCheck. Options involve risk and are not suitable for all investors.

Review the Characteristics and Risks of Standardized Options brochure (PDF) before you begin trading options. Options investors may lose the entire amount of their investment in a relatively short period of time.