Which API should I use?

  • Use v20 REST API only if you have a v20 account
  • Use v1 REST API if your account id contains only digits (ie. 2534253) as it is a legacy account

Order Endpoints

Get orders for an account

This will return all pending orders for an account. Note: pending take profit or stop loss orders are recorded in the open trade object, and will not be returned in this request.

GET /v1/accounts/:account_id/orders

Input Query Parameters

maxId: Optional The server will return orders with id less than or equal to this, in descending order (for pagination).

count: Optional Maximum number of open orders to return. Default: 50. Max value: 500.

instrument: Optional Retrieve open orders for a specific instrument only. Default: all.

ids: Optional An URL encoded comma (%2C) separated list of orders to retrieve. Maximum number of ids: 50. No other parameter may be specified with the ids parameter.

Pagination

Orders can be paginated with the count and maxId parameters. At most, a maximum of 50 orders can be returned in one query. If more orders exist than specified by the given or default count, a URL with maxId set to the next unreturned order will be returned within the Link header.

Example

$curl -X GET "https://api-fxtrade.oanda.com/v1/accounts/12345/orders?instrument=EUR_USD&count=2"

Response

Header

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 660
Link: <https://api-fxtrade.oanda.com/v1/accounts/12345/orders?count=2&instrument=EUR_USD&maxId=175427625>; rel="next"
X-Result-Count: 8

Body

{
  "orders" : [
    {
      "id" : 175427639,
      "instrument" : "EUR_USD",
      "units" : 20,
      "side" : "buy",
      "type" : "marketIfTouched",
      "time" : "2014-02-11T16:22:07Z",
      "price" : 1,
      "takeProfit" : 0,
      "stopLoss" : 0,
      "expiry" : "2014-02-15T16:22:07Z",
      "upperBound" : 0,
      "lowerBound" : 0,
      "trailingStop" : 0
    },
    {
      "id" : 175427637,
      "instrument" : "EUR_USD",
      "units" : 10,
      "side" : "sell",
      "type" : "marketIfTouched",
      "time" : "2014-02-11T16:22:07Z",
      "price" : 1,
      "takeProfit" : 0,
      "stopLoss" : 0,
      "expiry" : "2014-02-12T16:22:07Z",
      "upperBound" : 0,
      "lowerBound" : 0,
      "trailingStop" : 0
    }
  ]
}

Create a new order

POST /v1/accounts/:account_id/orders

Input Data Parameters (inside body)

instrument:* Required Instrument to open the order on.

units: Required The number of units to open order for.

side: Required Direction of the order, either ‘buy’ or ‘sell’.

type: Required The type of the order ‘limit’, ‘stop’, ‘marketIfTouched’ or ‘market’.

expiry: Required If order type is ‘limit’, ‘stop’, or ‘marketIfTouched’. The order expiration time in UTC. The value specified must be in a valid datetime format.

price: Required If order type is ‘limit’, ‘stop’, or ‘marketIfTouched’. The price where the order is set to trigger at.

lowerBound: Optional The minimum execution price.

upperBound: Optional The maximum execution price.

stopLoss: Optional The stop loss price.

takeProfit: Optional The take profit price.

trailingStop: Optional The trailing stop distance in pips, up to one decimal place.

Example (‘market’ order)

$curl -X POST -d "instrument=EUR_USD&units=2&side=sell&type=market" "https://api-fxtrade.oanda.com/v1/accounts/12345/orders"

Response

Header

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 204

Body

{
  "instrument" : "EUR_USD",
  "time" : "2013-12-06T20:36:06Z", // Time that order was executed
  "price" : 1.37041,               // Trigger price of the order
  "tradeOpened" : {
    "id" : 175517237,              // Order id
    "units" : 2,                   // Number of units
    "side" : "sell",               // Direction of the order
    "takeProfit" : 0,              // The take-profit associated with the order, if any
    "stopLoss" : 0,                // The stop-loss associated with the order, if any
    "trailingStop" : 0             // The trailing stop associated with the order, if any
  },
  "tradesClosed" : [],
  "tradeReduced" : {}
}

Example (‘marketIfTouched’ order)

$curl -X POST -d "instrument=EUR_USD&units=2&side=sell&type=marketIfTouched&price=1.2&expiry=2013-04-01T00%3A00%3A00Z" "https://api-fxtrade.oanda.com/v1/accounts/12345/orders"

Response

Header

HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 292
Location: https://api-fxtrade.oanda.com/v1/accounts/12345/orders/175517237

Body

{
  "instrument" : "EUR_USD",
  "time" : "2013-01-01T20:36:06Z",      // Time that order was executed
  "price" : 1.2,                        // Trigger price of the order
  "orderOpened" : {             
    "id" : 175517237,                   // Order id
    "units" : 2,                        // Number of units
    "side" : "sell",                    // Direction of the order
    "takeProfit" : 0,                   // The take-profit associated with the order, if any
    "stopLoss" : 0,                     // The stop-loss associated with the order, if any
    "expiry" : "2013-02-01T00:00:00Z",  // The time the order expires (in RFC3339 format)
    "upperBound" : 0,                   // The maximum execution price associated with the order, if any
    "lowerBound" : 0,                   // The minimum execution price associated with the order, if any
    "trailingStop" : 0                  // The trailing stop associated with the order, if any
  }
}

Get information for an order

GET /v1/accounts/:account_id/orders/:order_id

Example

$curl -X GET "https://api-fxtrade.oanda.com/v1/accounts/1234/orders/43211"

Response

Header

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 290

Body

{
  "id" : 43211,                        // The ID of the order
  "instrument" : "EUR_USD",            // The symbol of the instrument of the order
  "units" : 5,                         // The number of units in the order
  "side" : "buy",                      // The direction of the order
  "type" : "limit",                    // The type of the order
  "time" : "2013-01-01T00:00:00Z",     // The time of the order (in RFC3339 format)
  "price" : 1.45123,                   // The price the order was executed at
  "takeProfit" : 1.7,                  // The take-profit associated with the order, if any
  "stopLoss" : 1.4,                    // The stop-loss associated with the order, if any
  "expiry" : "2013-02-01T00:00:00Z",   // The time the order expires (in RFC3339 format)
  "upperBound" : 0,                    // The maximum execution price associated with the order, if any
  "lowerBound" : 0,                    // The minimum execution price associated with the order, if any
  "trailingStop" : 10                  // The trailing stop associated with the order, if any
}

Modify an existing order

PATCH /v1/accounts/:account_id/orders/:order_id

Input Data Parameters (inside body)

Note: Only the specified parameters will be modified. All other parameters will remain unchanged. To remove an optional parameter, set its value to 0.

units: Optional The number of units to open order for.

price: Optional The price at which the order is set to trigger at.

expiry: Optional The order expiration time in UTC. The value specified must be in a valid datetime format.

lowerBound: Optional The minimum execution price.

upperBound: Optional The maximum execution price.

stopLoss: Optional The stop loss price.

takeProfit: Optional The take profit price.

trailingStop: Optional The trailing stop distance in pips, up to one decimal place.

Example

$curl -X PATCH -d "stopLoss=1.3" "https://api-fxtrade.oanda.com/v1/accounts/12345/orders/43211"

Response

Header

HTTP/1.1 200 OK
Server: nginx/1.2.9
Content-Type: application/json
Content-Length: 284

Body

{
  "id" : 43211,                        // The ID of the order
  "instrument" : "EUR_USD",            // The symbol of the instrument of the order
  "units" : 5,                         // The number of units in the order
  "side" : "buy",                      // The direction of the order
  "type" : "limit",                    // The type of the order
  "time" : "2013-01-01T00:00:00Z",     // The time of the order (in RFC3339 format)
  "price" : 1.45123,                   // The price the order was executed at
  "takeProfit" : 1.7,                  // The take-profit associated with the order, if any
  "stopLoss" : 1.3,                    // The stop-loss associated with the order, if any
  "expiry" : "2013-02-01T00:00:00Z",   // The time the order expires (in RFC3339 format)
  "upperBound" : 0,                    // The maximum execution price associated with the order, if any
  "lowerBound" : 0,                    // The minimum execution price associated with the order, if any
  "trailingStop" : 10                  // The trailing stop associated with the order, if any
}

Close an order

DELETE /v1/accounts/:account_id/orders/:order_id

Example

$curl -X DELETE "https://api-fxtrade.oanda.com/v1/accounts/12345/orders/43211"

Response

Header

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 130

Body

{
  "id" : 54332,                   // The ID of the close order transaction
  "instrument" : "EUR_USD",       // The symbol of the instrument of the order
  "units" : 2,
  "side" : "sell",
  "price" : 1.30601,              // The price at which the order executed
  "time" : "2013-01-01T00:00:00Z" // The time at which the order executed
}