Skip to content

Conversation

@pranay123-stack
Copy link

Description

Fixes #923 - Stop-loss orders are now automatically cancelled when the associated position is completely closed.

Problem

When users closed their positions, trigger orders (stop-loss/take-profit) remained active, creating a risk of:

  • Unintended order execution
  • Opening new unwanted positions
  • Financial losses for users

Solution

  • Added cancel_trigger_orders_for_closed_position() function to handle automatic cancellation
  • Modified fulfill_perp_order() to detect when a position reaches zero and trigger cancellation
  • Only affects trigger orders (TriggerMarket and TriggerLimit) for the specific market

Changes Made

  1. New Function: cancel_trigger_orders_for_closed_position() (lines 587-636)

    • Iterates through all user orders
    • Filters for trigger orders matching the closed position's market
    • Cancels each matching trigger order
  2. Modified Function: fulfill_perp_order() (lines 2096-2112)

    • Checks if position is completely closed after fulfillment (base_asset_amount == 0)
    • Calls cancellation function for all trigger orders on that market

Testing

  • ✅ Code compiles successfully
  • ✅ Follows existing code patterns and conventions
  • ✅ Proper error handling with DriftResult
  • ✅ No breaking changes to existing functionality

Risk Mitigation

This fix prevents the following scenario:

  1. User opens a short position with a stop-loss order
  2. User manually closes the short position
  3. Previously: SL order remained active and could execute unexpectedly
  4. Now: SL order is automatically cancelled when position closes

@pranay123-stack pranay123-stack force-pushed the fix/cancel-trigger-orders-on-position-close branch from 8fc22ff to 629c26a Compare November 23, 2025 06:39
Resolves issue drift-labs#923 where stop-loss/take-profit (trigger) orders were not being
automatically cancelled when a user closed their position.

## Problem
When users closed their positions, trigger orders remained active, creating risks:
- Unintended order execution after position closure
- Opening new unwanted positions
- Potential financial losses for users

## Solution
Implemented automatic cancellation of trigger orders when positions are fully closed:

### Code Changes
1. **New Function**: `cancel_trigger_orders_for_closed_position()` (lines 587-668)
   - Iterates through all user orders
   - Identifies and cancels trigger orders (TriggerMarket/TriggerLimit) for a specific market
   - Properly documented with rustdoc comments

2. **Modified**: `fulfill_perp_order()` (lines 2128-2148)
   - Checks if perp position is completely closed (base_asset_amount == 0)
   - Automatically cancels all trigger orders for that market

3. **Modified**: `fulfill_spot_order()` (lines 4788-4808)
   - Checks if spot position is completely closed (scaled_balance == 0)
   - Automatically cancels all trigger orders for that market

### Tests
Added comprehensive integration test suite in `tests/cancelTriggerOrdersOnPositionClose.ts`:
- Test 1: Verifies trigger orders are cancelled when position fully closes
- Test 2: Verifies trigger orders remain active on partial position close
- Test 3: Verifies multiple trigger orders are all cancelled together

## Coverage
- ✅ Perp markets (perpetual futures)
- ✅ Spot markets
- ✅ Multiple trigger orders per market
- ✅ Partial vs full position closure handling

## Testing
- Code compiles successfully with no errors
- Follows existing code patterns and conventions
- Proper error handling with DriftResult
- Comprehensive inline documentation

Author: Pranay
Email: pranaygaurav4555@gmail.com
GitHub: @pranay123-stack
@pranay123-stack pranay123-stack force-pushed the fix/cancel-trigger-orders-on-position-close branch from 629c26a to 8d9f930 Compare November 23, 2025 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected SL Order

1 participant