oandapyV20 package

Submodules

oandapyV20.exceptions module

Exceptions.

exception oandapyV20.exceptions.StreamTerminated

Bases: exceptions.Exception

StreamTerminated.

exception oandapyV20.exceptions.V20Error(code, msg)

Bases: exceptions.Exception

Generic error class.

In case of HTTP response codes >= 400 this class can be used to raise an exception representing that error.

oandapyV20.oandapyV20 module

OANDA API wrapper for OANDA’s REST-V20 API.

class oandapyV20.oandapyV20.API(access_token, environment='practice', headers=None, request_params=None)

Bases: object

API - class to handle APIRequests objects to access API endpoints.

Examples

# get a list of trades
from oandapyV20 import API
import oandapyV20.endpoints.trades as trades

api = API(access_token="xxx")
accountID = "101-305-3091856-001"

r = trades.TradesList(accountID)
# show the endpoint as it is constructed for this call
print("REQUEST:{}".format(r))
rv = api.request(r)
print("RESPONSE:\n{}".format(json.dumps(rv, indent=2)))

Output:

REQUEST:v3/accounts/101-305-3091856-001/trades
RESPONSE:
"trades": [
    {
      "financing": "0.0000",
      "openTime": "2016-07-21T15:47:05.170212014Z",
      "price": "10133.9",
      "unrealizedPL": "8.0000",
      "realizedPL": "0.0000",
      "instrument": "DE30_EUR",
      "state": "OPEN",
      "initialUnits": "-10",
      "currentUnits": "-10",
      "id": "1032"
    },
    {
      "financing": "0.0000",
      "openTime": "2016-07-21T15:47:04.963590941Z",
      "price": "10134.4",
      "unrealizedPL": "13.0000",
      "realizedPL": "0.0000",
      "instrument": "DE30_EUR",
      "state": "OPEN",
      "initialUnits": "-10",
      "currentUnits": "-10",
      "id": "1030"
    }
  ],
  "lastTransactionID": "1040"
}
# reduce a trade by it's id
from oandapyV20 import API
import oandapyV20.endpoints.trades as trades

api = API(access_token="...")

accountID = "101-305-3091856-001"
tradeID = "1030"
cfg = {"units": 5}
r = trades.TradeClose(accountID, tradeID=tradeID, data=cfg)
# show the endpoint as it is constructed for this call
print("REQUEST:{}".format(r))
rv = api.request(r)
print("RESPONSE\n{}".format(json.dumps(rv, indent=2)))

Output:

REQUEST:v3/accounts/101-305-3091856-001/trades/1030/close
RESPONSE: {
  "orderFillTransaction": {
    "orderID": "1041",
    "financing": "-0.1519",
    "instrument": "DE30_EUR",
    "userID": 1435156,
    "price": "10131.6",
    "tradeReduced": {
      "units": "5",
      "financing": "-0.1519",
      "realizedPL": "14.0000",
      "tradeID": "1030"
    },
    "batchID": "1041",
    "accountBalance": "44876.2548",
    "reason": "MARKET_ORDER_TRADE_CLOSE",
    "time": "2016-07-21T17:32:51.361464739Z",
    "units": "5",
    "type": "ORDER_FILL",
    "id": "1042",
    "pl": "14.0000",
    "accountID": "101-305-3091856-001"
  },
  "orderCreateTransaction": {
    "timeInForce": "FOK",
    "positionFill": "REDUCE_ONLY",
    "userID": 1435156,
    "batchID": "1041",
    "instrument": "DE30_EUR",
    "reason": "TRADE_CLOSE",
    "tradeClose": {
      "units": "5",
      "tradeID": "1030"
    },
    "time": "2016-07-21T17:32:51.361464739Z",
    "units": "5",
    "type": "MARKET_ORDER",
    "id": "1041",
    "accountID": "101-305-3091856-001"
  },
  "relatedTransactionIDs": [
    "1041",
    "1042"
  ],
  "lastTransactionID": "1042"
}
request(endpoint)

Perform a request for the APIRequest instance ‘endpoint’.

Parameters:endpoint (APIRequest) – The endpoint parameter contains an instance of an APIRequest containing the endpoint, method and optionally other parameters or body data.
Raises:V20Error in case of HTTP response code >= 400
request_params

request_params property.

Module contents