Mr_James
Mr_James

Reputation: 91

Stream pricing from oanda V20 rest api using python requests

I'm trying to stream the price of an instrument from Oandas' V20 rest api but with not much success. I am using python requests as that worked for regular get requests. Here is where i have got to:

import requests
url = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing?instruments=EUR_USD'
head = {'Content-type':"application/json",
        'Accept-Datetime-Format':"RFC3339",
        'Authorization':"Bearer MY_ACCESS8TOKEN"}


r = requests.get(url, headers=head, stream=True)
print(r)

for line in r.iter_lines():

    if line:
        decoded_line = line.decode('utf-8')
        print(json.loads(decoded_line))

The response error code is 405 meaning the method is not supported. What am I doing wrong?

Upvotes: 6

Views: 4270

Answers (2)

hootnot
hootnot

Reputation: 1014

Your URL is not valid (see developer.oanda.com/rest-live-v20/pricing-ep/ ),
it should be:

url_OK = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing/stream?instruments=EUR_USD'

instead of:

urlNOT = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing?instruments=EUR_USD'
//        |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||vvvvvvvv
// _OK = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing/stream?instruments=EUR_USD'

If you don't want the hassle of constructing URL's, you could use one of the V20 bindings: https://github.com/search?utf8=%E2%9C%93&q=v20&type=

Check for instance the examplecode in those repositories, for example: https://github.com/hootnot/oandapyV20-examples

Upvotes: 5

user3666197
user3666197

Reputation: 1

Step 0.: validate you access credentials:

Using a default OANDA curl example, validate your access credentials:

curl \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
  "https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/pricing?instruments=EUR_USD%2CUSD_CAD"

CASE isERR: proceed to OANDA Support for fixing your issues with invalid credentials.

CASE wasOK: proceed to Step 1.


Step 1.: replicate the curl call syntax AS-IS inside a one-shot python

nothing added, nothing excluded. Ought provide the same results as in Step 0.

CASE isERR: review your one-shot python-code, so as to meet 1:1 the working OANDA specification, already proved to be working in Step 0.

  • Review OANDA Service Status, using curl below:
    curl http://api-status.oanda.com/api/v1/services
  • Review reported Error details.

CASE wasOK: proceed to Step 2.


Step 2.: extend the python code so as to request and process response

yet, keep in mind, not to go beyond OANDA stated maximum count of requests per day and similar restrictive limits, which ought be handled with care.

CASE isERR: Review OANDA Service Status, using curl below and possible reported Error details:

curl http://api-status.oanda.com/api/v1/services

CASE wasOK: Congrats, your pricing-source got working end-to-end up to the specifications.


Error 405 is not a "not supported" service BUT "not allowed"

405 Method Not Allowed

A “405 Method Not Allowed” response may be returned from the v20 REST API when the client is attempting to access an API endpoint with an HTTP method that is not supported. The response Content-Type will be application/json and has the following schema:

{
    # 
    # The code of the error that has occurred.  This field may not be returned
    # for some errors.
    # 
    errorCode : (string),

    # 
    # The human-readable description of the error that has occurred.
    # 
    errorMessage : (string, required)
}

Upvotes: 0

Related Questions