if - Conditional Execution
AdvancedControl 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
// Continue only if condition is true
if <condition> continue
// Exit (stop processing) if condition is true
if <condition> exitParameters
conditionRequiredType: keyword
The condition to evaluate:
| Condition | True When |
|---|---|
posAny | You have an open position (long or short) |
posNone | You have NO open position (flat) |
posLong | The reported open position is long |
posShort | The reported open position is short |
continue | exitRequiredType: action keyword
Both outcomes of each action:
| Action | Condition true | Condition false |
|---|---|---|
continue | Keep processing subsequent commands | Stop this block |
exit | Stop this block | Keep processing subsequent commands |
Understanding the Logic
An exit skips the rest of this block. Other blocks in the message are unaffected:
if posAny continue= "If I have a position, keep going. Otherwise, STOP."if posNone exit= "If I don't have a position, STOP. Otherwise, keep going."
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.
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.
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.
SOLUSDT on My_API_Key
// Guard against "nothing to close" errors
if posAny continue
// Close the position
pos 0
// Cancel any pending orders
rem allPrevents 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.
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
posLongbefore sell-side protection andposShortbefore buy-side protectionUse exact condition names -
posAny,posNone,posLongandposShortare 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