Create Oanda Order Using httr::POST

31 views Asked by At

I am trying to create a market order on Oanda using httr::POST and am receiving a status code 415 error and the order is not being created. No expert here so please help me see what I am doing wrong.

Documentation from Oanda on API requests can be found here: https://developer.oanda.com/rest-live-v20/order-ep/#collapse_endpoint_2

1

There are 1 answers

0
Mark On

After a health amount of googling and trial here is what works in case anyone else needs it. Note the content_type_json() inside POST:

URL_Orders<-'https://api-fxpractice.oanda.com/v3/accounts/{AccountID}/orders'
#replace {AccountID} with the account ID for the account (no {}) Token<-{Token} #fill with API Token generated from Oanda (no {})

#create the body for the request req = list(   order = list(instrument="EUR_USD",
               units="1000", 
               type="MARKET",
               timeInForce="FOK",
               positionFill="DEFAULT"
               ) )

#format body as JSON body<-toJSON(req, auto_unbox = T, pretty = FALSE)

#post request Test<-httr::POST(URL_Orders, 
                 content_type_json(),
                 add_headers("Authorization" = paste("Bearer ", Token)), 
                 body=body,
                 encode = "form",
                 verbose(data_out = TRUE, data_in = TRUE, info = TRUE)) 

- OP