Indicator · Pine Script v4 · On the chart

Efficient Trend Step Mod

by capissimo

This indicator plots a dynamic step trendline directly on the price chart using Kaufman Efficiency Ratio (ER).

View on GitHub

GitHub stars114on the repository
Pine versionv4older, still runs
Lines of code131
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Nov 2024

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

131 lines of Pine Script v4 from scripts/106-Efficient-Trend-Step-Mod-(v3).pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

106-Efficient-Trend-Step-Mod-(v3).pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © capissimo

//@version=4
study('Efficient Trend Step Mod (v.3)', '', true, precision=2)

// This is a version 3 of my mod of a wonderful script by alexgrover - Efficient Trend Step.

// It is based on calculation of Kaufman's efficiency ratio (ER):

// ER = Direction / Volatility
// where:
// Direction  = ABS (Close – Close[n])
// Volatility = n ∑(ABS(Close – Close[1]))
// n          = The efficiency ratio period.

//-------------------- Inputs

ds     = input(close,  'Dataset')
p      = input(7,      'Data Lag',          minval=1, maxval=2160)
minlag = input(3,      'Std Minimum Lag',   minval=1, maxval=2160)
maxlag = input(15,     'Std Maximum Lag',   minval=1, maxval=2160)
labels = input(true,   'Show Labels?')
ftype  = input('Both', 'Filter Signals by', options=['Volatility','Volume','Both','None'])

//-------------------- System Variables

var color c     = na
var float steps = 0.0
var int   BUY   = -1
var int   SELL  =  1
var int   HOLD  =  0

//-------------------- Dynamic Arrays

var int signal = 0

//-------------------- Custom Function

changed(x) => x!=x[1]

volumeBreak(thres) =>  
    rsivol   = rsi(volume, 14)  
    osc      = hma(rsivol, 10)  
    osc > thres                     

volatilityBreak(volmin, volmax) => atr(volmin) > atr(volmax)

//-------------------- Logic

filter =   ftype=='Volatility' ? volatilityBreak(1, 10) 
         : ftype=='Volume'     ? volumeBreak(49) 
         : ftype=='Both'       ? volatilityBreak(1, 10) and volumeBreak(49)
         :                       true
         
er     = abs(change(ds, p)) / sum(abs(change(ds)), p)                    // efficiency ratio
dev    = er * stdev(ds * 2, minlag) + (1 - er) * stdev(ds * 2, maxlag)   // deviation
steps := ds > nz(steps[1]) + dev ? ds : ds < nz(steps[1]) - dev ? ds : nz(steps[1])
c     := steps > nz(steps[1]) ? color.blue : steps < nz(steps[1]) ? color.red : nz(c[1])

signal := changed(c) and c==color.blue and filter ? BUY : changed(c) and c==color.red and filter ? SELL : nz(signal[1])

startLongTrade  = change(signal) and signal==BUY
startShortTrade = change(signal) and signal==SELL
endLongTrade    = change(signal) and signal==SELL
endShortTrade   = change(signal) and signal==BUY

//-------------------- Rendering

plot(steps, '', c, 2, transp=0)
plotshape(labels and startLongTrade  ? low  : na, '', shape.labelup,  location.belowbar,  color.blue, 0, size=size.tiny, text="B", textcolor=color.white)
plotshape(labels and startShortTrade ? high : na, '', shape.labeldown, location.abovebar, color.red,  0, size=size.tiny, text="S", textcolor=color.white)

//-------------------- Notification

alertcondition(startLongTrade,  'Buy',  'Go long')
alertcondition(startShortTrade, 'Sell', 'Go short') 
alertcondition(startLongTrade or startShortTrade, 'General Alert', 'Deal Time!')

//-------------------- Backtesting

show_info = input(true, '===Information===')
lot_size  = input(0.01,  'Lot Size', options=[0.01,0.1,0.2,0.3,0.5,1,2,3,5,10,20,30,50,100,1000]) 

tbase   = (time - time[1]) / 1000
tcurr   = (timenow - time_close[1]) / 1000
barlife = tcurr / tbase

bidask  = (open+high+low)/3

var float start_long_trade  = bidask  
var float long_trades       = 0.
var float start_short_trade = bidask  
var float short_trades      = 0.
var int   wins              = 0
var int   trade_count       = 0

if startLongTrade 
    start_long_trade := bidask 
if endLongTrade
    ldiff         = (bidask - start_long_trade)
    wins         := ldiff > 0 ? 1 : 0
    long_trades  := ldiff * lot_size   
    trade_count  := 1
if startShortTrade 
    start_short_trade := bidask
if endShortTrade
    sdiff         = (start_short_trade - bidask)
    wins         := sdiff > 0 ? 1 : 0
    short_trades := sdiff * lot_size 
    trade_count  := 1
    
cumreturn   = cum(long_trades) + cum(short_trades)  //-- cumulative return 
totaltrades = cum(trade_count)
totalwins   = cum(wins)  
totallosses = totaltrades - totalwins == 0 ? 1 : totaltrades - totalwins

//------------------- Information

var label lbl = na

info =  'CR='          + tostring(cumreturn,             '#.#') 
      + '\nTrades: '   + tostring(totaltrades,           '#')
      + '\nWin/Loss: ' + tostring(totalwins/totallosses, '#.##') 
      + '\nWinrate: '  + tostring(totalwins/totaltrades, '#.#%')       
      + '\nBar Time: ' + tostring(barlife,               '#.#%')

if show_info and barstate.islast  
    lbl := label.new(bar_index, ohlc4, info, xloc.bar_index, yloc.price, 
                     color.new(color.blue, 100), label.style_label_left, color.black, size.small, text.align_left)
    label.delete(lbl[1])

Overview

Efficient Trend Step Mod blends short and long standard deviations based on price efficiency to construct adaptive step thresholds. Trend flips trigger buy and sell signals, which can be filtered by ATR volatility, volume RSI oscillators, or a combination of both. The indicator plots a colored trend line, visual buy and sell labels, and includes configurable alerts.

Details

Published by

capissimo on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Trend, Volatility & bands, Volume

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.

Settings

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

SettingTypeDefaultVariable
Datasetsourcecloseds
Data Lagint7p
Std Minimum Lagint3minlag
Std Maximum Lagint15maxlag
Show Labels?booltruelabels
Filter Signals bystringBothftype
===Information===booltrueshow_info
Lot Sizefloat0.01lot_size
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

The Momentum Setup is a multi-indicator visual tool designed for trend and momentum analysis directly on the price chart. It combines Relative Strength Index (RSI), Money Flow Index (MFI), Commodity Channel Index (CCI), Supertrend, and a 250-bar percentage cha

Indicator v5 Oscillators & momentumTrend
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - Ankush Bajaj Momentum Investing Setup
92 stars

This indicator plots a volume-weighted average price of the typical price on the chart and colors that line by ADX strength. It draws a standard-deviation envelope around VWAP and tints the fill green when plus DI leads and red when minus DI leads. Triangle ma

Indicator v5 TrendOscillators & momentum
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev VWAP x ADX Explosion

This overlay indicator marks Wyckoff-style buying and selling climaxes on the price chart. It filters trend with an exponential moving average and a higher-timeframe EMA, then flags accumulation or distribution when price and ATR sit relative to their swing av

Indicator v6 TrendVolatility & bands
Updated 1 year ago · MIT GitHub repository of Ultimate Wyckoff Indicator V3 (Refined)
9 stars

This indicator plots a volume-weighted moving average as a central trend baseline and surrounds it with ATR-based volatility bands. It colors the trend and optional price bars according to whether price is on the bullish or bearish side of those bands. Users c

Indicator v6 TrendMoving averages
Updated 4 months ago · MIT GitHub repository of Volume Weighted Trend [QuantAlgo]

EMA System

by eddiebelaval
7 stars

EMA System plots four exponential moving averages on the price chart and marks a long-only pullback setup. It treats price above the 200 EMA and the 21 EMA above the 50 EMA as a valid trend, then flags a buy when price is near the 21 EMA, volume is below its s

Indicator v6 TrendMoving averages
Updated 3 months ago · None stated GitHub repository of EMA System

This indicator combines Chaikin Money Flow (CMF), a SuperTrend baseline, and an Average True Range (ATR) calculation inside a separate sub-pane. It calculates a standard 20-period CMF plotted as a line and colored histogram columns around a zero baseline, trac

Indicator v6 TrendVolume
Updated 6 months ago · Apache-2.0 GitHub repository of Institutional View: CMF & SuperTrend

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