Home  >  Q&A  >  body text

python3.x - python connects oanda's simulated trading api to obtain json question second question

In the first question, it has been solved to connect oanda's simulated trading api to obtain the real-time exchange rate of EUR_USD. Thank you again @prolifes for your enthusiastic help. The procedure is as follows:
import requests
import json
url = " https://api-fxpractice.oanda.com/v1/prices"
instruments = 'EUR_USD'
account_id = 'cawa11'
params = {'instruments':instruments,'accountId':account_id}
access_token = 'a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380'
headers = {'Authorization':'Bearer ' access_token} #There is a space after Bearer
r = requests.get(url,headers = headers, params=params )
print(r.json())
The resulting json is:
{'prices': [{'bid': 1.0926, 'time': '2017-05-03T05:45:25.737018Z', 'ask': 1.09274, 'instrument': 'EUR_USD'}]}
Now I want to get the real-time exchange rates of EUR_USD and USD_CAD at the same time, and get json in the following form:
{'prices': [{'instrument' : 'EUR_USD', 'ask': 1.09324, 'time': '2017-05-03T04:44:38.200174Z', 'bid': 1.09311},{'instrument': 'USD_CAD', 'ask': 1.37270, 'time': '2017-05-03T04:44:38.200174Z', 'bid': 1.37251}]}

迷茫迷茫2712 days ago654

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-05-18 10:57:43

    问题已解决,谢谢各位关注:
    import requests
    import json

    url = "https://api-fxpractice.oanda.com/v1/prices"
    instruments = 'EUR_USD,USD_CAD'
    account_id = 'cawa11'
    params = {'instruments':instruments,'accountId':account_id}
    access_token = 'a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380'
    headers = {'Connection': 'Keep-Alive','Accept-Encoding': 'gzip,deflate','Authorization':'Bearer '+access_token}
    r = requests.get(url,headers = headers, params=params)
    price = r.json()
    print(r.json())
    print(price'prices'['instrument'].replace('_','/'),':',round((price'prices'['ask']+price'prices'['bid'])/2,4),' ',price'prices'['time'])
    print(price'prices'['instrument'].replace('_','/'),':',round((price'prices'['ask']+price'prices'['bid'])/2,4),' ',price'prices'['time'])

    输出:
    {'prices': [{'bid': 1.09171, 'ask': 1.09184, 'instrument': 'EUR_USD', 'time': '2017-05-03T06:44:19.750556Z'}, {'bid': 1.37203, 'ask': 1.37219, 'instrument': 'USD_CAD', 'time': '2017-05-03T06:44:19.738338Z'}]}
    EUR/USD : 1.0918 2017-05-03T06:44:19.750556Z
    USD/CAD : 1.3721 2017-05-03T06:44:19.738338Z

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-18 10:57:43

    This is no longer a technical issue. You should find out whether its API provides this function. If it does not provide the function of obtaining two at the same time, then you can only obtain them separately and then merge them together

    reply
    0
  • Cancelreply