Editors' picksOPEN-SOURCE SCRIPT

Bjorgum AutoTrail

Updated
One Time Trade Risk Management

Incorporating the new interactive feature, this script is meant as a one time trailing stop for the active trader to manage positional risk of an ongoing trade. As a crypto trader or Fx trader, many may find themselves in a position late into the evening, or perhaps daily life is calling while a trade progresses in their favor. Adding a trailing stop to a position thats trending can help to keep you in the trade and lock in gains if things turn around when you are unable to react.

To use the trail, the user would add the script to the chart. Once added, a set of crosshairs will appear allowing the user to choose a point to begin. Often choosing to start a trail from a swing high/ low can be an ideal option. This tends to provide some protection for a stop by placing it under support for a long trade or above resistance for a short trade.

Price based trail

snapshot

The trail will automatically plot and the offset is a factor of the distance from price action selected by the crosshairs. If placed above price action the script will plot a short trail, if placed below it will trail for a long position.

Additionally, there are several other trail types other than price based. There is also percent based, which offsets the trail as a percent from close. A hard stop is placed at the cross hair value, then once the distance is exceeded by the percentage specified, the trail begins.

There are 2 more volatility based trails. There is a PSAR trail which can provide quicker and tighter stops that accelerate with the trend locking in gains faster, and an ATR trail that keeps a distance from price action as a function of volatility. Volatility levels can be adjusted from the menu.

Volatility based trail (ATR)

snapshot

Volatility based trail (PSAR)

snapshot

Lastly, within the code for more the more technical savvy, is some starting setups for string alerts to be sent to exchanges via 3rd party or custom API applications. Some string manipulation is required for specific providers to meet their requirements, but there is some building block alerts that will take the ticker symbol, recognize the asset your trading (Fx, Crypto, etc) and take input quantity or exchange names from the settings via inputs.

Complex strings can be built to perform almost any trade related task when to comes to alerts via web hook. A little setup this way with some technology to back your system can mean a semi-automated half man, half machine setup that actually manages your trail stop while you cannot. For those that don’t go this far, there is some basic alert functionality that well trigger when a trail is hit so you can react and make a decision.

Please note that for now, interactive mode is engaged only when the script is added to the chart. Additional stops, or for adjustments to be made it is best to add a new version. Also as real trades could be at play managing an actual position, alerts are designed to go off only once to ensure no duplicate orders are sent meaning alerts are not reoccurring. Once an alert is triggered, a new trail is to be set up.

A modified version of the TradingView built in SAR equation was used in this script. To provide the value of the SAR on the stop candle, it was necessary to alter the equation to extract this value as the regular SAR “flips” at this point. Thank you to TradingView for supplying access to the built in formula so that this SAR could behave the same as the built-in function outside of these alterations

Example of SAR value maintained in trigger candle

snapshot

Cheers and happy trading.
Release Notes
Added variable source input to allow open and close to be a choice in addition to the original high/low default.
Release Notes
Added time check to make Runtime error more robust. Runtime error will occur if stop is placed between the high and the low of the initialization candle as this triggers a stopped condition within the code.
Release Notes
Added color differentiation between short and long bias - adjustable from settings menu
Release Notes
Implemented a new level check which changes the starting point for ATR and Percent type trailing stops. Previously, if the price point for these two trail types was ABOVE the calculated ATR or percent trail start point, the start point would drop down to the calculated point causing an increase in risk. Now the script will use the point selected with the mouse to start the stop loss, and the script will begin trailing at such a time that the start point is exceeded by the calculated trailing value. If the calculated value is greater than where the cursor is placed, the start point will be risk adjusted upwards to the calculated value.

snapshot
automatedAverage True Range (ATR)BESTbotcryptoeffectiveParabolic Stop and Reverse (PSAR)riskscripttrailingstopVolatility Stop

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Disclaimer