Strategy · Pine Script v6 · Own pane

Risk limits probe r2

by GammaExpansion

This strategy probes TradingView risk-limit behavior in a separate pane.

View on GitHub

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

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

source.pine
//@version=6
strategy("Risk limits probe r2", overlay=false, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1, pyramiding=0, commission_type=strategy.commission.percent, commission_value=0, slippage=0, margin_long=0, margin_short=0, calc_on_order_fills=false, calc_on_every_tick=false, process_orders_on_close=false)

// Risk calls must remain in global scope. Inactive rules use unreachable cash/count limits.
mode = input.int(1, "Mode: 0 control, 1 drawdown cash, 2 drawdown percent, 3 day loss cash, 4 day loss percent, 5 losing days", minval=0, maxval=5)
cashLimit = input.float(100, "Cash limit", minval=0.01)
percentLimit = input.float(1, "Percent limit", minval=0.01, maxval=100)
losingDays = input.int(2, "Consecutive losing days", minval=1)
cycle = input.int(4, "Cycle bars", minval=3)
direction = input.int(0, "Direction: 0 alternate, 1 long, -1 short", minval=-1, maxval=1)
start = input.time(1704067200000, "Start") // 2024-01-01 00:00 UTC

strategy.risk.max_drawdown(mode == 1 ? cashLimit : mode == 2 ? percentLimit : 1000000000000, mode == 2 ? strategy.percent_of_equity : strategy.cash)
strategy.risk.max_intraday_loss(mode == 3 ? cashLimit : mode == 4 ? percentLimit : 1000000000000, mode == 4 ? strategy.percent_of_equity : strategy.cash)
strategy.risk.max_cons_loss_days(mode == 5 ? losingDays : 1000000)

var int activeIndex = -1
var int requestedSide = 0
if time >= start
    activeIndex += 1
    phase = activeIndex % cycle
    side = direction != 0 ? direction : int(math.floor(activeIndex / cycle)) % 2 == 0 ? 1 : -1
    if phase == 0
        requestedSide := side
        strategy.entry("E", side == 1 ? strategy.long : strategy.short, qty=1)
        // The remote order exposes cancellation when a risk rule halts the strategy.
        strategy.order("pending", side == 1 ? strategy.long : strategy.short, qty=1, limit=side == 1 ? close * 0.1 : close * 10)
    if phase == cycle - 2
        strategy.close_all(comment="scheduled close")
    if phase == cycle - 1
        strategy.cancel_all()

last = strategy.closedtrades - 1
plot(2, "probe_revision")
plot(mode, "probe_mode")
plot(bar_index, "bar_index")
plot(activeIndex, "active_index")
plot(time("D"), "daily_time")
plot(requestedSide, "requested_side")
plot(strategy.position_size, "position_size")
plot(strategy.position_avg_price, "position_avg_price")
plot(strategy.equity, "equity")
plot(strategy.openprofit, "openprofit")
plot(strategy.netprofit, "netprofit")
plot(strategy.closedtrades, "closed_trades")
plot(strategy.opentrades, "open_trades")
plot(last >= 0 ? strategy.closedtrades.profit(last) : na, "last_profit")
plot(last >= 0 ? strategy.closedtrades.entry_price(last) : na, "last_entry_price")
plot(last >= 0 ? strategy.closedtrades.exit_price(last) : na, "last_exit_price")
plot(last >= 0 ? strategy.closedtrades.entry_bar_index(last) : na, "last_entry_bar")
plot(last >= 0 ? strategy.closedtrades.exit_bar_index(last) : na, "last_exit_bar")

Overview

Risk limits probe r2 cycles timed entries in a chosen direction, then closes and cancels orders on a fixed bar schedule. A mode input activates one of drawdown cash, drawdown percent, intraday cash loss, intraday percent loss, or consecutive losing days, while inactive rules stay at unreachable limits. Settings change cash and percent limits, losing-day count, cycle length, direction, and start time. Diagnostic plots show equity, profits, position size, and last-trade fields. It has no price stop; risk rules can halt trading.

Details

Published by

GammaExpansion on GitHub

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
Mode: 0 control, 1 drawdown cash, 2 drawdown percent, 3 day loss cash, 4 day loss percent, 5 losing daysint1mode
Cash limitfloat100cashLimit
Percent limitfloat1percentLimit
Consecutive losing daysint2losingDays
Cycle barsint4cycle
Direction: 0 alternate, 1 long, -1 shortint0direction
Starttime1704067200000start
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