How to get the list of available instruments from OANDA?

I assume that OANDA is one of the best Forex trading platforms with a very good API. I use Python to deals with OANDA API. Periodically, I need to get the proper instrument name to use when OANDA API requests.

The list of all available instruments is here: https://www1.oanda.com/forex-trading/markets/recent. Unfortunately, the name of the instrument to use in OANDA API (e.g. ‘GBP_USD’, ‘EUR_USD’ etc.) is unavailable.

Let’s import all needed Python libs to work with OANDA API and specify tokens for your account to access:

#@title Import libs
%%capture
!pip install git+https://github.com/hootnot/oanda-api-v20.git

import json

import oandapyV20.endpoints.accounts as accounts 
import oandapyV20
import pandas as pd
import numpy as np

#@title Tokens to deals with OANDA API 
access_token = 'xxxxxxxxx-xxxxxxx'
accountID = 'xxxxxx'

api = oandapyV20.API(access_token=access_token)

Here is a simple function, which shows the list of all available OANDA instruments by type: ‘CFD’, ‘METAL’, ‘CURRENCY’.

#@title Получение списка всех доступных инструментов
#type - тип инструмента, например, 'CFD', 'METAL', 'CURRENCY'
def getInstrumentsList(type, api, accountID):

  params={"instruments": None}
  r = accounts.AccountInstruments(accountID=accountID, params = params)

  instruments = []
  try:
    rv = api.request(r)
  except oandapyV20.exceptions.V20Error as err:  
    print("Error:", r.status_code, err)
  else:
    print("The result:")
    res = json.dumps(rv, indent=2)
    result = json.loads(res)
    for instrument in result['instruments']:
      if (instrument['type'] == type): #Выводим только сивмволы с типом type
        instruments.append([instrument['name'], instrument['displayName']])
  
  return instruments

E.g. let’s see the list of OANDA instruments where Australian dollar is present:

instruments = getInstrumentsList("CURRENCY", api, accountID)
cur = pd.DataFrame(instruments, columns=["Name", "Details"])
dataFrameOut = cur[cur['Name'].str.contains('AUD')]
dataFrameOut

The result of getting OANDA instruments containing ‘AUD’ symbol is:

        Name	Details
8	AUD_CHF	AUD/CHF
12	AUD_HKD	AUD/HKD
13	AUD_NZD	AUD/NZD
14	AUD_JPY	AUD/JPY
17	AUD_SGD	AUD/SGD
32	AUD_CAD	AUD/CAD
45	EUR_AUD	EUR/AUD
54	AUD_USD	AUD/USD
56	GBP_AUD	GBP/AUD
Spread the love
Запись опубликована в рубрике IT рецепты с метками , . Добавьте в закладки постоянную ссылку.

Обсуждение закрыто.