Loading page…
Loading page…
Simulate ticks vs price offsets, partial take-profit, and trailing stop activation for TradingView strategy.exit() with compiled Pine v6 code.
Configure instrument tick size, exit units, partial take-profit, and trailing stop activation. Compiled code updates live.
mintick = 0.1 · 1 tick = $0.10 -> 1,000 ticks = $100 price move
profit, loss, trail_points, and trail_offset in ticks (multiples of syminfo.mintick), never dollar or pip units.1//@version=62strategy("Verified strategy.exit (BINANCE:BTCUSDT.P)", overlay = true, process_orders_on_close = false)3 4// --- Inputs ---5i_mode = "percent"6i_tpPct = input.float(2.4, 'Take Profit (%)', step = 0.1) * 0.017i_slPct = input.float(1.2, 'Stop Loss (%)', step = 0.1) * 0.018i_trActPct = input.float(1.4, 'Trail Activation (%)', step = 0.1) * 0.019i_trOffPct = input.float(0.6, 'Trail Offset (%)', step = 0.1) * 0.0110 11// --- Example Non-Repainting Entry Signal ---12fastEma = ta.ema(close, 21)13slowEma = ta.ema(close, 55)14entryCond = ta.crossover(fastEma, slowEma) and strategy.position_size == 015if entryCond16 strategy.entry("Long", strategy.long)17 18// --- Lock Exit Levels Using Position Average Price ---19float avgPx = strategy.position_avg_price20if strategy.position_size != 021 float tpPrice = avgPx * (1 + i_tpPct)22 float slPrice = avgPx * (1 - i_slPct)23 float tp1Price = avgPx * (1 + i_tpPct * 0.55)24 strategy.exit("TP1", from_entry = "Long", qty_percent = 50, limit = tp1Price, stop = slPrice)25 float trailActPrice = avgPx * (1 + i_trActPct)26 int trailOffTicks = math.max(1, math.round((avgPx * i_trOffPct) / syminfo.mintick))27 strategy.exit("FinalExit", from_entry = "Long", limit = tpPrice, stop = slPrice, trail_price = trailActPrice, trail_offset = trailOffTicks)28 29plot(fastEma, "EMA 21", color = color.new(#2962ff, 0))30plot(slowEma, "EMA 55", color = color.new(#ff9800, 0))1//@version=62strategy("Broken Trailing Stop", overlay = true)3if ta.crossover(ta.ema(close, 9), ta.ema(close, 21))4 strategy.entry("Long", strategy.long)5 6// BUG 1: trail_offset alone never activates trailing.7// BUG 2: passing 0.01 (1%) into ticks equals 0 ticks.8strategy.exit("Trail", "Long", trail_offset = 0.01)1//@version=62strategy("Fixed Trailing Stop", overlay = true)3if ta.crossover(ta.ema(close, 9), ta.ema(close, 21))4 strategy.entry("Long", strategy.long)5 6// FIX: Supply trail_price AND convert offset to integer ticks.7if strategy.position_size > 08 float actPx = strategy.position_avg_price * 1.0159 int offTicks = math.max(1, math.round((strategy.position_avg_price * 0.006) / syminfo.mintick))10 strategy.exit("Trail", from_entry = "Long", trail_price = actPx, trail_offset = offTicks)| Market & Mintick | Exact Tick Conversion Rule | Runtime Constraint | Copy |
|---|---|---|---|
| EURUSD (0.00001) | 1 pip = 10 ticks. A 25-pip stop requires loss = 250, not 25. | syminfo.mintick = 0.00001 | |
| BTCUSDT.P (0.1) | $100 move = 1,000 ticks. Never hardcode ticks across altcoins. | Normalize via / syminfo.mintick | |
| ES1! (0.25) | 1 index point = 4 ticks ($50/contract). 10-point trail = 40 ticks. | syminfo.mintick = 0.25 | |
| AAPL / SPY (0.01) | $1.00 move = 100 ticks. Prefer limit/stop prices for % targets. | syminfo.mintick = 0.01 |
Open → Low → High → Close and a red bar moves Open → High → Low → Close. Tight trail_offset values under 0.5× ATR produce artificially inflated historical fills that fail in live tick streams.strategy.exit() only inside if entryCond works for static brackets, but if you want dynamic ATR or break-even stop updates you must call strategy.exit() on every bar while strategy.position_size != 0 using the same exit id.TP1 exits qty_percent = 50 without a stop parameter, the 50% tranche remains unprotected if price reverses before TP1 hits. Always pass stop on every partial exit call.