OpenAPI

How to close positions with multiple related orders?

This article explains how to explicitly close a position, which as related orders.

However, please beware that Saxo Bank is no longer encouraging the practice to explicitly linking individual orders to individual positions.

Also please beware the in the future clients will be able to choose between two different netting modes:

  • Traditional netting, which still allows linking orders to individual positions
  • Real Time netting, where orders can not be not be linked to individual positions.



Below is an example of a position with a related take-profit limit order and stop-loss stop order. In your user interface, you may want to allow the user to "close the position". To do this, the first inclination would probably be to place a new (market or aggressive limit) order and specify the position you want to close in the "PositionId" field of this new market order.

Something like:

POST /../openapi/trade/v2/orders
{
....
     "OrderType":"Market",
	 "PositionId":"Id of position to close"
}

This will work well, if the position has no related orders.

If the position already has related orders:

  • Placing an additional related (closing) order can be done, if there is only one related order, but it is somewhat complicated
  • Placing an additional related (closing) order cannot be done at all if there are two related orders as shown in the example above.

A better approach is therefore:

  • If the position has one related order, change that order to become the closing order (see below).
  • If the position has two related orders, change the limit order to become the closing order (see below).

The closing order, is an order, which you expect will be filled, resulting in an opposite position, thus in effect closing the open position. The closing order can either be a market order or an aggressive limit order. For some asset types (e.g. stock options) it is only possible to place limit orders.


Example using market order

Consider the position information returned from port/v1/positions:

{
      "NetPositionId": "AAPL:xswx__Share",
      "PositionBase": {
        "AccountId": "Demo_612895",
        "Amount": 100,
        "AssetType": "Stock",
        "CanBeClosed": true,
        "ClientId": "612895",
        "CloseConversionRateSettled": false,
        "CorrelationKey": "685f8368-7b19-45db-80d9-6826bf00222a",
        "ExecutionTimeOpen": "2018-03-28T14:12:52.768000Z",
        "OpenPrice": 161,
        "RelatedOpenOrders": [
          {
            "Amount": 100,
            "Duration": {
              "DurationType": "GoodTillCancel"
            },
            "OpenOrderType": "Limit",
            "OrderId": "71693527",
            "OrderPrice": 170
          },
          {
            "Amount": 100,
            "Duration": {
              "DurationType": "GoodTillCancel"
            },
            "OpenOrderType": "StopIfTraded",
            "OrderId": "71693525",
            "OrderPrice": 150
          }
        ],
        "SourceOrderId": "71693519",
        "Status": "Open",
        "Uic": 7516928,
        "ValueDate": "2018-04-03T00:00:00.000000Z"
      },


Notice the two related orders: 71693527 (limit) and 7163525 (stop).

To close this position, we will modify the limit order to a market order:

PATCH /../openapi/trade/v2/orders
{
    "AssetType":"Stock",
    "AccountKey":"KDP9uS3Re3uwk1aBA2PdCw==",
    "OrderId":"71693527",
    "OrderType":"Market",
    "OrderDuration":{
        "DurationType":"DayOrder"
    }
}
Returning: 200 OK
{
  "OrderId": "71693527"
}


The order is now changed to a market order, and if the market is open, the order will be executed and the position will be closed.