Strategy · Pine Script v6 · Own pane

S_exit_partial

by GammaExpansion

This strategy is a deterministic test fixture that repeats a 48-bar cycle based on bar index.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code63
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

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

source.pine
//@version=6
// exit 的数量预留、qty_percent 分批、无 from_entry 的 exit 持续作用于后续 entry、from_entry 不匹配无效
strategy("S_exit_partial", overlay = false,
  initial_capital = 100000, currency = currency.NONE,
  default_qty_type = strategy.fixed, default_qty_value = 20, pyramiding = 2,
  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 = 48
int phase = bar_index % cycleLen
int cycle = math.floor(bar_index / cycleLen)

float limA  = close * (1 + 0.003)
float limB  = close * (1 + 0.006)
float limP1 = close * (1 + 0.003)
float stopP2 = close * (1 - 0.010)
float limAny = close * (1 + 0.003)
float limGhost = close * (1 + 0.001)
if phase == 0
    strategy.entry("L1", strategy.long)                       // 20 股
    strategy.exit("A", "L1", qty = 19, limit = limA)          // 预留 19
    strategy.exit("B", "L1", qty = 20, limit = limB)          // 只剩 1 股可用
if phase == 12
    strategy.close_all(comment = "c1")
if phase == 14
    strategy.entry("L2", strategy.long)
    strategy.exit("P1", "L2", qty_percent = 50, limit = limP1)
    strategy.exit("P2", "L2", qty_percent = 50, stop = stopP2)
if phase == 24
    strategy.close_all(comment = "c2")
if phase == 26
    strategy.entry("L3", strategy.long)
    strategy.exit("Any", limit = limAny)                      // 无 from_entry:作用于所有 entry,持续到仓位关闭
if phase == 30
    strategy.entry("L4", strategy.long)                       // pyramiding=2,Any 是否也管它
if phase == 36
    strategy.close_all(comment = "c3")
if phase == 38
    strategy.entry("L5", strategy.long)
    strategy.exit("Ghost", "NOPE", limit = limGhost)          // from_entry 不匹配:不应产生任何单
if phase == 44
    strategy.close_all(comment = "c4")
if phase == 47
    strategy.cancel_all()
plot(limA, "lim_a")
plot(limB, "lim_b")
plot(limP1, "lim_p1")
plot(stopP2, "stop_p2")
plot(limAny, "lim_any")
plot(limGhost, "lim_ghost")

// ---- 对齐列 ----
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_exit_partial opens fixed-size long entries at set phases and attaches limit and stop exits that reserve quantity, split size by percent, apply to later entries when no entry id is given, or do nothing when the entry id does not match. It closes all positions at scheduled phases and cancels open orders near the end of each cycle. Pyramiding is two. Limit, stop, position, and equity series plot in a separate pane. It has no market signal.

Details

Published by

GammaExpansion on GitHub

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy can add several entries in the same direction
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

330 stars

HA UnivLong&Short Futures calculates Heikin Ashi candles and builds a channel using simple moving averages of Heikin Ashi highs and lows. It enters long positions when the upper channel line crosses above the lower channel line and opens short positions on the

Strategy v5 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of HA UnivLong&Short Futures

Q-Trend + KVO Strategy

by TradersPost
228 stars

This strategy combines the ATR-banded Q-Trend indicator with the Klinger Volume Oscillator (KVO) to execute confluence trades. It enters long when a Q-Trend buy trigger aligns with a positive KVO value and a bullish candle confirmation, taking short trades und

Strategy v6 TrendVolume
Updated 3 months ago · None stated GitHub repository of Q-Trend + KVO Strategy

MACD Crossover (tutorial)

by pineforge-4pass
193 stars

This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr

Strategy v6 Oscillators & momentumMoving averages
Updated 2 days ago · Apache-2.0 GitHub repository of MACD Crossover (tutorial)
114 stars

This strategy estimates price behavior using ordinary least squares regression between higher-timeframe data and a generated dataset, smoothed by a Karobein oscillator. It enters long positions when the oscillator turns upward and short positions when it turns

Strategy v4 Oscillators & momentumAI / machine learning Repaints

10PreBuyer

by hasnocool
106 stars

10PreBuyer is a dip-buying strategy that submits limit orders below recent price action using a discounted Hull Moving Average (HMA). It calculates the highest HMA value of high prices over a selected period, multiplied by a percentage discount factor, and pla

Strategy v4 Moving averagesMean reversion
Updated 2 years ago · GPL-3.0 GitHub repository of 10PreBuyer

Bar States Example S

by be-thomas
65 stars

This Pine Script strategy demonstrates the usage of bar state variables by highlighting the chart background color. It evaluates whether the current candle is the final bar on the chart using the barstate.islast condition and applies a silver background color

Strategy v5
Updated 6 days ago · GPL-3.0 GitHub repository of Bar States Example S

More strategies

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