Home  >  Q&A  >  body text

python3.x - Question 6 of getting json from python's simulated trading api with oanda

I solved the ordering problem with everyone's help in the third question, but when I tested the historical data today, I discovered a strange thing. I want to operate on 6 currency pairs (GBP_USD, EUR_USD, USD_CAD and USD_CHF, USD_JPY, AUD_USD). I want GBP_USD, EUR_USD, USD_CAD to buy when placing a buy order, and USD_CHF, USD_JPY, and AUD_USD to sell when placing a buy order. Start a few transactions. There is no problem, but after trying to sell when placing a buy order once, GBP_USD, EUR_USD, USD_CAD also changed to selling when placing a buy order. The procedure is as follows:

import requests

def trade(action,pairs,unit="1"):
    account_id = '101-011-5898545-001'
    access_token = '33c7d4049fe8720c37918482bc830c12-06467701c963e60220d7e18436f3225d'   
    url = 'https://api-fxpractice.oanda.com/v3/accounts/'+account_id+'/orders'
    headers = {'Content-Type' : 'application/json','Authorization':'Bearer '+access_token}
    
    if pairs == "GBP_USD" or "EUR_USD" or "AUD_USD" :        
        if action == "buy" :
            data = {"order":{"instrument":pairs,"type":"MARKET","units":unit}}
        if action == "sell" :
            data = {"order":{"instrument":pairs,"type":"MARKET","units":"-"+unit}}
            
    if pairs == "USD_CHF" or "USD_JPY" or "USD_CAD" :
        if action == "buy" :
            data = {"order":{"instrument":pairs,"type":"MARKET","units":"-"+unit}}
        if action == "sell" :
            data = {"order":{"instrument":pairs,"type":"MARKET","units":unit}}
    
    req = requests.post(url,json=data,headers=headers)
    #print(req.text)

if __name__=='__main__' :
    trade("buy","GBP_USD","3")

Please check the transaction status at https://trade.oanda.com/, username: cawa11, password: 1122334455, thank you

PHP中文网PHP中文网2686 days ago900

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-06-12 09:29:26

    There is something wrong with your code

    if pairs == "GBP_USD" or "EUR_USD" or "AUD_USD"
    应该改成
    if pairs == "GBP_USD" or pairs == "EUR_USD" or pairs == "AUD_USD"
    但我更推荐你这样写
    if pairs in ["GBP_USD", "EUR_USD", "AUD_USD"]
    

    Your code can be simplified to this. Buy and sell orders are determined by whether the unit is positive or negative:

    # coding: utf-8
    
    import requests
    
    def trade(pairs, unit=1):
        account_id = '101-011-5898545-001'
        access_token = '33c7d4049fe8720c37918482bc830c12-06467701c963e60220d7e18436f3225d'
        url = 'https://api-fxpractice.oanda.com/v3/accounts/'+account_id+'/orders'
        headers = {'Content-Type' : 'application/json','Authorization':'Bearer '+access_token}
        
        #你逻辑里只提到当货币为["USD_CHF", "USD_JPY", "USD_CAD"]时,只要是买单就要变成卖单
        if pairs in ["USD_CHF", "USD_JPY", "USD_CAD"] and unit > 0:
            unit *= -1
    
        data = {"order":{"instrument":pairs,"type":"MARKET","units":unit}}
        req = requests.post(url,json=data,headers=headers)
        #print(req.text)
    
    if __name__=='__main__' :
        trade("GBP_USD", 1)     #买
        trade("GBP_USD", -1)    #卖

    reply
    0
  • Cancelreply