# Take Profit Stop Loss SDK

*[Documentation index](/llms.txt) · [Full index](/llms-full.txt)*

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`).

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

### `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**

- `managerAddress`: String representing the address of the margin manager whose orders to execute.
- `poolKey`: String that identifies the DeepBook pool.
- `maxOrdersToExecute`: Number representing the maximum number of orders to execute in this call.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

## 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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

### `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`).

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

## Read-only functions

### `conditionalOrderIds`

Query all conditional order IDs for a margin manager.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

### `conditionalOrder`

Query a specific conditional order by ID.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

### `lowestTriggerAbovePrice`, `highestTriggerBelowPrice`

Query trigger prices for conditional orders. `lowestTriggerAbovePrice` returns the lowest trigger price among trigger-above orders (or `max_u64` if none exist). `highestTriggerBelowPrice` returns the highest trigger price among trigger-below orders (or `0` if none exist).

<!-- External code reference: packages/deepbook-v3/src/transactions/marginTPSL.ts -->

## Examples

### Set up a stop loss order

```tsx
// 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

```tsx
// 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)

```tsx
// Example: Execute conditional orders as a keeper
const executeOrders = (tx: Transaction) => {
	const managerAddress = '0x...'; // Address of margin manager
	// Execute up to 10 triggered orders
	traderClient.marginTPSL.executeConditionalOrders(managerAddress, 'SUI_USDC', 10)(tx);
};
```
