python3.x - python連oanda的模擬交易api取得json問題第二問
迷茫
迷茫 2017-05-18 10:55:43
0
2
639

在第一問中已解決連接oanda的模擬交易api獲得EUR_USD的即時匯率,再次感謝@prolifes的熱情幫助,其程序如下:
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'get:'Bearer 'accyl. params=params)
print(r.json())
所得json為:
{'prices': [{'bid': 1.0926, 'time': '2017-05-03T05:45:25.737018Z', 'ask': 1.09274, 'instrument': 'EUR_USD'}]}
現在我想同時獲得EUR_USD和USD_CAD的即時匯率,得到如下形式的json:
{'prices': [{'instrument' : 'EUR_USD', 'ask': 1.09324, 'time': '2017-05-03T04:44:38.200174Z', 'bid': 1.09311},{'instrument': 'USD_CAD', 'ask': 1. '時間': '2017-05-03T04:44:38.200174Z', 'bid': 1.37251}]}

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(2)
大家讲道理

問題已解決,謝謝各位關注:
導入請求
導入json

url = "https://api-fxpractice.oanda.com/v1/prices"
instruments = 'EUR_USD,USD_CAD'
account_id = 'cawa11'
params = {'instruments':instruments,'accountId':account_idid}
access_token = 'a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380'
headers = {'def':'
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),' ',價格'價格'['時間'])

輸出:
{'prices': [{'bid': 1.09171, 'ask': 1.09184, 'instrument': 'EUR_USD', 'time': '2017-05-03T06:44:19.75056Z'}, {56Z'}, {56Z' '買價': 1.37203, '賣價': 1.37219, '工具': 'USD_CAD', '時間': '2017-05-03T06:44:19.738338Z'}]}
歐元/美元: 1.0918 52015 -03T06 :44:19.750556Z
美元/加幣: 1.3721 2017-05-03T06:44:19.738338Z

漂亮男人

這個已經不是技術層次的問題了,你應該去了解一下他的api有沒有提供這個功能,如果沒有提供同時獲取兩個的功能,那你只能分開來取,然後再合併起來

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!