OANDA执行命令REST API

时间:2016-06-05 23:29:49

标签: python api rest

我试图在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.

是连接问题还是订单创建问题?

2 个答案:

答案 0 :(得分:1)

我多次收到这个错误,这让我烦恼,因为我无法弄清楚,访问令牌和帐户ID都是正确的。然后我意识到我在创建访问令牌后添加了子帐户。您将能够获得历史价格,因为这些不是特定于帐户的,但它不会让您进入帐户。所以我不得不撤销并重新生成我的令牌,它工作得很好。我知道这已经过时了,但我今天遇到了这个问题。

答案 1 :(得分:0)

虽然在问题排查错误中未指定代码52,但我很确定您的请求中包含400状态代码:

  

400 Bad Request无效或格式错误的参数:[arg]参数   指定格式不正确或是不可接受的值

仔细检查accountId

http://developer.oanda.com/rest-live/troubleshooting-errors/

请注意:

  

"代码" :[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)