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

Cannot place order #156

Closed cbengibson2 closed 2 years ago

cbengibson2 commented 2 years ago

Hi All,

I installed the new Github version this morning and was unable to place an order. The line in RobinHood::api_orders seems to be the issue:

    detail <- data.frame(account = RH$url.account_id, instrument = instrument_id, 
                         symbol = symbol, type = type, time_in_force = time_in_force, 
                         trigger = trigger, price = price, stop_price = stop_price, 
                         quantity = quantity, side = side, client_id = RH$api_client_id)

R is looking for RH$api_client_id but doesn't exist. If I run RH$api_client_id<-RH$api_request.client_id prior to placing the order, it works fine.

HTH

JestonBlu commented 2 years ago

It feels like you still arent on the correct package version. In the patch I made, i changed the structure of the RH object which is why that code no longer works.

Snippet from api_login.R

  # Storage for api data
  RH <- c(
    # APIs
    api_request = list(
      grant_type = "password",
      client_id = "c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS",
      device_token = uuid::UUIDgenerate(),
      scope = "internal"
      ),
    api_response = list(
      access_token = "000",
      refresh_token = "000",
      expires_in = 0,
      token_type = "000",
      scope = "000",
      mfa_code = "000",
      backup_code = "000"
    )
  )

Snippet from api_orders


  if (action == "order") {

    url <- RobinHood::api_endpoints("orders")
    token <- paste("Bearer", RH$api_response.access_token)

    detail <- data.frame(account = RH$url.account_id,
                         instrument = instrument_id,
                         symbol = symbol,
                         type = type,
                         time_in_force = time_in_force,
                         trigger = trigger,
                         price = price,
                         stop_price = stop_price,
                         quantity = quantity,
                         side = side,
                         client_id = RH$api_request.client_id)

    dta <- POST(url = url,
                add_headers("Accept" = "application/json",
                            "Content-Type" = "application/json",
                            "Authorization" = token),
                body = mod_json(detail, type = "toJSON"))
    httr::stop_for_status(dta)

    dta <- RobinHood::mod_json(dta, "fromJSON")
    dta <- as.list(dta)

The new version should be trying to get your client id from your new RH object client_id = RH$api_request.client_id so if you had to copy it from your old structure, i wonder if you are really on the latest version. If thats not the case I will trouble shoot further.

cbengibson2 commented 2 years ago

Ah, I see the problem -- I saved the RH authentication object before you updated the package, then used the older object to place the order. My bad! The new version works great. Thanks!

ericvaughn11 commented 2 years ago

Thanks JestonBlu again for tackling the API issue so quickly. I have a related issue I thought I'd bring up here.

I'm running into an odd error with api_orders as well where I can seem to place a sell order, but get Bad Request (HTTP 400). when trying to place a buy order. I've tested this a few times and it rejects only buy orders. This is on 1.6.8. Any ideas?