Indicator · Pine Script v5 · On the chart

Elephant Bars Single Retracement

by FranklyBrandon· code credited to MelonLordRobertshttps://www.tradingview.com/chart/lpuvUwJC/?

A variant of the Elephant Bars overlay that considers only one-bar retracements.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code74
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update3 months ago18 Jul 2026

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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

74 lines of Pine Script v5 from Denali.V1/Denali.Visualizer/pineScript_single_retracement.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

pineScript_single_retracement.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MelonLordRobertshttps://www.tradingview.com/chart/lpuvUwJC/?symbol=NASDAQ%3AAAPL#

//@version=5
indicator("Elephant Bars", overlay=true)

//--Elephants Input--
averageRangeBacklog = input.int(100, title="Length of the lookback period in the calculation of SMA for the body range of the bars.")
elephantBarThreshold = input.float(1.5, title="The threshold a bar's body range must exceed in order to be classied as an Elephant.")
retracementBodyThreshold = input.float(0.50, title="The threshold a bar's body range must NOT exceed in order to be classified as a Retracemnt, calculated as a percentage of the Elephant bar's body range.")
retracementPriceThreshold = input.float(0.25, title="The threshold a bar's price must NOT exceed away from the Elephant bar's close to be classified as a retracement, calculated as a percentage of the Elephant bar's body range.")
retracementConsolidatonMaxLength = input.int(2, title="The total number of bars a retracment consolidation can last before an Elephant is abondoned")


//--Current Tick calculations
bodyRange = math.abs(open - close)
bodyRangeAverage = ta.sma(bodyRange, averageRangeBacklog)
bodyMidPoint = math.avg(open, close)
bool elephantBar = (bodyRange >= bodyRangeAverage * elephantBarThreshold)


//For now, only consider retracements that last one bar
retracementBar = false
long =false
if(elephantBar[1])
    retracementBody = false
    retracementPrice = false
    //Whether or not we are looking for a long or short continuation
    long := (close[1] > open[1])
    retracementBody := (bodyRange <= bodyRange[1] * retracementBodyThreshold)
    if(long)
        retracementPrice := ((bodyRange[1] * retracementPriceThreshold) + close[1]) >= math.max(open, close)
    else
        retracementPrice := ((bodyRange[1] * retracementPriceThreshold) - close[1]) <= math.min(open, close)

    retracementBar := (retracementBody and retracementPrice)
    
buy = false
if(retracementBar[1] and (close[2] > open[2]) and close > math.max(open[1], close[1]))
    buy := true
    
plotchar(elephantBar, "Elephant", "🐘", location.abovebar, color.silver, size = size.tiny)
plotchar(retracementBar, "Retracement", "❕", location.abovebar, color.silver, size = size.tiny)
plotchar(buy, "Retracement", "🐉", location.abovebar, color.silver, size = size.tiny)

//--Retracement--
bool retracement = elephantBar[1]
bool retracmentThresholdMet = bodyRange <= bodyRange[1] * retracementBodyThreshold
float midpoint = math.avg(open[1], close[1])
bool candleOpen = (close[1] > open[1])



//SMAs
// Checkbox's for the other 2 MA's
a = input(true, title="Enable 1st MA")
b = input(true, title="Enable 2nd MA")
c = input(true, title="Enable 3rd MA")

len1 = input.int(3, minval=1, title="Length1")
len2 = input.int(8, minval=1, title="Length2")
len3 = input.int(21, minval=1, title="Length3")

src1 = input(close, title="Source1")
src2 = input(close, title="Source2")
src3 = input(close, title="Source3")

out1 = ta.sma(src1, len1)
out2 = ta.sma(src2, len2)
out3 = ta.sma(src3, len3)

plot(a and out1 ? out1: na, title="SMA1", color=color.green, linewidth=1, transp=50)
plot(b and out2 ? out2: na, title="SMA2", color=color.blue, linewidth=2, transp=50)
plot(c and out3 ? out3: na, title="SMA3", color=color.orange, linewidth=3, transp=50)

Overview

Elephant Bars Single Retracement flags candles whose body is at least a set multiple of the average body, then marks a following bar as a retracement when its body is small relative to the elephant bar and its price stays within a set fraction of the elephant bar's range. A further marker appears when a later bar closes beyond the retracement bar in the elephant's direction. Three optional simple moving averages are included.

Details

Published by

FranklyBrandon on GitHub

Code credit

MelonLordRobertshttps://www.tradingview.com/chart/lpuvUwJC/?, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Candlestick patterns, Moving averages

Markets

Stocks

Timeframes

M15-H1

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.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Length of the lookback period in the calculation of SMA for the body range of the bars.int100averageRangeBacklog
The threshold a bar's body range must exceed in order to be classied as an Elephant.float1.5elephantBarThreshold
The threshold a bar's body range must NOT exceed in order to be classified as a Retracemnt, calculated as a percentage ofloat0.50retracementBodyThreshold
The threshold a bar's price must NOT exceed away from the Elephant bar's close to be classified as a retracement, calculfloat0.25retracementPriceThreshold
The total number of bars a retracment consolidation can last before an Elephant is abondonedint2retracementConsolidatonMaxLength
Enable 1st MAbooltruea
Enable 2nd MAbooltrueb
Enable 3rd MAbooltruec
Length1int3len1
Length2int8len2
Length3int21len3
Source1sourceclosesrc1
Show all 14 settings
SettingTypeDefaultVariable
Source2sourceclosesrc2
Source3sourceclosesrc3
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

285 stars

Mean Reversion Study is an indicator that identifies trade setups combining exponential moving averages, RSI extremes, and engulfing candlestick patterns. It highlights bullish setups when a bullish engulfing candle forms below the EMA while RSI is oversold, a

Indicator v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Study

Smoothed candles

by be-thomas
65 stars

This indicator draws smoothed candlesticks on the price chart. It applies a simple moving average of a user-set length, defaulting to nine, separately to open, high, low, and close, then plots those four values with plotcandle. Wick color is green when the raw

Indicator v5 Moving averagesCandlestick patterns
Updated 5 days ago · GPL-3.0 GitHub repository of Smoothed candles

Major Buy Setup

by dcaoyuan
55 stars

Major Buy Setup plots a 20-period and a 50-period simple moving average on the price chart. It marks a buy when the current bar is both a bullish engulfing candle and a bald bull candle, defined as a green candle whose upper shadow is no more than five percent

Indicator v5 Moving averagesCandlestick patterns Repaints
Updated 6 months ago · Apache-2.0 GitHub repository of Major Buy Setup

30 min Pivot

by ChartsLector
41 stars

30 min Pivot is an overlay indicator that plots an exponential moving average (EMA) calculated from a specified timeframe (default 30 minutes) and flags specific multi-bar candle setups. It checks the relative distance between price and the higher-timeframe EM

Indicator v5 Moving averagesCandlestick patterns
Updated 3 days ago · None stated GitHub repository of 30 min Pivot

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers