Pine Script Partial Take Profit
Closes a portion of the position at a first target with qty_percent, then lets the remainder run toward a second, wider target.
Pine Script
//@version=6
strategy("Target Filled - Partial Take Profit Example", overlay=true, calc_on_every_tick=false,
default_qty_type=strategy.fixed, default_qty_value=2)
// --- Inputs ---------------------------------------------------
firstTargetTicks = input.int(20, "First Target (ticks)", minval=1)
firstTargetPct = input.float(50, "First Target Size (%)", minval=1, maxval=100, step=5)
runnerTicks = input.int(60, "Runner Target (ticks)", minval=1)
stopTicks = input.int(30, "Stop (ticks)", minval=1)
// --- Sample signal — replace with your own entry logic ---------
if ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) and strategy.position_size == 0
strategy.entry("Long", strategy.long)
// --- Scale out in two stages --------------------------------------
// The first exit closes qty_percent of the position at the closer
// target; the second carries the remainder to the runner target.
// Both share the same stop so the runner is never left unprotected.
strategy.exit("Scale 1", "Long", qty_percent=firstTargetPct, profit=firstTargetTicks, loss=stopTicks)
strategy.exit("Runner", "Long", profit=runnerTicks, loss=stopTicks) Settings
| Input | Default | Purpose |
|---|---|---|
| firstTargetTicks | 20 | Distance to the first, closer target. |
| firstTargetPct | 50 | Percent of the position closed at the first target. |
| runnerTicks | 60 | Distance to the runner's target. |
| stopTicks | 30 | Shared stop distance for both exits. |
How to use
- Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
- Position size needs to be at least 2 contracts for a 50% partial to actually fill — set it in the
strategy()declaration or withqty=on the entry. - Both exits share the same
loss=value, so the runner is never left without a stop once the first target fills. - Add a third
strategy.exitwith its ownqty_percentfor a three-stage scale-out.
Frequently Asked Questions
How do I take partial profits in Pine Script?
Attach two
strategy.exit calls to the same position. The first sets qty_percent to the share you want to close at the near target; the second has no qty_percent and takes whatever remains. Give both the same loss value so the entire position stays covered by one stop the whole time.What does qty_percent do in strategy.exit?
qty_percent tells strategy.exit what share of the current position it is allowed to close, instead of the whole thing. Set to 50 on a 2-contract position, it closes 1. Because it is a percentage of the position size at the moment the exit triggers, TradingView rounds the result to a whole number of contracts.