0% found this document useful (0 votes)
10 views7 pages

DMI/ADX Functions in Pine Script

This Pine Script™ code implements a trading indicator named 'Indicator Normal Strong' that utilizes various technical analysis functions such as DMI, ADX, EMA, SuperTrend, and RSI to determine entry and exit conditions for long and short trades. It includes sections for calculating daily high/low, tracking trades, and visualizing entry and exit points on a chart. The script is designed for intraday trading and allows customization of parameters like DMI period, EMA lengths, and ATR settings.

Uploaded by

maheswarra.rd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

DMI/ADX Functions in Pine Script

This Pine Script™ code implements a trading indicator named 'Indicator Normal Strong' that utilizes various technical analysis functions such as DMI, ADX, EMA, SuperTrend, and RSI to determine entry and exit conditions for long and short trades. It includes sections for calculating daily high/low, tracking trades, and visualizing entry and exit points on a chart. The script is designed for intraday trading and allows customization of parameters like DMI period, EMA lengths, and ATR settings.

Uploaded by

maheswarra.rd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/// This Pine Script™ code is subject to the terms of the Mozilla Public License

2.0 at [Link]
// © goelsaurabh1980

//@version=6

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Start of Code //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// Name of the strategy


indicator('Indicator Normal Strong', overlay = true)

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Section of Functions: DMI/ADX and Day High/Low //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// DMI + ADX Function


calcDMI(srcHigh, srcLow, srcClose, len) =>
upMove = srcHigh - srcHigh[1]
downMove = srcLow[1] - srcLow
plusDM = upMove > downMove and upMove > 0 ? upMove : 0
minusDM = downMove > upMove and downMove > 0 ? downMove : 0
tr = [Link]([Link](srcHigh - srcLow, [Link](srcHigh -
srcClose[1])), [Link](srcLow - srcClose[1]))
smoothedTR = [Link](tr, len)
plusDM14 = [Link](plusDM, len)
minusDM14 = [Link](minusDM, len)
diPlus = 100 * plusDM14 / smoothedTR
diMinus = 100 * minusDM14 / smoothedTR
dx = 100 * [Link](diPlus - diMinus) / (diPlus + diMinus)
adx = [Link](dx, len)
[diPlus, diMinus, adx]

// Input
length = [Link](14, "DMI Period")

// Daily high/low function


getDayHighLow(srcHigh, srcLow) =>
var float dHigh = na
var float dLow = na
isNewDay = dayofweek != dayofweek[1]
if isNewDay
dHigh := srcHigh
dLow := srcLow
else
dHigh := [Link](dHigh, srcHigh)
dLow := [Link](dLow, srcLow)
[dHigh, dLow]

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Section of Indicators: EMA, SuperTrend, DMI, ADX, RSI, Day High/Low //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Select EMAs length
fastemaLength = [Link](title = 'Fast EMA Length', defval = 5, minval = 1, maxval
= 25, step = 1)
slowemaLength = [Link](title = 'Slow EMA Length', defval = 14, minval = 1,
maxval = 25, step = 1)

// Calculate EMA
fastema = [Link](close, fastemaLength)
slowema = [Link](close, slowemaLength)

// Select RSI length


rsiLength = [Link](14, minval = 1, title = 'RSI Length')

// Calculate RSI
rsi = [Link](close, rsiLength)

// Select atr period and factor


atrPeriod = [Link](14, 'ATR Length', minval = 1)
factor = [Link](1.6, 'Factor', minval = 0.01, step = 0.01)

// Calculate and Plot Supertrend


[supertrend, direction] = [Link](factor, atrPeriod)

// Fetch OHLC of chart


High = [Link]([Link], [Link], high)
Low = [Link]([Link], [Link], low)
Close = [Link]([Link], [Link], close)
Open = [Link]([Link], [Link], open)

// Calculate the day's high and low


[plus, minus, adx] = calcDMI(High, Low, Close, length)

// Calculate Day High/Low


[DayHigh, DayLow] = getDayHighLow(High, Low)

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Normal //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Section of Long Entry, Long Exit, Short Entry and Short Exit Conditions //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// Initialize variables to store entry prices


var bool longentry = false
var bool shortentry = false
var bool longexit = false
var bool shortexit = false
var bool longexit_low = false
var bool shortexit_high = false
var bool leinTrade = false
var bool seinTrade = false
var float lelow = 0.0
var float sehigh = 0.0
// Long entry condition
le = close[0] > open[0] and rsi[0] > 60 and direction[0] < 0 and close[0] >
fastema[0] and plus[0] > minus[0] and plus[0] > 25 and adx[0] > adx[1]
longentry := le

// Short entry condition


se = close[0] < open[0] and rsi[0] < 40 and direction[0] > 0 and close[0] <
fastema[0] and plus[0] < minus[0] and minus[0] > 25 and adx[0] > adx[1]
shortentry := se

// Long entry low at the time of entry


if longentry and not leinTrade
lelow := low // Store long entry low price at entry
leinTrade := true

// Short entry high at the time of entry


if shortentry and not seinTrade
sehigh := high // Store short entry high price at entry
seinTrade := true

// Long exit condition


longexit := close[0] < open[0] and rsi[0] < 50 and direction[0] > 0 and close[0] <
fastema[0]

// Short exit condition


shortexit := close[0] > open[0] and rsi[0] > 50 and direction[0] < 0 and close[0] >
fastema[0]

// Reset long entry low at the time of exit


if longexit
lelow := 0.0
leinTrade := false

// Reset short entry high at the time of exit


if shortexit
sehigh := 0.0
seinTrade := false

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Section of Trade Track and P&L //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// Initialize trade state and other variables


var bool longinTrade = false
var bool shortinTrade = false
var bool shortBg = false
var bool longBg = false

// Session timings
entry1 = [Link](title = 'Entry session 1', defval = '0918-1529', group =
'Intraday Timing')
t1 = time == time([Link], entry1)

// Track trade state for both long and short


if shortentry and t1
shortinTrade := true
shortinTrade

if shortexit or not t1
shortinTrade := false
shortinTrade

if longentry and t1
longinTrade := true
longinTrade

if longexit or not t1
longinTrade := false
longinTrade

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Section of Highlight and Plot Labels of Entry and Exit //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// Select entry highlight method


highlight_trades = [Link]('Yes', title = 'Highlight Trades', options =
['Yes', 'No'])

// Highlight background when in trade for both long and short


bgcolor(shortinTrade and highlight_trades == 'Yes' ? [Link]([Link], 90) : na,
title = 'Trade Background')
bgcolor(longinTrade and highlight_trades == 'Yes' ? [Link]([Link], 90) :
na, title = 'Trade Background')

// Plot labels of entry and exit


plotshape(longinTrade and not longinTrade[1], title = 'N Long Entry', text = 'NLE',
style = [Link], location = [Link], color = [Link], textcolor =
[Link], size = [Link])
plotshape(longinTrade[1] and not longinTrade, title = 'N Long Exit', text = 'NLX',
style = [Link], location = [Link], color = [Link], textcolor =
[Link], size = [Link])
plotshape(shortinTrade and not shortinTrade[1], title = 'N Short Entry', text =
'NSE', style = [Link], location = [Link], color = [Link],
textcolor = [Link], size = [Link])
plotshape(shortinTrade[1] and not shortinTrade, title = 'N Short Exit', text =
'NSX', style = [Link], location = [Link], color = [Link],
textcolor = [Link], size = [Link])

plot(lelow>0?lelow:na, color = [Link], title = 'Prev Low', style =


plot.style_circles, linewidth = 2)
plot(sehigh>0?sehigh:na, color = [Link], title = 'Prev High', style =
plot.style_circles, linewidth = 2)

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Strong //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// Initialize variables to store entry prices


var bool longentry_s = false
var bool shortentry_s = false
var bool longexit_s = false
var bool shortexit_s = false
var bool longexit_low_s = false
var bool shortexit_high_s = false
var bool leinTrade_s = false
var bool seinTrade_s = false
var float lelow_s = 0.0
var float sehigh_s = 0.0

// Entry conditions
longentry_s := close[0] > open[0] and rsi[0] > 65 and rsi[1] > 60 and rsi[2] > 55
and direction[0] < 0 and [Link](plus) > [Link](minus) and adx[0] > adx[1] and
adx[1] > adx[2] and adx[2] > adx[3] and [Link](plus) >= 35 and adx[0] > 30 and
[Link](plus[0]) > [Link](plus[1])
shortentry_s := close[0] < open[0] and rsi[0] < 35 and rsi[0] < 40 and rsi[0] < 55
and direction[0] > 0 and [Link](minus) > [Link](plus) and adx[0] > adx[1] and
adx[1] > adx[2] and adx[2] > adx[3] and [Link](minus) >= 35 and adx[0] > 30 and
[Link](minus[0]) > [Link](minus[1])

// Long entry low at the time of entry


if longentry_s and not leinTrade_s
lelow_s := low // Store long entry low price at entry
leinTrade_s := true

// Short entry high at the time of entry


if shortentry_s and not seinTrade_s
sehigh_s := high // Store short entry high price at entry
seinTrade_s := true

// Long exit condition


lxcond1 = close[0] < open[0] and direction[0] > 0 and rsi[0] < 45 and rsi[1] < 45
lxcond2 = (rsi[1] - rsi[0] >= 20 or rsi[2] - rsi[0] >= 25 or rsi[3] - rsi[0] >= 30
and rsi[0] < 50)
lxcond3 = close[0] < open[0] and close[1] < open[1] and close[2] < open[2] and
close[0] < close[1] and close[1] < close[2] and rsi[0] < 50
lxcond4 = rsi[0] < 50 and rsi[1] < 50 and adx[0] < adx[1] and adx[1] < adx[2]
lxcond5 = rsi[0] < 55 and adx[0] < adx[1] and [Link](plus[0]) < 40 and fastema[0]
< fastema[1]
longexit_low_s := low[0] < lelow_s and close[0] < open[0] and ((rsi[0] < 50 and
fastema[0] < fastema[1]) or (fastema[0] < fastema[1] and fastema[1] < fastema[2]
and fastema[2] < fastema[3]))
longexit_s := shortentry_s or lxcond1 or lxcond2 or lxcond3 or lxcond4 or lxcond5
or (adx[0] < 10 and adx[1] < 10 and adx[0] < adx[1]) or longexit_low_s

// Short exit condition


sxcond1 = close[0] > open[0] and direction[0] < 0 and rsi[0] > 55 and rsi[0] > 55
sxcond2 = (rsi[0] - rsi[1] >= 20 or rsi[0] - rsi[2] >= 25 or rsi[0] - rsi[3] >= 30
and rsi[0] > 50)
sxcond3 = close[0] > open[0] and close[1] > open[1] and close[2] > open[2] and
close[0] > close[1] and close[1] > close[2] and rsi[0] > 50
sxcond4 = rsi[0] > 50 and rsi[1] > 50 and adx[0] < adx[1] and adx[1] < adx[2]
sxcond5 = rsi[0] > 45 and adx[0] < adx[1] and [Link](minus[0]) < 40 and
fastema[0] > fastema[1]
shortexit_high_s := high[0] > sehigh_s and close[0] > open[0] and ((rsi[0] > 50 and
fastema[0] > fastema[1]) or (fastema[0] > fastema[1] and fastema[1] > fastema[2]
and fastema[2] > fastema[3]))
shortexit_s := longentry_s or sxcond1 or sxcond2 or sxcond3 or sxcond4 or sxcond5
or (adx[0] < 10 and adx[1] < 10 and adx[0] < adx[1]) or shortexit_high_s

// Reset long entry low at the time of exit


if longexit_s
lelow_s := 0.0
leinTrade_s := false

// Reset short entry high at the time of exit


if shortexit_s
sehigh_s := 0.0
seinTrade_s := false

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Section of Trade Track and P&L //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// Initialize trade state and other variables


var bool longinTrade_s = false
var bool shortinTrade_s = false
var bool shortBg_s = false
var bool longBg_s = false

// Session timings
entry_s = [Link](title = 'Entry session 1', defval = '0918-1529', group =
'Intraday Timing')
t_s = time == time([Link], entry_s)

// Track trade state for both long and short


if shortentry_s and t_s
shortinTrade_s := true
shortinTrade_s

if shortexit_s or not t_s


shortinTrade_s := false
shortinTrade_s

if longentry_s and t_s


longinTrade_s := true
longinTrade_s

if longexit_s or not t_s


longinTrade_s := false
longinTrade_s

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// Section of Highlight and Plot Labels of Entry and Exit //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

// Select entry highlight method


highlight_trades_s = [Link]('Yes', title = 'Highlight Trades', options =
['Yes', 'No'])

// Highlight background when in trade for both long and short


bgcolor(shortinTrade_s and highlight_trades_s == 'Yes' ? [Link]([Link], 90) :
na, title = 'Trade Background')
bgcolor(longinTrade_s and highlight_trades_s == 'Yes' ? [Link]([Link],
90) : na, title = 'Trade Background')
// Plot labels of entry and exit
plotshape(longinTrade_s and not longinTrade_s[1], title = 'N Long Entry', text =
'SLE', style = [Link], location = [Link], color = [Link],
textcolor = [Link], size = [Link])
plotshape(longinTrade_s[1] and not longinTrade_s, title = 'N Long Exit', text =
'SLX', style = [Link], location = [Link], color = [Link],
textcolor = [Link], size = [Link])
plotshape(shortinTrade_s and not shortinTrade_s[1], title = 'N Short Entry', text =
'SSE', style = [Link], location = [Link], color = [Link],
textcolor = [Link], size = [Link])
plotshape(shortinTrade_s[1] and not shortinTrade_s, title = 'N Short Exit', text =
'SSX', style = [Link], location = [Link], color = [Link],
textcolor = [Link], size = [Link])

plot(lelow_s>0?lelow_s:na, color = [Link], title = 'Prev Low', style =


plot.style_circles, linewidth = 2)
plot(sehigh_s>0?sehigh_s:na, color = [Link], title = 'Prev High', style =
plot.style_circles, linewidth = 2)

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
// End of Code //
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////

You might also like