Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Entries, exits, stops and targets in code

Lesson 11 of 16 in Coding strategies in Pine ScriptModule 3: The strategy tester

Advanced · Coding strategies in Pine Script

Entries, exits, stops and targets in code

Wildflower Trading School · Lesson 7.4.3.2

Wildflower

Last time, a strategy got into trades, but it only got out by turning round. Your rule sheets say more than that: where the stop goes, where the target is, and when to leave. By the end of this lesson, you'll know how to code a stop and a target. You'll place a stop at a price from the chart. You'll also close a trade on a signal.

1 of 9

What you'll learn

  • Place a stop and target. strategy.exit sets both. Only one side fills.
  • Match the names. The exit must name the entry exactly, or no exit is placed.
  • Use ticks or prices. loss and profit take ticks. stop and limit take prices.
  • Close on a signal. strategy.close leaves at market at the next open.
  • Check fills by hand. Stops can fill worse than their price. Check trades that matter.

Exercise

  1. Add the Bracket test script to a 5-minute MES1! chart.
  2. Find one trade in the List of trades and check its stop and target prices by hand from the fill.
  3. Then change loss = 16 to stop = swingLow and see how the exits move.

Next: Position size and futures contract values.

Quick check

  1. 1. In strategy.exit(), what units does loss use?

  2. 2. strategy.exit("Long exit", "Buy", loss = 16) is meant for an entry named "Long". What happens?

  3. 3. One candle touches both stop and target. What does TradingView do?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Position size and futures contract values” is waiting next time.

Read the full notes

In Pine Script, strategy.entry() gets into a test trade, strategy.exit() places a stop and target (loss and profit in ticks, or stop and limit as prices), and strategy.close() leaves at market on a signal.

  • strategy.exit("Long exit", "Long", loss = 16, profit = 32) places a 16 and a 32 ticks from the entry's fill. The second name must match the entry's name exactly, or no exit is placed. Only one side fills; the other is cancelled.
  • With a made-up fill at 5,016.00 on (tick 0.25 = $1.25): stop 5,012.00 ($20 a contract), target 5,024.00 ($40 a contract).
  • loss and profit take ticks; stop and limit take prices. A stop: swingLow = ta.lowest(low, 5) - syminfo.mintick, then stop = swingLow. Your rule sheet must say which candles count as the swing: see Why vague rules fail.
  • strategy.close("Long") closes that trade with a , filling at the next open.
  • If one touches both stop and target, TradingView assumes the high came first when the open is nearer the high, otherwise the low. Stops can fill worse than their price (); check key trades by hand. See Spread and slippage.

Worked example

Made-up numbers

Turn ticks into prices

Made-up example · MES1! · long fills at 5,016.00 · loss = 16, profit = 32

  1. Stop: 16 × 0.25 = 4 points below: 5,012.00, 16 × $1.25 = $20 a contract
  2. Target: 32 × 0.25 = 8 points above: 5,024.00, $40 a contract
  3. Whichever is touched first fills; the other is cancelled

loss and profit are counted in ticks from the actual fill.

Your turn

Drill

Made-up example · strategy.exit("Long exit", "Long", loss = 24) · MES1! · tick size 0.25

Trading is risky. You can lose money. Test results are hypothetical, and a good test doesn't mean future profits. This lesson explains how Pine Script works. It isn't advice.