OPEN-SOURCE SCRIPT
Aggiornato

Trailing stop

4 165
Hi all!

This script helps to alert you when a trailing stop is hit. More specifically it alerts you when the low of the candle crosses below your trailing stop. A trailing stop follows a price positive movements. It raises your stop when price goes up, but keeps it at the same level if price goes down, so it "locks" in your profit. You define your long entry bar and choose one of the following methods for the stop:

ATR
The Average True Range (ATR) is popular to trail stops. The trailing stop is raised by the low minus the ATR (times a factor that can be set under the settings for ATR).

EMA
The Exponential Moving Average (EMA) can be used to trail your stop. When the low goes below the EMA an alert is sent about the stop. Its length can be set in the settings.

SMA
The Simple Moving Average (SMA) can be used to trail your stop. When the low goes below the SMA an alert is sent about the stop. Its length can be set in the settings.

Source
An external source can be useful as a stop signal. You can use this option that will stop you out when the signal returns anything else than "na". E.g. if you want a stop when KivancOzbilgic script "SuperTrend" (tradingview.com/script/r6dAP7yi/) turns red, you set the source to "Supertrend: Down Trend". This option will not draw pretty things on the chart, but it will alert you!

Please note that this is for long entries only.

Best of trading luck!
Note di rilascio

  • Only alert once per bar
  • Some refactoring
  • Error if source is the method and the input is close only on the first bar
Note di rilascio
Hi!

• Force the user to set the entry for the trailing stop loss
• The trailing stop line is drawn until it's broken
• Code refactoring
• Only set if the stop is hit, not the bar index'

Good luck trading!
Note di rilascio
Hi all!

Here's a small but important update.

∙ Add option to force the bar to be closed
∙ Add option to select price (open, high, low or close)

Good luck trading!
Note di rilascio
Hi all!

This update is a major one. I want to use this indicator in my own trading so that's a big reason. First off it's updated to Pine Script version 6. I have also worked a lot on making the entry be more precise by using [Pine]varip[/Pine] that makes variables keep their value intrabar. I also added the possibility to trail the stop based on market structure, a market structure trailing stop loss is a popular way to trail your stop loss. This is possible with my library 'PriceAction' (tradingview.com/script/nAGqSrxy-PriceAction/). More in how that can be used later on. This update will make this indicator much more useful and correct.

General

Even though this indicator will give you visual feedback on entries earlier than the last bar its power lies in the alert that you can create. Select the type of how you want to trail your stop loss (more on that later) and let the script alert you for an exit when you have created an alert.

Settings

Firstly you can set Entry which is an optional entry time. If none is set the entry time will be on the first tick on the last bar, this allows for you to add an alert on the indicator without setting the entry bar. The entry will, if none set, be as soon as the indicator is loaded in the alert system. This setting can be used to see the trailing stop on already open trades. It also allows you to set a trailing stop if you already have opened the long trade on an earlier bar. Entry is used on all examples provided to get visual feedback.

Set the Price for the exit. Select which price of open, high, low or close that must be below the trailing stop.

Force close forces the bar to be closed (with your chosen price) below the trailing stop.

Stop will cause an error in the bar after an alert is sent to keep it from running on.

Source; choose the source of the trailing stop loss. More settings for each one is below.

I.e. if you set the Price to 'high' and Force close to true the entire bar must be below the trailing stop loss and the bar needs to be closed. If you instead set Price to 'low' and Force close to 'false' the indicator will alert you as soon as the price turns below the trailing stop loss. Note that 'low' on realtime bars will be the same as 'close'.

Types

- ATR (Average True Range)

The stop will be trailed (move higher) if the bar's low price is higher than the previous stop loss price. The price of the stop loss is determined by the bar's low minus the ATR (of set length) times the set factor.

Example:

istantanea

The price is trailed upwards when the price is going up. It's trailed to the 'low' minus the ATR (of set length) times the set factor.

- EMA (Exponential Moving Average)

The stop will be trailed with the EMA price and stop you out if the bars price selected in the 'Price' setting (open, high, low or close) is lower.

Example:

istantanea

The price is trailed upwards with the value of the EMA of default length.

- SMA (Simple Moving Average)

The stop will be trailed with the SMA price and stop you out if the bars price selected in the 'Price' setting (open, high, low or close) is lower.

Example:

istantanea

The price is trailed upwards with the value of the SMA of default length.

- Source

The stop will be trailed following an external input value (from an indicator).

A selection between 'Value', 'Any' and 'None' needs to be made. For indicators giving a value close to price all the time a value of 'Value' can be used. The stop loss will then happen if the selected 'Price' crosses below the indicator value. For indicators giving you a signal for a sell 'Any' should be used. An alert for selling (trailing stop loss hit) will be sent when the 'Source' gives anything other than 'na'. Vice versa is for 'None' (the indicator will alert you when the indicator source stops giving a value and gives 'na' instead).

Example:

istantanea

The price is trailed upwards with the value of the external indicator that gives the source value. In this case it's trailed to 'Up Trend' from SuperTrend by KivancOzbilgic (tradingview.com/script/r6dAP7yi/) with the 'Type' set to 'Value'.

- Market structure

When a structure break occurs (BOS or CHoCH/CHoCH+) the stop is trailed up to the lowest price within the structure break. When price then goes below the previous lower low the script will signal you to exit the trade. Note that structure breaks can be repainting (happen and then disappear) if you don't have 'Force close' checked. If so, the script will only check for structural breaks on closed bars. Note that the trailing stop can also look like it's at a level that's not the lowest low. This is because there was a break of structure earlier that was replaced by a longer one. The new, longer one, might have a lower low but since a trailing stop loss cannot move lower it cannot be lowered.

You will find settings for the market structure (BOS/CHoCH/CHoCH+). Select left and right pivot lengths and if the pivots should have a label or not.

The addition of market structure data is possible with my library 'PriceAction' (tradingview.com/script/nAGqSrxy-PriceAction/).

When you select market structure as trailing stop structural breaks will be visualized.

You will see solid lines for break of structures (BOS), change of characters (CHoCH) and change of character plus (CHoCH+).

When the closing price breaks a previous pivot a break of structure (BOS) or a change of character (CHoCH or CHoCH+) will be created. The latest 5 pivots found within the current trend will be kept to take action on.

A break of structure is removed if an earlier pivot within the same trend is broken and the pivots high price for a bullish trend or low price for a bearish trend is more extreme than the BOS pivots price.

You are able to show the pivots that are used. "HH" (higher high), "HL" (higher low), "LH" (lower high), "LL" (lower low) and "H"/"L" (for pivots (high/low) when the trend has changed) are the labels used.

Example:

istantanea

The price is trailed upwards when break of structures (BOS) happens. It's set to the lowest price within the BOS. It's not moved down, only up, and is hit to the right of the chart

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.