OPEN-SOURCE SCRIPT

Julius Single Trail

65
Julius Single Trail — How it works
This indicator combines a Kalman-like smoothed Donchian midline with an ATR-style volatility buffer to create a single adaptive trailing line that flips with trend. It also recolors candles to reflect regime and visually marks ranging conditions using Bollinger Band width. Optionally, it adds a dotted price line and can hide default candles for a clean, unified look.

Core logic
Donchian midpoint: Calculates the middle of the highest high and lowest low over Donchian Length. This is the directional anchor.
Kalman-like smoothing: Applies a lightweight exponential update to the Donchian midpoint using Alpha, reducing noise while staying responsive.
Volatility buffer: Uses RMA of True Range over Volatility Length multiplied by Volatility Multiplier to form an adaptive offset around the smoothed midline.
Dynamic trail:
Up-trend regime (regime = 1): The trail is kMid - offset and only ratchets upward (math.max), acting like a long stop.
Down-trend regime (regime = -1): The trail is kMid + offset and only ratchets downward (math.min), acting like a short stop.
Flip conditions: Regime flips only when price is on the far side of both the smoothed midpoint and the current trail:
Flip to down when close < kMid and close < dynTrail
Flip to up when close > kMid and close > dynTrail
Candle styling:
Wick color shows immediate price direction (green for bullish, red for bearish).
Body color follows the trail’s regime (Uptrend Color or Downtrend Color).
In ranging conditions, all candle elements turn gray.
Ranging detection:
Computes Bollinger Bands on close with BB Length and BB Multiplier.
Calculates width as a percentage of the basis. If width% (optionally smoothed) is below Range Threshold %, candles are gray to signal consolidation.

What it plots
Dynamic Trail: A single, thick line that changes color by regime:
Uptrend: Uptrend Color (default lime)
Downtrend: Downtrend Color (default red)
Optional Trail Fill to Close: A translucent band between the trail and the close (disabled by default).
Optional Dotted Price Line: A dotted horizontal line at the current price (toggle via Show Dotted Price Line).
Candle treatment:
You can hide default candles (Hide Default Candles), then use a separate custom-candle script for wick/body/border mapping. In this script, default candles can be made fully transparent to let the trail and colors dominate.

Inputs
Donchian Length: Window for the highest/lowest used to form the midline.
Kalman Alpha 0–1: Smoothing factor for the midline. Higher = more responsive, lower = smoother.
Volatility Length: RMA length of True Range for the volatility buffer.
Volatility Multiplier: Scales the buffer around the midline. Higher widens the trail, reducing flips.
Uptrend Color / Downtrend Color: Trail and body color by regime.
Show Cloud To Close: Fills between price and trail using the trail’s color.
Hide Default Candles: Makes the native candles fully transparent.
Show Dotted Price Line / Price Line Color: Toggles and colors the dotted price line.
Ranging parameters:
BB Length (Ranging) and BB Multiplier (Ranging): Bollinger Band settings.
Range Threshold %: If BB width% < threshold, candles turn gray to indicate range.
Use Smoothed Width / Width Smoothing Length: Smooths BB width% before comparison.

Signals and interpretation
Regime shifts:
Bullish flip: When price closes above both the smoothed midpoint and the current trail. Trail switches to the lower band (kMid - offset) and ratchets up.
Bearish flip: When price closes below both the smoothed midpoint and the current trail. Trail switches to the upper band (kMid + offset) and ratchets down.
Trend bias:
Green trail/body: Favor long bias; trail can serve as a dynamic stop.
Red trail/body: Favor short bias; trail can serve as a dynamic stop.
Ranging filter:
Gray candles: Lower-probability trend continuation; consider reducing position sizing, waiting for a breakout, or using mean-reversion tactics.

How to use it
Trend following:
Enter in the direction of the regime when flips occur or on pullbacks that respect the trail.
Use the trail as a stop-loss guide: exit when price closes beyond the trail and the regime flips.
Range awareness:
When candles turn gray, avoid trend entries or switch to range tactics. Wait for color to return and a clean flip.
Tuning suggestions:
Faster, more responsive: Lower Donchian Length, increase Alpha, lower Volatility Length and/or Volatility Multiplier.
Smoother, fewer flips: Increase Donchian Length, decrease Alpha, increase Volatility Length and/or Volatility Multiplier.
Ranging strictness: Increase Range Threshold % to mark ranges more often; smooth the width to avoid choppiness.

Example settings
Swing trading:
Donchian Length: 50
Alpha: 0.25
Vol Length: 14
Vol Mult: 1.6
BB Length: 20, BB Mult: 2.0, Range Threshold %: 2.0, Smoothed width ON (20)
Intraday (more responsive):
Donchian Length: 20–30
Alpha: 0.4–0.6
Vol Length: 10–14
Vol Mult: 1.2–1.6
Range Threshold %: 1.5–2.5 depending on instrument

Alerts (suggested)
Regime flips:
Condition: close > dynTrail and close > kMid -> Alert: Bullish regime
Condition: close < dynTrail and close < kMid -> Alert: Bearish regime
Range state:
Condition: BB width% < threshold -> Alert: Ranging
You can wire these using alertcondition() on the flip conditions and isRange variable inside the script.

Notes and limitations
This is a single-side ratcheting trail per regime, designed to reduce whipsaw by requiring price to clear both the midpoint and the trail before flipping.
Like all trend tools, it can lag tops/bottoms and may chop in low-volatility, sideways markets.
For assets with highly irregular volatility, retune Volatility Multiplier and Range Threshold %.
Short description (for header):
Adaptive, single-line trailing stop based on Kalman-smoothed Donchian mid + ATR-style buffer. Colors candles by regime, grays out ranges via BB width. Optional price line and cloud.

If you want, I can add alertcondition() for the flip and range events and a light custom-candle overlay so you can publish with built-in alert templates and consistent candle styling.

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。