HAHA
HAHA
// Settings
display = display.all - display.status_line
// Moving Averages
maShort1 = ta.sma(close, 14)
maShort2 = ta.sma(close, 21)
maShort3 = ta.sma(close, 50)
maShort4 = ta.sma(close, 67)
maLong1 = ta.sma(close, 100)
maLong2 = ta.sma(close, 200)
type ICTMS
float lastPrice
float midPrice
float prevPrice
int lastIndex
int midIndex
int prevIndex
label lastLabel
label midLabel
label prevLabel
bool isCrossed
int marketStructure
line msLine
box msLabel
type MS
int type = 0
// Generic Variables
BAR bar = BAR.new()
// Functions / Methods
textSize(sizeInText) =>
switch sizeInText
'Tiny' => size.tiny
'Small' => size.small
'Normal' => size.normal
=> size.large
lineStyle(styleInText) =>
switch styleInText
'Solid' => line.style_solid
'Dotted' => line.style_dotted
'Dashed' => line.style_dashed
if labelEnabled
this.msLabel := box.new(this.lastIndex, this.lastPrice, bar.index,
this.lastPrice, color(na), bgcolor = color(na),
text = marketStructure.type == (isBullish ? -1 : 1) ? termText + '-
MSS' : termText + '-BOS', text_size = textSize('Tiny'), text_halign =
text.align_left, text_valign = isBullish ? text.align_bottom : text.align_top,
text_color = color.new(color, 17))
marketStructure.setType(isBullish ? 1 : -1)
if stMarketStructures
if not stLow.isCrossed
stViolations += 1
if stHigh.isCrossed
stPoints := stPoints + 10 - stViolations
stViolations := 0
stLow.renderStructures(false, stMS, 'ST', stBearMSColor, stMSLineStyle,
stMSLineWidth, stMSText)
stHigh.renderStructures(true, stMS, 'ST', stBullMSColor, stMSLineStyle,
stMSLineWidth, stMSText)
cITL = stLow.queryPatterns(false)
if itSwings != 'Disabled'
TX = itSwings == '△▽' ? '△' : itSwings == '◈' ? '◈' : itLow.lastPrice >
itLow.midPrice ? 'IT-LH' : 'IT-LL'
stLow.midLabel.set_text(TX)
stLow.midLabel.set_size(textSize(itSwingsSZ))
stLow.midLabel.set_textcolor(itBearMSColor)
cITH = stHigh.queryPatterns(true)
if itSwings != 'Disabled'
TX = itSwings == '△▽' ? '▽' : itSwings == '◈' ? '◈' : itHigh.lastPrice >
itHigh.midPrice ? 'IT-HH' : 'IT-HL'
stHigh.midLabel.set_text(TX)
stHigh.midLabel.set_size(textSize(itSwingsSZ))
stHigh.midLabel.set_textcolor(itBullMSColor)
if itMarketStructures
if not itLow.isCrossed
itViolations += 1
if itHigh.isCrossed
itPoints := itPoints + 10 - itViolations
itViolations := 0
itLow.renderStructures(false, itMS, 'IT', itBearMSColor, itMSLineStyle,
itMSLineWidth, itMSText)
itHigh.renderStructures(true, itMS, 'IT', itBullMSColor, itMSLineStyle,
itMSLineWidth, itMSText)
cLTL = itLow.queryPatterns(false)
ltLow.lastPrice := itLow.midPrice
ltLow.lastIndex := itLow.midIndex
if ltSwings != 'Disabled'
itLow.midLabel.set_text(TX)
itLow.midLabel.set_size(textSize(ltSwingsSZ))
itLow.midLabel.set_textcolor(ltBearMSColor)
cLTH = itHigh.queryPatterns(true)
ltHigh.lastPrice := itHigh.midPrice
ltHigh.lastIndex := itHigh.midIndex
if ltSwings != 'Disabled'
itHigh.midLabel.set_text(TX)
itHigh.midLabel.set_size(textSize(ltSwingsSZ))
itHigh.midLabel.set_textcolor(ltBullMSColor)
if ltMarketStructures
if not ltLow.isCrossed
ltViolations += 1
if ltHigh.isCrossed
ltPoints := ltPoints + 10 - ltViolations
ltViolations := 0
ltLow.renderStructures(false, ltMS, 'LT', ltBearMSColor, ltMSLineStyle,
ltMSLineWidth, ltMSText)
ltHigh.renderStructures(true, ltMS, 'LT', ltBullMSColor, ltMSLineStyle,
ltMSLineWidth, ltMSText)
// Calculate RSI
rsi = ta.rsi(close, rsiLength)
// Calculate Volume MA
volumeMa = ta.sma(volume, volumeMaLength)
// Plot QQE
plot(qqeLine, title="QQE Line", color=color.blue)
plot(qqeSignal, title="QQE Signal", color=color.red)
// Plot RSI
plot(rsi, title="RSI", color=color.green)
hline(rsiOverbought, "RSI Overbought", color=color.red)
hline(rsiOversold, "RSI Oversold", color=color.blue)
// Plot Volume MA
plot(volumeMa, title="Volume MA", color=color.orange, linewidth=2)
// Plot Volume
plot(volume, title="Volume", color=color.purple, style=plot.style_columns,
linewidth=1)
// Alert Conditions
alertcondition(longCondition, title="Long Signal Alert", message="RSI has crossed
above the oversold level. Consider buying.")
alertcondition(shortCondition, title="Short Signal Alert", message="RSI has crossed
below the overbought level. Consider selling.")
//---------------------------------------------------------------------------------
-----------------//
//---------------------------------------------------------------------------------
------------------------------------}
// User Defined Types
//---------------------------------------------------------------------------------
------------------------------------{
type ICTMS1
float lastPrice
float midPrice
float prevPrice
int lastIndex
int midIndex
int prevIndex
label lastLabel
label midLabel
label prevLabel
bool isCrossed
bool isConfirmed
int marketStructure
line msLine
box msLabel
box swpBox
type MS1
int type = 0
type ZZ
int [] d
int [] x
float [] y
type liq
box bx
box bxz
box bxt
bool brZ
bool brL
line ln
line lne
type ob
float top = na
float btm = na
int loc = bar_index
bool breaker = false
int break_loc = na
type SWING
float y = na
int x = na
bool crossed = false
type vector
array<SWING> v
type htfBar
float price = na
int index = na
//---------------------------------------------------------------------------------
------------------------------------}
// Generic Variables
//---------------------------------------------------------------------------------
------------------------------------{
//---------------------------------------------------------------------------------
------------------------------------}
// Functions / Methods
//---------------------------------------------------------------------------------
------------------------------------{
timeframe(userTimeframe) =>
float chartTFinM = timeframe.in_seconds() / 60
switch
userTimeframe == "Auto" and chartTFinM <= 60 => 'D'
userTimeframe == "Auto" and chartTFinM <= 1440 => 'W'
userTimeframe == "5 Minutes" and chartTFinM <= 5 => '5'
userTimeframe == "15 Minutes" and chartTFinM <= 15 => '15'
userTimeframe == "1 Hour" and chartTFinM <= 60 => '60'
userTimeframe == "4 Hours" and chartTFinM <= 240 => '240'
userTimeframe == "1 Day" and timeframe.isintraday => 'D'
(userTimeframe == "Auto" or userTimeframe == "1 Week") and
(timeframe.isdaily or timeframe.isintraday) => 'W'
(userTimeframe == "Auto" or userTimeframe == "1 Month") and
(timeframe.isweekly or timeframe.isdaily or timeframe.isintraday) => 'M'
=> timeframe.period
if labelEnabled
this.msLabel := box.new(this.lastIndex, this.lastPrice, bar.index,
this.lastPrice, color(na), bgcolor = color(na),
text = marketStructure.type == (isBullish ? -1 : 1) ? 'MSS' : 'BOS',
text_size = size.tiny, text_halign = text.align_left, text_valign = isBullish ?
text.align_bottom : text.align_top, text_color = color.new(color, 17))
marketStructure.setType(isBullish ? 1 : -1)
method in_out(ZZ aZZ, int _d, int _x, float _y) =>
aZZ.d.unshift(_d), aZZ.x.unshift(_x), aZZ.y.unshift(_y), aZZ.d.pop(),
aZZ.x.pop(), aZZ.y.pop()
if get_v.size() == 3
pivot = switch mode
'bull' => math.max(get_v.get(0).y, get_v.get(1).y, get_v.get(2).y)
'bear' => math.min(get_v.get(0).y, get_v.get(1).y, get_v.get(2).y)
if pivot == get_v.get(1).y
if i < depth - 1
id.get(i+1).v.unshift(get_v.get(1))
if id.get(i+1).v.size() > 3
id.get(i+1).v.pop()
else
swingLevel := SWING.new(get_v.get(1).y, get_v.get(1).x)
get_v.pop()
get_v.pop()
swingLevel
if id.breaker
bxOBB.push(box.new(id.loc, id.top, id.break_loc, id.btm, color, bgcolor =
color, xloc = xloc.bar_time))
lnOBB.push(line.new(id.break_loc, id.top, timenow, id.top, xloc.bar_time,
extend.none, breakColor))
lnOBB.push(line.new(id.break_loc, id.btm, timenow, id.btm, xloc.bar_time,
extend.none, breakColor))
lnOBB.push(line.new(id.loc, avg, timenow, avg, xloc.bar_time, extend.none,
breakColor, style = line.style_dotted))
if not id.breaker
bxOBB.push(box.new(id.loc, id.top, timenow, id.btm, na, bgcolor = color,
extend = extend.none, xloc = xloc.bar_time))
lnOBB.push(line.new(id.loc, id.top, timenow, id.top, xloc.bar_time,
extend.none, color))
lnOBB.push(line.new(id.loc, id.btm, timenow, id.btm, xloc.bar_time,
extend.none, color))
lnOBB.push(line.new(id.loc, avg, timenow, avg, xloc.bar_time, extend.none,
breakColor, style = line.style_dotted))
//---------------------------------------------------------------------------------
------------------------------------}
// Calculations - Swings
//---------------------------------------------------------------------------------
------------------------------------{
cITL1 = stLow1.queryPatterns1(false)
itLow1.lastLabel := stLow1.midLabel
cITH1 = stHigh1.queryPatterns1(true)
itHigh1.lastLabel := stHigh1.midLabel
cLTL1 = itLow1.queryPatterns1(false)
ltLow1.lastPrice := itLow1.midPrice
ltLow1.lastIndex := itLow1.midIndex
ltHigh1.lastPrice := itHigh1.midPrice
ltHigh1.lastIndex := itHigh1.midIndex
//---------------------------------------------------------------------------------
------------------------------------}
// Calculations - Market Structures
//---------------------------------------------------------------------------------
------------------------------------{
if msShow
if msTerm == 'Short Term'
stLow1.renderStructures1(false, stMS1, msBearColor, msLineStyle, msText)
stHigh1.renderStructures1(true, stMS1, msBullColor, msLineStyle, msText)
//---------------------------------------------------------------------------------
------------------------------------}
// Calculations - Buyside & Sellside Liquidity
//---------------------------------------------------------------------------------
------------------------------------{
maxSize = 50
atr = ta.atr(10)
var int dir = na, var int x1 = na, var float y1 = na, var int x2 = na, var float y2
= na
if not liqBuySell
topLQ := SWING.new(0, 0)
btmLQ := SWING.new(0, 0)
swingHigh = topLQ.y
swingLow = btmLQ.y
x2 := bar.index - 1
if dir < 1
aZZ.in_out(1, x2, y2)
else
if dir == 1 and swingHigh > y1
aZZ.x.set(0, x2), aZZ.y.set(0, y2)
if true
count = 0
st_P = 0.
st_B = 0
minP = 0.
maxP = 10e6
for i = 0 to maxSize - 1
if aZZ.d.get(i) == 1
if aZZ.y.get(i) > swingHigh + (atr / liqMar)
break
else
if aZZ.y.get(i) > swingHigh - (atr / liqMar) and aZZ.y.get(i) <
swingHigh + (atr / liqMar)
count += 1
st_B := aZZ.x.get(i)
st_P := aZZ.y.get(i)
if aZZ.y.get(i) > minP
minP := aZZ.y.get(i)
if aZZ.y.get(i) < maxP
maxP := aZZ.y.get(i)
if count > 2
getB = b_liq_B.get(0)
if st_B == getB.bx.get_left()
getB.bx.set_top(math.avg(minP, maxP) + (atr / liqMar))
getB.bx.set_rightbottom(bar.index + 10, math.avg(minP, maxP) - (atr
/ liqMar))
else
b_liq_B.unshift(
liq.new(
box.new(st_B, math.avg(minP, maxP) + (atr / liqMar), bar.index +
10, math.avg(minP, maxP) - (atr / liqMar), bgcolor=color(na),
border_color=color(na)),
box.new(na, na, na, na, bgcolor = color(na), border_color =
color(na)),
box.new(st_B, st_P, bar.index + 10, st_P, text = 'Buyside
liquidity', text_size = size.tiny, text_halign = text.align_left, text_valign =
text.align_bottom, text_color = color.new(cLIQ_B, 25), bgcolor = color(na),
border_color = color(na)),
false,
false,
line.new(st_B , st_P, bar.index - 1, st_P, color =
color.new(cLIQ_B, 0)),
line.new(bar.index - 1, st_P, na , st_P, color =
color.new(cLIQ_B, 0), style = line.style_dotted))
)
if dir > -1
aZZ.in_out(-1, x2, y2)
else
if dir == -1 and swingLow < y1
aZZ.x.set(0, x2), aZZ.y.set(0, y2)
if true
count = 0
st_P = 0.
st_B = 0
minP = 0.
maxP = 10e6
for i = 0 to maxSize - 1
if aZZ.d.get(i) == -1
if aZZ.y.get(i) < swingLow - (atr / liqMar)
break
else
if aZZ.y.get(i) > swingLow - (atr / liqMar) and aZZ.y.get(i) <
swingLow + (atr / liqMar)
count += 1
st_B := aZZ.x.get(i)
st_P := aZZ.y.get(i)
if aZZ.y.get(i) > minP
minP := aZZ.y.get(i)
if aZZ.y.get(i) < maxP
maxP := aZZ.y.get(i)
if count > 2
getB = b_liq_S.get(0)
if st_B == getB.bx.get_left()
getB.bx.set_top(math.avg(minP, maxP) + (atr / liqMar))
getB.bx.set_rightbottom(bar.index + 10, math.avg(minP, maxP) - (atr
/ liqMar))
else
b_liq_S.unshift(
liq.new(
box.new(st_B, math.avg(minP, maxP) + (atr / liqMar), bar.index +
10, math.avg(minP, maxP) - (atr / liqMar), bgcolor=color(na),
border_color=color(na)),
box.new(na, na, na, na, bgcolor=color(na),
border_color=color(na)),
box.new(st_B, st_P, bar.index + 10, st_P, text = 'Sellside
liquidity', text_size = size.tiny, text_halign = text.align_left, text_valign =
text.align_top, text_color = color.new(cLIQ_S, 25), bgcolor=color(na),
border_color=color(na)),
false,
false,
line.new(st_B , st_P, bar.index - 1, st_P, color =
color.new(cLIQ_S, 0)),
line.new(bar.index - 1, st_P, na , st_P, color =
color.new(cLIQ_S, 0), style = line.style_dotted))
)
for i = 0 to b_liq_B.size() - 1
x = b_liq_B.get(i)
if not x.brL
x.lne.set_x2(bar.index)
for i = 0 to b_liq_S.size() - 1
x = b_liq_S.get(i)
if not x.brL
x.lne.set_x2(bar.index)
//---------------------------------------------------------------------------------
------------------------------------}
// Calculations - Liquidity Voids
//---------------------------------------------------------------------------------
------------------------------------{
if bull
l = 13
if bull[1]
st = math.abs(bar.low - bar.low[1]) / l
for i = 0 to l - 1
b_liq_V.push(box.new(bar.index - 2, bar.low[1] + i * st, bar.index,
bar.low[1] + (i + 1) * st, border_color = na, bgcolor = cLQV_B ))
else
st = math.abs(bar.low - bar.high[2]) / l
for i = 0 to l - 1
if lqText and i == 0
array.push(b_liq_V, box.new(bar.index - 2, bar.high[2] + i *
st, bar.index, bar.high[2] + (i + 1) * st, text = 'Liquidity Void ', text_size =
size.tiny, text_halign = text.align_right, text_valign = text.align_bottom,
text_color = na, border_color = na, bgcolor = cLQV_B ))
else
array.push(b_liq_V, box.new(bar.index - 2, bar.high[2] + i *
st, bar.index, bar.high[2] + (i + 1) * st, border_color = na, bgcolor = cLQV_B ))
if bear
l = 13
if bear[1]
st = math.abs(bar.high[1] - bar.high) / l
for i = 0 to l - 1
array.push(b_liq_V, box.new(bar.index - 2, bar.high + i * st,
bar.index, bar.high + (i + 1) * st, border_color = na, bgcolor = cLQV_S ))
else
st = math.abs(bar.low[2] - bar.high) / l
for i = 0 to l - 1
if lqText and i == l - 1
array.push(b_liq_V, box.new(bar.index - 2, bar.high + i * st,
bar.index, bar.high + (i + 1) * st, text = 'Liquidity Void ', text_size =
size.tiny, text_halign = text.align_right, text_valign = text.align_top, text_color
= na, border_color = na, bgcolor = cLQV_S ))
else
array.push(b_liq_V, box.new(bar.index - 2, bar.high + i * st,
bar.index, bar.high + (i + 1) * st, border_color = na, bgcolor = cLQV_S ))
if b_liq_V.size() > 0
qt = b_liq_V.size()
for bn = qt - 1 to 0
if bn < b_liq_V.size()
cb = b_liq_V.get(bn)
ba = math.avg(cb.get_bottom(), cb.get_top())
//---------------------------------------------------------------------------------
------------------------------------}
// Order & Breaker Blocks
//---------------------------------------------------------------------------------
------------------------------------{
if barstate.isfirst
for i = 0 to depth - 1
fractalHigh.push(vector.new(array.new<SWING>(0)))
fractalLow.push(vector.new(array.new<SWING>(0)))
fractalHigh.get(0).v.unshift(SWING.new(bar.high, bar.index))
fractalLow.get(0).v.unshift(SWING.new(bar.low, bar.index))
if fractalHigh.get(0).v.size() > 3
fractalHigh.get(0).v.pop()
if fractalLow.get(0).v.size() > 3
fractalLow.get(0).v.pop()
if obbShow
minima = max[1]
maxima = min[1]
loc = time[1]
if bullish_ob.size() > 0
for i = bullish_ob.size() - 1 to 0
element = bullish_ob.get(i)
if not element.breaker
if math.min(bar.close, bar.open) < element.btm
element.breaker := true
element.break_loc := time
else
if bar.close > element.top
bullish_ob.remove(i)
minima = min[1]
maxima = max[1]
loc = time[1]
if bearish_ob.size() > 0
for i = bearish_ob.size() - 1 to 0
element = bearish_ob.get(i)
if not element.breaker
if math.max(bar.close, bar.open) > element.top
element.breaker := true
element.break_loc := time
else
if bar.close < element.btm
bearish_ob.remove(i)
if bxOBB.size() > 0
for i = 0 to bxOBB.size() - 1
box.delete(bxOBB.shift())
if lnOBB.size() > 0
for i = 0 to lnOBB.size() - 1
line.delete(lnOBB.shift())
if barstate.islast
if obbBullLast > 0 and bullish_ob.size() > 0
for i = 0 to math.min(obbBullLast - 1, bullish_ob.size() - 1)
get_ob = bullish_ob.get(i)
get_ob.renderBlocks(obbBullColor, obbBullBreakColor)
//-----------------------------------------------------------------------------{
//CONSTANTS
//-----------------------------------------------------------------------------{
color TRANSP_CSS = #ffffff00
//Tooltips
string MODE_TOOLTIP = 'Allows to display historical Structure or only the
recent ones'
string STYLE_TOOLTIP = 'Indicator color theme'
string COLOR_CANDLES_TOOLTIP = 'Display additional candles with a color reflecting
the current trend detected by structure'
string SHOW_INTERNAL = 'Display internal market structure'
string CONFLUENCE_FILTER = 'Filter non significant internal structure
breakouts'
string SHOW_SWING = 'Display swing market Structure'
string SHOW_SWING_POINTS = 'Display swing point as labels on the chart'
string SHOW_SWHL_POINTS = 'Highlight most recent strong and weak high/low
points on the chart'
string HIGHLIGHT_MIT_OB = 'Highlight mitigated orderblocks with other color'
string INTERNAL_OB = 'Display internal order blocks on the chart\n\
nNumber of internal order blocks to display on the chart'
string SWING_OB = 'Display swing order blocks on the chart\n\nNumber
of internal swing blocks to display on the chart'
string FILTER_OB = 'Method used to filter out volatile order blocks \n\
nIt is recommended to use the cumulative mean range method when a low amount of
data is available'
string SHOW_EQHL = 'Display equal highs and equal lows on the chart'
string EQHL_BARS = 'Number of bars used to confirm equal highs and
equal lows'
string EQHL_THRESHOLD = 'Sensitivity threshold in a range (0, 1) used for
the detection of equal highs & lows\n\nLower values will return fewer but more
pertinent results'
string SHOW_FVG = 'Display fair values gaps on the chart'
string AUTO_FVG = 'Filter out non significant fair value gaps'
string FVG_TF = 'Fair value gaps timeframe'
string EXTEND_FVG = 'Determine how many bars to extend the Fair Value
Gap boxes on chart'
string PED_ZONES = 'Display premium, discount, and equilibrium zones on
chart'
//-----------------------------------------------------------------------------{
//Settings
//-----------------------------------------------------------------------------{
//General
//----------------------------------------{
mode = input.string('Historical'
, options = ['Historical', 'Present']
, group = 'Smart Money Concepts'
, tooltip = MODE_TOOLTIP)
style = input.string('Colored'
, options = ['Colored', 'Monochrome']
, group = 'Smart Money Concepts'
, tooltip = STYLE_TOOLTIP)
//----------------------------------------}
//Internal Structure
//----------------------------------------{
show_internals = input(true, 'Show Internal Structure'
, group = 'Real Time Internal Structure'
, tooltip = SHOW_INTERNAL)
//Bear Structure
show_ibear = input.string('All', 'Bearish Structure'
, options = ['All', 'BOS', 'CHoCH']
, inline = 'ibear'
, group = 'Real Time Internal Structure')
//----------------------------------------}
//Swing Structure
//----------------------------------------{
show_Structure = input(true, 'Show Swing Structure'
, group = 'Real Time Swing Structure'
, tooltip = SHOW_SWING)
//Bull Structure
show_bull = input.string('All', 'Bullish Structure'
, options = ['All', 'BOS', 'CHoCH']
, inline = 'bull'
, group = 'Real Time Swing Structure')
//Bear Structure
show_bear = input.string('All', 'Bearish Structure'
, options = ['All', 'BOS', 'CHoCH']
, inline = 'bear'
, group = 'Real Time Swing Structure')
//Swings
show_swings = input(false, 'Show Swings Points'
, inline = 'swings'
, group = 'Real Time Swing Structure'
, tooltip = SHOW_SWING_POINTS)
//----------------------------------------}
//Order Blocks
//----------------------------------------{
show_iob = input(true, 'Internal Order Blocks'
, inline = 'iob'
, group = 'Order Blocks'
, tooltip = INTERNAL_OB)
//----------------------------------------}
//EQH/EQL
//----------------------------------------{
show_eq = input(true, 'Equal High/Low'
, group = 'EQH/EQL'
, tooltip = SHOW_EQHL)
//----------------------------------------}
//Fair Value Gaps
//----------------------------------------{
show_fvg = input(false, 'Fair Value Gaps'
, group = 'Fair Value Gaps'
, tooltip = SHOW_FVG)
//----------------------------------------}
//Previous day/week high/low
//----------------------------------------{
//Daily
show_pdhl = input(false, 'Daily'
, inline = 'daily'
, group = 'Highs & Lows MTF')
//Weekly
show_pwhl = input(false, 'Weekly'
, inline = 'weekly'
, group = 'Highs & Lows MTF')
//Monthly
show_pmhl = input(false, 'Monthly'
, inline = 'monthly'
, group = 'Highs & Lows MTF')
//----------------------------------------}
//Premium/Discount zones
//----------------------------------------{
show_sd = input(false, 'Premium/Discount Zones'
, group = 'Premium & Discount Zones'
, tooltip = PED_ZONES)
//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
n = bar_index
atr1 = ta.atr(200)
cmean_range = ta.cum(high - low) / n
if mode == 'Present'
line.delete(structure_line[1])
label.delete(structure_lbl[1])
//Swings detection/measurements
swings(len)=>
var os = 0
upper = ta.highest(len)
lower = ta.lowest(len)
[top, btm]
//Search for highest/lowest high within the structure interval and get range
if use_max
for i = 1 to (n - loc)-1
if (high[i] - low[i]) < ob_threshold[i] * 2
max := math.max(high[i], max)
min := max == high[i] ? low[i] : min
idx := max == high[i] ? i : idx
else
for i = 1 to (n - loc)-1
if (high[i] - low[i]) < ob_threshold[i] * 2
min := math.min(low[i], min)
max := min == low[i] ? high[i] : max
idx := min == low[i] ? i : idx
array.unshift(target_top, max)
array.unshift(target_btm, min)
array.unshift(target_left, time[idx])
array.unshift(target_right, time[idx])
array.unshift(target_type, use_max ? -1 : 1)
array.unshift(target_mit, 0)
color css = na
if swing
if style == 'Monochrome'
css := array.get(target_type, i) == 1 ? color.new(#b2b5be, 80) :
color.new(#5d606b, 80)
border_css = array.get(target_type, i) == 1 ? #b2b5be : #5d606b
box.set_border_color(get_box, border_css)
else
if array.get(target_mit, i) > 0
css := array.get(target_type, i) == 1 ? bullm_ob_css :
bearm_ob_css
else
css := array.get(target_type, i) == 1 ? bull_ob_css :
bear_ob_css
box.set_border_color(get_box, css)
box.set_bgcolor(get_box, css)
else
if style == 'Monochrome'
css := array.get(target_type, i) == 1 ? color.new(#b2b5be, 80) :
color.new(#5d606b, 80)
else
if array.get(target_mit, i) > 0
css := array.get(target_type, i) == 1 ? ibullm_ob_css :
ibearm_ob_css
else
css := array.get(target_type, i) == 1 ? ibull_ob_css :
ibear_ob_css
box.set_border_color(get_box, css)
box.set_bgcolor(get_box, css)
hy = ta.valuewhen(h != h[1], h, 1)
hx = ta.valuewhen(h == high, time, 1)
ly = ta.valuewhen(l != l[1], l, 1)
lx = ta.valuewhen(l == low, time, 1)
if barstate.islast
ext = time + (time - time[1])*20
//High
line.set_xy1(high_line, hx, hy)
line.set_xy2(high_line, ext, hy)
//Low
line.set_xy1(low_line, lx, ly)
line.set_xy2(low_line, ext, ly)
//-----------------------------------------------------------------------------}
//Global variables
//-----------------------------------------------------------------------------{
var trend = 0, var itrend = 0
//Alerts
bull_choch_alert = false
bull_bos_alert = false
bear_choch_alert = false
bear_bos_alert = false
bull_ichoch_alert = false
bull_ibos_alert = false
bear_ichoch_alert = false
bear_ibos_alert = false
bull_iob_mit = false
bear_iob_mit = false
bull_ob_mit = false
bear_ob_mit = false
bull_iob_break = false
bear_iob_break = false
bull_ob_break = false
bear_ob_break = false
eqh_alert = false
eql_alert = false
//Structure colors
var bull_css = style == 'Monochrome' ? #b2b5be
: swing_bull_css
//Swings
[top1, btm1] = swings(length)
//-----------------------------------------------------------------------------}
//Pivot High
//-----------------------------------------------------------------------------{
var line extend_top = na
if top1
top_cross := true
txt_top := top1 > top_y ? 'HH' : 'LH'
if show_swings
top_lbl = label.new(n-length, top1, txt_top
, color = TRANSP_CSS
, textcolor = bear_css
, style = label.style_label_down
, size = size.small)
if mode == 'Present'
label.delete(top_lbl[1])
top_y := top1
top_x := n - length
trail_up := top1
trail_up_x := n - length
if itop
itop_cross := true
itop_y := itop
itop_x := n - 5
//Trailing maximum
trail_up := math.max(high, trail_up)
trail_up_x := trail_up == high ? n : trail_up_x
label.set_x(extend_top_lbl, n + 20)
label.set_y(extend_top_lbl, trail_up)
label.set_text(extend_top_lbl, trend < 0 ? 'Strong High' : 'Weak High')
//-----------------------------------------------------------------------------}
//Pivot Low
//-----------------------------------------------------------------------------{
var line extend_btm = na
if btm1
btm_cross := true
txt_btm := btm1 < btm_y ? 'LL' : 'HL'
if show_swings
btm_lbl = label.new(n - length, btm1, txt_btm
, color = TRANSP_CSS
, textcolor = bull_css
, style = label.style_label_up
, size = size.small)
if mode == 'Present'
label.delete(btm_lbl[1])
btm_y := btm1
btm_x := n-length
trail_dn := btm1
trail_dn_x := n-length
if ibtm
ibtm_cross := true
ibtm_y := ibtm
ibtm_x := n - 5
//Trailing minimum
trail_dn := math.min(low, trail_dn)
trail_dn_x := trail_dn == low ? n : trail_dn_x
label.set_x(extend_btm_lbl, n + 20)
label.set_y(extend_btm_lbl, trail_dn)
label.set_text(extend_btm_lbl, trend > 0 ? 'Strong Low' : 'Weak Low')
//-----------------------------------------------------------------------------}
//Order Blocks Arrays
//-----------------------------------------------------------------------------{
var iob_top = array.new_float(0)
var iob_btm = array.new_float(0)
var iob_left = array.new_int(0)
var iob_right = array.new_int(0)
var iob_type = array.new_int(0)
var iob_mit = array.new_int(0)
//-----------------------------------------------------------------------------}
//Pivot High BOS/CHoCH
//-----------------------------------------------------------------------------{
//Filtering
var bull_concordant = true
if ifilter_confluence
bull_concordant := high - math.max(close, open) > math.min(close, open - low)
if itrend < 0
choch := true
bull_ichoch_alert := true
else
bull_ibos_alert := true
if show_internals
if show_ibull == 'All' or (show_ibull == 'BOS' and not choch) or
(show_ibull == 'CHoCH' and choch)
display_Structure(itop_x, itop_y, txt, ibull_css, true, true,
size.tiny)
itop_cross := false
itrend := 1
if trend < 0
choch := true
bull_choch_alert := true
else
bull_bos_alert := true
if show_Structure
if show_bull == 'All' or (show_bull == 'BOS' and not choch) or (show_bull
== 'CHoCH' and choch)
display_Structure(top_x, top_y, txt, bull_css, false, true, size.small)
//Order Block
if show_ob
ob_coord(false, top_x, ob_top, ob_btm, ob_left, ob_right, ob_type, ob_mit)
top_cross := false
trend := 1
//-----------------------------------------------------------------------------}
//Pivot Low BOS/CHoCH
//-----------------------------------------------------------------------------{
var bear_concordant = true
if ifilter_confluence
bear_concordant := high - math.max(close, open) < math.min(close, open - low)
if itrend > 0
choch := true
bear_ichoch_alert := true
else
bear_ibos_alert := true
if show_internals
if show_ibear == 'All' or (show_ibear == 'BOS' and not choch) or
(show_ibear == 'CHoCH' and choch)
display_Structure(ibtm_x, ibtm_y, txt, ibear_css, true, false,
size.tiny)
ibtm_cross := false
itrend := -1
if trend > 0
choch := true
bear_choch_alert := true
else
bear_bos_alert := true
if show_Structure
if show_bear == 'All' or (show_bear == 'BOS' and not choch) or (show_bear
== 'CHoCH' and choch)
display_Structure(btm_x, btm_y, txt, bear_css, false, false,
size.small)
//Order Block
if show_ob
ob_coord(true, btm_x, ob_top, ob_btm, ob_left, ob_right, ob_type, ob_mit)
btm_cross := false
trend := -1
//-----------------------------------------------------------------------------}
//Order Blocks
//-----------------------------------------------------------------------------{
//Set order blocks
var iob_boxes = array.new_box(0)
var ob_boxes = array.new_box(0)
if ob_highlight_mit
if element == 1 and low[1] > array.get(ob_top, index) and low <=
array.get(ob_top, index)
array.set(ob_mit, index, array.get(ob_mit, index) + 1)
array.set(ob_right, index, time)
bull_ob_mit := true
else if element == -1 and high[1] < array.get(ob_btm, index) and high >=
array.get(ob_btm, index)
array.set(ob_mit, index, array.get(ob_mit, index) + 1)
array.set(ob_right, index, time)
bear_ob_mit := true
iob_size = array.size(iob_type)
ob_size = array.size(ob_type)
if barstate.isfirst
if show_iob
for i = 0 to iob_showlast-1
array.push(iob_boxes, box.new(na,na,na,na, xloc = xloc.bar_time))
if show_ob
for i = 0 to ob_showlast-1
array.push(ob_boxes, box.new(na,na,na,na, xloc = xloc.bar_time))
if iob_size > 0
if barstate.islast
display_ob(iob_boxes, iob_top, iob_btm, iob_left, iob_right, iob_type,
iob_mit, iob_showlast, false, iob_size)
if ob_size > 0
if barstate.islast
display_ob(ob_boxes, ob_top, ob_btm, ob_left, ob_right, ob_type, ob_mit,
ob_showlast, true, ob_size)
//-----------------------------------------------------------------------------}
//EQH/EQL
//-----------------------------------------------------------------------------{
var eq_prev_top = 0.
var eq_top_x = 0
var eq_prev_btm = 0.
var eq_btm_x = 0
if show_eq
eq_top = ta.pivothigh(eq_len, eq_len)
eq_btm = ta.pivotlow(eq_len, eq_len)
if eq_top
max = math.max(eq_top, eq_prev_top)
min = math.min(eq_top, eq_prev_top)
if mode == 'Present'
line.delete(eqh_line[1])
label.delete(eqh_lbl[1])
eqh_alert := true
eq_prev_top := eq_top
eq_top_x := n-eq_len
if eq_btm
max = math.max(eq_btm, eq_prev_btm)
min = math.min(eq_btm, eq_prev_btm)
if min > max - atr1 * eq_threshold
eql_line = line.new(eq_btm_x, eq_prev_btm, n-eq_len, eq_btm
, color = bull_css
, style = line.style_dotted)
eql_alert := true
if mode == 'Present'
line.delete(eql_line[1])
label.delete(eql_lbl[1])
eq_prev_btm := eq_btm
eq_btm_x := n-eq_len
//-----------------------------------------------------------------------------}
//Fair Value Gaps
//-----------------------------------------------------------------------------{
var bullish_fvg_max = array.new_box(0)
var bullish_fvg_min = array.new_box(0)
float bullish_fvg_avg = na
float bearish_fvg_avg = na
bullish_fvg_cnd = false
bearish_fvg_cnd = false
if show_fvg
delta_per = (src_c1 - src_o1) / src_o1 * 100
change_tf = timeframe.change(fvg_tf)
//FVG conditions
bullish_fvg_cnd := src_l > src_h2
and src_c1 > src_h2
and delta_per > threshold
and change_tf
//FVG Areas
if bullish_fvg_cnd
array.unshift(bullish_fvg_max, box.new(n-1, src_l, n + fvg_extend,
math.avg(src_l, src_h2)
, border_color = bull_fvg_css
, bgcolor = bull_fvg_css))
if bearish_fvg_cnd
array.unshift(bearish_fvg_max, box.new(n-1, src_h, n + fvg_extend,
math.avg(src_h, src_l2)
, border_color = bear_fvg_css
, bgcolor = bear_fvg_css))
for bx in bullish_fvg_min
if low < box.get_bottom(bx)
box.delete(bx)
box.delete(array.get(bullish_fvg_max, array.indexof(bullish_fvg_min,
bx)))
for bx in bearish_fvg_max
if high > box.get_top(bx)
box.delete(bx)
box.delete(array.get(bearish_fvg_min, array.indexof(bearish_fvg_max,
bx)))
//-----------------------------------------------------------------------------}
//Previous day/week high/lows
//-----------------------------------------------------------------------------{
//Daily high/low
[pdh, pdl] = request.security(syminfo.tickerid, 'D', hl()
, lookahead = barmerge.lookahead_on)
//Weekly high/low
[pwh, pwl] = request.security(syminfo.tickerid, 'W', hl()
, lookahead = barmerge.lookahead_on)
//Monthly high/low
[pmh, pml] = request.security(syminfo.tickerid, 'M', hl()
, lookahead = barmerge.lookahead_on)
//Display Daily
if show_pdhl
phl(pdh, pdl, 'D', pdhl_css)
//Display Weekly
if show_pwhl
phl(pwh, pwl, 'W', pwhl_css)
//Display Monthly
if show_pmhl
phl(pmh, pml, 'M', pmhl_css)
//-----------------------------------------------------------------------------}
//Premium/Discount/Equilibrium zones
//-----------------------------------------------------------------------------{
var premium = box.new(na, na, na, na
, bgcolor = color.new(premium_css, 80)
, border_color = na)
label.set_xy(eq_lbl, n, avg)
//-----------------------------------------------------------------------------}
//Trend
//-----------------------------------------------------------------------------{
var color trend_css = na
if show_trend
if style == 'Colored'
trend_css := itrend == 1 ? bull_css : bear_css
else if style == 'Monochrome'
trend_css := itrend == 1 ? #b2b5be : #5d606b
//-----------------------------------------------------------------------------}
//Alerts
//-----------------------------------------------------------------------------{
//Internal Structure
alertcondition(bull_ibos_alert, 'Internal Bullish BOS', 'Internal Bullish BOS
formed')
alertcondition(bull_ichoch_alert, 'Internal Bullish CHoCH', 'Internal Bullish CHoCH
formed')
//Swing Structure
alertcondition(bull_bos_alert, 'Bullish BOS', 'Internal Bullish BOS formed')
alertcondition(bull_choch_alert, 'Bullish CHoCH', 'Internal Bullish CHoCH formed')
//order Blocks
alertcondition(bull_iob_break, 'Bullish Internal OB Breakout', 'Price broke bullish
iternal OB')
alertcondition(bear_iob_break, 'Bearish Internal OB Breakout', 'Price broke bearish
iternal OB')
//EQH/EQL
alertcondition(eqh_alert, 'Equal Highs', 'Equal highs detected')
alertcondition(eql_alert, 'Equal Lows', 'Equal lows detected')
//FVG
alertcondition(bullish_fvg_cnd, 'Bullish FVG', 'Bullish FVG formed')
alertcondition(bearish_fvg_cnd, 'Bearish FVG', 'Bearish FVG formed')
//-----------------------------------------------------------------------------}
//------------------------------------------------------------------------------
// ICT UNICORN MODEL Settings
//-----------------------------------------------------------------------------{
len = input.int ( 10 , 'Swings'
, group='Unicorn')
//Targets
risk = input.float ( 1 ,
'Risk/Reward' , step = .1, inline = 'r' , group='Targets')
reward = input.float ( 1 ,
':' , step = .1, inline = 'r' , group='Targets')
rr = reward / risk
//-----------------------------------------------------------------------------}
//UDT's
//-----------------------------------------------------------------------------{
type piv
int b
float p
bool br
type ZZ1
int [] d
int [] x
float [] y
type unicorn
box box
box fvg
box Risk
box Reward
line lT
line lB
line SL
line linSL
label labC
bool active
bool isTaR
bool isTsL
bool trig
float swing
float TaR
float TsL
//-----------------------------------------------------------------------------}
//Variables
//-----------------------------------------------------------------------------{
var x = 0
var y = 0.
var z = 0.
piv[] swingH = na
piv[] swingL = na
float mnPiv = na
float mxPiv = 0
//-----------------------------------------------------------------------------}
//Methods
//-----------------------------------------------------------------------------{
method in_out(ZZ1 aZZ1, int d, int Cx, float Cy, int Dx, float Dy) =>
aZZ1.d.unshift(d), aZZ1.x.unshift(Dx), aZZ1.y.unshift(Dy), aZZ1.d.pop(),
aZZ1.x.pop(), aZZ1.y.pop()
//-----------------------------------------------------------------------------}
//Execution
//-----------------------------------------------------------------------------{
n1 = bar_index
atr2 = ta.atr(14)
cFg = chart.fg_color
lenL = 1
ph = ta.pivothigh(len, lenL)
pl = ta.pivotlow (len, lenL)
highest = ta.highest(5)
lowest = ta.lowest (5)
firstBl = aUniBl.first()
firstBr = aUniBr.first()
if dr_ < 1 // if previous point was a pl, add, and change direction ( 1)
aZZ1.in_out( 1, Cx_, Cy_, Dx_, Dy_)
else
if dr_ == 1 and ph > Cy_
aZZ1.x.set(0, Dx_), aZZ1.y.set(0, Dy_)
if pl.n()
if pl < mnPiv
mnPiv := pl
if swingL.size() > 0
for i = swingL.size() -1 to 0
get = swingL.get(i)
if pl <= get.p
swingL.remove(i)
if dr_ > -1 // if previous point was a ph, add, and change direction (-1)
aZZ1.in_out(-1, Cx_, Cy_, Dx_, Dy_)
else
if dr_ == -1 and pl < Cy_
aZZ1.x.set(0, Dx_), aZZ1.y.set(0, Dy_)
Ax = aZZ1.x.get (3)
Ay = aZZ1.y.get (3)
Bx = aZZ1.x.get (2)
By = aZZ1.y.get (2)
Cx = aZZ1.x.get (1)
Cy = aZZ1.y.get (1)
Dx = aZZ1.x.get (0)
Dy = aZZ1.y.get (0)
dir1 = aZZ1.d.get (0)
//Unicorn Pattern
if firstBl.active
if combine or (not combine and (not firstBl.active[1] or chS_bl))
if not secndBl.trig
secndBl.remove()
if firstBr.active
if combine or (not combine and (not firstBr.active[1] or chS_br))
if not secndBr.trig
secndBr.remove()
if n1 -lenL == Dx
// BRB
switch
close[n1 - Bx +0] < open[n1 - Bx +0] => x := Bx -0, y := high[n1 - Bx
+0], z := low[n1 - Bx +0]
close[n1 - Bx +1] < open[n1 - Bx +1] => x := Bx -1, y := high[n1 - Bx
+1], z := low[n1 - Bx +1]
close[n1 - Bx +2] < open[n1 - Bx +2] => x := Bx -2, y := high[n1 - Bx
+2], z := low[n1 - Bx +2]
close[n1 - Bx +3] < open[n1 - Bx +3] => x := Bx -3, y := high[n1 - Bx
+3], z := low[n1 - Bx +3]
close[n1 - Bx +4] < open[n1 - Bx +4] => x := Bx -4, y := high[n1 - Bx
+4], z := low[n1 - Bx +4]
// FVG
for i = 0 to n1 - Cx
fvgT = low[i+2]
fvgB = high[i]
if fvgB < fvgT
and ((fvgT < y and fvgT > z) or (fvgB < y and fvgB > z))
and fvgT - fvgB > atr2 * 0.05 // FVG large enough
if y != firstBr.box.get_top()
aUniBr.unshift(unicorn.new(
box = box.new( x , y , n1 -i-2 , z
, border_color = color.new( cFg , 100)
, bgcolor = color.new( cFg , 97))
, fvg = box.new(n1 -i-2, low[i+2], n1-1, high[i]
, border_color = color.new( na , na )
, bgcolor = color.new( colBr , 75))
, active = true , swing = Cy
, isTaR = false, TaR = na
, lT = line.new(x, y, n1-1, y
, color=color.new(cFg, 50)
, style=line.style_dashed)
, lB = line.new(x, z, n1-1, z
, color=color.new(cFg, 50)
, style=line.style_dashed)
, SL = line.new(Cx, Cy, Cx, Cy
, color=color.new(cFg, 65)
, style=line.style_dotted)
, trig=false
)
)
if not combine
if not firstBl.trig
firstBl.remove()
firstBl.active := false
firstBl.TaR := na
firstBl.TsL := na
firstBl.swing := na
break
// BRB
switch
close[n1 - Bx +0] > open[n1 - Bx +0] => x := Bx -0, y := high[n1 - Bx
+0], z := low[n1 - Bx +0]
close[n1 - Bx +1] > open[n1 - Bx +1] => x := Bx -1, y := high[n1 - Bx
+1], z := low[n1 - Bx +1]
close[n1 - Bx +2] > open[n1 - Bx +2] => x := Bx -2, y := high[n1 - Bx
+2], z := low[n1 - Bx +2]
close[n1 - Bx +3] > open[n1 - Bx +3] => x := Bx -3, y := high[n1 - Bx
+3], z := low[n1 - Bx +3]
close[n1 - Bx +4] > open[n1 - Bx +4] => x := Bx -4, y := high[n1 - Bx
+4], z := low[n1 - Bx +4]
// FVG
for i = 0 to n1 - Cx
fvgB = high[i+2]
fvgT = low [i]
if fvgB < fvgT
and ((fvgT < y and fvgT > z) or (fvgB < y and fvgB > z))
and fvgT - fvgB > atr2 * 0.05 // FVG large enough
if y != firstBl.box.get_top()
aUniBl.unshift(unicorn.new(
box = box.new( x , y , n1-i-2 , z
, border_color = color.new( cFg , 100)
, bgcolor = color.new( cFg , 97))
, fvg = box.new(n1 -i-2, low[i], n1-1, high[i+2]
, border_color = color.new( na , na )
, bgcolor = color.new( colBl , 75))
, active = true , swing = Cy
, isTaR = false, TaR = na
, lT = line.new(x, y, n1-1, y
, color=color.new(cFg, 50)
, style=line.style_dashed)
, lB = line.new(x, z, n1-1, z
, color=color.new(cFg, 50)
, style=line.style_dashed)
, SL = line.new(Cx, Cy, Cx, Cy
, color=color.new(cFg, 65)
, style=line.style_dotted)
, trig = false
)
)
if not combine
if not firstBr.trig
firstBr.remove()
firstBr.active := false
firstBr.TaR := na
firstBr.TsL := na
firstBr.swing := na
break
if firstBr.active
rgt = firstBr.fvg.get_right()
top = firstBr.lT .get_y2 ()
btm = firstBr.lB .get_y2 ()
firstBr.active := false
firstBr.TaR := na
firstBr.TsL := na
firstBr.swing := na
if not firstBr.trig
firstBr.fvg.set_right(n1-1)
if firstBr.isTaR
if low < firstBr.TaR
if showTargets
line.new(
rgt, firstBr.TaR
, n1, firstBr.TaR
, color
= color.new(cFg, 50))
firstBr.isTaR := false
firstBr.active := false
firstBr.TaR := na
firstBr.TsL := na
if trail
switch
close[1] > firstBr.TsL[1] => firstBr.TsL := na
close > firstBr.TsL => firstBr.isTsL := false
if phTsL.n()
firstBr.TsL := math.min(firstBr.TsL, phTsL)
else
trigger =
close < btm
and open > btm and open < top
and math.max(close[1], open[1]) > btm
and math.min(close[1], open[1]) < top
if trigger
label.new(n1, high, text='●'
, color=color.new(na, na), textcolor=colBr
, style=label.style_label_center
, size=size.tiny
)
firstBr.trig := true
firstBr.isTaR := true
if showTargets
firstBr.Risk := box.new(
chart.point.from_index(n1 , firstBr.swing)
, chart.point.from_index(n1 +1, btm )
, border_color=color.new(na , na )
, bgcolor =colRisk
)
firstBr.Reward := box.new(
chart.point.from_index(n1 , btm )
, chart.point.from_index(n1 +1, btm - diff)
, border_color=color.new(na , na )
, bgcolor =colReward
)
if firstBl.active
firstBl.active := false
firstBl.TaR := na
firstBl.TsL := na
firstBl.swing := na
if not firstBl.trig
firstBl.fvg.set_right(n1-1)
if firstBl.isTaR
if high > firstBl.TaR
if showTargets
line.new(
rgt, firstBl.TaR
, n1, firstBl.TaR
, color
= color.new(cFg, 50))
firstBl.isTaR := false
firstBl.active := false
firstBl.TaR := na
firstBl.TsL := na
if trail
switch
close[1] < firstBl.TsL[1] => firstBl.TsL := na
close < firstBl.TsL => firstBl.isTsL := false
if plTsL.n()
firstBl.TsL := math.max(firstBl.TsL, plTsL)
else
trigger =
close > top
and open < top and open > btm
and math.min(close[1], open[1]) < top
and math.max(close[1], open[1]) > btm
if trigger
label.new(n1, low, text='●'
, color=color.new(na, na), textcolor=colBl
, style=label.style_label_center
, size=size.tiny
)
firstBl.trig := true
firstBl.isTaR := true
if showTargets
firstBl.Risk := box.new(
chart.point.from_index(n1 , firstBl.swing)
, chart.point.from_index(n1 +1, top )
, border_color=color.new(na , na )
, bgcolor =colRisk
)
firstBl.Reward := box.new(
chart.point.from_index(n1 , top )
, chart.point.from_index(n1 +1, top + diff)
, border_color=color.new(na , na )
, bgcolor =colReward
)
//-----------------------------------------------------------------------------}
//Plot
//-----------------------------------------------------------------------------{
plot(trail and (firstBl.TsL >= firstBl.TsL[1] or na(firstBl.TsL[1])) ?
firstBl.TsL : na, color = colBl, style=plot.style_linebr)
plot(trail and (firstBr.TsL <= firstBr.TsL[1] or na(firstBr.TsL[1])) ?
firstBr.TsL : na, color = colBr, style=plot.style_linebr)
//-----------------------------------------------------------------------------//--
---------------------------------------------------------------------------}
// Initialize trade variables
var float entryPrice = na
var float exitPrice = na
var float profitLoss = na
var bool inTrade = false
var string tradeSignal = ""
// Input parameters
RSI_Period = input.int(14, title='RSI Length', minval=1)
SF = input.int(5, title='RSI Smoothing', minval=1, maxval=100) // Number input for
RSI Smoothing
QQE = input.float(4.238, title='Fast QQE Factor')
ThreshHold = input.float(10, title="Thresh-hold")
RsiMaThreshold = input.float(50, title="RsiMa Threshold") // Customizable threshold
for RsiMa
// Source input
srcInput = input.string("close", title="Source for Calculations", options=["close",
"open", "high", "low"])
src = switch srcInput
"open" => open
"high" => high
"low" => low
=> close // Default is close
// MA type selection
maType = input.string("EMA", title="Moving Average Type", options=["SMA", "EMA",
"DEMA", "TEMA", "WMA", "VWMA", "HMA", "RMA", "Combined VWMA"])
// Define MA function
ma(type, src, len) =>
switch type
"SMA" => ta.sma(src, len)
"EMA" => ta.ema(src, len)
"DEMA" =>
e = ta.ema(src, len)
2 * e - ta.ema(e, len)
"TEMA" =>
e = ta.ema(src, len)
3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
"WMA" => ta.wma(src, len)
"VWMA" => ta.vwma(src, len)
"HMA" =>
wma1 = ta.wma(src, len)
wma2 = ta.wma(src, len / 2)
2 * wma2 - wma1
"RMA" => ta.rma(src, len)
"Combined VWMA" =>
sma = ta.sma(src, len)
ema = ta.ema(src, len)
wma = ta.wma(src, len)
hma = 2 * ta.wma(src, len / 2) - ta.wma(src, len)
rma = ta.rma(src, len)
// Sum weights
for i = 0 to array.size(weights) - 1
totalWeight := totalWeight + array.get(weights, i)
// Calculate QQE
Wilders_Period = RSI_Period * 2 - 1
Rsi = ta.rsi(src, RSI_Period)
RsiMa = ma(maType, Rsi, SF) // Use selected MA type here
AtrRsi = math.abs(RsiMa[1] - RsiMa)
MaAtrRsi = ma(maType, AtrRsi, Wilders_Period) // Use selected MA type here
dar = ma(maType, MaAtrRsi, Wilders_Period) * QQE // Use selected MA type here
DeltaFastAtrRsi = dar
RSIndex = RsiMa
newshortband = RSIndex + DeltaFastAtrRsi
newlongband = RSIndex - DeltaFastAtrRsi
var float up = na
var float down = na
float atrHigh = na
float atrLow = na
float arrowUp = na
float arrowDown = na
atr2a = ta.atr(100) / 2
dev = channelDeviation * atr2a
if nextTrend == 1
maxLowPrice := na(maxLowPrice) ? lowPrice : math.max(lowPrice, maxLowPrice)
previousLow = na(low[1]) ? low : low[1] // Get previous low safely
if highma < maxLowPrice and close < previousLow
trenda := 1
nextTrend := 0
minHighPrice := highPrice
else
minHighPrice := na(minHighPrice) ? highPrice : math.min(highPrice,
minHighPrice)
if trenda == 0
if not na(trenda[1]) and trenda[1] != 0
up := na(down[1]) ? down : down[1]
arrowUp := up - atr2a
else
up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
atrHigh := up + dev
atrLow := up - dev
else
if not na(trenda[1]) and trenda[1] != 1
down := na(up[1]) ? up : up[1]
arrowDown := down + atr2a
else
down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
atrHigh := down + dev
atrLow := down - dev
ht = trenda == 0 ? up : down
// Plot Super Trend with different colors for rising and falling
plot(superTrend, color=direction == 1 ? color.red : color.green, linewidth=2,
title="Super Trend")