Skip to content
Sign In

if - Conditional Execution

Advanced

Control webhook execution flow based on position state. Execute subsequent commands only when conditions are met.

What is if?

The if command allows you to conditionally execute or skip the remaining commands in the current symbol/API-key block based on position state.

  • Guard Against Empty Positions

    Skip protection orders if you don't have a position

  • Early Exit

    Stop processing if conditions aren't met

  • Multi-Symbol Strategies

    Check the position state independently in each block

Syntax

Smart Syntax
// Continue only if condition is true
if <condition> continue

// Exit (stop processing) if condition is true
if <condition> exit

Parameters

conditionRequired

Type: keyword

The condition to evaluate:

ConditionTrue When
posAnyYou have an open position (long or short)
posNoneYou have NO open position (flat)
posLongThe reported open position is long
posShortThe reported open position is short

continue | exitRequired

Type: action keyword

Both outcomes of each action:

ActionCondition trueCondition false
continueKeep processing subsequent commandsStop this block
exitStop this blockKeep processing subsequent commands

Real-World Examples

1. Only Add Protection If Position Exists

For a long position, continue only when the reported position is long before placing sell-side protection.

Smart Syntax
BTCUSDT on My_API_Key
// Only proceed for a long position
if posLong continue
// Sell-side protection for the long position
sl-or-tp short 100%p sl off 2% tp off 5%

Without the if, the sl-or-tp would fail with "no position" error. This guard skips protection when the reported position is flat or short.

2. Exit Early If No Position

Same logic, different phrasing - stop processing if there's nothing to protect.

Smart Syntax
ETHUSDT on My_API_Key
// Exit immediately if no position
if posNone exit
// Position exists - add protection
stoploss off 2%
takeprofit off 5%

If you're flat, this block stops at the if line. The following protection commands are skipped.

3. Close Only If a Position Exists

Only close position if one actually exists.

Smart Syntax
SOLUSDT on My_API_Key
// Guard against "nothing to close" errors
if posAny continue
// Close the position
pos 0
// Cancel any pending orders
rem all

Prevents errors when a close signal fires but position was already closed.

4. Multi-Step Strategy with Guards

Check the reported position after attempting a market entry.

Smart Syntax
BTCUSDT on My_API_Key
// Step 1: Try to enter
market long 0.01

// Step 2: Add sell-side protection if a long position exists
if posLong continue
sl-or-tp short 100%p sl off 2% tp off 5%

A long position may already have existed before the order. The if checks position state, not whether that specific entry succeeded; it is not an order-fill confirmation.

Best Practices

  • Use before SL/TP commands - Prevents "no position" errors when position doesn't exist

  • Place early in webhook - Guards at the start prevent wasted API calls

  • Match the position side - Use posLong before sell-side protection and posShort before buy-side protection

  • Use exact condition names - posAny, posNone, posLong and posShort are case-sensitive

Related Commands

sl-or-tp

- Often used after if to add protection

stoploss / takeprofit

- Require a position to exist

pos

- Position targeting, guard with if to prevent errors