Long and short rules¶
Write separate directional rules, or use opposite to reverse a supported directional condition.
An explicit rule and its counterpart¶
//@version=1
symbol: RELIANCE
run: each bar
size: 10% per instrument
entry:
long when: close > ema(10) and close > ema(20)
short when: opposite(long)
exit:
long when: close < ema(50)
short when: opposite of long
The short entry becomes close < ema(10) and close < ema(20). Entry and exit select counterparts within their own section. Each needs exactly one explicit source rule. Quantities are not inherited; use a global size or an explicit destination quantity.
Opposite is not logical NOT¶
Comparisons swap direction and crosses swap over/under. Thresholds and AND/OR stay the same. opposite(rsi(14) > 70) becomes rsi(14) < 70, not an oversold threshold of 30. Equality can match neither strict direction.
| Original condition | Derived opposite |
|---|---|
close > ema(20) |
close < ema(20) |
close >= ema(20) |
close <= ema(20) |
ema(20) crosses over ema(50) |
ema(20) crosses under ema(50) |
rsi(14) > 50 and close > ema(20) |
rsi(14) < 50 and close < ema(20) |
Logical not negates whether the entire condition is true. Directional opposite reverses its supported comparisons while preserving the connectors. If you want different thresholds for long and short, write separate explicit rules instead of expecting opposite to invent them.
Keep shared filters outside¶
//@version=1
symbol: RELIANCE
run: each bar
size: 10% per instrument
define eligible = after("09:30") and before("15:00")
define bullish = close > ema(10) and close > ema(20)
entry:
long when: eligible and bullish
short when: eligible and opposite(bullish)
Both sides use the same time filter. Only bullish reverses.
Limits¶
Use this for ordinary symbol/universe conditions with independent sizing. Equality tests, NOT, position/time predicates, opaque boolean functions, block boolean definitions, nested opposites, undefined/cyclic sources and conflicting destination rules are unsupported. Keep nondirectional filters outside it. Active shared-budget, hold, pairs/options and pyramiding forms are unsupported.
Every environment executing the strategy must support the operation; an older validator's unknown-function advisory is not proof it can run.