You are on page 1of 7

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// © islammessaoudi

////////////////////////////////////////

// Commercial Use Protection Notice:


// This code is protected for commercial use. Any unauthorized reproduction,
distribution, or modification
// of this code for commercial purposes is strictly prohibited and may result in
legal actions.

// All rights reserved to @islammessaoudi


// Contacts:
// Discord: @islammessaoudi
// Telegram: @islammessaoudi

////////////////////////////////////////

//@version=5
indicator("stracturre.x", "islammessaoudi", true,
max_bars_back = 5000, max_labels_count = 500, max_lines_count = 500)

//#region variable declaration


var bool mnUp = na
var bool mnDn = na
float mnStrc = na
var top = high
var bot = low
var puUp = high
var puDn = low
var L = low
var H = high
var idmB = low
var idmS = high
var lastH = high
var lastL = low
var lastHH = high
var lastLL = low
//bar indexes
var int puUpbar = na
var int puDnbar = na
var int idmB_bar = na
var int idmS_bar = na
var int Hbar = bar_index
var int Lbar = bar_index
var int lastHbar = bar_index
var int lastLbar = bar_index
var int lastHHbar = bar_index
var int lastLLbar = bar_index
//structure confirmaions
lastHL = math.max(Hbar, Lbar)
var bool isBosUp = 0
var bool isBosDn = 0
var bool isCocUp = 1
var bool isCocDn = 1

//color
color transp = color.new(#59ff62, 5)
//drawing options
equalHL = input.bool(0,"Use equal H/L", "To calculate valid pullbacks and minor
structure", group = "Calculation method")

showHL = input.bool(1, "Mark H/L", group = "H/L marking options")


HLcolor = input.color(color.rgb(0, 0, 0), "H/L color", group = "H/L marking
options")

showMn = input.bool(0, "Mark out internal structure", group = "Internal structue")


puUpco = input.color(color.rgb(255, 82, 82) , "High pivots", inline = "mnco",
group = "Internal structue")
puDnco = input.color(color.green, "Low pivots" , inline = "mnco", group = "Internal
structue")

showBC = input.bool(1, "Mark BoS/ChoCH", group = "BoS/Choch")


bull = input.color(color.green, "Bull color", inline = "BSclor" ,group =
"BoS/Choch")
bear = input.color(color.red , "Bear color", inline = "BSclor" ,group =
"BoS/Choch")

showbarcolor = input.bool(0,"Bar color", group = "Bar color")


showSCOB = input.bool(1, "Show SCOB pattern", group = "Bar color")
scobUp = input.color(color.aqua , "Bullish SCOB", inline = "scob" , group = "Bar
color")
scobDn = input.color(color.fuchsia, "Bearish SCOB", inline = "scob" , group = "Bar
color")

showIDM = input.bool(1,"Mark previous IDM", group = "IDM")


showliveIDM = input.bool(1,"Mark live IDM", group = "IDM")
idmColor = input.color(color.rgb(21, 6, 230), "IDM color", group = "IDM")
var label lv_lbl = na
var line lv_line = na

showSw = input.bool(1, "Show H/L sweeping lines", group = "H/L sweeps")


markX = input.bool(1, 'Mark "X"', group = "H/L sweeps")
swColor = input.color(color.white, "Sweeping line color", group = "H/L sweeps")

//max line length


maxlen = bar_index - 500

//#endregion

//#region drawing functions


mnMark(bool UD) =>
if showMn
label.new(
x = UD ? puUpbar : puDnbar,
y = UD ? puUp : puDn,
yloc = UD ? yloc.abovebar : yloc.belowbar,
text = "",
color = UD ? puUpco : puDnco,
style = UD ? label.style_arrowdown : label.style_arrowup,
size = size.tiny
)

BoS_ChoCh(bool B_C, bool UpDn) =>


[HLbarid, BCprc, BCcolor, BCtxt, BCstyle] = switch
B_C == 1 and UpDn == 1 => [lastHHbar, lastHH, bull, "BoS" ,
label.style_label_down]
B_C == 0 and UpDn == 1 => [lastHbar , lastH , bull, "ChoCh",
label.style_label_down]
B_C == 0 and UpDn == 0 => [lastLbar , lastL , bear, "ChoCh",
label.style_label_up ]
B_C == 1 and UpDn == 0 => [lastLLbar, lastLL, bear, "BoS" ,
label.style_label_up ]
HLbarid := HLbarid < maxlen ? maxlen : HLbarid
if showBC
line.new(HLbarid, BCprc, bar_index, BCprc, color = BCcolor, style =
line.style_dashed)
label.new(
int( math.avg(bar_index,HLbarid) ), BCprc
,BCtxt
,color = transp
,style = BCstyle
,textcolor = BCcolor
)

cfHL(bool ifHL) =>


[ifHLbarid, HLprc] = switch
ifHL =>
if high > H
[bar_index, high]
else
[Hbar, H]
=>
if low < L
[bar_index, low]
else
[Lbar, L]
ifHL_txt = if ifHL
if H == lastHH
"HH"
else
"LH"
else
if L == lastLL
"LL"
else
"HL"
cfHLStyle = ifHL ? label.style_label_down : label.style_label_up
if showHL
label.new(ifHLbarid,HLprc,ifHL_txt, color = transp, textcolor = HLcolor,
style = cfHLStyle)

sweep(bool swHL, bool swHrLr) =>


swStyle = swHL ? label.style_label_down : label.style_label_up
[swHLbarid, swprc, swHL_txt] = switch
swHL == 1 and swHrLr == 1 => [lastHHbar, lastHH, "HH"]
swHL == 1 and swHrLr == 0 => [lastHbar , lastH , "LH"]
swHL == 0 and swHrLr == 1 => [lastLbar , lastL , "HL"]
swHL == 0 and swHrLr == 0 => [lastLLbar, lastLL, "LL"]
swHLbarid := swHLbarid < maxlen ? maxlen : swHLbarid
//draw sweeping line
if showSw
line.new(swHLbarid,swprc,bar_index,swprc,color = swColor,style =
line.style_dotted)
if markX
label.new(int(math.avg(bar_index,swHLbarid)),swprc, "X", color =
transp, textcolor = swColor, style = swStyle, size = size.small)

IDM(bool BS) =>


IDMid = BS ? idmB_bar : idmS_bar
idmprc = BS ? idmB : idmS
idmStyle = BS ? label.style_label_up : label.style_label_down
IDMid := IDMid < maxlen ? maxlen : IDMid
//draw idm takeout line
if showIDM
line.new(IDMid,idmprc,bar_index,idmprc,color = idmColor,style =
line.style_dotted)
label.new(int(math.avg(bar_index,IDMid)),idmprc, "IDM", color = transp,
textcolor = idmColor, style = idmStyle,size = size.small)

//Bar color
ba_color = if showSCOB
if low[1] == puDn and low >= low[1] and close > high[1] and close[1] > low[2]
scobUp
else if high[1] == puUp and high <= high[1] and close < low[1] and close[1] <
high[2]
scobDn
else if showbarcolor
isCocUp?bull:bear
else
na
barcolor(ba_color,-1)
//#endregion

//#region structure mapping


//update IDM
if (high > H or (high == H and equalHL)) and low > idmB
if low <= puDn
idmB := low
idmB_bar := bar_index
else
idmB := puDn
idmB_bar := puDnbar
if (low < L or (low == L and equalHL)) and high < idmS
if high >= puUp
idmS := high
idmS_bar := bar_index
else
idmS := puUp
idmS_bar := puUpbar

//Check for IDM and ChoCh


if isCocUp and lastHL != Lbar
if low < idmB
if idmB != lastL
IDM(1)
isBosUp := 0
lastH := H
lastHbar := Hbar
lastHH := H
lastHHbar := Hbar
cfHL(1)
L := low
Lbar := bar_index
else if lastH != lastHH and high > lastH
cfHL(0)
isCocDn := 0
isBosDn := 0
if close > lastH
BoS_ChoCh(0,1)
isCocUp := 1
else
sweep(1,0)

if isCocDn and lastHL != Hbar


if high > idmS
if idmS != lastH
IDM(0)
isBosDn := 0
lastL := L
lastLbar := Lbar
lastLL := L
lastLLbar := Lbar
cfHL(0)
H := high
Hbar := bar_index
else if low < lastL and lastL != lastLL
if close < lastL
BoS_ChoCh(0,0)
cfHL(1)
isCocDn := 1
isCocUp := 0
isBosUp := 0
else
sweep(0,1)

//Check for BoS


if isBosUp == 0
if high > lastHH
if close > lastHH
BoS_ChoCh(1,1)
cfHL(0)
lastL := L
lastLbar := Lbar
isCocUp := 1
isBosUp := 1
isCocDn := 0
isBosDn := 0
else
sweep(1,1)
if isBosDn == 0
if low < lastLL
if close < lastLL
BoS_ChoCh(1,0)
cfHL(1)
lastH := H
lastHbar := Hbar
isCocUp := 0
isBosUp := 0
isCocDn := 1
isBosDn := 1
else
sweep(0,0)
//#endregion

//#region internal structure


if equalHL
if high >= top
if low > bot
mnDn :=0
mnUp := 1
if low <= bot
if high < top
mnUp := 0
mnDn := 1
if mnUp[1] and not mnUp
mnMark(1)
if mnDn[1] and not mnDn
mnMark(0)
else
if high > top
if low > bot
mnDn :=0
mnUp := 1
if low < bot
if high < top
mnUp := 0
mnDn := 1
if mnUp[1] and not mnUp
mnMark(1)
if mnDn[1] and not mnDn
mnMark(0)

if equalHL
if high >= top
puUp := high
puUpbar := bar_index
if low <= bot
puDn := low
puDnbar := bar_index
top := high
bot := low
if high >= top
top := high
bot := low
else
if high > top
puUp := high
puUpbar := bar_index
if low < bot
puDn := low
puDnbar := bar_index
top := high
bot := low
if high > top
top := high
bot := low
//#endregion

//#region auto update High and Low vars


if high > H or (high == H and equalHL)
H := high
Hbar := bar_index
if high > idmS
idmS := high
idmS_bar := bar_index
if high > lastH or (high == lastH and equalHL)
lastH := high
lastHbar := bar_index
if high > lastHH or (high == lastHH and equalHL)
lastHH := high
lastHHbar := bar_index

if low < L or (low == L and equalHL)


L := low
Lbar := bar_index
if low < idmB
idmB := low
idmB_bar := bar_index
if low < lastL or (low == lastL and equalHL)
lastL := low
lastLbar := bar_index
if low < lastLL or (low == lastLL and equalHL)
lastLL := low
lastLLbar := bar_index

//#endregion

//#region live IDM


if showliveIDM and barstate.islast
[liveIDM, liveIDMbar] = if isCocUp and lastHL == Hbar
[idmB, idmB_bar]
else if isCocDn and lastHL == Lbar
[idmS, idmS_bar]
lv_line := line.new(liveIDMbar, liveIDM, bar_index + 20, liveIDM, color =
idmColor, style = line.style_dotted),
lv_lbl := label.new(bar_index + 20, liveIDM, "IDM", color = transp, textcolor =
idmColor, style = isCocUp ? label.style_label_down : label.style_label_up, size =
size.small)
else
lv_line := na
lv_lbl := na
line.delete(lv_line[1])
label.delete(lv_lbl[1])
//#endregion

//#region fixing bugs


// plot(H,"H")
// plot(lastH, "lastH")
// plot(lastHH, "lastHH")
// plot(idmS, "idmS")
// plot(puUp, "puUp")
// plot(L,"L")
// plot(lastL, "lastL")
// plot(lastLL, "lastLL")
// plot(idmB, "idmB")
// plot(puDn, "puDn")
//#endregion

You might also like