Library · Pine Script v6 · Library

CodonATRStop

by codon-pro

This library exports a function that returns a dynamic trailing stop price based on Average True Range.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code30
Settings0inputs
LicenceMIT
Last update10 months ago29 Dec 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
  4. 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.

Details

Published by

codon-pro on GitHub

Type

Library, Pine Script v6

Draws

Library (no plots)

Category

Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

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.

Similar Pine Scripts

Bands

by heyphat
26 stars

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

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Public

by wanjo-tech
6 stars

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

Library v5 Volatility & bandsVolume
Updated 2 years ago · None stated GitHub repository of Public

VolTargeting

by Robot-Wealth
4 stars

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

Library v5 Volatility & bands
Updated 2 years ago · None stated GitHub repository of VolTargeting

Daily Deviation

by Electrified-Trading
2 stars

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

Library v5 Volatility & bandsSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of Daily Deviation

ta

by ivansoft
1 stars

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

Library v5 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ta

FunctionLinearRegression

by ricardosantos79
261 stars

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

Library v5 Trend
Updated 4 years ago · None stated GitHub repository of FunctionLinearRegression

More libraries

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers