Support and Resistance Signals MTF+1+1
Support and Resistance Signals MTF+1+1
0
International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5
//
//"Support and Resistance Signals MTF" LuxAlgo + "Trend Lines [LuxAlgo]" +
ALGOEXTREMEP Profit 18385% DD 6% ON BTC TF45
//------------------------------------------------------------------------------
// Settings
//-----------------------------------------------------------------------------{
srMR = input.float(2, 'Support Resistance Margin', minval = .1, maxval = 10, step
= .1, group = srGR)
mnGR = 'Manupulations'
mnSH = input.bool(true, 'Manupulation Zones', group = mnGR)
mnMR = input.float(1.3, 'Manupulation Margin', minval = .1, maxval = 10, step
= .1, group = mnGR)
mnSZC = input(color.new(#2962ff, 73), 'Manupulation Zones, Support', inline =
'LQ', group = mnGR)
mnRZC = input(color.new(#ff9800, 73), 'Resistance', inline = 'LQ', group = mnGR)
sigGR = 'Signals'
srFBT = 'Filters the breakouts that failed to continue beyond a level'
srFBO = input.bool(true, 'Avoid False Breakouts', group = sigGR, tooltip = srFBT)
othGR = 'Others'
swSH = input.string('None', "Swing Levels", options=['Auto', 'Small', 'Normal',
'Large', 'None'], inline = 'sw', group = othGR)
swHC = input(color.new(#f23645, 33), 'H', inline = 'sw', group = othGR)
swLC = input(color.new(#089981, 33), 'L', inline = 'sw', group = othGR)
//-----------------------------------------------------------------------------}
// User Defined Types
//-----------------------------------------------------------------------------{
type bar
float o = open
float h = high
float l = low
float c = close
float v = volume
int i = bar_index
type pivotPoint
int x
int x1
float h
float h1
float l
float l1
bool hx
bool lx
type SnR
box bx
box lq
line ln
bool b
bool t
bool r
bool l
float m
//-----------------------------------------------------------------------------}
// Variables
//-----------------------------------------------------------------------------{
bar b = bar.new()
var SnR lR = SnR.new(box(na), box(na), line(na), false, false, false, false, na)
var SnR lS = SnR.new(box(na), box(na), line(na), false, false, false, false, na)
var SnR lRt = SnR.new(box(na), box(na), line(na), false, false, false, false, na)
var SnR lSt = SnR.new(box(na), box(na), line(na), false, false, false, false, na)
//-----------------------------------------------------------------------------}
// General Calculations
//-----------------------------------------------------------------------------{
ch_m = if timeframe.isintraday
timeframe.multiplier
else if timeframe.isdaily
1440
else if timeframe.isweekly
10080
else if timeframe.ismonthly
10080 * 30
atr = ta.atr(17)
isLLS = math.abs(b.l - math.min(b.o, b.c)) >= 1.618 * atr
isLUS = math.abs(b.h - math.max(b.o, b.c)) >= 1.618 * atr
//-----------------------------------------------------------------------------}
// Functions/Methods
//-----------------------------------------------------------------------------{
//-----------------------------------------------------------------------------}
// Calculations
//-----------------------------------------------------------------------------{
if not na(pp_h)
pp.h1 := pp.h
pp.h := pp_h
pp.x1 := pp.x
pp.x := b.i[srLN]
pp.hx := false
if R.size() > 1
lR := R.get(0)
lRt := R.get(1)
R.unshift(
SnR.new(
box.new(pp.x, pp.h, b.i, pp.h * (1 - ((pHST - pLST) /
pHST) * .17 * srMR), border_color = color(na), bgcolor = srRZC),
box.new(na, na, na, na, bgcolor = color(na), border_color
= color(na)),
line.new(pp.x, pp.h, b.i, pp.h, color = srRLC, width =
srMR <= .5 ? 2 : 3),
false, false, false, false, (pHST - pLST) / pHST))
lS.t := false
else
lRt.bx.set_right(b.i)
lRt.ln.set_x2(b.i)
lS.t := false
if swSH != 'None'
StS = pp.x - pp.x1
tradedVolume = f_getTradedVolume(StS, srLN)
swH = pp.h > pp.h1 ? "Higher High" : pp.h < pp.h1 ? "Lower High" : na
rTT = 'Swing High (' + swH + ') : ' + str.tostring(pp.h, format.mintick) +
(mss == -1 and pp.h < pp.h1 ? '\n *Counter-Trend Move' : '') +
'\n -Price Change : ↑ %' + str.tostring((pp.h - pp.l) * 100 / pp.l ,
'#.##') +
(nz(b.v) ? '\n -Traded Volume : ' + str.tostring(tradedVolume,
format.volume) + ' (' + str.tostring(StS - 1) + ' bars)' +
'\n *Average Volume/Bar : ' + str.tostring(tradedVolume / (StS -
1), format.volume) : '')
label.new(pp.x, pp.h, '◈', color = color(na), style =
label.style_label_down, textcolor = swHC, size = f_getSize(swSH), tooltip = rTT)
alert('New ' + swH + (mss == -1 and pp.h < pp.h1 ? ' (counter-trend
move)' : '') + ' formed\n' + syminfo.ticker + ' price (' + str.tostring(b.c,
format.mintick) + '), timeframe ' + timeframe.period)
if b.c[1] > pp.h and b.c > pp.h and not pp.hx
pp.hx := true
mss := 1
if not na(pp_l)
pp.l1 := pp.l
pp.l := pp_l
pp.x1 := pp.x
pp.x := b.i[srLN]
pp.lx := false
if S.size() > 2
lS := S.get(0)
lSt := S.get(1)
S.unshift(
SnR.new(
box.new(pp.x, pp.l * (1 + ((pHST - pLST) / pHST) * .17 *
srMR), b.i, pp.l, border_color = color(na), bgcolor = srSZC),
box.new(na, na, na, na, bgcolor = color(na), border_color
= color(na)),
line.new(pp.x, pp.l, b.i, pp.l, color = srSLC, width =
srMR <= .5 ? 2 : 3),
false, false, false, false, (pHST - pLST) / pHST))
lR.t := false
else
lSt.bx.set_right(b.i)
lSt.ln.set_x2(b.i)
lR.t := false
if swSH != 'None'
StS = pp.x - pp.x1
tradedVolume = f_getTradedVolume(StS, srLN)
swL = pp.l < pp.l1 ? "Lower Low" : pp.l > pp.l1 ? "Higher Low" : na
sTT = 'Swing Low (' + swL + ') : ' + str.tostring(pp.l, format.mintick) +
(mss == 1 and pp.l > pp.l1 ? '\n *Counter-Trend Move' : '') +
'\n -Price Change : ↓ %' + str.tostring((pp.h - pp.l) * 100 / pp.h ,
'#.##') +
(nz(b.v) ? '\n -Traded Volume : ' + str.tostring(tradedVolume,
format.volume) + ' (' + str.tostring(StS - 1) + ' bars)' +
'\n *Average Volume/Bar : ' + str.tostring(tradedVolume / (StS -
1), format.volume) : '')
label.new(pp.x, pp.l, '◈', color = color(na), style = label.style_label_up,
textcolor = swLC, size = f_getSize(swSH), tooltip = sTT)
alert('New ' + swL + (mss == 1 and pp.l > pp.l1 ? ' (counter-trend move)' :
'') + ' formed\n' + syminfo.ticker + ' price (' + str.tostring(b.c, format.mintick)
+ '), timeframe ' + timeframe.period)
if b.c[1] < pp.l and b.c < pp.l and not pp.lx
pp.lx := true
mss := -1
if R.size() > 0
lR := R.get(0)
if srFBO and b.c[1] > lR.bx.get_top() * (1 + lR.m * .17) and not lR.b
lR.bx.set_right(b.i[1])
lR.ln.set_x2(b.i[1])
lR.b := true
lR.r := false
S.unshift(
SnR.new(
box.new(b.i[1], lR.bx.get_top(), b.i + 1, lR.bx.get_bottom(),
border_color = color(na), bgcolor = srSZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lR.bx.get_bottom(), b.i + 1, lR.bx.get_bottom(),
color = srSLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lR.m))
//R.remove(0)
if srBS != 'None'
alert('Bullish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if b.c[1] > lR.bx.get_top() and not lR.b and not srFBO
lR.bx.set_right(b.i[1])
lR.ln.set_x2(b.i[1])
lR.b := true
lR.r := false
S.unshift(
SnR.new(
box.new(b.i[1], lR.bx.get_top(), b.i + 1, lR.bx.get_bottom(),
border_color = color(na), bgcolor = srSZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lR.bx.get_bottom(), b.i + 1, lR.bx.get_bottom(),
color = srSLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lR.m))
//R.remove(0)
if srBS != 'None'
alert('Bullish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if lS.b and b.o[1] < lR.bx.get_top() and b.h[1] > lR.bx.get_bottom() and
b.c[1] < lR.bx.get_bottom() and not lR.r and b.i[1] != lR.bx.get_left()
label.new(b.i[1], b.h[1] * (1 + lR.m * .017), 'R', color = srRDC, style =
label.style_label_down , textcolor = srRTC, size = f_getSize(srRS), tooltip = 'Re-
test of Resistance Zone' + vST[1] )
lR.r := true //
lR.bx.set_right(b.i)
lR.ln.set_x2(b.i)
if srRS != 'None'
alert('Re-test of resistance zone detected\n' + syminfo.ticker + '
price (' + str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if b.h[1] > lR.bx.get_bottom() and b.c[1] < lR.bx.get_top() and b.c <
lR.bx.get_top() and not lR.t and not lR.r and not lR.b and not lS.b and b.i[1] !=
lR.bx.get_left()
label.new(b.i[1], b.h[1] * (1 + lR.m * .017), 'T', color = srTDC, style =
label.style_label_down , textcolor = srTTC, size = f_getSize(srTS), tooltip = 'Test
of Resistance Zone' + vST[1] )
lR.t := true
lR.bx.set_right(b.i)
lR.ln.set_x2(b.i)
if srTS != 'None'
alert('Test of resistance zone detected\n' + syminfo.ticker + ' price
(' + str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period,
alert.freq_once_per_bar_close)
else if b.h > lR.bx.get_bottom() * (1 - lR.m * .17) and not lR.b //and
lR.bx.get_top() != lS.bx.get_top()
if b.h > lR.bx.get_bottom()
lR.bx.set_right(b.i)
lR.ln.set_x2(b.i)
if mnSH
if b.h > lR.bx.get_top() and b.c <= lR.bx.get_top() * (1 + lR.m * .17 *
mnMR) and not lR.l and b.i == lR.bx.get_right()
if lR.lq.get_right() + srLN > b.i
lR.lq.set_right(b.i + 1)
lR.lq.set_top(math.min(math.max(b.h, lR.lq.get_top()),
lR.bx.get_top() * (1 + lR.m * .17 * mnMR)))
else
lR.lq.set_lefttop(b.i[1], math.min(b.h, lR.bx.get_top() * (1 + lR.m
* .17 * mnMR)))
lR.lq.set_rightbottom(b.i + 1, lR.bx.get_top())
lR.lq.set_bgcolor(mnRZC)
lR.l := true
else if b.h > lR.bx.get_top() and b.c <= lR.bx.get_top() * (1 + lR.m * .17
* mnMR) and lR.l and b.i == lR.bx.get_right()
lR.lq.set_right(b.i + 1)
lR.lq.set_top(math.min(math.max(b.h,lR.lq.get_top()), lR.bx.get_top() *
(1 + lR.m * .17 * mnMR)))
else if lR.l and (b.c >= lR.bx.get_top() * (1 + lR.m * .17 * mnMR) or b.c <
lR.bx.get_bottom())
lR.l := false
if srFBO and b.c[1] > lRt.bx.get_top() * (1 + lRt.m * .17) and not lRt.b
lRt.bx.set_right(b.i[1])
lRt.ln.set_x2(b.i[1])
lRt.b := true
lRt.r := false
S.unshift(
SnR.new(
box.new(b.i[1], lRt.bx.get_top(), b.i + 1, lRt.bx.get_bottom(),
border_color = color(na), bgcolor = srSZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lRt.bx.get_bottom(), b.i + 1,
lRt.bx.get_bottom(), color = srSLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lRt.m))
//R.remove(1)
if srBS != 'None'
alert('Bullish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if b.c[1] > lRt.bx.get_top() and not lRt.b and not srFBO
lRt.bx.set_right(b.i[1])
lRt.ln.set_x2(b.i[1])
lRt.b := true
lRt.r := false
S.unshift(
SnR.new(
box.new(b.i[1], lRt.bx.get_top(), b.i + 1, lRt.bx.get_bottom(),
border_color = color(na), bgcolor = srSZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lRt.bx.get_bottom(), b.i + 1,
lRt.bx.get_bottom(), color = srSLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lRt.m))
//R.remove(1)
if srBS != 'None'
alert('Bullish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
if srRS != 'None'
alert('Re-test of resistance zone detected\n' + syminfo.ticker + '
price (' + str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if b.h[1] > lRt.bx.get_bottom() and b.c[1] < lRt.bx.get_top() and b.c
< lRt.bx.get_top() and not lRt.t and not lRt.b and not lSt.b and b.i[1] !=
lRt.bx.get_left()
label.new(b.i[1], b.h[1] * (1 + lRt.m * .017), 'T', color = srTDC,
style = label.style_label_down , textcolor = srTTC, size = f_getSize(srTS), tooltip
= 'Test of Resistance Zone' + vST[1] )
lRt.t := true
lRt.bx.set_right(b.i)
lRt.ln.set_x2(b.i)
if srTS != 'None'
alert('Test of resistance zone detected\n' + syminfo.ticker + '
price (' + str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period,
alert.freq_once_per_bar_close)
if mnSH
if b.h > lRt.bx.get_top() and b.c <= lRt.bx.get_top() * (1 + lRt.m
* .17 * mnMR) and not lRt.l and b.i == lRt.bx.get_right()
if lRt.lq.get_right() + srLN > b.i
lRt.lq.set_right(b.i + 1)
lRt.lq.set_top(math.min(math.max(b.h, lRt.lq.get_top()),
lRt.bx.get_top() * (1 + lRt.m * .17 * mnMR)))
else
lRt.lq.set_lefttop(b.i[1], math.min(b.h, lRt.bx.get_top() * (1
+ lRt.m * .17 * mnMR)))
lRt.lq.set_rightbottom(b.i + 1, lRt.bx.get_top())
lRt.lq.set_bgcolor(mnRZC)
lRt.l := true
if S.size() > 1
lS := S.get(0)
if srFBO and b.c[1] < lS.bx.get_bottom() * (1 - lS.m * .17) and not lS.b
lS.bx.set_right(b.i[1])
lS.ln.set_x2(b.i[1])
lS.b := true
lS.r := false
label.new(b.i[1], b.h[1] * (1 + lS.m * .017), 'B\n\n▼', color = srBDC,
style = label.style_label_down , textcolor = srBTC, size = f_getSize(srBS), tooltip
= 'Bearish Breakout' + vST[1] )
//label.new(b.i[1], b.h[1] * (1 + lS.m * .017), 'B\n\n▼', color =
color.yellow, style = label.style_label_down , textcolor = srBTC, size =
f_getSize(srBS), tooltip = 'Bearish Breakout' + vST[1] )
R.unshift(
SnR.new(
box.new(b.i[1], lS.bx.get_top(), b.i + 1, lS.bx.get_bottom(),
border_color = color(na), bgcolor = srRZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lS.bx.get_top(), b.i + 1, lS.bx.get_top(), color =
srRLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lS.m))
//S.remove(0)
if srBS != 'None'
alert('Bearish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
R.unshift(
SnR.new(
box.new(b.i[1], lS.bx.get_top(), b.i + 1, lS.bx.get_bottom(),
border_color = color(na), bgcolor = srRZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lS.bx.get_top(), b.i + 1, lS.bx.get_top(), color =
srRLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lS.m))
//S.remove(0)
if srBS != 'None'
alert('Bearish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if lR.b and b.o[1] > lS.bx.get_bottom() and b.l[1] < lS.bx.get_top() and
b.c[1] > lS.bx.get_top() and not lS.r and b.i[1] != lS.bx.get_left()
label.new(b.i[1], b.l[1] * (1 - lS.m * .017), 'R', color = srRUC, style =
label.style_label_up , textcolor = srRTC, size = f_getSize(srRS), tooltip = 'Re-
test of Support Zone' + vST[1] )
lS.r := true //
lS.bx.set_right(b.i)
lS.ln.set_x2(b.i)
if srRS != 'None'
alert('Re-test of support zone detected\n' + syminfo.ticker + ' price
(' + str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if b.l[1] < lS.bx.get_top() and b.c[1] > lS.bx.get_bottom() and b.c >
lS.bx.get_bottom() and not lS.t and not lS.b and not lR.b and b.i[1] !=
lS.bx.get_left()
label.new(b.i[1], b.l[1] * (1 - lS.m * .017), 'T', color = srTUC, style =
label.style_label_up , textcolor = srTTC, size = f_getSize(srTS), tooltip = 'Test
of Support Zone' + vST[1] )
lS.t := true
lS.bx.set_right(b.i)
lS.ln.set_x2(b.i)
if srTS != 'None'
alert('Test of support zone detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period,
alert.freq_once_per_bar_close)
else if b.l < lS.bx.get_top() * (1 + lS.m * .17) and not lS.b //and
lS.bx.get_bottom() != lR.bx.get_bottom()
if b.l < lS.bx.get_top()
lS.bx.set_right(b.i)
lS.ln.set_x2(b.i)
if mnSH
if b.l < lS.bx.get_bottom() and b.c >= lS.bx.get_bottom() * (1 - lS.m * .17
* mnMR) and not lS.l and b.i == lS.bx.get_right()
if lS.lq.get_right() + srLN > b.i
lS.lq.set_right(b.i + 1)
lS.lq.set_bottom(math.max(math.min(b.l, lS.lq.get_bottom()),
lS.bx.get_bottom() * (1 - lS.m * .17 * mnMR)))
else
lS.lq.set_lefttop(b.i[1], lS.bx.get_bottom())
lS.lq.set_rightbottom(b.i + 1, math.max(b.l, lS.bx.get_bottom() *
(1 - lS.m * .17 * mnMR)))
lS.lq.set_bgcolor(mnSZC)
lS.l := true
if lS.bx.get_bottom() != lSt.bx.get_bottom()
if srFBO and b.c[1] < lSt.bx.get_bottom() * (1 - lSt.m * .17) and not
lSt.b //and b.i[1] != lR.bx.get_left()
lSt.bx.set_right(b.i[1])
lSt.ln.set_x2(b.i[1])
lSt.b := true
lSt.r := false
R.unshift(
SnR.new(
box.new(b.i[1], lSt.bx.get_top(), b.i + 1, lSt.bx.get_bottom(),
border_color = color(na), bgcolor = srRZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lSt.bx.get_top(), b.i + 1, lSt.bx.get_top(),
color = srRLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lSt.m))
//S.remove(1)
if srBS != 'None'
alert('Bearish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if b.c[1] < lSt.bx.get_bottom() and not lSt.b and not srFBO //and
b.i[1] != lR.bx.get_left()
lSt.bx.set_right(b.i[1])
lSt.ln.set_x2(b.i[1])
lSt.b := true
lSt.r := false
R.unshift(
SnR.new(
box.new(b.i[1], lSt.bx.get_top(), b.i + 1, lSt.bx.get_bottom(),
border_color = color(na), bgcolor = srRZC),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
line.new(b.i[1], lSt.bx.get_top(), b.i + 1, lSt.bx.get_top(),
color = srRLC, width = srMR <= .5 ? 2 : 3),
false, false, false, false, lSt.m))
//S.remove(1)
if srBS != 'None'
alert('Bearish breakout detected\n' + syminfo.ticker + ' price (' +
str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
if srRS != 'None'
alert('Re-test of support zone detected\n' + syminfo.ticker + '
price (' + str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period)
else if b.l[1] < lSt.bx.get_top() and b.c[1] > lSt.bx.get_bottom() and b.c
> lSt.bx.get_bottom() and not lSt.t and not lSt.b and not lRt.b and b.i[1] !=
lSt.bx.get_left()
label.new(b.i[1], b.l[1] * (1 - lSt.m * .017), 'T', color = srTUC,
style = label.style_label_up , textcolor = srTTC, size = f_getSize(srTS), tooltip =
'Test of Support Zone' + vST[1] )
lSt.t := true
lSt.bx.set_right(b.i)
lSt.ln.set_x2(b.i)
if srTS != 'None'
alert('Test of support zone detected\n' + syminfo.ticker + ' price
(' + str.tostring(b.c, format.mintick) + '), timeframe ' + timeframe.period,
alert.freq_once_per_bar_close)
if mnSH
if b.l < lSt.bx.get_bottom() and b.c >= lSt.bx.get_bottom() * (1 -
lSt.m * .17 * mnMR) and not lSt.l and b.i == lSt.bx.get_right()
if lSt.lq.get_right() + srLN > b.i
lSt.lq.set_right(b.i + 1)
lSt.lq.set_bottom(math.max(math.min(b.l, lSt.lq.get_bottom()),
lSt.bx.get_bottom() * (1 - lSt.m * .17 * mnMR)))
else
lSt.lq.set_lefttop(b.i[1], lSt.bx.get_bottom())
lSt.lq.set_rightbottom(b.i + 1, math.max(b.l,
lSt.bx.get_bottom() * (1 - lSt.m * .17 * mnMR)))
lSt.lq.set_bgcolor(mnSZC)
lSt.l := true
//-----------------------------------------------------------------------------}
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0
International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5
// indicator("Trend Lines [LuxAlgo]", shorttitle= "LuxAlgo - Trend
Lines", max_lines_count = 500, max_labels_count = 500, overlay = true)
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
NN = "Disabled"
AB = "Point A - Point B"
AC = "Point A - Current bar"
length = input.int ( 50 , minval =
2 , group= "Swings" )
toggle = input.string( NN , 'Check breaks between:' , options=
[ AB, AC, NN ] , group= "Trendline validation" )
source = input.string( "close" , 'source (breaks)' , options=
["close", "H/L"] , group= "Trendline validation" )
count = input.int ( 3 , 'Minimal bars' , minval =
0 , group= "Trendline breaks"
, tooltip=
'Uninterrupted Trendline for at least x bars')
showA = input.bool ( true , 'show Angles'
, group= "Angles" )
ratio = input.float ( 3 , 'Ratio X-Y axis' , step =0.1
, group= "Angles" )
anglA = input.float ( 0.1 ,'Only Trendlines between:', minval =0.1,
inline= 'angle', group= "Angles" )
anglB = input.float ( 90 , ' - ' , minval =0.1,
inline= 'angle', group= "Angles" )
upCss = input.color (#2962ff, 'Up' ,
group= "Colours" )
dnCss = input.color (#f23645, 'Down' ,
group= "Colours" )
//-----------------------------------------------------------------------------}
//Variables
//-----------------------------------------------------------------------------{
//Downtrendline
var int phx1 = na
var float phslope = na
var float phy1 = na
var float upper = na
var float plotH = na
var bool isOnH = false
//Uptrendline
var int plx1 = na
var float plslope = na
var float ply1 = na
var float lower = na
var float plotL = na
var bool isOnL = false
//-----------------------------------------------------------------------------}
//Calculations
//-----------------------------------------------------------------------------{
n = bar_index
bg = chart.bg_color
fg = chart.fg_color
ph = ta.pivothigh (length, length)
pl = ta.pivotlow (length, length)
bars = 500 , height = bars / ratio
Xaxis = math.min(math.max(1, n), bars)
Yaxis = ta.highest(Xaxis) - ta.lowest(Xaxis)
srcBl = source == "close" ? close : high
srcBr = source == "close" ? close : low
//-----------------------------------------------------------------------------}
//Function
//-----------------------------------------------------------------------------{
calculate_slope(x1, x2, y1, y2) =>
diffX = x2 - x1, diffY = y2 - y1
diffY_to_Yaxis = Yaxis / diffY
normalised_slope = (height / diffY_to_Yaxis) / diffX
slope = diffY / diffX
angle = math.round(math.atan(normalised_slope) * 180 / math.pi, 2)
[normalised_slope, slope, angle]
//-----------------------------------------------------------------------------}
//Execution
//-----------------------------------------------------------------------------{
if not na(ph)
if ph < phy1
[normalised_slope, slope, angle]= calculate_slope(phx1, n-length, phy1, ph)
testLine.set_xy1(phx1, phy1), testLine.set_xy2(n, ph + slope * length)
src = source == "close" ? close : high, max_bars_back(src, 2000)
isOnH := false
broken = false
if math.abs(angle) > anglA and math.abs(angle) < anglB
if toggle != NN
for i = (toggle == AB ? length : 0) to n - phx1
if src[i] > testLine.get_price(n - i)
broken := true
break
if not broken
phslope := slope, isOnH := true, upper := ph + slope * length
line.new(phx1, phy1, n, ph + slope * length
, color= dnCss, style= line.style_dotted)
if showA
label.new(phx1, phy1, text= str.tostring(angle)
, style = label.style_label_down
, color = color.new(bg, 100)
, textcolor = dnCss)
phy1 := ph
phx1 := n-length
upper += phslope
plotH := not na(ph) and ta.change(phslope) ? na : srcBl[1] > upper[1] ? na : upper
bs_H = ta.barssince (na(plotH ))
if not na(pl)
if pl > ply1
[normalised_slope, slope, angle]= calculate_slope(plx1, n-length, ply1, pl)
testLine.set_xy1(plx1, ply1), testLine.set_xy2(n, pl + slope * length)
src = source == "close" ? close : low , max_bars_back(src, 2000)
isOnL := false
broken = false
if angle > anglA and angle < anglB
if toggle != NN
for i = (toggle == AB ? length : 0) to n - plx1
if src[i] < testLine.get_price(n - i)
broken := true
break
if not broken
plslope := slope, isOnL := true, lower := pl + slope * length
line.new(plx1, ply1, n, pl + slope * length
, color= upCss, style= line.style_dotted)
if showA
label.new(plx1, ply1, text= str.tostring(angle)
, style = label.style_label_up
, color = color.new(bg, 100)
, textcolor = upCss)
ply1 := pl
plx1 := n-length
lower += plslope
plotL := not na(pl) and ta.change(plslope) ? na : srcBr[1] < lower[1] ? na : lower
bs_L = ta.barssince (na(plotL ))
//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
plot(plotH, 'Down Trendline'
, dnCss
, 1
, plot.style_linebr)
plotshape(
bs_H > count and srcBl > upper and srcBl[1] <= upper[1]
, 'Bullish break'
, shape.labelup
, location.belowbar
, dnCss
, size = size.tiny)
plotshape(
bs_L > count and srcBr < lower and srcBr[1] >= lower[1]
, 'Bearish break'
, shape.labeldown
, location.abovebar
, upCss
, size = size.tiny)
//-----------------------------------------------------------------------------}
//END SETTINGS
//FUNCTIONS
else if swing_type == -1
if array.get(array, 0) >= array.get(array, 1)
label_text := 'HL'
else
label_text := 'LL'
label.new(
bar_index - swing_length,
array.get(array,0),
text = label_text,
style = label.style_label_up,
textcolor = swing_type_color,
color = swing_type_color,
size = size.tiny)
atr_threshold = atrValue * 2
okay_to_draw = true
for i = 0 to array.size(box_array) - 1
top = box.get_top(array.get(box_array, i))
bottom = box.get_bottom(array.get(box_array, i))
poi = (top + bottom) / 2
if zone_type == 1
for i = 0 to array.size(box_array) - 1
level_to_break = box.get_top(array.get(box_array,i))
// if ta.crossover(close, level_to_break)
if close >= level_to_break
copied_box = box.copy(array.get(box_array,i))
f_array_add_pop(bos_array, copied_box)
mid = (box.get_top(array.get(box_array,i)) +
box.get_bottom(array.get(box_array,i))) / 2
box.set_top(array.get(bos_array,0), mid)
box.set_bottom(array.get(bos_array,0), mid)
box.set_extend( array.get(bos_array,0), extend.none)
box.set_right( array.get(bos_array,0), bar_index)
box.set_text( array.get(bos_array,0), 'BOS' )
box.set_text_color( array.get(bos_array,0), bos_label_color)
box.set_text_size( array.get(bos_array,0), size.small)
box.set_text_halign( array.get(bos_array,0), text.align_center)
box.set_text_valign( array.get(bos_array,0), text.align_center)
box.delete(array.get(box_array, i))
box.delete(array.get(label_array, i))
if zone_type == -1
for i = 0 to array.size(box_array) - 1
level_to_break = box.get_bottom(array.get(box_array,i))
// if ta.crossunder(close, level_to_break)
if close <= level_to_break
copied_box = box.copy(array.get(box_array,i))
f_array_add_pop(bos_array, copied_box)
mid = (box.get_top(array.get(box_array,i)) +
box.get_bottom(array.get(box_array,i))) / 2
box.set_top(array.get(bos_array,0), mid)
box.set_bottom(array.get(bos_array,0), mid)
box.set_extend( array.get(bos_array,0), extend.none)
box.set_right( array.get(bos_array,0), bar_index)
box.set_text( array.get(bos_array,0), 'BOS' )
box.set_text_color( array.get(bos_array,0), bos_label_color)
box.set_text_size( array.get(bos_array,0), size.small)
box.set_text_halign( array.get(bos_array,0), text.align_center)
box.set_text_valign( array.get(bos_array,0), text.align_center)
box.delete(array.get(box_array, i))
box.delete(array.get(label_array, i))
for i = 0 to array.size(box_array) - 1
box.set_right(array.get(box_array, i), bar_index + 100)
//
//END FUNCTIONS
//
//
//CALCULATIONS
//
stratRes = timeframe.ismonthly ? str.tostring(timeframe.multiplier * intRes,
'###M') :
timeframe.isweekly ? str.tostring(timeframe.multiplier * intRes,
'###W') :
timeframe.isdaily ? str.tostring(timeframe.multiplier * intRes,
'###D') :
timeframe.isintraday ? str.tostring(timeframe.multiplier * intRes,
'####') :
'60'
src = h ? request.security(ticker.heikinashi(syminfo.tickerid),
timeframe.period, close, lookahead = barmerge.lookahead_off) : close
// CALCULATE ATR
atrValue = ta.atr(50)
f_extend_box_endpoint(current_supply_box)
f_extend_box_endpoint(current_demand_box)
// if barstate.islast
// label.new(x = bar_index + 10, y = close[1], text =
str.tostring( array.size(current_supply_poi) ))
// label.new(x = bar_index + 20, y = close[1], text =
str.tostring( box.get_bottom( array.get(current_supply_box, 0))))
// label.new(x = bar_index + 30, y = close[1], text =
str.tostring( box.get_bottom( array.get(current_supply_box, 1))))
// label.new(x = bar_index + 40, y = close[1], text =
str.tostring( box.get_bottom( array.get(current_supply_box, 2))))
// label.new(x = bar_index + 50, y = close[1], text =
str.tostring( box.get_bottom( array.get(current_supply_box, 3))))
// label.new(x = bar_index + 60, y = close[1], text =
str.tostring( box.get_bottom( array.get(current_supply_box, 4))))
// // Functions
// supertrend(_src, factor, atrLen) =>
// atr = ta.atr(atrLen)
// upperBand = _src + factor * atr
// lowerBand = _src - factor * atr
// prevLowerBand = nz(lowerBand[1])
// prevUpperBand = nz(upperBand[1])
// lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ?
lowerBand : prevLowerBand
// upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ?
upperBand : prevUpperBand
// int direction = na
// float superTrend = na
// prevSuperTrend = superTrend[1]
// if na(atr[1])
// direction := 1
// else if prevSuperTrend == prevUpperBand
// direction := close > upperBand ? -1 : 1
// else
// direction := close < lowerBand ? 1 : -1
// superTrend := direction == -1 ? lowerBand : upperBand
// [superTrend, direction]
lr_slope(_src, _len) =>
x = 0.0, y = 0.0, x2 = 0.0, xy = 0.0
for i = 0 to _len - 1
val = _src[i]
per = i + 1
x += per
y += val
x2 += per * per
xy += val * per
_slp = (_len * xy - x * y) / (_len * x2 - x * x)
_avg = y / _len
_int = _avg - _slp * x / _len + _slp
[_slp, _avg, _int]
lr_dev(_src, _len, _slp, _avg, _int) =>
upDev = 0.0, dnDev = 0.0
val = _int
for j = 0 to _len - 1
price = high[j] - val
if price > upDev
upDev := price
price := val - low[j]
if price > dnDev
dnDev := price
price := _src[j]
val += _slp
[upDev, dnDev]
// // Get Components
// ocAvg = math.avg(open, close)
// sma1 = ta.sma(close, 5)
// sma2 = ta.sma(close, 6)
// sma3 = ta.sma(close, 7)
// sma4 = ta.sma(close, 8)
// sma5 = ta.sma(close, 9)
// sma6 = ta.sma(close, 10)
// sma7 = ta.sma(close, 11)
// sma8 = ta.sma(close, 12)
// sma9 = ta.sma(close, 13)
// sma10 = ta.sma(close, 14)
// sma11 = ta.sma(close, 15)
// sma12 = ta.sma(close, 16)
// sma13 = ta.sma(close, 17)
// sma14 = ta.sma(close, 18)
// sma15 = ta.sma(close, 19)
// sma16 = ta.sma(close, 20)
// psar = ta.sar(0.02, 0.02, 0.2)
[_, upperKC1, lowerKC1] = ta.kc(close, 80, 10.5)
[_, upperKC2, lowerKC2] = ta.kc(close, 80, 9.5)
[_, upperKC3, lowerKC3] = ta.kc(close, 80, 8)
[_, upperKC4, lowerKC4] = ta.kc(close, 80, 3)
barsL = 10
barsR = 10
pivotHigh = fixnan(ta.pivothigh(barsL, barsR)[1])
pivotLow = fixnan(ta.pivotlow(barsL, barsR)[1])
cource1 = close, period = 150
[s, a, i] = lr_slope(cource1, period)
[upDev, dnDev] = lr_dev(cource1, period, s, a, i)
// // Colors
// green = #00d9ff, green2 = #00d9ff
// red = #ff0090, red2 = #ff0090
// // Plots
// k1 = plot(ta.ema(upperKC1, 50), "k1", na, editable = false)
// k2 = plot(ta.ema(upperKC2, 50), "k2", na, editable = false)
// k3 = plot(ta.ema(upperKC3, 50), "k3", na, editable = false)
// k4 = plot(ta.ema(upperKC4, 50), "k4", na, editable = false)
// k5 = plot(ta.ema(lowerKC4, 50), "k5", na, editable = false)
// k6 = plot(ta.ema(lowerKC3, 50), "k6", na, editable = false)
// k7 = plot(ta.ema(lowerKC2, 50), "k7", na, editable = false)
// k8 = plot(ta.ema(lowerKC1, 50), "k8", na, editable = false)
// fill(k1, k2, channelBal ? color.new(red2, 40) : na, editable = false)
// fill(k2, k3, channelBal ? color.new(red2, 65) : na, editable = false)
// fill(k3, k4, channelBal ? color.new(red2, 90) : na, editable = false)
// fill(k5, k6, channelBal ? color.new(green2, 90) : na, editable = false)
// fill(k6, k7, channelBal ? color.new(green2, 65) : na, editable = false)
// fill(k7, k8, channelBal ? color.new(green2, 40) : na, editable = false)
float ob = na
array.unshift(ob_top, top)
array.unshift(ob_btm, btm)
array.unshift(ob_avg, avg)
ob := ob_val
array.remove(ob_top, idx)
array.remove(ob_btm, idx)
array.remove(ob_avg, idx)
array.remove(ob_left, idx)
mitigated
//Global elements
var os = 0
var target_bull = 0.
var target_bear = 0.
// // Constants colours that include fully non-transparent option.
// green100 = #008000FF
// lime100 = #00FF00FF
// red100 = #FF0000FF
// blue100 = #0000FFFF
// aqua100 = #00FFFFFF
// darkred100 = #8B0000FF
// gray100 = #808080FF
/////////////////////////////////////////////
// Create non-repainting security function
rp_security(_symbol, _res, _src) =>
request.security(_symbol, _res, _src[barstate.isrealtime ? 1 : 0])
// Main indicator1
// Functions
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r
: x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100
securityNoRep(sym, res, src) => request.security(sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on)
swingPoints(prd) =>
pivHi = ta.pivothigh(prd, prd)
pivLo = ta.pivotlow (prd, prd)
last_pivHi = ta.valuewhen(pivHi, pivHi, 1)
last_pivLo = ta.valuewhen(pivLo, pivLo, 1)
hh = pivHi and pivHi > last_pivHi ? pivHi : na
lh = pivHi and pivHi < last_pivHi ? pivHi : na
hl = pivLo and pivLo > last_pivLo ? pivLo : na
ll = pivLo and pivLo < last_pivLo ? pivLo : na
[hh, lh, hl, ll]
f_chartTfInMinutes() =>
float _resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
f_kc(src, len, sensitivity) =>
basis = ta.sma(src, len)
span = ta.atr(len)
[basis + span * sensitivity, basis - span * sensitivity]
wavetrend(src, chlLen, avgLen) =>
esa = ta.ema(src, chlLen)
d = ta.ema(math.abs(src - esa), chlLen)
ci = (src - esa) / (0.015 * d)
wt1 = ta.ema(ci, avgLen)
wt2 = ta.sma(wt1, 3)
[wt1, wt2]
f_top_fractal(_src) => _src[4] < _src[2] and _src[3] < _src[2] and _src[2] >
_src[1] and _src[2] > _src[0]
f_bot_fractal(_src) => _src[4] > _src[2] and _src[3] > _src[2] and _src[2] <
_src[1] and _src[2] < _src[0]
top_fractal = f_top_fractal(src)
bot_fractal = f_bot_fractal(src)
f_fractalize (_src) => top_fractal ? 1 : bot_fractal ? -1 : 0
f_findDivs(src, topLimit, botLimit) =>
fractalTop = f_fractalize(src) > 0 and src[2] >= topLimit ? src[2] : na
fractalBot = f_fractalize(src) < 0 and src[2] <= botLimit ? src[2] : na
highPrev = ta.valuewhen(fractalTop, src[2], 0)[2]
highPrice = ta.valuewhen(fractalTop, high[2], 0)[2]
lowPrev = ta.valuewhen(fractalBot, src[2], 0)[2]
lowPrice = ta.valuewhen(fractalBot, low[2], 0)[2]
bearSignal = fractalTop and high[1] > highPrice and src[1] < highPrev
bullSignal = fractalBot and low[1] < lowPrice and src[1] > lowPrev
[bearSignal, bullSignal]
// Get user input
enableSR = input(false , "SR On/Off", group="SR")
colorSup = input(#00000000 , "Support Color", group="SR")
colorRes = input(#00000000 , "Resistance Color", group="SR")
strengthSR = input.int(2 , "S/R Strength", 1, group="SR")
lineStyle = input.string("Dotted", "Line Style", ["Solid", "Dotted", "Dashed"],
group="SR")
lineWidth = input.int(2 , "S/R Line Width", 1, group="SR")
useZones = input(true , "Zones On/Off", group="SR")
useHLZones = input(true , "High Low Zones On/Off", group="SR")
zoneWidth = input.int(2 , "Zone Width %", 0,
tooltip = "it's calculated using % of the distance between highest/lowest in
last 300 bars", group="SR")
expandSR = input(true , "Expand SR")
// Get components
rb = 10
prd = 284
ChannelW = 10
label_loc = 55
style = lineStyle == "Solid" ? line.style_solid :
lineStyle == "Dotted" ? line.style_dotted : line.style_dashed
ph1 = ta.pivothigh(rb, rb)
pl1 = ta.pivotlow (rb, rb)
sr_levels = array.new_float(21, na)
prdhighest = ta.highest(prd)
prdlowest = ta.lowest(prd)
cwidth = percWidth(prd, ChannelW)
zonePerc = percWidth(300, zoneWidth)
aas = array.new_bool(41, true)
u1 = 0.0, u1 := nz(u1[1])
d1 = 0.0, d1 := nz(d1[1])
highestph = 0.0, highestph := highestph[1]
lowestpl = 0.0, lowestpl := lowestpl[1]
// Get components
rsi = ta.rsi(close, 28)
//rsiOb = rsi > 78 and rsi > ta.ema(rsi, 10)
//rsiOs = rsi < 27 and rsi < ta.ema(rsi, 10)
rsiOb = rsi > 65 and rsi > ta.ema(rsi, 10)
rsiOs = rsi < 35 and rsi < ta.ema(rsi, 10)
dHigh = securityNoRep(syminfo.tickerid, "D", high [1])
dLow = securityNoRep(syminfo.tickerid, "D", low [1])
dClose = securityNoRep(syminfo.tickerid, "D", close[1])
ema = ta.ema(close, 144)
emaBull = close > ema
equal_tf(res) => str.tonumber(res) == f_chartTfInMinutes() and not
timeframe.isseconds
higher_tf(res) => str.tonumber(res) > f_chartTfInMinutes() or timeframe.isseconds
too_small_tf(res) => (timeframe.isweekly and res=="1") or (timeframe.ismonthly and
str.tonumber(res) < 10)
securityNoRep1(sym, res, src) =>
bool bull_ = na
bull_ := equal_tf(res) ? src : bull_
bull_ := higher_tf(res) ? request.security(sym, res, src, barmerge.gaps_off,
barmerge.lookahead_on) : bull_
bull_array = request.security_lower_tf(syminfo.tickerid, higher_tf(res) ?
str.tostring(f_chartTfInMinutes()) + (timeframe.isseconds ? "S" : "") :
too_small_tf(res) ? (timeframe.isweekly ? "3" : "10") : res, src)
if array.size(bull_array) > 1 and not equal_tf(res) and not higher_tf(res)
bull_ := array.pop(bull_array)
array.clear(bull_array)
bull_
// TF1Bull = securityNoRep1(syminfo.tickerid, "1" , emaBull)
// TF3Bull = securityNoRep1(syminfo.tickerid, "3" , emaBull)
// TF5Bull = securityNoRep1(syminfo.tickerid, "5" , emaBull)
// TF15Bull = securityNoRep1(syminfo.tickerid, "15" , emaBull)
// TF30Bull = securityNoRep1(syminfo.tickerid, "30" , emaBull)
// TF60Bull = securityNoRep1(syminfo.tickerid, "60" , emaBull)
// TF120Bull = securityNoRep1(syminfo.tickerid, "120" , emaBull)
// TF240Bull = securityNoRep1(syminfo.tickerid, "240" , emaBull)
// TF480Bull = securityNoRep1(syminfo.tickerid, "480" , emaBull)
// TFDBull = securityNoRep1(syminfo.tickerid, "1440", emaBull)
// [wt1, wt2] = wavetrend(close, 5, 10)
// [wtDivBear1, wtDivBull1] = f_findDivs(wt2, 15, -40)
// [wtDivBear2, wtDivBull2] = f_findDivs(wt2, 45, -65)
// wtDivBull = wtDivBull1 or wtDivBull2
// wtDivBear = wtDivBear1 or wtDivBear2
////////////////////////////////////////////////////////
// === BASE FUNCTIONS ===
// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len, offSig, offALMA) =>
v1 = ta.sma(src, len) // Simple
v2 = ta.ema(src, len) // Exponential
v3 = 2 * v2 - ta.ema(v2, len) // Double Exponential
v4 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len) // Triple
Exponential
v5 = ta.wma(src, len) // Weighted
v6 = ta.vwma(src, len) // Volume Weighted
v7 = 0.0
sma_1 = ta.sma(src, len) // Smoothed
v7 := na(v7[1]) ? sma_1 : (v7[1] * (len - 1) + src) / len
v8 = ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len),
math.round(math.sqrt(len))) // Hull
v9 = ta.linreg(src, len, offSig) // Least Squares
v10 = ta.alma(src, len, offALMA, offSig) // Arnaud Legoux
v11 = ta.sma(v1, len) // Triangular (extreme smooth)
// SuperSmoother filter
// 2013 John F. Ehlers
a1 = math.exp(-1.414 * 3.14159 / len)
b1 = 2 * a1 * math.cos(1.414 * 3.14159 / len)
c2 = b1
c3 = -a1 * a1
c1 = 1 - c2 - c3
v12 = 0.0
v12 := c1 * (src + nz(src[1])) / 2 + c2 * nz(v12[1]) + c3 * nz(v12[2])
type == 'EMA' ? v2 : type == 'DEMA' ? v3 : type == 'TEMA' ? v4 : type ==
'WMA' ? v5 : type == 'VWMA' ? v6 : type == 'SMMA' ? v7 : type == 'HullMA' ? v8 :
type == 'LSMA' ? v9 : type == 'ALMA' ? v10 : type == 'TMA' ? v11 : type == 'SSMA' ?
v12 : v1
// plotcandle(
// open, high, low, close,
// title = 'plotcandle',
// color = close > open ? color.rgb(120, 9, 139) : color.rgb(69, 155, 225),
// wickcolor = close > open ? color.rgb(120, 9, 139) : color.rgb(69, 155, 225))
// // === strategy1 ===
// // stop loss
// slPoints = input.int(defval=0, title = 'Initial Stop Loss Points (zero to
disable)', minval=0)
// tpPoints = input.int(defval=0, title = 'Initial Target Profit Points (zero for
disable)', minval=0)
// // Include bar limiting algorithm
// ebar = input.int(defval=4000, title = 'Number of Bars for Back Testing',
minval=0)
// dummy = input (false, title = '- SET to ZERO for Daily or Longer
Timeframes')
// //
// // Calculate how many mars since last bar
// tdays = (timenow - time) / 60000.0 // number of minutes since last bar
// tdays := timeframe.ismonthly ? tdays / 1440.0 / 5.0 / 4.3 /
timeframe.multiplier :
// timeframe.isweekly ? tdays / 1440.0 / 5.0 / timeframe.multiplier
:
// timeframe.isdaily ? tdays / 1440.0 / timeframe.multiplier
:
// tdays / timeframe.multiplier // number of bars since last bar
// //
// //set up exit parameters
// TP = tpPoints > 0 ? tpPoints : na
// SL = slPoints > 0 ? slPoints : na
// if sell
// //strategy1.close("Long" , alert_message = i_alert_txt_exit_long)
// strategy1.entry("Short" , strategy1.short, alert_message =
i_alert_txt_entry_short)
// alert(message = "SHort position")
//#endregion }
// ——————————— <↑↑↑ G_SCRIPT01 ↑↑↑>
//<triggers>
lxTrigger = false
sxTrigger = false
leTrigger = ta.crossover (closeSeriesAlt, openSeriesAlt)
seTrigger = ta.crossunder(closeSeriesAlt, openSeriesAlt)
// ——————————— <constant_declarations>
//Tooltip
T_LVL = '(%) Exit Level'
T_QTY = '(%) Adjust trade exit volume'
T_MSG = 'Paste JSON message for your bot'
//Webhook Message
O_LEMSG = 'Long Entry'
O_LXMSGSL = 'Long SL'
O_LXMSGTP1 = 'Long TP1'
O_LXMSGTP2 = 'Long TP2'
O_LXMSGTP3 = 'Long TP3'
O_LXMSG = 'Long Exit'
O_SEMSG = 'Short Entry'
O_SXMSGSL = 'Short SL'
O_SXMSGA = 'Short TP1'
O_SXMSGB = 'Short TP2'
O_SXMSGC = 'Short TP3'
O_SXMSGX = 'Short Exit'
// ——————————— <input> | | |
Line length guide |
i_lxLvlTP1 = input.float (0.2, 'Level TP1' ,
group = G_RISK,
tooltip = T_LVL)
i_lxQtyTP1 = input.float (80.0, 'Qty TP1' ,
group = G_RISK,
tooltip = T_QTY)
i_lxLvlTP2 = input.float (0.5, 'Level TP2' , group
= G_RISK,
tooltip = T_LVL)
i_lxQtyTP2 = input.float (10.0, 'Qty TP2' ,
group = G_RISK,
tooltip = T_QTY)
i_lxLvlTP3 = input.float (7.0, 'Level TP3' ,
group = G_RISK,
tooltip = T_LVL)
i_lxQtyTP3 = input.float (2, 'Qty TP3' , group
= G_RISK,
tooltip = T_QTY)
i_lxLvlSL = input.float (0.5, 'Stop Loss' , group
= G_RISK,
tooltip = T_LVL)
i_sxLvlTP1 = i_lxLvlTP1
i_sxQtyTP1 = i_lxQtyTP1
i_sxLvlTP2 = i_lxLvlTP2
i_sxQtyTP2 = i_lxQtyTP2
i_sxLvlTP3 = i_lxLvlTP3
i_sxQtyTP3 = i_lxQtyTP3
i_sxLvlSL = i_lxLvlSL
G_DISPLAY = 'Display'
//<display>
i_alertOn = input.bool (true, 'Alert Labels On/Off' , group
= G_DISPLAY)
i_barColOn = input.bool (true, 'Bar Color On/Off' , group
= G_DISPLAY)
// ——————————— <function_declarations>
// @function Calculate the Take Profit line, and the crossover or crossunder
f_tp(_condition, _conditionValue, _leTrigger, _seTrigger, _src, _lxLvlTP,
_sxLvlTP)=>
var float _tpLine = 0.0
_topLvl = _src + (_src * (_lxLvlTP / 100))
_botLvl = _src - (_src * (_sxLvlTP / 100))
_tpLine := _condition[1] != _conditionValue and _leTrigger ? _topLvl :
_condition[1] != -_conditionValue and _seTrigger ? _botLvl :
nz(_tpLine[1])
[_tpLine]
// ——————————— <calculations>
//<set initial values>
var float condition = 0.0
var float slLine = 0.0
var float entryLine = 0.0
switch
leTrigger and condition[1] <= 0.0 => condition := 1.0
seTrigger and condition[1] >= 0.0 => condition := -1.0
tp3Long and condition[1] == 1.2 => condition := 1.3
tp3Short and condition[1] == -1.2 => condition := -1.3
tp2Long and condition[1] == 1.1 => condition := 1.2
tp2Short and condition[1] == -1.1 => condition := -1.2
tp1Long and condition[1] == 1.0 => condition := 1.1
tp1Short and condition[1] == -1.0 => condition := -1.1
slLong and condition[1] >= 1.0 => condition := 0.0
slShort and condition[1] <= -1.0 => condition := 0.0
lxTrigger and condition[1] >= 1.0 => condition := 0.0
sxTrigger and condition[1] <= -1.0 => condition := 0.0
// ——————————— <strategy1_calls> {
//<long orders>
// ——————————— <visuals>
c_tp = leTrigger or seTrigger ? na :
condition == 0.0 ? na : color.green
c_entry = leTrigger or seTrigger ? na :
condition == 0.0 ? na : color.blue
c_sl = leTrigger or seTrigger ? na :
condition == 0.0 ? na : color.red
p_tp1Line = plot (
condition == 1.0 or
condition == -1.0 ? tp1Line : na,
title = "TP Line 1",
color = c_tp,
linewidth = 1,
style = plot.style_linebr)
p_tp2Line = plot (
condition == 1.0 or
condition == -1.0 or
condition == 1.1 or
condition == -1.1 ? tp2Line : na,
title = "TP Line 2",
color = c_tp,
linewidth = 1,
style = plot.style_linebr)
p_tp3Line = plot (
condition == 1.0 or
condition == -1.0 or
condition == 1.1 or
condition == -1.1 or
condition == 1.2 or
condition == -1.2 ? tp3Line : na,
title = "TP Line 3",
color = c_tp,
linewidth = 1,
style = plot.style_linebr)
p_entryLine = plot (
condition >= 1.0 or
condition <= -1.0 ? entryLine : na,
title = "Entry Line",
color = c_entry,
linewidth = 1,
style = plot.style_linebr)
p_slLine = plot (
condition == 1.0 or
condition == -1.0 or
condition == 1.1 or
condition == -1.1 or
condition == 1.2 or
condition == -1.2 ? slLine : na,
title = "SL Line",
color = c_sl,
linewidth = 1,
style = plot.style_linebr)
fill(
p_tp3Line, p_entryLine,
color = leTrigger or seTrigger ? na :color.new(color.green, 90))
fill(
p_entryLine, p_slLine,
color = leTrigger or seTrigger ? na :color.new(color.red, 90))
//<alerts labels>
plotshape(
i_alertOn and longE,
title = 'Long',
text = 'Long',
textcolor = color.white,
color = color.green,
style = shape.labelup,
size = size.tiny,
location = location.belowbar)
plotshape(
i_alertOn and shortE,
title = 'Short',
text = 'Short',
textcolor = color.white,
color = color.red,
style = shape.labeldown,
size = size.tiny,
location = location.abovebar)
plotshape(
i_alertOn and (longX or shortX) ? close : na,
title = 'Close',
text = 'Close',
textcolor = color.white,
color = color.gray,
style = shape.labelup,
size = size.tiny,
location = location.absolute)
l_tp = i_alertOn and (longTP1 or shortTP1) ? close : na
plotshape(
l_tp,
title = "TP1 Cross",
text = "TP1",
textcolor = color.white,
color = color.olive,
style = shape.labelup,
size = size.tiny,
location = location.absolute)
plotshape(
i_alertOn and (longTP2 or shortTP2) ? close : na,
title = "TP2 Cross",
text = "TP2",
textcolor = color.white,
color = color.olive,
style = shape.labelup,
size = size.tiny,
location = location.absolute)
plotshape(
i_alertOn and (longTP3 or shortTP3) ? close : na,
title = "TP3 Cross",
text = "TP3",
textcolor = color.white,
color = color.olive,
style = shape.labelup,
size = size.tiny,
location = location.absolute)
plotshape(
i_alertOn and (longSL or shortSL) ? close : na,
title = "SL Cross",
text = "SL",
textcolor = color.white,
color = color.maroon,
style = shape.labelup,
size = size.tiny,
location = location.absolute)
//<debug>
plot(
na,
title = "─── <debug> ───",
editable = false,
display = display.data_window)
plot(
condition,
title = "condition",
editable = false,
display = display.data_window)
//#endregion }
// ——————————— <↑↑↑ G_RISK ↑↑↑>
line.delete(
f_qDq(a_slLine,
line.new(
entryIndex,
slLine,
last_bar_index + l_right,
slLine,
style = line.style_solid,
color = c_sl)))
line.delete(
f_qDq(a_entryLine,
line.new(
entryIndex,
entryLine,
last_bar_index + l_right,
entryLine,
style = line.style_solid,
color = color.blue)))
line.delete(
f_qDq(a_tp3Line,
line.new(
entryIndex,
tp3Line,
last_bar_index + l_right,
tp3Line,
style = line.style_solid,
color = c_tp)))
line.delete(
f_qDq(a_tp2Line,
line.new(
entryIndex,
tp2Line,
last_bar_index + l_right,
tp2Line,
style = line.style_solid,
color = c_tp)))
line.delete(
f_qDq(a_tp1Line,
line.new(
entryIndex,
tp1Line,
last_bar_index + l_right,
tp1Line,
style = line.style_solid,
color = c_tp)))
label.delete(
f_qDq(a_slLabel,
label.new(
last_bar_index + l_right,
slLine,
'SL: ' + str.tostring(slLine, '##.###'),
style = label.style_label_left,
textcolor = color.white,
color = c_sl)))
label.delete(
f_qDq(a_entryLabel,
label.new(
last_bar_index + l_right,
entryLine,
'Entry: ' + str.tostring(entryLine, '##.###'),
style = label.style_label_left,
textcolor = color.white,
color = color.blue)))
label.delete(
f_qDq(a_tp3label,
label.new(
last_bar_index + l_right,
tp3Line,
'TP3: ' + str.tostring(tp3Line, '##.###'),
style = label.style_label_left,
textcolor = color.white,
color = c_tp)))
label.delete(
f_qDq(a_tp2label,
label.new(
last_bar_index + l_right,
tp2Line,
'TP2: ' + str.tostring(tp2Line, '##.###'),
style = label.style_label_left,
textcolor = color.white,
color = c_tp)))
label.delete(
f_qDq(a_tp1label,
label.new(
last_bar_index + l_right,
tp1Line,
'TP1: ' + str.tostring(tp1Line, '##.###'),
style = label.style_label_left,
textcolor = color.white,
color = c_tp)))
//#endregion }
// ——————————— <↑↑↑ G_SCRIPT02 ↑↑↑>
// ——————————— <alerts>
//<any_alert_function_call>
if longE or shortE or longX or shortX
alert(message = 'Any Alert', freq = alert.freq_once_per_bar_close)
if longE
alert(message = 'Long Entry', freq = alert.freq_once_per_bar_close)
if shortE
alert(message = 'Short Entry', freq = alert.freq_once_per_bar_close)
if longX
alert(message = 'Long Exit', freq = alert.freq_once_per_bar_close)
if shortX
alert(message = 'Short Exit', freq = alert.freq_once_per_bar_close)
//#endregion }
// ——————————— <↑↑↑ G_SCRIPT03 ↑↑↑>