Code Scripts
Code Scripts
0
International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5
indicator( 'Swing Breakouts Tests & Retests [LuxAlgo]', shorttitle='LuxAlgo -
Swing Breakouts Tests & Retests', max_labels_count=500, max_boxes_count=500,
overlay=true)
//--------------------------------------------------------------------------------
-------------------------------------}
// Settings
//--------------------------------------------------------------------------------
-------------------------------------{
display = input.string('Bullish AND Bearish', 'Display' , options=['Bullish
OR Bearish', 'Bullish AND Bearish', 'Bearish', 'Bullish'])
mult = input.float (1 , 'Width' , step =0.1
)
maxBars = input.int (500 , 'Maximum Bars' , tooltip=
'Maximum bars without signal' )
showReTest = input.string('All', 'Display Test/Retest Labels' , options=
['All', 'Last'] )
minBars = input.int (0 , 'Minimum Bars' , minval =0,
tooltip= 'Minimum required amount of bars between each labels')
setBack = input.bool (true, 'Set Back To Last Retest' , tooltip='When the
right side of the area is too far, stop updating and set back to position of last
test/retest')
left = input.int (10 , 'Left' , minval =1,
group='Swings'), right = input.int (1, 'Right' , minval =1 , group
='Swings' )
//Style
bull1_color = input.color (#08998145, 'Bullish ' , inline = 'bull' ,
group= 'Style' )
bull2_color = input.color (#f2364545, '' , inline = 'bull' ,
group= 'Style' )
bear1_color = input.color (#f2364545, 'Bearish' , inline = 'bear' ,
group= 'Style' )
bear2_color = input.color (#08998145, '' , inline = 'bear' ,
group= 'Style' )
labSize = input.string(size.small , 'Label Size' ,
options=[size.tiny, size.small, size.normal, size.large] )
//--------------------------------------------------------------------------------
-------------------------------------}
//UDT
//--------------------------------------------------------------------------------
-------------------------------------{
//--------------------------------------------------------------------------------
-------------------------------------}
//Methods
//--------------------------------------------------------------------------------
-------------------------------------{
n = bar_index
// @function Fetches the red, green and blue value from the input
// @param col color
// @returns color.rgb() without transparancy
method colorRGB(color col) =>
color.rgb(
color.r(col)
, color.g(col)
, color.b(col)
)
//--------------------------------------------------------------------------------
-------------------------------------}
//Variables
//--------------------------------------------------------------------------------
-------------------------------------{
var bull = bin.new(box(na), 0, na, array.new<label>(), array.new<label>())
var bear = bin.new(box(na), 0, na, array.new<label>(), array.new<label>())
//--------------------------------------------------------------------------------
-------------------------------------}
//Execution
//--------------------------------------------------------------------------------
-------------------------------------{
atr = nz(ta.atr(200), ta.cum(high - low) / (n + 1)) * mult
ph = ta.pivothigh(left, right)
pl = ta.pivotlow (left, right)
box_bull_left = bull.bx.get_left ()
box_bull_right = bull.bx.get_right ()
box_bull_top = bull.bx.get_top ()
box_bull_bottom = bull.bx.get_bottom()
box_bear_left = bear.bx.get_left ()
box_bear_right = bear.bx.get_right ()
box_bear_top = bear.bx.get_top ()
box_bear_bottom = bear.bx.get_bottom()
//Bullish
if display == 'Bullish'
or display == 'Bullish AND Bearish'
or (display == 'Bullish OR Bearish'
and bear.state == 0
)
switch
bull.state == 0 =>
if not na(pl)
bull.bx := box.new(n - right, pl + atr, n, pl,
bgcolor=bull1_color, border_color=color(na))
bull.state := 1 //Swing found; if condition is ok - state 0 -> 1
bull.lab1.clear() //chuck labels out of array without deleting
bull.lab2.clear()
bull.price := pl
bull.state == 1 =>
if box_bull_right - box_bull_left >= maxBars //Right side is too far
from start
if bull.lab1.size() == 0
bull.bx.delete()
else
if setBack
bull.bx.set_right(bull.lab1.first().get_x())
bull.state := 0 //Reset
else
bull.bx.set_right(n)
bull.state == 2 =>
//Bearish
if display == 'Bearish'
or display == 'Bullish AND Bearish'
or (display == 'Bullish OR Bearish'
and bull.state == 0
)
switch
bear.state == 0 =>
if not na(ph)
bear.bx := box.new(n - right, ph, n, ph - atr,
bgcolor=bear1_color, border_color=color(na))
bear.state := 1
bear.lab1.clear() //chuck labels out of array without deleting
bear.lab2.clear()
bear.price := ph
bear.state == 1 =>
if box_bear_right - box_bear_left >= maxBars
if bear.lab1.size() == 0
bear.bx.delete()
else
if setBack
bear.bx.set_right(bear.lab1.first().get_x())
bear.state := 0
else
bear.bx.set_right(n)
bear.state == 2 =>
//--------------------------------------------------------------------------------
-------------------------------------}