Cancel on disconnect
In case of network outages, extreme latency, or similar issues your orders can remain open and be filled at unfavorable prices while you are disconnected.
To prevent this, you can use Cancel on disconnect which automatically cancels your open orders if there is no activity from you within a set period of time.
You cannot use Cancel on disconnect for market orders.
How it works
- You send a
cancelOrdersAfterrequest to:- Create a group identifier for new orders:
codGroupId. - Set the time countdown timer after which the orders are canceled:
expiryAfterSeconds.
- Create a group identifier for new orders:
- You create orders and add that
codGroupIdin the body of the requests. - You keep sending
cancelOrdersAfterrequests to reset the countdown timer. - If there is no
cancelOrdersAftermessage from you before the countdown timer expires, we cancel all open orders with thatcodGroupId.
Orders that are cancelled on disconnect always have cancelOnDisconnect specified as their restatementReason.
Prerequisites
Before you start, make sure you have:
Also, be sure to read the Authentication instructions for WebSocket and REST API.
Step 1: Create a codGroupId
Firstly, you need to create a group identifier that you can specify when creating new orders. This enables Cancel on disconnect for those open orders and sets the countdown timer after which the orders are automatically canceled.
To create a group identifier:
- Send either a WebSocket or REST API request to create a
codGroupId. - In the body of the request specify:
codGroupId: your identifier for a group of orders.expiryAfterSeconds: your countdown timer in seconds after which open orders are canceled. The minimum allowed value is 10.
- WebSocket
- REST
{
"action": "privateCancelOrdersAfter",
"codGroupId": 1,
"expiryAfterSeconds": 30,
"requestId": 1
}
curl -L 'https://api.bitvavo.com/v2/order' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Bitvavo-Access-Key: <Bitvavo-Access-Key>' \
-H 'Bitvavo-Access-Timestamp: <Bitvavo-Access-Timestamp>' \
-H 'Bitvavo-Access-Signature: <Bitvavo-Access-Signature>' \
-H 'Bitvavo-Access-Window: <Bitvavo-Access-Window>' \
-d '{
"codGroupId": 1,
"expiryAfterSeconds": 30
}'
- The
codGroupIdis created with thetimeOfExpirySecondscountdown timer in the number of seconds you specified.
You can now specify that codGroupId when creating new orders to enable the Cancel on disconnect.
Step 2: Create orders with the codGroupId
After you have created a codGroupId, you can create orders as usual with either the WebSocket or REST API.
To add an order to a group:
- Send either a WebSocket or REST API request to create an order.
- In the body of the request specify the
codGroupIdparameter with the value you created.
- WebSocket
- REST
{
"action": "privateCreateOrder",
"requestId": 1,
"market": "BTC-EUR",
"side": "buy",
"orderType": "market",
"operatorId": 543462,
"clientOrderId": "2be7d0df-d8dc-7b93-a550-8876f3b393e9",
"amount": "1.567",
"amountQuote": "5000",
"price": "6000",
"triggerAmount": "4000",
"triggerType": "price",
"triggerReference": "lastTrade",
"timeInForce": "GTC",
"postOnly": false,
"selfTradePrevention": "decrementAndCancel",
"responseRequired": true,
"codGroupId": 1
}
curl -L 'https://api.bitvavo.com/v2/order' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Bitvavo-Access-Key: <Bitvavo-Access-Key>' \
-H 'Bitvavo-Access-Timestamp: <Bitvavo-Access-Timestamp>' \
-H 'Bitvavo-Access-Signature: <Bitvavo-Access-Signature>' \
-H 'Bitvavo-Access-Window: <Bitvavo-Access-Window>' \
-d '{
"market": "BTC-EUR",
"side": "buy",
"orderType": "market",
"operatorId": 543462,
"clientOrderId": "string",
"amount": "1.567",
"amountQuote": "5000",
"price": "6000",
"triggerAmount": "4000",
"triggerType": "price",
"triggerReference": "lastTrade",
"timeInForce": "GTC",
"postOnly": false,
"selfTradePrevention": "decrementAndCancel",
"responseRequired": true,
"codGroupId": 1
}'
- An order is created in the order book and added to the
codGroupIdyou specified.
Cancel on disconnect is now active for your order. You now have to keep sending requests periodically to reset the countdown timer.
If you want to disable Cancel on disconnect, you need to remove the group.
Step 3: Reset the countdown timer
To reset the countdown timer you can use the same request that you used to create the codGroupId:
- Send either a WebSocket or REST API request.
- In the request body, specify the
codGroupIdyou want to send the heartbeat for. - Keep sending requests to before the specified countdown timer expires.
To prevent consuming your rate limits, avoid sending reset messages too often. For example, if you set the expiryAfterSeconds to 30 seconds, we recommend that you send a reset message once every 15 seconds.
For example:
-
You send a WebSocket message:
privateCancelOrdersAfter{
"action": "privateCancelOrdersAfter",
"codGroupId": 1,
"expiryAfterSeconds": 30,
"requestId": 12
} -
If successful, the countdown timer resets and you receive the 200 OK response:
privateCancelOrdersAfter response{
"action": "privateCancelOrdersAfter",
"requestId": 12,
"response": {
"codGroupId": 1,
"timeOfExpirySeconds": 17202139111
}
} -
If it fails, the countdown timer does not reset and you receive the 400 Bad Request response:
errorCode: this is always 433error: description of the reason. Can be:-
limit of group ids is reached: the maximum of number of groups -
invalid group id: the invalidgroupIdprovided -
invalid expiryAfterSeconds: the invalid value
error response{
"errorCode":433,
"error": "limit of group ids is reached: 5"
}-
If this happens, to reset the countdown timer, you need to send a new cancelOrdersAfter message with the correct parameters depending on the rejectionReason you received.
Possible rejection reasons are:
LIMIT_REACHED: you have reached the maximum number of groups you can create. Wait for one of the existing groups to expire or remove a group.INVALID_GROUP_ID: check thecodGroupIdand send the request again.INVALID_EXPIRY_AFTER_SECONDS: check theexpiryAfterSecondsvalue and send the request again.
(Optional) Remove the group
If you want to disable Cancel on disconnect but keep your orders open, you can remove the group you created. Those orders will no longer have a countdown timer.
However, be aware that removing the group does not automatically remove the codGroupId parameter from those open orders.
This is important because you can reuse the codGroupId to create a new group. If you use the same codGroupId to create a new group, the open orders that already have that codGroupId
will not have cancel on disconnect enabled.
To remove a group:
- Send either a WebSocket message or REST API request and specify:
codGroupId: the group identifier you want to remove.expiryAfterSeconds: set this to 0.
- WebSocket
- REST
{
"action": "privateCancelOrdersAfter",
"codGroupId": 1,
"expiryAfterSeconds": 0,
"requestId": 2
}
curl -L -X POST 'https://api.bitvavo.com/v2/cancelOrdersAfter' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Bitvavo-Access-Key: <Bitvavo-Access-Key>' \
-H 'Bitvavo-Access-Timestamp: <Bitvavo-Access-Key>' \
-H 'Bitvavo-Access-Signature: <Bitvavo-Access-Key>' \
-H 'Bitvavo-Access-Window: <Bitvavo-Access-Key>' \
-d '{
"codGroupId": 1,
"expiryAfterSeconds": 0
}'
- If successful, you receive the 200 OK response and your
codGroupIdis removed. Your orders with thatcodGroupIdstay open in the order book.