Strategy · Pine Script v6 · On the chart

PineForge - Relay Enum Regime

by pineforge-4pass· code credited to PineForge

This strategy executes trades based on one of two selectable regimes: trend-following or mean reversion.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code47
Settings8inputs
LicenceApache-2.0
Last update5 days ago23 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

47 lines of Pine Script v6 from validation/enum-relay-regime-01/strategy.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

strategy.pine
//@version=6
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 PineForge
// PineForge Corporate Strategy: Relay Enum Regime
// An owned enum selects one of two coherent signal regimes at compile/runtime.
strategy("PineForge — Relay Enum Regime", overlay=true,
     initial_capital=100000,
     default_qty_type=strategy.fixed, default_qty_value=2,
     pyramiding=0, commission_type=strategy.commission.percent,
     commission_value=0.05, slippage=1,
     margin_long=100, margin_short=100,
     process_orders_on_close=false, calc_on_order_fills=false)

enum RelayMode
    Trend
    Reversion

mode = input.enum(RelayMode.Trend, "Signal Regime", group="Signal")
fastLength = input.int(16, "Fast EMA", minval=2, group="Signal")
slowLength = input.int(45, "Slow EMA", minval=3, group="Signal")
rsiLength = input.int(12, "RSI Length", minval=2, group="Signal")
reversionEntry = input.float(31.0, "Reversion Entry RSI", minval=10, maxval=45, step=0.5, group="Signal")
reversionExit = input.float(53.0, "Reversion Exit RSI", minval=45, maxval=75, step=0.5, group="Signal")
atrLength = input.int(16, "ATR Length", minval=2, group="Risk")
stopAtr = input.float(2.0, "Stop ATR", minval=0.5, step=0.1, group="Risk")

fastEma = ta.ema(close, fastLength)
slowEma = ta.ema(close, slowLength)
momentum = ta.rsi(close, rsiLength)
atrValue = ta.atr(atrLength)

trendMode = mode == RelayMode.Trend
entrySignal = trendMode ? ta.crossover(fastEma, slowEma) : ta.crossunder(momentum, reversionEntry)
exitSignal = trendMode ? ta.crossunder(fastEma, slowEma) : ta.crossover(momentum, reversionExit)

if entrySignal
    strategy.entry("Relay Long", strategy.long)

if strategy.position_size > 0 and exitSignal
    strategy.close("Relay Long", comment=trendMode ? "Trend relay ended" : "Reversion completed")

if strategy.position_size > 0
    strategy.exit("Relay Guard", from_entry="Relay Long",
         stop=strategy.position_avg_price - atrValue * stopAtr)

plot(fastEma, "Fast EMA", color=color.aqua)
plot(slowEma, "Slow EMA", color=color.blue)

Overview

In trend mode, it enters long positions on a bullish EMA crossover and exits on a bearish crossover. In reversion mode, it buys when the RSI dips below an oversold threshold and closes when the RSI recovers above an exit level. Both regimes protect open positions with an ATR-based stop loss calculated from the average entry price. Fast and slow EMAs are plotted directly on the price chart.

Details

Published by

pineforge-4pass on GitHub

Code credit

PineForge, as written in the script's header

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Mean reversion, Moving averages, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingbacktestingparitypine-scriptpinescriptquantitative-financetest-corpustradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Signal RegimeenumRelayMode.Trendmode
Fast EMAint16fastLength
Slow EMAint45slowLength
RSI Lengthint12rsiLength
Reversion Entry RSIfloat31.0reversionEntry
Reversion Exit RSIfloat53.0reversionExit
ATR Lengthint16atrLength
Stop ATRfloat2.0stopAtr
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

106 stars

This trend-following strategy identifies buying opportunities during bullish pullbacks on the price chart. It defines a bull market when the 50-period simple moving average trades above the 200-period moving average. Long entries trigger when the RSI drops bel

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of Buy The Dips in Bull Market
4 stars

This strategy trades pullbacks in the direction of a moving-average trend and draws on the price chart. An uptrend requires the 50-period EMA above the 200-period EMA with price above the slower average, and a downtrend is the reverse. It enters long or short

Strategy v6 TrendOscillators & momentum
4 stars

This strategy executes mean-reversion trades based on RSI overbought and oversold levels directly on the price chart. It generates long signals when the RSI crosses back above the oversold boundary and short signals when it crosses below the overbought boundar

Strategy v5 Mean reversionOscillators & momentum
Updated 11 months ago · Custom licence GitHub repository of RSI Mean Reversion Strategy [Lab]
3 stars

The Buy/Sell Position Strategy combines a long-term Exponential Moving Average (EMA) with a short-term Relative Strength Index (RSI) to capture pullbacks in an uptrend. It enters a long position on the price chart when the price is above the EMA filter and the

Strategy v5 TrendOscillators & momentum
Updated 4 months ago · None stated GitHub repository of Buy/Sell Position Strategy

Calculates a long-term Exponential Moving Average as a baseline for fair value alongside a Relative Strength Index. Generates long entry signals when the close price trades at least five percent below the moving average while the RSI is oversold. Exits trades

Strategy v6 TrendOscillators & momentum
Updated 1 year ago · MIT GitHub repository of Buffett-Inspired Value Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers