Strategy · Pine Script v6 · Own pane

S Trailing

by GammaExpansion

S Trailing is a Pine Script v6 strategy that applies trailing stops to periodic cycle-based entries.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code46
Settings0inputs
LicenceMIT
Last update7 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

46 lines of Pine Script v6 from packages/golden/fixtures/strategy/v6/S_trailing/source.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

source.pine
//@version=6
// trailing stop:trail_price(激活价)/ trail_points(激活距离,tick)+ trail_offset(tick),多空各一
strategy("S_trailing", overlay = false,
  initial_capital = 100000, currency = currency.NONE,
  default_qty_type = strategy.fixed, default_qty_value = 1, pyramiding = 1,
  commission_type = strategy.commission.percent, commission_value = 0, slippage = 0,
  margin_long = 0, margin_short = 0, close_entries_rule = "FIFO",
  process_orders_on_close = false, calc_on_order_fills = false, calc_on_every_tick = false,
  backtest_fill_limits_assumption = 0, use_bar_magnifier = false, fill_orders_on_standard_ohlc = false)
int cycleLen = 40
int phase = bar_index % cycleLen
int cycle = math.floor(bar_index / cycleLen)

float trailPxL = close * (1 + 0.003)
float trailPxS = close * (1 - 0.003)
if phase == 0
    strategy.entry("L", strategy.long)
    strategy.exit("TrailPx", "L", trail_price = trailPxL, trail_offset = 30)
if phase == 12
    strategy.close_all(comment = "c1")
if phase == 14
    strategy.entry("S", strategy.short)
    strategy.exit("TrailPxS", "S", trail_price = trailPxS, trail_offset = 30)
if phase == 26
    strategy.close_all(comment = "c2")
if phase == 28
    strategy.entry("L", strategy.long)
    strategy.exit("TrailPts", "L", trail_points = 30, trail_offset = 20)
if phase == 38
    strategy.close_all(comment = "c3")
if phase == 39
    strategy.cancel_all()
plot(trailPxL, "trail_price_long")
plot(trailPxS, "trail_price_short")

// ---- 对齐列 ----
plot(bar_index, "bar_index")
plot(cycle, "cycle")
plot(phase, "phase")
plot(strategy.position_size, "pos")
plot(strategy.position_avg_price, "avg_price")
plot(strategy.equity, "equity")
plot(strategy.openprofit, "openprofit")
plot(strategy.netprofit, "netprofit")
plot(strategy.closedtrades, "closedtrades")
plot(strategy.opentrades, "opentrades")

Overview

S Trailing divides the bar index into cycles of 40 bars and activates at specific phases: long entry with trailing price stop at phase 0, short entry at phase 14, plus tests of trailing points stop at phase 28. Positions close automatically at phases 12, 26 and 38 while cancelling orders at phase 39. The strategy draws trailing price lines and various metrics including bar index, cycle, phase, position size, average price, equity, open and closed trades in a separate pane. No user inputs exist and all parameters are hardcoded.

Details

Published by

GammaExpansion on GitHub

Draws

Separate pane below the chart

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.

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

Quick Flip Scalper

by TradersPost
228 stars

This strategy executes an opening-range liquidity fade based on the first candle of a session. It qualifies the opening range against the daily ATR to confirm manipulation or liquidity spikes. If qualified, it searches for reversal candlestick patterns, such a

Strategy v6 Sessions & timeCandlestick patterns
Updated 3 months ago · None stated GitHub repository of Quick Flip Scalper
2 stars

This strategy identifies price consolidation ranges during Asia session hours and trades mean-reversion reclaims following liquidity sweeps. It marks an initial range box, measures sweep depths past box boundaries using ATR multiples, and enters trades when pr

Strategy v6 Sessions & timeDivergence
Updated 4 weeks ago · Custom licence GitHub repository of MP Asia Range Sweep & Reclaim

A long-only strategy for short-term crypto trading that combines Bollinger Bands, RSI, a 9-period EMA, a 21-period SMA and a Fibonacci window covering about ten minutes of bars. It sizes positions as a percentage of equity and can require a minimum gain before

Strategy v6 ScalpingSessions & time
Updated 8 days ago · None stated GitHub repository of Crypto Long 10m Scalper BB RSI EMA Fib

8 Whittle Down

by hasnocool
106 stars

This strategy executes short positions during the end-of-day session (14:29–15:00) when the price is below the 200-day simple moving average. If price moves higher against the initial short trade, it scales into the position by submitting a second short order

Strategy v4 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 8 Whittle Down

BTC True Scalp

by iamrichardD
57 stars

This strategy runs an ultra-fast MACD in a separate pane to capture short-term momentum scalps. It filters trade entries using micro-trend direction, volume spikes, and candle body strength within specified session hours. Positions default to two percent of eq

Strategy v6 Oscillators & momentumScalping Repaints
Updated 1 year ago · Apache-2.0 GitHub repository of BTC True Scalp

More Mean reversion scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers