Pine Script Risk-Reward Ratio Exits
Set your stop in ticks and the take-profit auto-calculates from your target R:R — no manual math per trade.
//@version=6
strategy("Risk-Reward Exits", overlay=true, calc_on_every_tick=false)
// ── Inputs ──────────────────────────────────────────────
stopTicks = input.int(30, "Stop Distance (ticks)", minval=1)
rr = input.float(2.0, "Risk:Reward Ratio", minval=0.5, step=0.5,
tooltip="2.0 = take profit at twice the stop distance (2:1).")
// ── Auto-calculated take profit ──────────────────────────
tpTicks = math.round(stopTicks * rr)
// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21))
if longCondition
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=tpTicks, loss=stopTicks)
if shortCondition
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=tpTicks, loss=stopTicks)
Settings
stopTicks— stop distance in ticks. Default 30.rr— target risk:reward ratio; take-profit = stop × ratio. Default 2.0.
How to use
- Paste into the Pine Editor and replace the EMA-crossover placeholder with your own entry conditions.
- Change only
rrto test 1.5:1, 2:1, 3:1 — the target recalculates automatically. - Both legs are in ticks from entry, so the ratio holds on any symbol; only dollar amounts change with tick value.
- Combine with the position size calculator to fix dollar risk per trade as well.
Frequently Asked Questions
How do I set a 2:1 risk reward in Pine Script?
Set your stop distance in ticks, multiply it by 2 to get the take-profit distance, and pass both to strategy.exit via the loss and profit parameters. With a 30-tick stop, profit=60 and loss=30 gives exactly 2:1 on every trade regardless of entry price.
How is take profit calculated from risk reward?
Take profit equals stop distance times the target ratio. A 30-tick stop at 2:1 means a 60-tick target; at 3:1 it means 90 ticks. Because both legs are defined in ticks from entry, the ratio holds on any symbol — only the dollar amounts change with tick value.
Related
Want a complete eval-ready strategy with R:R already optimized?
This snippet is a building block. Our paid scripts are complete strategies — entries, tuned exits, kill switch, session filter, and EOD flatten tested on 3 years of MES/NQ data. Instant delivery from $50.
View Plans — From $50