What about errors ?

Errors can occur. Wrong parameters or connection errors for example. Lets take the example from Orders and make the instrument one that does not exist. The orderspecfication itself is still valid. Wrap it up in a try/except to catch the error:


In [3]:
import json
import oandapyV20
from oandapyV20.exceptions import V20Error
import oandapyV20.endpoints.orders as orders
from oandapyV20.contrib.requests import (
    MarketOrderRequest,
    TakeProfitDetails,
    StopLossDetails)
from exampleauth import exampleauth

accountID, access_token = exampleauth.exampleAuth()
client = oandapyV20.API(access_token=access_token)

mktOrder = MarketOrderRequest(instrument="EUR_UDS",    # EUR_UDS ... the faulty instrument
                              units=10000,
                              takeProfitOnFill=TakeProfitDetails(price=1.10).data,
                              stopLossOnFill=StopLossDetails(price=1.05).data
                              ).data
r = orders.OrderCreate(accountID=accountID, data=mktOrder)
try:
    rv = client.request(r)
except V20Error as err:
    print("V20Error occurred: {}".format(err))
else:
    print("Response: {}\n{}".format(r.status_code, json.dumps(rv, indent=2)))


V20Error occurred: {"errorMessage":"Invalid value specified for 'order.instrument'"}

As we can see the REST-API returned an error response saying the instrument is invalid.