Skip to main content

Take Profit Stop Loss SDK

The TPSL (Take Profit Stop Loss) SDK provides functions for managing conditional orders that automatically execute when certain price conditions are met.

TPSL functions

The DeepBook Margin SDK provides the following functions for managing conditional orders.

addConditionalOrder

Use addConditionalOrder to add a conditional order that executes when a price condition is met. The call returns a function that takes a Transaction object.

Parameters

  • marginManagerKey: String that identifies the margin manager.
  • conditionalOrderId: Number representing the unique ID for this conditional order.
  • triggerBelowPrice: Boolean indicating whether to trigger when price falls below the trigger price.
  • triggerPrice: Number representing the price at which to trigger the order.
  • pendingOrder: Object containing the order details (either PendingLimitOrderParams or PendingMarketOrderParams).

cancelConditionalOrder

Use cancelConditionalOrder to cancel a specific conditional order. The call returns a function that takes a Transaction object.

Parameters

  • marginManagerKey: String that identifies the margin manager.
  • conditionalOrderId: String representing the ID of the conditional order to cancel.

cancelAllConditionalOrders

Use cancelAllConditionalOrders to cancel all conditional orders for a margin manager. The call returns a function that takes a Transaction object.

Parameters

  • marginManagerKey: String that identifies the margin manager.

executeConditionalOrders

Use executeConditionalOrders to execute conditional orders that have been triggered. This is a permissionless function that can be called by anyone. The call returns a function that takes a Transaction object.

Parameters

  • marginManagerKey: String that identifies the margin manager.
  • maxOrdersToExecute: Number representing the maximum number of orders to execute in this call.

Helper functions

These helper functions create conditions and pending orders for conditional orders.

newCondition

Use newCondition to create a trigger condition for a conditional order. The call returns a function that takes a Transaction object.

Parameters

  • poolKey: String that identifies the pool.
  • triggerBelowPrice: Boolean indicating whether to trigger when price falls below the trigger price.
  • triggerPrice: Number representing the price at which to trigger.

newPendingLimitOrder

Use newPendingLimitOrder to create a pending limit order for use in conditional orders. The call returns a function that takes a Transaction object.

Parameters

  • poolKey: String that identifies the pool.
  • params: PendingLimitOrderParams object containing:
    • clientOrderId: Number for client tracking.
    • orderType: Optional order type (default: NO_RESTRICTION).
    • selfMatchingOption: Optional self-matching option (default: SELF_MATCHING_ALLOWED).
    • price: Number representing the limit price.
    • quantity: Number representing the order quantity.
    • isBid: Boolean indicating if this is a buy order.
    • payWithDeep: Optional boolean for fee payment (default: true).
    • expireTimestamp: Optional expiration timestamp.

newPendingMarketOrder

Use newPendingMarketOrder to create a pending market order for use in conditional orders. The call returns a function that takes a Transaction object.

Parameters

  • poolKey: String that identifies the pool.
  • params: PendingMarketOrderParams object containing:
    • clientOrderId: Number for client tracking.
    • selfMatchingOption: Optional self-matching option (default: SELF_MATCHING_ALLOWED).
    • quantity: Number representing the order quantity.
    • isBid: Boolean indicating if this is a buy order.
    • payWithDeep: Optional boolean for fee payment (default: true).

Read-only functions

conditionalOrderIds

Query all conditional order IDs for a margin manager.

conditionalOrder

Query a specific conditional order by ID.

triggerBelowOrders, triggerAboveOrders

Query the lists of conditional orders sorted by trigger price.

numConditionalOrders

Query the total number of conditional orders for a margin manager.

lowestTriggerAbovePrice, highestTriggerBelowPrice

Query trigger prices for conditional orders.

Examples

Set up a stop loss order

// Example: Create a stop loss order that sells when price drops below 2.0
const setStopLoss = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
traderClient.marginTPSL.addConditionalOrder({
marginManagerKey: managerKey,
conditionalOrderId: 1,
triggerBelowPrice: true, // Trigger when price falls below
triggerPrice: 2.0,
pendingOrder: {
clientOrderId: 100,
quantity: 50,
isBid: false, // Sell order
payWithDeep: true,
},
})(tx);
};

Set up a take profit order

// Example: Create a take profit order that sells when price rises above 5.0
const setTakeProfit = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
traderClient.marginTPSL.addConditionalOrder({
marginManagerKey: managerKey,
conditionalOrderId: 2,
triggerBelowPrice: false, // Trigger when price rises above
triggerPrice: 5.0,
pendingOrder: {
clientOrderId: 101,
price: 5.0, // Limit order at 5.0
quantity: 50,
isBid: false, // Sell order
payWithDeep: true,
},
})(tx);
};

Execute triggered orders (keeper)

// Example: Execute conditional orders as a keeper
const executeOrders = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
// Execute up to 10 triggered orders
traderClient.marginTPSL.executeConditionalOrders(managerKey, 10)(tx);
};
DeepBook Margin package

The DeepBook Margin package on GitHub.

DeepBookV3 SDK node package

The DeepBookV3 SDK node package on NPM.