这次我想以现时市场价买入50份USD_CAD合约
程序:
import requests
import json
url = 'https://api-fxpractice.oanda....'
instruments = 'USD_CAD'
account_id = 'cawa11' #用户名
params = {'instruments':instruments,'accountId':account_id}
access_token = 'a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380'
headers = {'Authorization':'Bearer '+access_token}
r = requests.get(url,headers = headers, params=params)
price = r.json()
print(price)
url = 'https://api-fxpractice.oanda....' #5898545为账号101-011-5898545-001的一部分
headers = {'Content-Type' : 'application/x-www-form-urlencoded','Authorization':'Bearer '+access_token}
data = {'instrument':'USD_CAD','accountId':'cawa11','units':50,'type':'market','side':'buy'}
req = requests.post(url,data=data,headers=headers)
print(req.text)
print(req.json())
第二段程序报错:
{"code" : 4,"message" : "The access token provided does not allow this request to be made","moreInfo":"http://developer.oanda.com/docs/v1/auth/#overview"}
{'message': 'The access token provided does not allow this request to be made', 'moreInfo': 'http://developer.oanda.com/do...', 'code': 4}
但通过第一段程序的正常运行access token是没有问题的,同时http://developer.oanda.com/do...中错误列表错误列表
HTTP状态代码 HTTP状态消息 信息 详细说明
4 401 擅自 提供不允许该请求的访问令牌进行 确保与请求提供的访问令牌是正确的,是有效的。请参阅认证以获取更多细节。
附上一份oanda公司的文档:
import http.client
import urllib
import json
import datetime
def checkAndTrade():
conn = httplib.HTTPSConnection("api-sandbox.oanda.com")
conn.request("GET", "/v1/prices?instruments=USD_CAD")
response = conn.getresponse()
resptext = response.read()
if response.status == 200:
data = json.loads(resptext)
if data['prices'][0]['ask'] > 0.994:
headers = {"Content-Type" : "application/x-www-form-urlencoded"}
params = urllib.urlencode({"instrument" : "USD_CAD",
"units" : 50,
"type" : 'market',
"side" : "buy"})
conn.request("POST", "/v1/accounts/8026346/orders", params, headers)
print(conn.getresponse().read())
else:
print(resptext)
def order():
now = datetime.datetime.now()
expire = now + datetime.timedelta(days=1)
expire = expire.isoformat('T') + "Z"
conn = httplib.HTTPSConnection("api-sandbox.oanda.com")
params = urllib.urlencode({"instrument": "USD_CAD",
"units" : 50,
"price" : 0.994,
"expiry" : expire,
"side" : "buy",
"type" : "limit"})
headers = {"Content-Type" : "application/x-www-form-urlencoded"}
conn.request("POST", "/v1/accounts/8026346/orders", params, headers)
print(conn.getresponse().read())
order()
checkAndTrade()
此程序可能时间比较久其api地址与现在的api地址不同
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号