json - python连oanda的模拟交易api下订单问题第三问

查看:97
本文介绍了json - python连oanda的模拟交易api下订单问题第三问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

这次我想以现时市场价买入50份USD_CAD合约
程序:
import requests
import json

显示当前USD_CAD的汇率------------------------------------------------------------------------------------------

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)

以现时市场价买入50份USD_CAD合约---------------------------------------------------------------------------------

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

该文件包含一些非常简单的对OANDA API的调用

这表明如果超过阈值,则获取仪器的当前价格并进行交易

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)

这样设定了与上述价格相同的订单,当价格跨越0.994时将执行交易

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地址不同

解决方案

账户有权限读取数据,不一定有权限下单
你现在的问题都是api里的东西,应该仔细研究api文档,不然你后面会遇到无数类似的问题

除非刚好有人对这份api熟悉,不然也给不了你什么帮助
=========================================
如果你是从网站下单监听的话你就会发现你的url和data完全和网站的不一样

import requests

access_token = '81e6f8b8ae23d2c0122cfee68cd26fc2-5681d2e539e0c8d01925bad19ff141f5'
url = 'https://api-fxpractice.oanda.com/v3/accounts/101-011-5898545-001/orders'
headers = {'Content-Type' : 'application/json','Authorization':'Bearer '+access_token}
data = {"order":{"instrument":"EUR_USD","type":"MARKET","units":"1"}}
req = requests.post(url,json=data,headers=headers)
print(req.text)

===================================================================

这篇关于json - python连oanda的模拟交易api下订单问题第三问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆