Pine Script Partial Take Profit
Closes part of your position at the first target with qty_percent and lets the runner work toward a second target.
//@version=6
strategy("Partial Take Profit", overlay=true, calc_on_every_tick=false,
default_qty_type=strategy.fixed, default_qty_value=2)
// ── Inputs ──────────────────────────────────────────────
tp1Ticks = input.int(20, "TP1 Distance (ticks)", minval=1)
tp1Pct = input.float(50, "TP1 Size (% of position)", minval=1, maxval=100, step=5)
tp2Ticks = input.int(60, "TP2 Distance (ticks)", minval=1)
stopTicks = input.int(30, "Stop (ticks)", minval=1)
// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
if longCondition and strategy.position_size == 0
strategy.entry("Long", strategy.long)
// ── Scale-out exits ──────────────────────────────────────
// TP1 closes qty_percent of the position; the runner exits at TP2.
// Both share the same stop so the whole position stays protected.
strategy.exit("TP1", "Long", qty_percent=tp1Pct, profit=tp1Ticks, loss=stopTicks)
strategy.exit("TP2", "Long", profit=tp2Ticks, loss=stopTicks)
Settings
tp1Ticks— first target distance in ticks. Default 20.tp1Pct— percent of the position closed at TP1. Default 50.tp2Ticks— runner target distance in ticks. Default 60.stopTicks— shared stop distance in ticks. Default 30.
How to use
- Paste into the Pine Editor and replace the EMA-crossover placeholder with your own entry conditions.
- Position size must be at least 2 contracts for a 50% partial to fill — set it in the
strategy()declaration or withqty=on the entry. - Both exits carry the same
loss=so the runner is never left unprotected after TP1 fills. - Add a third
strategy.exitwith its ownqty_percentfor three-stage scale-outs.
Frequently Asked Questions
How do I take partial profits in Pine Script?
Use two strategy.exit calls on the same position: the first with qty_percent set to the portion you want to close at target one, the second without qty_percent for the remainder. Each exit gets its own profit distance; both should share the same loss parameter so the entire position stays protected.
What does qty_percent do in strategy.exit?
qty_percent tells strategy.exit what percentage of the current position it may close, instead of closing everything. qty_percent=50 on a 2-contract position closes 1 contract. It is a percentage of the position at the time the exit triggers, and TradingView rounds to whole contracts.
Related
Want a complete eval-ready strategy with scale-outs pre-wired?
This snippet is a building block. Our paid scripts are complete strategies — entries, staged exits, kill switch, session filter, and EOD flatten tested on 3 years of MES/NQ data. Instant delivery from $50.
View Plans — From $50