Cette fois, je souhaite acheter 50 contrats USD_CAD au prix actuel du marché
Procédure :
demandes d'importation
importer json
url = 'https://api-fxpractice.oanda....'
instruments = 'USD_CAD'
account_id = 'cawa11' #Username
params = {'instruments':instruments,'accountId':account_id}
access_token = 'a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380'
headers = {'Autorisation':'Bearer '+access_token}
r = requêtes.get(url,headers = headers, params=params)
prix = r.json()
print(prix)
url = 'https://api-fxpractice.oanda....' #5898545 fait partie du compte 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' :'acheter'}
req = requêtes.post(url,data=data,headers=headers)
print(req.text)
print(req.json())
Le deuxième rapport d'erreur du programme :
{"code" : 4,"message" : "Le jeton d'accès fourni ne permet pas d'effectuer cette requête","moreInfo":"http://developer.oanda.com/docs /v1/auth/#overview"}
{'message' : 'Le jeton d'accès fourni ne permet pas d'effectuer cette requête', 'moreInfo' : 'http://developer.oanda.com/do...' , 'code' : 4}
Mais il n'y a aucun problème avec le jeton d'accès grâce au fonctionnement normal du premier programme. En même temps, il y a une liste d'erreurs sur http://developer.oanda.com/do. . Liste d'erreurs
HTTP状态代码 HTTP状态消息 信息 详细说明
4 401 擅自 提供不允许该请求的访问令牌进行 确保与请求提供的访问令牌是正确的,是有效的。请参阅认证以获取更多细节。
Ci-joint un document de la société 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)
ordre def() :
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()
Ce programme est peut-être plus ancien et son adresse API est différente de l'adresse API actuelle
Les comptes ont l'autorisation de lire les données, mais peuvent ne pas avoir l'autorisation de passer des commandes
.Les problèmes que vous rencontrez actuellement sont tous liés à des éléments de l'API. Vous devez étudier attentivement la documentation de l'API, sinon vous rencontrerez d'innombrables problèmes similaires à l'avenir
À moins que quelqu'un ne soit familier avec cette API, je ne peux pas vous aider. =========
================================================= === ==================Si vous commandez et surveillez depuis le site Web, vous constaterez que votre URL et vos données sont complètement différentes de celles du site Web
@prolifes:
test.py--------------------------------------------------------- ----------
from vnoanda import OandaApi
if name == '__main__':
vnoanda.py---------------------------------------------------- -------------------
encodage : utf-8
import json
import request
from Queue import Queue, Empty
from threading import Thread
API_SETTING = {}
API_SETTING['practice'] = {'rest' : 'https://api-fxpractice.oanda.com','stream' : 'https://stream-fxpractice .oan...'}
API_SETTING['trade'] = {'rest' : 'https://api-fxtrade.oanda.com','stream' : 'https://stream-fxtrade. oanda....'}
FUNCTIONCODE_GETPRICEHISTORY = 0
FUNCTIONCODE_GETPRICES = 1
FUNCTIONCODE_GETPRICEHISTORY = 2
FUNCTIONCODE_GETACCOUNTS = 3
FUNCTIONCODE_GETACCOUNTINFO = 4#🎜 🎜#FUNCTIONCODE_GETORDERS = 5
FUNCTIONCODE_SENORDER = 6# 🎜🎜#FUNCTIONCODE_GETORDERINFO = 7
FUNCTIONCODE_MODIFYORDER = 8
FUNCTIONCODE_CANCELORDER = 9
FUNCTIONCODE_GETTRADES = 10
FUNCTIONCODE_GETTRADEINFO = 11#🎜 🎜#FUNCTIONCODE_MODIFYTRADE=12
FUNCTIONCODE_CLOSETRADE = 13#🎜 🎜#FUNCTIONCODE_GETPOSITIONS = 14
FUNCTIONCODE_GETPOSITIONINFO= 15
FUNCTIONCODE_CLOSEPOSITION = 16
FUNCTIONCODE_GETTRANSACTIONS = 17
FUNCTIONCODE_GETTRANSACTIONINFO = 18#🎜 🎜#FUNCTIONCODE_GETACCOUNTHISTORY = 19
FUNCTIONCODE_GETCALENDAR = 20#🎜🎜 #FUNCTIONCODE_GETPOSITIONRATIOS = 21
FUNCTIONCODE_GETSPREADS = 22
FUNCTIONCODE_GETCOMMIMENTS = 23
FUNCTIONCODE_GETORDERBOOK = 24
FUNCTIONCODE_GETAUTOCHARTIST = 25#🎜🎜 #FUNCTIONCODE_STREAMPRICES = 26
FUNCTIONCODE_STREAMEVENTS = 27
class OandaApi(objet):