1 | initial version |
In Pine script, the time of a trailing stop trigger can be obtained using the time
built-in variable. This variable returns the Unix timestamp of the current bar or candle.
For example, if you want to record the time when a trailing stop is triggered, you can assign the value of time
to a variable or use it in a label
function like this:
//@version=4
strategy("My Strategy")
trailing_stop = strategy.position_avg_price * 0.9 // 10% trailing stop
trailing_stop_triggered_time = 0
if strategy.position_size > 0
if close < trailing_stop
strategy.exit("Trailing Stop", "Buy", trail_percent = 10)
trailing_stop_triggered_time := time
plot(trailing_stop)
label.new(bar_index, trailing_stop, "Trailing Stop", color.red)
if trailing_stop_triggered_time
label.new(bar_index, low - atr(14), tostring(trailing_stop_triggered_time), xloc.bar_time)
In this example, the trailing_stop_triggered_time
variable is assigned the value of time
when the trailing stop is triggered. Then, in the if trailing_stop_triggered_time
block, a label is added to the chart to display the timestamp of the trigger.