saxo_openapi.contrib.orders.onfillΒΆ

The saxo_openapi.contrib.orders.onfill module offers some classes to simplify the creation of TakeProfit, StopLoss and TrailingStopLoss OCO-orders.

The next example shows how to create a marketorder for FX:

go LONG 10000 EURUSD place a take profit order after fill, to take profit @1.14 place a stoploss order after fill, to sell in case price drops below 1.12
>>> import json
>>> from saxo_openapi import API
>>> import saxo_openapi.endpoints.trading as tr
>>> import saxo_openapi.endpoints.portfolio as pf
>>> from saxo_openapi.contrib.orders import (
...     tie_account_to_order,
...     MarketOrderFxSpot,
...     TakeProfitDetails,
...     StopLossDetails)
>>> token = "..."
>>> client = API(access_token=token)
>>> r = pf.accounts.AccountsMe()
>>> rv = client.request(r)
>>> # take the first account ...
>>> acct = rv['Data'][0]
>>> AccountKey = acct['AccountKey']

At time of writing EURUSD = 1.1250, lets take profit at 1.14, GoodTillCancel (default) lets take the loss when the price drops below at 1.12, GoodTillCancel (default)

TP-details can simply be constructed by using TakeProfitDetails:

>>> tpDetails = TakeProfitDetails(price=1.14)
>>> print(tpDetails.data)
{
  "OrderType": "Limit",
  "OrderDuration": {
      "DurationType": "GoodTillCancel"
  },
  "ManualOrder": False,
  "OrderPrice": "1.14000"
}

So, a MarketOrder with TP-details and SL-details:

>>> ordr = MarketOrderFxSpot(
...     Uic=21,
...     Amount=10000,
...     TakeProfitOnFill=TakeProfitDetails(price=1.14),
...     StopLosstOnFill=StopLosstDetails(price=1.12)
... )
>>> print(json.dumps(ordr.data, indent=4))
{
  "Uic": 21,
  "AssetType": "FxSpot",
  "Amount": 10000,
  "BuySell": "Buy",
  "OrderType": "Market",
  "ManualOrder": false,
  "AmountType": "Quantity",
  "OrderDuration": {
      DurationType": "FillOrKill"
  },
  "Orders": [
    {
      "OrderDuration": {
          DurationType": "GoodTillCancel"
      },
      "ManualOrder": false,
      "OrderType": "Limit",
      "OrderPrice": "1.14000",
      "BuySell": "Sell",
      "AssetType": "FxSpot",
      "Amount": 10000
    },
    {
      "OrderDuration": {
          DurationType": "GoodTillCancel"
      },
      "ManualOrder": false,
      "OrderType": "Stop",
      "OrderPrice": "1.12000",
      "BuySell": "Sell",
      "AssetType": "FxSpot",
      "Amount": 10000
    }
  ]
}

Before an order can be placed the account needs to be attached to the orderbody also:

>>> ordr = tie_account_to_order(AccountKey=AccountKey,
...             MarketOrderFxSpot(
...                 Uic=21,
...                 Amount=10000,
...                 TakeProfitOnFill=TakeProfitDetails(price=1.14),
...                 StopLosstOnFill=StopLosstDetails(price=1.12)
...             ))
>>> print(json.dumps(ordr, indent=4))
{
  "Uic": 21,
  "AssetType": "FxSpot",
  "Amount": 10000,
  "BuySell": "Buy",
  "OrderType": "Market",
  "AmountType": "Quantity",
  "ManualOrder": false,
  "OrderDuration": {
      DurationType": "FillOrKill"
  },
  "Orders": [
    {
      "OrderDuration": {
          DurationType": "GoodTillCancel"
      },
      "ManualOrder": false,
      "OrderType": "Limit",
      "OrderPrice": "1.14000",
      "BuySell": "Sell",
      "AssetType": "FxSpot",
      "Amount": 10000,
      "AccountKey": "fOA0tvOyQqW2aHpWi9P5bw=="
    },
    {
      "OrderDuration": {
          DurationType": "GoodTillCancel"
      },
      "ManualOrder": false,
      "OrderType": "Stop",
      "OrderPrice": "1.12000",
      "BuySell": "Sell",
      "AssetType": "FxSpot",
      "Amount": 10000,
      "AccountKey": "fOA0tvOyQqW2aHpWi9P5bw=="
    }
  ],
  "AccountKey": "fOA0tvOyQqW2aHpWi9P5bw=="
}

Now the order can be placed:

>>> r = tr.orders.Order(data=ordr)
>>> rv = client.request(r)
{
  "OrderId": "76697286",
  "Orders": [
    {
      "OrderId": "76697287"
    },
    {
      "OrderId": "76697288"
    }
  ]
}