UPDATE 18 May 2009: Fixed error as noted in comments below (Thanks, Jeff!)
UPDATE 23 Feb 2009: Added option to use High/Low as well as Close as the volatility stop switching trigger. Also changed “AverageTrueRange” to exponential average of High-Low so it works on tick charts.
This Thinkscript indicator is a volatility-based trailing stop, similar to the Chandelier Stop. When direction switches from short to long (or vice versa), the initial stop level is a specified number of multiples of the Average True Range (ATR) of the last ‘n’ bars. Then the value is trailed, getting tighter should volatility decrease or price move to new extremes. A close through the stop level signifies a trend change in this indicator; crossing the value and closing back on the other side does not.
The part of this indicator that was tricky is the trailing stop logic, as well as switching the direction of the indicator–long or short. Thinkscript runs in very linear fashion, and you can’t define an indicator at one point and then change the value at another point. The workaround is to put complicated logic gates into the definition itself.
There are two inputs here: The ATR factor, and the length for the ATR calculation. These can be reset from the studies window without changing the Thinkscript. If you want a different default value, change the code below to the value you want.
The code can be found on my Google site.