Black Friday: 2 years for the price of 1.Or 3 months for the price of 1. On Pro.2 d 6 hleftSee plans →
Free tool

How do I code a daily profit target in Pine Script?

Tested code that ends the day at your target: the open trade counted and the target in the trade's limit. Run on MNQ from 4 November 2024 to 22 October 2026. Written in Pine Script? Paste it here: a free account with no card runs it on CME Group futures with $2.50 a contract and a tick of slippage charged.

Free plan · unlimited backtests · no card
MNQ opening range breakout with a $500 daily target · in the trade's limit against noneMNQ1! · 5-minute · 4 November 2024 → 22 October 2026
Target in the limit over 2 years
+$16,700
No target over 2 years
+$38,615
Days the target ended
359
Worst drawdown with the target
−$16,962.50
$500 target in the limit+$16,700
No target+$38,615
Nov 2024Feb 2026Oct 2026

The $50,000 account at each day's last exit. 5 MNQ contracts pay $2.50 a contract each side and a tick of slippage. Dashed: the same strategy with no target. Shaded: from 24 February 2026.

Paste your script

Pine v6, as TradingView has it.

MNQ opening range breakout with a $500 daily target · in the trade's limit against noneMNQ1! · 5-minute · 4 November 2024 → 22 October 2026
Target in the limit over 2 years
+$16,700
No target over 2 years
+$38,615
Days the target ended
359
Worst drawdown with the target
−$16,962.50
$500 target in the limit+$16,700
No target+$38,615
Nov 2024Feb 2026Oct 2026

The $50,000 account at each day's last exit. 5 MNQ contracts pay $2.50 a contract each side and a tick of slippage. Dashed: the same strategy with no target. Shaded: from 24 February 2026.

Count the day from the 18:00 New York reopen, count the open trade and put the target's price in the trade's limit.

The script below does all three. Code that checks only closed trades never acted in our test: on two years of MNQ this breakout never took another trade after a day had booked $500.

In the trade's limit a $500 target ended 359 days. 329 of them booked $500 to the tick. On the other 30 the trade went past the target on its first candle before the limit had the fill's price. It also cut two years from $38,615 to $16,700 with 5 MNQ contracts. On the days it ended the same trades with no target made $21,915 more. Checked at each candle's close the target booked whatever the candle closed at: $497.50 to $2,137.50.

The code in three parts

  1. 1

    The day's own books

    The day's P&L is what its closed trades made after their costs: each trade's strategy.closedtrades.profit added up from the futures day's first bar: the 18:00 New York reopen that session.isfirstbar marks.

  2. 2

    The target as a price

    From the candle after the fill the trade's limit is the nearer of your own target and the price where this trade takes the day to the target after its commission both ways. A limit fills at its price. A trade that goes past the target on its first candle closes at the next open.

  3. 3

    Stop for the day

    Once the day's closed trades reach the target there is no new entry until the next futures day. Put not dpHit in front of each entry's condition.

The target on its own · 38 lines
// ── The daily profit target ──────────────────────────────────────────────────────────────────────
// An example number: take yours from your own plan
dpTarget = input.float(500, "Daily profit target ($)", minval=1, group="Daily profit target")
dpMode   = input.string("In the trade's limit", "How the target acts", options=["Off", "Closed trades only", "Checked at each close", "In the trade's limit"], group="Daily profit target")
dpFee    = input.float(2.5, "Commission per contract each side ($)", minval=0, group="Daily profit target")

// The day's books: what the day's closed trades made after their costs. A futures day starts at
// its first bar, the 18:00 New York reopen
var float dpBooked = 0.0    // the day's closed trades, after their costs
var int   dpSeen   = 0      // closed trades already counted
var bool  dpHit    = false  // the target was reached: no more trades today
var int   dpDays   = 0      // days the target ended
while dpSeen < strategy.closedtrades
    dpBooked += strategy.closedtrades.profit(dpSeen)
    dpSeen   += 1
if session.isfirstbar
    dpBooked := 0.0
    dpHit    := false

// What the open trade would add if it closed at a price, after its commission both ways
dpOpenAt(price) =>
    strategy.position_size == 0 ? 0.0 : (price - strategy.position_avg_price) * strategy.position_size * syminfo.pointvalue - 2 * dpFee * math.abs(strategy.position_size)

// The target as a price: where a trade of this size from this entry takes the day to the target
dpPriceLong(entry, size) =>
    entry + (dpTarget - dpBooked + 2 * dpFee * size) / (size * syminfo.pointvalue)
dpPriceShort(entry, size) =>
    entry - (dpTarget - dpBooked + 2 * dpFee * size) / (size * syminfo.pointvalue)

// Closed trades only: the day stops once its booked trades reach the target
if dpMode != "Off" and not dpHit and dpBooked >= dpTarget - 0.005
    dpHit  := true
    dpDays += 1
// Checked at each close: the open trade counted at the candle's close, and closed there
if dpMode == "Checked at each close" and not dpHit and strategy.position_size != 0 and dpBooked + dpOpenAt(close) >= dpTarget
    strategy.close_all(comment="Daily target", immediately=true)
    dpHit  := true
    dpDays += 1

Paste it under your strategy() line. Put not dpHit in front of each entry's condition and dpPriceLong or dpPriceShort in each exit's limit from the candle after the fill. The full script below does both. Set the commission input to your strategy's.

The $500 target four ways

HOW THE TARGET ACTSTRADESWONDAYS IT ENDED2 YEARS4 NOV 2024 → 23 FEB 202624 FEB 2026 → 22 OCT 2026WORST DRAWDOWN
No target63445.7%0+$38,615+$52,995−$14,380−$39,845
Closed trades only63445.7%0+$38,615+$52,995−$14,380−$39,845
Checked at each close61661.7%339+$38,267.50+$30,852.50+$7,415−$22,532.50
In the trade's limit60764.6%359+$16,700+$17,507.50−$807.50−$16,962.50

5 MNQ contracts on a $50,000 account pay $2.50 a contract each side and a tick of slippage. The 4 runs were fixed before they ran. The worst drawdown is the deepest fall from a high between 4 November 2024 and 22 October 2026.

What the target changed

In the trade's limit the target ended 359 of the 500 days the strategy traded. 329 of them booked $500 to $502.50 because the limit sits on the nearest tick. The other 30 booked more: the trade went past the target on its first candle and the limit filled at the next open. With no target those days made $210,585 against $188,670. On 198 of them the trade went on to make more. On 161 it fell back and 111 of those ended in a loss.

Checked at each candle's close the target ended 339 days. It booked what the candle closed at: $497.50 to $2,137.50. Only 18 of them ended within a tick of $500. Those days made $232,602.50 against $232,950 with no target.

On closed trades only the target never acted. No day took another trade after its closed trades reached $500. A breakout that wins leaves the price far from its range. That run is the no-target run trade for trade.

The target made the account steadier. Its worst drawdown was $16,962.50 in the limit against $39,845 with no target. With no target the strategy made $52,995 from 4 November 2024 to 23 February 2026 and lost $14,380 from 24 February 2026 to 22 October 2026. In the limit it made $17,507.50 and lost $807.50. Checked at each close it made money in both periods: $30,852.50 and $7,415.

The script as it ran

MNQ opening range breakout with a $500 daily target in the trade's limit

A 15-minute opening range from 9:30 New York. A close through it after a close inside it enters from 9:45 to 12:00. The day takes up to 3 trades. The stop sits at the range's other side and the target at 2R. The $500 daily target is in each trade's limit from the candle after its fill: the nearer of the 2R target and the price where the day reaches $500 after the trade's commission. 5 MNQ contracts. Its input sets how the target acts: off, closed trades only, checked at each close or in the trade's limit.

Trades
607
Won
64.6%
Profit factor
1.07
Net
+33.4%
Worst drawdown
−24.3%
Worst trade
−$2,912.50

MNQ1! · 5-minute · 4 Nov 2024 → 22 Oct 2026 · $2.50 a contract each side and a tick of slippage · 5 contracts on a $50,000 account

+$17,507.50 from 4 November 2024 to 23 February 2026, −$807.50 from 24 February 2026 to 22 October 2026.

Pine Script v6 · 104 linesRun this script
//@version=6
// Suggested timeframe: 5-minute
strategy("MNQ opening range breakout with a daily profit target", overlay=true, initial_capital=50000, default_qty_type=strategy.fixed, default_qty_value=5,
     commission_type=strategy.commission.cash_per_contract, commission_value=2.5, slippage=1, margin_long=0, margin_short=0)

// ── The daily profit target ──────────────────────────────────────────────────────────────────────
// An example number: take yours from your own plan
dpTarget = input.float(500, "Daily profit target ($)", minval=1, group="Daily profit target")
dpMode   = input.string("In the trade's limit", "How the target acts", options=["Off", "Closed trades only", "Checked at each close", "In the trade's limit"], group="Daily profit target")
dpFee    = input.float(2.5, "Commission per contract each side ($)", minval=0, group="Daily profit target")

// The day's books: what the day's closed trades made after their costs. A futures day starts at
// its first bar, the 18:00 New York reopen
var float dpBooked = 0.0    // the day's closed trades, after their costs
var int   dpSeen   = 0      // closed trades already counted
var bool  dpHit    = false  // the target was reached: no more trades today
var int   dpDays   = 0      // days the target ended
while dpSeen < strategy.closedtrades
    dpBooked += strategy.closedtrades.profit(dpSeen)
    dpSeen   += 1
if session.isfirstbar
    dpBooked := 0.0
    dpHit    := false

// What the open trade would add if it closed at a price, after its commission both ways
dpOpenAt(price) =>
    strategy.position_size == 0 ? 0.0 : (price - strategy.position_avg_price) * strategy.position_size * syminfo.pointvalue - 2 * dpFee * math.abs(strategy.position_size)

// The target as a price: where a trade of this size from this entry takes the day to the target
dpPriceLong(entry, size) =>
    entry + (dpTarget - dpBooked + 2 * dpFee * size) / (size * syminfo.pointvalue)
dpPriceShort(entry, size) =>
    entry - (dpTarget - dpBooked + 2 * dpFee * size) / (size * syminfo.pointvalue)

// Closed trades only: the day stops once its booked trades reach the target
if dpMode != "Off" and not dpHit and dpBooked >= dpTarget - 0.005
    dpHit  := true
    dpDays += 1
// Checked at each close: the open trade counted at the candle's close, and closed there
if dpMode == "Checked at each close" and not dpHit and strategy.position_size != 0 and dpBooked + dpOpenAt(close) >= dpTarget
    strategy.close_all(comment="Daily target", immediately=true)
    dpHit  := true
    dpDays += 1

// ── The example strategy: a 15-minute opening range breakout, up to three trades a day ──────────
// Your own entries go here, behind "not dpHit", with the target's price in the exit
orSess    = input.session("0930-0945", "Opening range (New York)", group="Strategy")
tradeSess = input.session("0945-1200", "Entry window (New York)", group="Strategy")
rMult     = input.float(2.0, "Target (R)", minval=0.5, step=0.5, group="Strategy")
maxDay    = input.int(3, "Trades per day", minval=1, group="Strategy")
qty       = input.int(5, "Contracts", minval=1, group="Strategy")

inOR    = not na(time(timeframe.period, orSess, "America/New_York"))
inTrade = not na(time(timeframe.period, tradeSess, "America/New_York"))

var float orHigh      = na
var float orLow       = na
var int   tradesToday = 0
var float stopLevel   = na
var float targetLevel = na
if session.isfirstbar
    orHigh      := na
    orLow       := na
    tradesToday := 0
if inOR
    orHigh := na(orHigh) ? high : math.max(orHigh, high)
    orLow  := na(orLow) ? low : math.min(orLow, low)

// The setup: a close through the range after a close inside it. The stop sits at the range's
// other side and the target at rMult times the risk. A new trade waits until the last one closed
rangeSet   = not inOR and not na(orHigh)
longSetup  = rangeSet and close > orHigh and close[1] <= orHigh
shortSetup = rangeSet and close < orLow and close[1] >= orLow
canTrade   = inTrade and strategy.position_size == 0 and tradesToday < maxDay and not dpHit and not session.islastbar
useLimit   = dpMode == "In the trade's limit"

if canTrade and longSetup and orLow < close
    stopLevel   := orLow
    targetLevel := close + (close - orLow) * rMult
    strategy.entry("Long", strategy.long, qty=qty)
    strategy.exit("Long exit", from_entry="Long", stop=stopLevel, limit=targetLevel)
    tradesToday += 1
if canTrade and shortSetup and orHigh > close
    stopLevel   := orHigh
    targetLevel := close - (orHigh - close) * rMult
    strategy.entry("Short", strategy.short, qty=qty)
    strategy.exit("Short exit", from_entry="Short", stop=stopLevel, limit=targetLevel)
    tradesToday += 1

// From the candle after the fill: the same stop, and the nearer of the target and the day's target
// price, worked out from the fill's own price. The entry is a market order, so its price is known only
// at the fill candle's close: a trade that goes past the target on that first candle closes at the next open
if strategy.position_size > 0
    strategy.exit("Long exit", from_entry="Long", stop=stopLevel, limit=useLimit ? math.min(targetLevel, dpPriceLong(strategy.position_avg_price, strategy.position_size)) : targetLevel)
if strategy.position_size < 0
    strategy.exit("Short exit", from_entry="Short", stop=stopLevel, limit=useLimit ? math.max(targetLevel, dpPriceShort(strategy.position_avg_price, -strategy.position_size)) : targetLevel)
if session.islastbar
    strategy.close_all("Session end", immediately=true)

// The tally, in the Data Window
plot(dpBooked, "Today's booked P&L", display=display.data_window)
plot(dpDays, "Days the target ended", display=display.data_window)
plot(rangeSet ? orHigh : na, "Range high", color=color.new(#16A56A, 0), style=plot.style_linebr)
plot(rangeSet ? orLow : na, "Range low", color=color.new(#E5484D, 0), style=plot.style_linebr)

A daily loss limit that holds is on our daily loss limit page. The same breakout with one trade a day and a VWAP filter is in Does a VWAP filter improve the NQ opening range breakout?.

Free account

No card, no time limit.

$0
  • Unlimited backtests, costs charged, with the out-of-sample check
  • The Pine script audit and the full trade list
  • The Pine Copilot writes and edits your script
  • 50 AI credits a day
The Alpha Charts terminal a free account opens: the Pine Copilot beside the chart

Prop Ready opens all two years of futures minute bars on a computer for $39 paid once and has 7 days money back. Pro adds the optimiser and every AI tool and has 30 days money back. See the plans

Questions

How do I code a daily profit target in Pine Script?

Keep the day's own books from the futures day's first bar: the 18:00 New York reopen that session.isfirstbar marks. Add each closed trade's profit from strategy.closedtrades.profit. From each trade's fill put in the trade's limit the price where the day reaches the target after the trade's commission both ways. Once the day's closed trades reach the target take no new entry. The script on this page does all three in a block that goes on top of your own strategy.

Why does my daily profit target not stop the trade?

Because it counts closed trades only. A target on strategy.netprofit acts between trades and the open trade runs on past it. In our test a $500 target on closed trades never acted in two years: the day never took another trade after booking $500.

Should the target be checked at the candle's close or put in the limit?

Put it in the limit if you want the day to end at the target. Checked at each close the day ends where the candle closes: in our test from $497.50 to $2,137.50 on a $500 target. In the trade's limit 329 of 359 days ended within a tick of $500. On the other 30 the trade went past the target on its first candle. The limit takes the fill's price only at that candle's close and then filled at the next open past the target.

Does a daily profit target make a strategy better?

Not in the trade's limit. With 5 MNQ contracts the breakout made $38,615 over two years with no target and $16,700 with the target in the limit. On the days the target ended the same trades with no target made $21,915 more. Checked at each close it made $38,267.50: about the same as no target. It cut the worst drawdown from $39,845 to $22,532.50 and made $7,415 from 24 February 2026 where no target lost $14,380. In the limit the worst drawdown was $16,962.50.

When does the day start for the target?

At the futures day's first bar. CME's futures session opens at 18:00 New York and session.isfirstbar marks it. dayofmonth changes at midnight and would count the evening's trades against the day before.

Can I run it myself?

Yes. Run opens the script on MNQ 5-minute bars in the Alpha Charts terminal. On a computer, a free account with no card runs it as published on all two years. Its inputs set the target, how it acts and the contracts. A changed input or line makes it your own script: a free account then tests the newest 30 days of a chart under an hour. Prop Ready ($39 paid once) opens all two years for 30 days.

Is a backtest a prediction?

No. It is what a strategy's rules would have done on past prices with costs charged. Live fills, news and your own execution differ. Read it as a test of the rules and not as a forecast.

Every number comes from Alpha Charts' tester on MNQ 5-minute bars from 4 November 2024 to 22 October 2026. The runs were made on 27 November 2026. Alpha Charts is software for testing trading scripts and gives no investment advice. A backtest shows what rules would have done on past data. It does not show what they will do. Our methodology is in Data and methodology. Updated 27 November 2026.

Test your Pine Script freeFree account, no cardPaste your script