Ask Your Question
4

How can the time of a trailing stop trigger be obtained in Pine script?

asked 2021-06-02 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-03-21 18:00:00 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-02 11:00:00 +0000

Seen: 18 times

Last updated: Mar 21 '22