我正在尝试创建一个代码来自动执行OANDA API中的订单
这是我订单的代码:
class Execution(object):
def __init__(self, domain, access_token, account_id):
self.domain = domain
self.access_token = access_token
self.account_id = account_id
self.conn = self.obtain_connection()
def obtain_connection(self):
return httplib.HTTPSConnection(self.domain)
def execute_order(self, instrument, units, order_type, side):
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + self.access_token
}
params = urllib.urlencode({
"instrument": instrument,
"units": units,
"type": order_type,
"side": side
})
self.conn.request(
"POST",
"/v1/accounts/%s/orders" % str(self.account_id),
params, headers
)
response = self.conn.getresponse().read()
print response
当我尝试执行命令时,我得到了一个我无法解决的奇怪错误:
{"code" : 52,
"message" : "Invalid or malformed resource identifier: accountId",
"moreInfo" : "http:\/\/developer.oanda.com\/docs\/v1\/troubleshooting\/#errors"
}
有人遇到过这种问题吗?我问,因为在与OANDA相关的错误教程(http://developer.oanda.com/rest-live/troubleshooting-errors/)中没有提及代码52。
是连接问题还是订单创建问题?
我多次收到这个错误,这让我烦恼,因为我无法弄清楚,访问令牌和帐户ID都是正确的。然后我意识到我在创建访问令牌后添加了子帐户。您将能够获得历史价格,因为这些不是特定于帐户的,但它不会让您进入帐户。所以我不得不撤销并重新生成我的令牌,它工作得很好。我知道这已经过时了,但我今天遇到了这个问题。
虽然在故障排除错误中未指定代码52,但我确信您从请求中获得了400状态代码:
400 Bad Request无效或格式错误的参数:[arg]指定的参数格式不正确或者是不可接受的值
仔细检查你的accountId
http://developer.oanda.com/rest-live/troubleshooting-errors/
请注意:
“code”:[OANDA错误代码,可能与HTTP状态代码相同或不同],
尝试“调试”或传递以下部分的变量:
import requests
aTok = 'acess token'
header = {'Authorization': 'Bearer '+aTok}
account_id = "your account id"
uri = 'https://api-fxpractice.oanda.com' #or non-practice api-fxtrade.oanda.com
resp = requests.get(uri+'/v1/accounts/{0}/orders'.format(account_id), headers=header)
response = resp.text
print(response)