S&R Power Divergence strategy

S&R Power Divergence strategy

AlgoMind Labs

What this strategy does

This strategy combines two signal modules (S&R Power and RSI Momentum Divergence Zones from ChartPrime) with a set of configurable filters and exits. You can run two independent entry systems (toggle each on/off), use a flexible waiting window, set behavior on opposite signals, and enable two exit systems that close positions around the S&R channel midline.

Everything evaluates on bar close (no intra-bar execution).


Modules & core logic

Module 1 — S&R Power

  • From the last i1_length bars we take extremes:
  • max = highest(high, i1_length), min = lowest(low, i1_length).
  • Build an S&R “channel” with offset ATR(200) * 0.5:
  • Top line: topLine = max + atr
  • Bottom line: botLine = min - atr
  • Buy Power zone top: buyTop = min + atr
  • Sell Power zone bottom: sellBottom = max - atr
  • Midline: mid = (topLine + botLine) / 2
  • “Diamond” confirmation:
  • Long: two bars ago Low was ≤ buyTop, and on the previous bar Low rose above buyTop.
  • Short: two bars ago High was ≥ sellBottom, and on the previous bar High fell below sellBottom.
  • Buy/Sell Power counts = number of bullish/bearish candles inside the i1_length window.

Module 2 — RSI Momentum Divergence Zones

  • RSI is computed on mom(close, 10) with length i2_rsiLength.
  • Divergences use RSI pivots (left/right “shoulders”: i2_divLookbackL/R) and higher/lower checks on RSI and price.
  • The script plots horizontal divergence levels:
  • Bullish levels (stored in bullLevels) from price lows,
  • Bearish levels (bearLevels) from price highs.
  • Each level tracks a broken/unbroken flag:
  • Bullish is considered broken if high rises above the level.
  • Bearish is broken if low falls below the level.
  • On-chart level count is limited by input.

Entry systems (both can be toggled)

System A — RSI Divergence Levels (Enable System A)

Signal when price touches the most recent active divergence level:

  • Long: on bar close, low intersects the latest bullish level.
  • Short: on bar close, high intersects the latest bearish level.

System B — S&R Diamond (Enable System B)

Signal is the S&R “diamond” confirmation:

  • Long: pattern “rise above buyTop”.
  • Short: pattern “drop below sellBottom”.
Entry occurs if any enabled system fires and all active filters pass, and direction is allowed (see “Trade Direction”).

Filters (each is independently switchable)

All filters are checked on the signal bar close and (optionally) during the waiting window. Entry executes once all enabled filters pass.

1) In-zone S&R filter

  • Long: LOW must be in the Buy Power zone (≤ buyTop).
  • Short: HIGH must be in the Sell Power zone (≥ sellBottom).

2) Diamond confirmation

  • Requires the corresponding S&R “diamond”.

3) Minimum BUY/SELL Power

  • Long: bullish candle count over the window ≥ threshold.
  • Short: bearish candle count ≥ threshold.

4) Minimum channel height (% of price)

  • Require (topLine - botLine) / close * 100 to be ≥ threshold.

5) Power difference (BUY−SELL / SELL−BUY)

  • Long: (buyCnt - sellCnt) ≥ threshold.
  • Short: (sellCnt - buyCnt) ≥ threshold.

6) Distance to S&R zone (%)

  • Long: price must not be more than X% above buyTop.
  • Short: price must not be more than X% below sellBottom.

7) Divergence proximity (by polarity)

Use the most recent divergence of the required polarity:

  • Long → last bullish divergence:
  • If unbroken: level must be below price by ≤ LONG: last Bull UNBROKEN level BELOW ≤ %.
  • If broken: level must be above price by ≤ LONG: last Bull BROKEN level ABOVE ≤ %.
  • Short → last bearish divergence:
  • If unbroken: level must be above price by ≤ SHORT: last Bear UNBROKEN level ABOVE ≤ %.
  • If broken: level must be below price by ≤ SHORT: last Bear BROKEN level BELOW ≤ %.
If the required level doesn’t exist yet (array is empty), the filter only blocks entry if it’s enabled and explicitly requires such a level; otherwise it’s neutral.

Waiting window

If a signal arrives but some enabled filters fail, the strategy starts a waiting window for a set number of bars:

  • Wait bars after LONG/SHORT signal:
  • 1 = “signal bar only”; >1 = you may enter later without a new signal if filters turn true during the window.
  • If filters become valid within the window, entry triggers at the close.
  • If the window expires first, the pending state is cleared.

Handling opposite signals (while in a position)

For each direction you can choose:

  • None — ignore opposite signals,
  • Close — close current position,
  • Reverse — close and flip to the opposite side (opens a new trade).
  • TP/SL and the “midline at entry” (for exit system 1) are recalculated on flip.

Take-profit & Stop-loss

On entry a linked strategy.exit is placed:

  • Long:
  • TP = entry * (1 + Long TP%)
  • SL = entry * (1 - Long SL%)
  • Short:
  • TP = entry * (1 - Short TP%)
  • SL = entry * (1 + Short SL%)

Guide lines draw at entry, TP and SL for visual tracking.


Exit systems (each can be toggled)

Both are symmetric by direction and evaluate on bar close.

Exit System 1 — Midline at entry

  • At entry, the strategy stores midAtEntry (the S&R channel midline on that bar).
  • Long: close the position when price closes above midAtEntry.
  • Short: close when price closes below midAtEntry.

Exit System 2 — Dynamic midline

  • Uses the current channel midline mid each bar.
  • Long: close when price closes above the current mid.
  • Short: close when price closes below the current mid.
You can enable both; whichever triggers first will close the trade.

Trade direction controls

Allow LONG and Allow SHORT fully enable/disable opening new positions of that type (they do not prevent exits of already open trades).


Parameters (quick map)

  • Entry Signal Systems
  • Enable System A — RSI Divergence Levels
  • Enable System B — S&R Diamond
  • Trade Direction
  • Allow LONG, Allow SHORT
  • Filters & Waiting
  • In-zone S&R, diamond, Buy/Sell Power, channel height, power difference
  • Wait bars after LONG/SHORT signal
  • S&R Distance Filter
  • Enable per side, with separate % thresholds
  • Divergence Proximity Filter
  • Enable per side, with four independent % thresholds (bull/bear × broken/unbroken)
  • Exit Systems
  • Exit System 1: ... at ENTRY
  • Exit System 2: ... CURRENT

Typical workflow

  1. Enable desired entry systems (A/B) and trade directions (Long/Short).
  2. Turn on filters and set thresholds. For conservative setups, start with:
  3. S&R in-zone + S&R Distance + Divergence Proximity,
  4. Waiting window of 2–3 bars.
  5. Choose exits: enabling both exit systems often works well—Exit 1 acts like a structure-aware trailing stop, Exit 2 adapts dynamically.
  6. Backtest on your symbol/TF and tune % thresholds and lengths.

Notes & limitations

  • All logic is evaluated on bar close (barstate.isconfirmed).
  • Divergence levels appear with a pivot delay (lookback L/R)—by design to avoid repainting.
  • If no suitable divergences exist yet, proximity filters won’t interfere unless you turn them on and require a specific level.
  • The S&R channel depends on i1_length and ATR(200). On very noisy instruments, consider tuning i1_length and filter thresholds.

Tuning tips

  • Timeframe: depends on the asset. Crypto often works well on 5m–2h; stocks/FX on 1h–4h.
  • Proximity %: commonly 0.4–0.7 for both sides; adjust to volatility.
  • S&R Distance %: try 0.3–0.8 to avoid chasing price far from the zone.

FAQ

Q: A signal appeared but no entry—why?

A: One (or more) enabled filters likely blocked it (S&R Distance, Proximity, channel height, etc.). If a pending window was set, the entry may still occur on a later bar when filters pass.

Q: What if systems A and B disagree?

A: Entries are combined as A OR B. Any enabled system can trigger an entry, provided all enabled filters pass.

Q: Should I enable both exit systems?

A: You can. They’re independent; the earliest to trigger closes the trade. In trends, the dynamic midline often exits earlier; in ranges, the fixed midAtEntry can serve as a protective exit.

Report Page