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 the system places 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 the price falls below the trigger price (sorted from high to low)trigger_above: Orders that trigger when the price rises above the trigger price (sorted from 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 the system places 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 the system places 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 system places the order 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 whose conditions are met. 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
The system emits this event when you add a conditional order to a margin manager.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.ConditionalOrderCancelled
The system emits this event when you cancel a conditional order.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.ConditionalOrderExecuted
The system emits this event when a conditional order executes.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.ConditionalOrderInsufficientFunds
The system emits this event when a conditional order fails to execute due to insufficient funds.
packages/deepbook_margin/sources/tpsl.move. You probably need to run `pnpm prebuild` and restart the site.