Skip to content
Sign In

Learning / Syntax / smart-oco

smart-oco - Position-Aware OCO Monitoring

Advanced OCO monitoring with automatic order cancellation when position closes or flips direction.

What is smart-oco?

smart-oco is a position-aware version of the oco command. It monitors your existing orders against your position state. When your position goes to zero or flips direction (long to short or vice versa), it automatically cancels the monitored orders.

Key Features:

  • Auto-cancel on position close: When your position goes to zero, all monitored orders are cancelled

  • Auto-cancel on position flip: When position switches from long to short (or vice versa), incompatible orders are cancelled

  • Background monitoring: Runs continuously in the background, checking the position and cancelling tracked orders when a condition is met

smart-oco vs oco: Key Differences

Understanding the difference between oco and smart-oco is crucial for choosing the right tool:

Featureocosmart-oco
Monitors OrdersYesYes
Monitors PositionNoYes
Cancels on Order FillPrimary ordersNo; position conditions only
Cancels on Position CloseNoYes (configurable)
Cancels on Position FlipNoYes (configurable)
ComplexitySimpleAdvanced

How It Works

smart-oco runs a background process that continuously monitors:

1. Order Status Changes

Individual fills or cancellations do not trigger Smart OCO. Partial exits can leave the remaining ladder active while the monitored position stays open on the same side.

2. Position Size Changes (cancelOnZero)

When cancelOnZero=true (default), the system watches your position size:

  • If position goes to 0, all monitored orders are cancelled
  • Prevents orphaned TP/SL orders after manual position close
  • Useful when closing positions via TradingView or exchange UI

3. Position Direction Changes (cancelOnFlip)

When cancelOnFlip=true (default), the system detects direction flips:

  • Long position switches to short: cancels all buy orders
  • Short position switches to long: cancels all sell orders
  • Critical for exchanges without hedge mode enabled
  • Prevents accidental order execution in wrong direction

Syntax

Both syntaxes are supported:

Smart Syntax
smart-oco [<which>] [cancelOnZero=<bool>] [cancelOnFlip=<bool>]
oco smart [<which>] [cancelOnZero=<bool>] [cancelOnFlip=<bool>]

Note: Both smart-oco and oco smart are equivalent. Use whichever feels more natural to you.

Parameters

<which>Optional

Type: keyword or tag | Default: all

Which orders to monitor. Determines the order list ONCE at startup:

ValueOrders Monitored
allAll open orders for this symbol (default)
sessionOnly orders created in this webhook execution
buy or longAll buy/long orders only
sell or shortAll sell/short orders only
#tagOrders with specific tag (e.g., #entry)

cancelOnZeroOptionalDefault: true

Type: boolean | Values: true, false | Default: true

Controls whether to cancel monitored orders when position size goes to zero:

  • cancelOnZero=true (recommended)

    Cancels orders when position closes. Prevents orphaned TP/SL orders.

  • cancelOnZero=false

    Keeps orders active even when position closes. Use if you plan to re-enter.


cancelOnFlipOptionalDefault: true

Type: boolean | Values: true, false | Default: true

Controls whether to cancel monitored orders when position direction flips:

  • cancelOnFlip=true (recommended)

    Cancels incompatible orders when position flips long to short or short to long.

    Essential for exchanges without hedge mode.

  • cancelOnFlip=false

    Keeps all orders active regardless of position direction.

    Use only if you have hedge mode enabled and want complex strategies.

Real-World Examples

1. Smart OCO on All Orders (Default Behavior)

Enable smart OCO monitoring on all orders with default settings.

Smart Syntax
BTCUSDT on My_API_Key
// Enter position
market long 0.01
// Place TP and SL
limit short 100%p off e5% reduce
stoploss short 100%p off e2%
// Enable smart OCO with all defaults
smart-oco

Monitors all orders. If position closes or flips, orders are cancelled automatically. Cleanup is requested when the monitored position closes or changes side, not merely because one order fills.

2. Smart OCO Only on Sell Orders

Monitor only sell orders (TP/SL for long position) with smart features.

Smart Syntax
ETHUSDT on My_API_Key
market long 0.5
// Exit orders
limit short 50%p off e3% reduce
stoploss short 100%p off e1.5%
limit short 50%p off e6% reduce
// Smart OCO only on sell orders
smart-oco sell

Only sell orders are monitored. If you have buy entry orders, they won't be affected by the smart OCO logic.

3. Disable Auto-Cancel on Position Flip

Keep orders active when position flips, but still cancel when position closes.

Smart Syntax
BTCUSDT on My_API_Key
market long 0.01
limit short 100%p off e4% reduce
stoploss short 100%p off e2%
// Smart OCO but keep orders on flip
smart-oco sell cancelOnZero=true cancelOnFlip=false

Orders are cancelled if position goes to zero, but NOT if position flips direction. Useful for advanced strategies where you want orders to survive direction changes.

4. Smart OCO with Tagged Orders

Monitor only orders with a specific tag, perfect for complex multi-level strategies.

Smart Syntax
BTCUSDT on My_API_Key
market long 0.02
// Primary exits (tight)
limit short 50%p off e2% reduce #primary
stoploss short 50%p off e1% #primary
// Secondary exits (wide)
limit short 50%p off e5% reduce #secondary
// Smart OCO only on primary group
smart-oco #primary

Only #primary tagged orders have smart OCO monitoring with default close/flip settings. The #secondary order remains independent.

5. Complete Setup: Entry + TP + SL + Smart OCO

Full trading setup with position entry, exits, and intelligent monitoring.

Smart Syntax
BTCUSDT on My_API_Key
// Clean slate
rem all
pos 0
// Enter long position
market long 0.01
// Take profit levels
limit short 30%p off e3% reduce #tp1
limit short 40%p off e5% reduce #tp2
limit short 30%p off e8% reduce #tp3
// Stop loss
stoploss short 100%p off e2% #sl
// Smart monitoring
smart-oco sell

Complete risk management: 3-level scaled take profits + stop loss, all monitored by smart-oco. If position closes manually or flips, all exit orders are cancelled automatically.

6. Position Flip Scenario with Auto-Cancel

Demonstrate how smart-oco protects you when position flips direction.

Smart Syntax
BTCUSDT on My_API_Key
// Start with long position
market long 0.01
limit short 100%p off e4% reduce #tp
stoploss short 100%p off e2% #sl
smart-oco sell
// --- Later: position flips to short ---
// (e.g., via manual trade or another webhook)
// smart-oco automatically cancels the sell orders
// because they're now incompatible with short position

When position flips from long to short, smart-oco detects the change and cancels all sell orders (which were meant to close long, but would now ADD to short position).

When to Use smart-oco

Protection from Orphaned Orders After Position Flip

If you close a long position and immediately open a short (or vice versa), old TP/SL orders become dangerous.smart-oco automatically cancels them, preventing accidental execution in the wrong direction.

Trading with Hedge Mode Disabled

Without hedge mode, you can't have both long and short positions simultaneously. smart-ocoensures old orders don't interfere when you switch sides. This is CRITICAL on most exchanges.

Strategies with Multiple Entry Points

If you have multiple entry orders and position management orders, smart-oco keeps everything synchronized. When position closes, all related orders are cleaned up automatically.

Manual Position Management

If you sometimes close positions manually via TradingView or exchange UI, smart-ocodetects the closure and cancels webhook-placed orders automatically. Inspect the cancellation results.

Best Practices

  • Use default settings initially - smart-oco without parameters is usually the best starting point (both cancelOnZero and cancelOnFlip enabled)

  • Essential without hedge mode - If your exchange doesn't support hedge mode, ALWAYS use smart-oco instead of oco

  • Use specific scopes - Instead of smart-oco all, prefersmart-oco sell or smart-oco #tag for better control

  • Test with small positions first - Understand how position monitoring works before using in production

  • Combine with session scope for safety - Use smart-oco sessionto avoid affecting old orders from previous webhooks

  • Monitor execution history - Check logs to verify smart-oco is cancelling orders as expected

Related Commands

oco

- Standard OCO monitoring (order fills only, no position tracking)

reset-oco

- Stop all OCO monitoring processes

position

- View or modify current position (what smart-oco monitors)

sl-or-tp

- Combined SL+TP with automatic OCO (simpler alternative for basic setups)

rem

- Manually cancel orders