1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.
Source Code
30 lines of Pine Script v6 from Codon_ATR_Stop.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.
Codon_ATR_Stop.pine
// 🧬 Generated by Codon.pro | The AI Pine Script Builder
// Module: ATR Trailing Stop Logic
// Description: Returns the dynamic trailing stop price based on ATR.
//@version=6
library("CodonATRStop", overlay=true)
// @function Calculates Chandelier/ATR Trailing Stop level
// @param length ATR Period (e.g., 14)
// @param mult ATR Multiplier (e.g., 3.0)
// @param isLong Boolean, true for Long logic, false for Short logic
// @returns float The trailing stop price level
export getStopLevel(int length, float mult, bool isLong) =>
float atr = ta.atr(length)
float stopLevel = na
if isLong
float proposed = close - (atr * mult)
stopLevel := na(stopLevel[1]) ? proposed : math.max(stopLevel[1], proposed)
// Reset if price dips below
if close < stopLevel[1]
stopLevel := proposed
else
float proposed = close + (atr * mult)
stopLevel := na(stopLevel[1]) ? proposed : math.min(stopLevel[1], proposed)
// Reset if price goes above
if close > stopLevel[1]
stopLevel := proposed
stopLevel
Overview
For longs it subtracts an ATR multiple from the close and ratchets the level upward, resetting the stop when price closes below the previous level. For shorts it adds the ATR multiple and ratchets the level downward, resetting when price closes above the previous level. Callers supply the ATR length, multiplier, and direction. It does not place orders, size positions, or draw plots itself.
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco
This Pine Script v5 library walks historical price, volume, time and amount series until a requested number of hours is covered, stopping at 4999 bars. TimeAligned returns the bar count, net and absolute price changes, net and absolute log-price changes, volum
VolTargeting is a Pine Script v5 library of functions for volatility-based position sizing. ewmaVol estimates exponentially weighted annualised volatility from squared simple returns and scales it by the square root of 365 days for crypto or 252 days otherwise
This library calculates arrays of daily price deviations from the open to the high, low, and close over a user-specified number of days. It optionally filters outliers using a maximum deviation threshold. The fromOpen function returns a normalized value showin
Provides a collection of technical analysis calculation functions for use across Pine scripts. It exports implementations for various indicators and mathematical formulas, including oscillators like the Awesome Oscillator, Aroon, and Schaff Trend Cycle, moving
This Pine Script v5 library provides functions to calculate and plot linear regression channels using sample point arrays. It evaluates paired X and Y arrays to calculate the regression slope, intercept, fitted values, maximum deviation, minimum deviation, and