JestonBlu / RobinHood

An R interface for the RobinHood.com no commision investing site
https://jestonblu.github.io/RobinHood/
GNU Lesser General Public License v3.0
45 stars 12 forks source link

get_historicals_crypto #116

Closed jgQuantScripts closed 3 years ago

jgQuantScripts commented 3 years ago

Hello, I have a wrapper to get historical data from RH using some of your api functions and wanted to know where to share it if you are interested.

JestonBlu commented 3 years ago

@jgQuantScripts you can past it in this thread and ill look at it

jgQuantScripts commented 3 years ago

Here is the function:

# interval = '15second'|'5minute'|'10minute'|'hour'|'day'|'week'
# span     =  'hour'|'day'|'week'|'month'|'3month'|'year'|'5year'
# bounds   =  '24_7'|'extended'|'regular'|'trading'
# 'Regular' is 6 hours a day  |'trading' is 9 hours a day 
# 'extended' is 16 hours a day|'24_7' is 24 hours a day.

get_historicals_crypto = function(COIN,interval,span,bounds,username,password){
  # establish RH connection
  RH = RobinHood(username = username, password = password)
  # URL and token
  url = paste0("https://nummus.robinhood.com/currency_pairs/?symbols=",COIN)
  token <- paste("Bearer", RH$tokens.access_token)

  # GET data - this is to get Crypto IDs
  reqID <- GET(url,
               add_headers("Accept" = "application/json",
                           "Content-Type" = "application/json",
                           "Authorization" = token))

  # Format return
  reqID <- mod_json(reqID, "fromJSON")
  reqID <- as.data.frame(reqID$results)
  # extract Crypto ID
  cryptoID = reqID[which(reqID$asset_currency$code == COIN),"id"]
  # url to get historical data
  url = paste0("https://api.robinhood.com/marketdata/forex/historicals/",cryptoID,
               "/?symbol=",COIN,"&interval=",interval,"&span=",span,"&bounds=",bounds)
  token <- paste("Bearer", RH$tokens.access_token)

  # GET historical data
  dta <- GET(url,
             add_headers("Accept" = "application/json",
                         "Content-Type" = "application/json",
                         "Authorization" = token
             ))

  # Format return
  dta2 <- mod_json(dta, "fromJSON")
  dta2 <- as.data.frame(dta2$data_points)
  # format Time Stamp
  dta2$begins_at <- as.POSIXct(as.character(dta2$begins_at), 
                               format="%Y-%m-%dT%H:%M:%SZ",TZ="UTC")
  # re-arrange Columns
  dta2 = dta2[,c("begins_at","open_price",
                 "high_price","low_price",
                 "close_price")]
  logout(RH)
  dta2
}
JestonBlu commented 3 years ago

Thanks, ill look at incorporating this. Thanks for the contribution.

jgQuantScripts commented 3 years ago

That would be great and no problem thanks for creating this awesome package

JestonBlu commented 3 years ago

@jgQuantScripts This should be incorporated now. Let me know if you see anything funny.

jgQuantScripts commented 3 years ago

@JestonBlu Will do so, thank you

JestonBlu commented 3 years ago

@jgQuantScripts I noticed the function was returning an error when certain combinations of span/interval and bounds returns not results from the API so i put in a fix to address this. I think this feature is pretty good. If you find anything else that needs to be addressed let me know and ill reopen. Im planning on pushing to CRAN soon.

Thanks again for the contribution.