Trail Blaze - (Multi Function Trailing Stop Loss) - [mutantdog]

mutantdog Uppdaterad   
Shorter version:

As the title states, this is a 'Trailing Stop' type indicator, albeit one with a whole bunch of additional functionality, making it far more versatile and customisable than a standard trailing stop.

The main set of features includes:

Three independent trailing types each with their own +/- multipliers:
- Standard % change
- ATR (aka Supertrend)
- IQR (inter-quartile range)
These can be used in isolation or summed together. A subsequent pair of direction specific multipliers are also included.

Two separate custom source inputs are available, both feature the standard options alongside a selection of 'weighted inputs' and the option to use another indicator (selected via 'AUX'):
- 'Centre' determines the value about which the trailing sum will be added to define the stop level.
- 'Trigger' determines the value used for crossing of stops, initiating trend changes and triggering alerts.
A selection of optional filters and moving averages are available for both.

Furthermore there are various useful visualisation options available, including the underlying bands that govern the stop levels. Preset alerts for trend reversals are also included.

This is not really an 'out-of-the-box' indicator. Depending upon the market and timeframe some adjustments will be necessary for it to function in a useful manner, these can be as simple or complex as the feature-set allows. Basic settings are easy to dial in however and the default state is intended as a good starting point. Alternatively with some experimentation, a plethora of unique and creative configurations are possible, making this a great tool for tweaking. Below is a more detailed overview followed by a bunch of simple example settings.


Lengthy Version:


Before we start breaking this down, a little background. This started off as an attempt to improve upon the ever-popular Supertrend indicator. Of course there are many excellent user created variants available utilising some interesting methods to overcome the drawbacks of the basic version. To that end, rather than copying the work of others, the direction here shifted towards a hybrid trailing stop loss with a bunch of additional user customisation options. At some point, a completely different project involving IQR got morphed into this one. After sitting through months of sideways chop (where this proved to be of limited use), at the time of publication the market has began to form some near term trend direction and it appears to be performing well in many different timeframes.

And so with that out of the way...


The standard Supertrend (and most other variants) includes a single source input, as default set to 'hl2' (candle mid-range). This is the centre around which the atr bands are added/subtracted to govern the stop levels. This is not however the value which is used to trigger the trend reversal, that is usually hard-coded to 'close'. For this version both source values are adjustable: labelled 'centre' and 'trigger' respectively.

Each has custom input selectors including the usual options, a selection of 'weighted inputs' and the option to use another indicator (selected from the Aux input). The 'weighted inputs' are those introduced in Weight Gain 4000, for more details please refer to that listing. These should be treated as experimental, however may prove useful in certain configurations. In this case 'hl-oc2' can be considered an estimate of the candle median and may be a good alternative to the default 'centre' setting of 'hl2', in contrast 'cc-ohlc4' can tend to favour the extremes in the trend direction so could be useful as a faster 'trigger' than the default 'close'.

To cap them off both come with a selection of moving average filters (SMA, EMA, WMA, RMA, HMA, VWMA and a simple VWEMA - note: not elastic) aswell as median and mid-range. 'Centre' can also be set to the output of 'trigger' post-filter which can be useful if working with fast/slow crosses as the basis.


This is the main section, comprised of three separate factors: 'TSL', 'ATR' and 'IQR'. The first two should be fairly obvious, 'TSL' (trailing stop loss) is simply a percentage of the 'centre' value while 'ATR' (average true range) is the standard RMA-based version as used in Supertrend, Volatility Stop etc.

The third factor is less common however: 'IQR' (inter-quartile range). In case you are unfamiliar the principle here is, for a given dataset, the greatest 25% and smallest 25% of samples are removed. The remainder is then treated as a set and the range is calculated by highest - lowest. This is a commonly used method in statistical analysis, by removing the extremes it is less prone to influence by outliers and gives a good representation of the main dispersion around the median. In practise i have found it can be a good alternative to ATR, translating better across multiple time-frames due to it representing a fraction of the total range rather than an average of per-candle range like ATR. Used in combination with the others it can also add a factor more representative of longer-term/higher-timeframe trend. By discarding outliers it also benefits from not being impacted by brief pumps/volatility, instead responding only to more sustained changes in trend, such as rallies and parabolic moves. In order to give an accurate result the IQR is calculated using a dataset of high, low and hlcc4 values for all bars within the lookback length. Once calculated this value is then halved which, strictly speaking, makes it a semi-interquartile range.

All three of these components can be used individually or summed together to create a hybrid dynamics factor. Furthermore each multiplier can be set to both positive and negative values allowing for some interesting and creative possibilities. An optional smoothing filter can be applied to the sum, this is a basic SWMA-4 which is can reduce the impact of sudden changes but does incur a noticeable lag. Finally, a basic limiter condition has been hard-coded here to prevent the sum total from ever going below zero.

Capping off this section is a pair of direction multipliers. These simply take the prior dynamics sum and allow for further multiplication applied only to one side (uptrend/lo-stop and downtrend/hi-stop). To see why this is useful consider that markets often behave differently in each direction, we've all seen prices steadily climb over several weeks and then abruptly dump in the process of a day or two, shorter time frames are no stranger to this either. A lack of downside liquidity, a panicked market, aggressive shorts. All these things contribute to significant differences in downward price action. This function allows for tighter stops in one direction compared to the other to reflect this imbalance.


With all of these options and possibilities, some visual aids are useful. Beneath the dynamics' section are several visual options including both sources post-filter and the actual 'bands' created by the dynamics. These are what govern the stop levels and seeing them in full can help to better understand what our various configurations actually do. We can even hide the stop levels altogether and just use the bands, making this a kind of expanded Keltner Channel. Here we can also find colour and opacity settings for everything we've discussed.


The obvious first example here is the standard %-change trailing stop loss which, from my experience, tends to be the best suited for lower time frames. Filtering should probably minimal here. In both charts here we use the default config for source inputs, the top is a standard bi-directional setup with 1.5% tsl while the bottom uses a 2.5% tsl with the histop multiplier reduced to 0 resulting in an uptrend only stoploss.

Shown here in grey is the standard Supertrend which uses 'hl2' as centre and 'close' as trigger, ATR(10) multiplied by 3. On top we have the default filtered source config with ATR(8) multiplied by 2 which gives a different yet functionally similar result, below is the same source config instead using IQR(12) multiplied by 2. Notice here the more 'stepped' response from IQR following the central rally, holding back for a while before closing in on price and ultimately initiating reversal much sooner. Unlike ATR, the length parameter for IQR is absolute and can more significantly affect its responsiveness.

Next we focus on the visualisation options, on top we have the default source config with ATR(8) multiplied by 2 and IQR(12) multiplied by 1. Here we have activated the switch to show 'bands', from this we can see the actual summed dynamics and how it influences the stop levels. Below that we have an altogether different config utilising the included filters which are now visible. In this example we have created a basic 8/21 EMA cross and set a 1% TSL, notice the brief fakeout in the middle which ordinarily might indicate a buy signal. Here the TSL functions as an additional requirement which in this case is not met and thus no buy signal is given.

Finally we have a couple of more 'experimental' examples. On top we have Lazybear's 'Variable Moving Average' in white which has been assigned via 'aux' as the centre with no additional filtering, the default config for trigger is used here and a basic TSL of 1.5% added. It's a simple example but it shows how this can be applied to other indicators. At the bottom we return to the default source config, combining a TSL of 8% with IQR(24) multiplied by -2. Note here the negative IQR with greater length which causes the stop to close in on price following significant deviations while otherwise remaining fairly wide. Combining positive and negative multiples of each factor can yield mixed results, some more useful than others depending upon suitable market conditions.

Since this has been quite lengthy, i shall leave it there. Suffice to say that there are plenty more ways to use this besides these examples. Please feel free to share any of your own ideas in the comments below. Enjoy.

Added a second AUX input.
PineScript has recently been updated to allow for multiple 'indicator-on-indicator' inputs. This means centre and trigger can now be set to use different indicators as their sources. To use crossings of your own choosing just assign them via AUX 1 and AUX 2.

Aside from that, code has been tidied a little and a minor bugfix has been added.
Skript med en öppen källkod

I sann TradingView-anda har författaren publicerat detta skript med öppen källkod så att andra handlare kan förstå och verifiera det. Hatten av för författaren! Du kan använda det gratis men återanvändning av den här koden i en publikation regleras av våra ordningsregler. Du kan ange den som favorit för att använda den i ett diagram.

Frånsägelse av ansvar

Informationen och publikationerna är inte avsedda att vara, och utgör inte heller finansiella, investerings-, handels- eller andra typer av råd eller rekommendationer som tillhandahålls eller stöds av TradingView. Läs mer i Användarvillkoren.

Vill du använda det här skriptet i ett diagram?