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
- Add the Bracket test script to a 5-minute MES1! chart.
- Find one trade in the List of trades and check its stop and target prices by hand from the fill.
- Then change loss = 16 to stop = swingLow and see how the exits move.
Quick check
1. In strategy.exit(), what units does loss use?
2. strategy.exit("Long exit", "Buy", loss = 16) is meant for an entry named "Long". What happens?
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
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 An order that closes a trade if price reaches a level you set against it. It aims to limit the loss, but in a fast market it can fill at a worse price.More in the glossary 16 The smallest step a price can move. On many stock index futures that’s 0.25 of a point.More in the glossary and a An exit order that closes a trade when price reaches a level you choose in your favour.More in the glossary 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 A small futures contract on the S&P 500, traded at CME Group. Each point is worth $5, and its smallest step of 0.25 is worth $1.25.More in the glossary (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 A dip on the chart: a candle whose low is below the lows of the candles on both sides of it.More in the glossary 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 An order to buy or sell straight away at the best price available. It fills, but the price isn’t promised.More in the glossary, filling at the next open.
- If one One bar on a price chart. It shows where price opened and closed, and the highest and lowest it reached, in one stretch of time.More in the glossary 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 (The difference between the price you expected and the price your order filled at.More in the glossary); check key trades by hand. See Spread and slippage.
Worked example
Made-up numbersTurn ticks into prices
Made-up example · MES1! · long fills at 5,016.00 · loss = 16, profit = 32
- Stop: 16 × 0.25 = 4 points below: 5,012.00, 16 × $1.25 = $20 a contract
- Target: 32 × 0.25 = 8 points above: 5,024.00, $40 a contract
- Whichever is touched first fills; the other is cancelled
loss and profit are counted in ticks from the actual fill.
Your turn
DrillMade-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.