Take Profit Stop Loss
The Take Profit Stop Loss (TPSL) module enables conditional orders that automatically execute when certain price conditions are met. This allows traders to set up automated trading strategies that protect against losses (stop loss) or lock in profits (take profit) without requiring constant monitoring.
How TPSL works
- Create a condition: Define whether the order should trigger when the price goes above or below a specified trigger price.
- Create a pending order: Specify the order details (limit or market order) that will be placed when the condition is met.
- Add conditional order: Combine the condition and pending order, and add them to your margin manager.
- Execution: Anyone can call the permissionless
execute_conditional_ordersfunction to execute orders whose conditions are met. This is typically handled by keepers or bots monitoring the market.
Conditional orders are stored in sorted vectors for efficient execution:
trigger_below: Orders that trigger when price falls below the trigger price (sorted high to low)trigger_above: Orders that trigger when price rises above the trigger price (sorted low to high)
API
Helper functions
Use these functions to create conditions and pending orders for conditional orders.
Create a condition
Create a new condition that specifies when the order should trigger.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.Create a pending limit order
Create a pending limit order that will be placed when the condition is met. Order type must be no_restriction or immediate_or_cancel.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.Create a pending market order
Create a pending market order that will be placed when the condition is met.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.Manage conditional orders
These functions are exposed on the MarginManager to manage conditional orders.
Add conditional order
Add a conditional order to the margin manager. The order will be placed when the condition is met. Validates that the trigger condition is valid relative to the current price.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Cancel conditional order
Cancel a specific conditional order by ID.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Cancel all conditional orders
Cancel all conditional orders for the margin manager.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Execute conditional orders
Execute conditional orders that have been triggered. This is a permissionless function that can be called by anyone (typically keepers or bots).
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Read endpoints
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.Events
ConditionalOrderAdded
Emitted when a conditional order is added to a margin manager.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.ConditionalOrderCancelled
Emitted when a conditional order is cancelled.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.ConditionalOrderExecuted
Emitted when a conditional order is executed.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.ConditionalOrderInsufficientFunds
Emitted when a conditional order cannot be executed due to insufficient funds.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.Related links
The DeepBook Margin package on GitHub.