Facebook
From Farhad Adeel, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 110
  1.  
  2.   , overlay = true
  3.   , max_labels_count = 500
  4.   , max_lines_count = 500
  5.   , max_boxes_count = 500
  6.   , max_bars_back = 500)
  7. //-----------------------------------------------------------------------------{
  8. //Constants
  9. //-----------------------------------------------------------------------------{
  10. color TRANSP_CSS = #ffffff00
  11.  
  12. //Tooltips
  13. string MODE_TOOLTIP          = 'Allows to display historical Structure or only the recent ones'
  14. string STYLE_TOOLTIP         = 'Indicator color theme'
  15. string COLOR_CANDLES_TOOLTIP = 'Display additional candles with a color reflecting the current trend detected by structure'
  16. string SHOW_INTERNAL         = 'Display internal market structure'
  17. string CONFLUENCE_FILTER     = 'Filter non significant internal structure breakouts'
  18. string SHOW_SWING            = 'Display swing market Structure'
  19. string SHOW_SWING_POINTS     = 'Display swing point as labels on the chart'
  20. string SHOW_SWHL_POINTS      = 'Highlight most recent strong and weak high/low points on the chart'
  21. string INTERNAL_OB           = 'Display internal order blocks on the chart\n\nNumber of internal order blocks to display on the chart'
  22. string SWING_OB              = 'Display swing order blocks on the chart\n\nNumber of internal swing blocks to display on the chart'
  23. 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'
  24. string SHOW_EQHL             = 'Display equal highs and equal lows on the chart'
  25. string EQHL_BARS             = 'Number of bars used to confirm equal highs and equal lows'
  26. 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'
  27. string SHOW_FVG              = 'Display fair values gaps on the chart'
  28. string AUTO_FVG              = 'Filter out non significant fair value gaps'
  29. string FVG_TF                = 'Fair value gaps timeframe'
  30. string EXTEND_FVG            = 'Determine how many bars to extend the Fair Value Gap boxes on chart'
  31. string PED_ZONES             = 'Display premium, discount, and equilibrium zones on chart'
  32.  
  33. //-----------------------------------------------------------------------------{
  34. //Settings
  35. //-----------------------------------------------------------------------------{
  36. //General
  37. //----------------------------------------{
  38. mode = input.string('Historical'
  39.   , options = ['Historical', 'Present']
  40.   , group = 'Smart Money Concepts'
  41.   , tooltip = MODE_TOOLTIP)
  42.  
  43. style = input.string('Colored'
  44.   , options = ['Colored', 'Monochrome']
  45.   , group = 'Smart Money Concepts'
  46.   , tooltip = STYLE_TOOLTIP)
  47.  
  48. show_trend = input(false, 'Color Candles'
  49.   , group = 'Smart Money Concepts'
  50.   , tooltip = COLOR_CANDLES_TOOLTIP)
  51.  
  52. //----------------------------------------}
  53. //Internal Structure
  54. //----------------------------------------{
  55. show_internals = input(true, 'Show Internal Structure'
  56.   , group = 'Real Time Internal Structure'
  57.   , tooltip = SHOW_INTERNAL)
  58.  
  59. show_ibull = input.string('All', 'Bullish Structure'
  60.   , options = ['All', 'BOS', 'CHoCH']
  61.   , inline = 'ibull'
  62.   , group = 'Real Time Internal Structure')
  63.  
  64. swing_ibull_css = input(#089981, ''
  65.   , inline = 'ibull'
  66.   , group = 'Real Time Internal Structure')
  67.  
  68. //Bear Structure
  69. show_ibear = input.string('All', 'Bearish Structure'
  70.   , options = ['All', 'BOS', 'CHoCH']
  71.   , inline = 'ibear'
  72.   , group = 'Real Time Internal Structure')
  73.  
  74. swing_ibear_css = input(#f23645, ''
  75.   , inline = 'ibear'
  76.   , group = 'Real Time Internal Structure')
  77.  
  78. ifilter_confluence = input(false, 'Confluence Filter'
  79.   , group = 'Real Time Internal Structure'
  80.   , tooltip = CONFLUENCE_FILTER)
  81.  
  82. internal_structure_size = input.string('Tiny', 'Internal Label Size'
  83.   , options = ['Tiny', 'Small', 'Normal']
  84.   , group = 'Real Time Internal Structure')
  85.  
  86. //----------------------------------------}
  87. //Swing Structure
  88. //----------------------------------------{
  89. show_Structure = input(true, 'Show Swing Structure'
  90.   , group = 'Real Time Swing Structure'
  91.   , tooltip = SHOW_SWING)
  92.  
  93. //Bull Structure
  94. show_bull = input.string('All', 'Bullish Structure'
  95.   , options = ['All', 'BOS', 'CHoCH']
  96.   , inline = 'bull'
  97.   , group = 'Real Time Swing Structure')
  98.  
  99. swing_bull_css = input(#089981, ''
  100.   , inline = 'bull'
  101.   , group = 'Real Time Swing Structure')
  102.  
  103. //Bear Structure
  104. show_bear = input.string('All', 'Bearish Structure'
  105.   , options = ['All', 'BOS', 'CHoCH']
  106.   , inline = 'bear'
  107.   , group = 'Real Time Swing Structure')
  108.  
  109. swing_bear_css = input(#f23645, ''
  110.   , inline = 'bear'
  111.   , group = 'Real Time Swing Structure')
  112.  
  113. swing_structure_size = input.string('Small', 'Swing Label Size'
  114.   , options = ['Tiny', 'Small', 'Normal']
  115.   , group = 'Real Time Swing Structure')
  116.  
  117. //Swings
  118. show_swings = input(false, 'Show Swings Points'
  119.   , inline = 'swings'
  120.   , group = 'Real Time Swing Structure'
  121.   , tooltip = SHOW_SWING_POINTS)
  122.  
  123. length = input.int(50, ''
  124.   , minval = 10
  125.   , inline = 'swings'
  126.   , group = 'Real Time Swing Structure')
  127.  
  128. show_hl_swings = input(true, 'Show Strong/Weak High/Low'
  129.   , group = 'Real Time Swing Structure'
  130.   , tooltip = SHOW_SWHL_POINTS)
  131.  
  132. //----------------------------------------}
  133. //Order Blocks
  134. //----------------------------------------{
  135. show_iob = input(true, 'Internal Order Blocks'
  136.   , inline = 'iob'
  137.   , group = 'Order Blocks'
  138.   , tooltip = INTERNAL_OB)
  139.  
  140. iob_showlast = input.int(5, ''
  141.   , minval = 1
  142.   , inline = 'iob'
  143.   , group = 'Order Blocks')
  144.  
  145. show_ob = input(false, 'Swing Order Blocks'
  146.   , inline = 'ob'
  147.   , group = 'Order Blocks'
  148.   , tooltip = SWING_OB)
  149.  
  150. ob_showlast = input.int(5, ''
  151.   , minval = 1
  152.   , inline = 'ob'
  153.   , group = 'Order Blocks')
  154.  
  155. ob_filter = input.string('Atr', 'Order Block Filter'
  156.   , options = ['Atr', 'Cumulative Mean Range']
  157.   , group = 'Order Blocks'
  158.   , tooltip = FILTER_OB)
  159.  
  160. ibull_ob_css = input.color(color.new(#3179f5, 80), 'Internal Bullish OB'
  161.   , group = 'Order Blocks')
  162.  
  163. ibear_ob_css = input.color(color.new(#f77c80, 80), 'Internal Bearish OB'
  164.   , group = 'Order Blocks')
  165.  
  166. bull_ob_css = input.color(color.new(#1848cc, 80), 'Bullish OB'
  167.   , group = 'Order Blocks')
  168.  
  169. bear_ob_css = input.color(color.new(#b22833, 80), 'Bearish OB'
  170.   , group = 'Order Blocks')
  171.  
  172. //----------------------------------------}
  173. //EQH/EQL
  174. //----------------------------------------{
  175. show_eq = input(true, 'Equal High/Low'
  176.   , group = 'EQH/EQL'
  177.   , tooltip = SHOW_EQHL)
  178.  
  179. eq_len = input.int(3, 'Bars Confirmation'
  180.   , minval = 1
  181.   , group = 'EQH/EQL'
  182.   , tooltip = EQHL_BARS)
  183.  
  184. eq_threshold = input.float(0.1, 'Threshold'
  185.   , minval = 0
  186.   , maxval = 0.5
  187.   , step = 0.1
  188.   , group = 'EQH/EQL'
  189.   , tooltip = EQHL_THRESHOLD)
  190.  
  191. eq_size = input.string('Tiny', 'Label Size'
  192.   , options = ['Tiny', 'Small', 'Normal']
  193.   , group = 'EQH/EQL')
  194.  
  195. //----------------------------------------}
  196. //Fair Value Gaps
  197. //----------------------------------------{
  198. show_fvg = input(false, 'Fair Value Gaps'
  199.   , group = 'Fair Value Gaps'
  200.   , tooltip = SHOW_FVG)
  201.  
  202. fvg_auto = input(true, "Auto Threshold"
  203.   , group = 'Fair Value Gaps'
  204.   , tooltip = AUTO_FVG)
  205.  
  206. fvg_tf = input.timeframe('', "Timeframe"
  207.   , group = 'Fair Value Gaps'
  208.   , tooltip = FVG_TF)
  209.  
  210. bull_fvg_css = input.color(color.new(#00ff68, 70), 'Bullish FVG'
  211.   , group = 'Fair Value Gaps')
  212.  
  213. bear_fvg_css = input.color(color.new(#ff0008, 70), 'Bearish FVG'
  214.   , group = 'Fair Value Gaps')
  215.  
  216. fvg_extend = input.int(1, "Extend FVG"
  217.   , minval = 0
  218.   , group = 'Fair Value Gaps'
  219.   , tooltip = EXTEND_FVG)
  220.  
  221. //----------------------------------------}
  222. //Previous day/week high/low
  223. //----------------------------------------{
  224. //Daily
  225. show_pdhl = input(false, 'Daily'
  226.   , inline = 'daily'
  227.   , group = 'Highs & Lows MTF')
  228.  
  229. pdhl_style = input.string('⎯⎯⎯', ''
  230.   , options = ['⎯⎯⎯', '----', '····']
  231.   , inline = 'daily'
  232.   , group = 'Highs & Lows MTF')
  233.  
  234. pdhl_css = input(#2157f3, ''
  235.   , inline = 'daily'
  236.   , group = 'Highs & Lows MTF')
  237.  
  238. //Weekly
  239. show_pwhl = input(false, 'Weekly'
  240.   , inline = 'weekly'
  241.   , group = 'Highs & Lows MTF')
  242.  
  243. pwhl_style = input.string('⎯⎯⎯', ''
  244.   , options = ['⎯⎯⎯', '----', '····']
  245.   , inline = 'weekly'
  246.   , group = 'Highs & Lows MTF')
  247.  
  248. pwhl_css = input(#2157f3, ''
  249.   , inline = 'weekly'
  250.   , group = 'Highs & Lows MTF')
  251.  
  252. //Monthly
  253. show_pmhl = input(false, 'Monthly'
  254.   , inline = 'monthly'
  255.   , group = 'Highs & Lows MTF')
  256.  
  257. pmhl_style = input.string('⎯⎯⎯', ''
  258.   , options = ['⎯⎯⎯', '----', '····']
  259.   , inline = 'monthly'
  260.   , group = 'Highs & Lows MTF')
  261.  
  262. pmhl_css = input(#2157f3, ''
  263.   , inline = 'monthly'
  264.   , group = 'Highs & Lows MTF')
  265.  
  266. //----------------------------------------}
  267. //Premium/Discount zones
  268. //----------------------------------------{
  269. show_sd = input(false, 'Premium/Discount Zones'
  270.   , group = 'Premium & Discount Zones'
  271.   , tooltip = PED_ZONES)
  272.  
  273. premium_css = input.color(#f23645, 'Premium Zone'
  274.   , group = 'Premium & Discount Zones')
  275.  
  276. eq_css = input.color(#b2b5be, 'Equilibrium Zone'
  277.   , group = 'Premium & Discount Zones')
  278.  
  279. discount_css = input.color(#089981, 'Discount Zone'
  280.   , group = 'Premium & Discount Zones')
  281.  
  282. //-----------------------------------------------------------------------------}
  283. //Functions
  284. //-----------------------------------------------------------------------------{
  285. n = bar_index
  286.  
  287. atr = ta.atr(200)
  288. cmean_range = ta.cum(high - low) / n
  289.  
  290. //HL Output function
  291. hl() => [high, low]
  292.  
  293. //Get ohlc values function
  294. get_ohlc()=> [close[1], open[1], high, low, high[2], low[2]]
  295.  
  296. //Display Structure function
  297. display_Structure(x, y, txt, css, dashed, down, lbl_size)=>
  298.     structure_line = line.new(x, y, n, y
  299.       , color = css
  300.       , style = dashed ? line.style_dashed : line.style_solid)
  301.  
  302.     structure_lbl = label.new(int(math.avg(x, n)), y, txt
  303.       , color = TRANSP_CSS
  304.       , textcolor = css
  305.       , style = down ? label.style_label_down : label.style_label_up
  306.       , size = lbl_size)
  307.  
  308.     if mode == 'Present'
  309.         line.delete(structure_line[1])
  310.         label.delete(structure_lbl[1])
  311.  
  312. //Swings detection/measurements
  313. swings(len)=>
  314.     var os = 0
  315.    
  316.     upper = ta.highest(len)
  317.     lower = ta.lowest(len)
  318.  
  319.     os := high[len] > upper ? 0 : low[len] < lower ? 1 : os[1]
  320.  
  321.     top = os == 0 and os[1] != 0 ? high[len] : 0
  322.     btm = os == 1 and os[1] != 1 ? low[len] : 0
  323.  
  324.     [top, btm]
  325.  
  326. //Order block coordinates function
  327. ob_coord(use_max, loc, target_top, target_btm, target_left, target_type)=>
  328.     min = 99999999.
  329.     max = 0.
  330.     idx = 1
  331.  
  332.     ob_threshold = ob_filter == 'Atr' ? atr : cmean_range
  333.  
  334.     //Search for highest/lowest high within the structure interval and get range
  335.     if use_max
  336.         for i = 1 to (n - loc)-1
  337.             if (high[i] - low[i]) < ob_threshold[i] * 2
  338.                 max := math.max(high[i], max)
  339.                 min := max == high[i] ? low[i] : min
  340.                 idx := max == high[i] ? i : idx
  341.     else
  342.         for i = 1 to (n - loc)-1
  343.             if (high[i] - low[i]) < ob_threshold[i] * 2
  344.                 min := math.min(low[i], min)
  345.                 max := min == low[i] ? high[i] : max
  346.                 idx := min == low[i] ? i : idx
  347.  
  348.     array.unshift(target_top, max)
  349.     array.unshift(target_btm, min)
  350.     array.unshift(target_left, time[idx])
  351.     array.unshift(target_type, use_max ? -1 : 1)
  352.  
  353. //Set order blocks
  354. display_ob(boxes, target_top, target_btm, target_left, target_type, show_last, swing, size)=>
  355.     for i = 0 to math.min(show_last-1, size-1)
  356.         get_box = array.get(boxes, i)
  357.  
  358.         box.set_lefttop(get_box, array.get(target_left, i), array.get(target_top, i))
  359.         box.set_rightbottom(get_box, array.get(target_left, i), array.get(target_btm, i))
  360.         box.set_extend(get_box, extend.right)
  361.  
  362.         color css = na
  363.        
  364.         if swing
  365.             if style == 'Monochrome'
  366.                 css := array.get(target_type, i) == 1 ? color.new(#b2b5be, 80) : color.new(#5d606b, 80)
  367.                 border_css = array.get(target_type, i) == 1 ? #b2b5be : #5d606b
  368.                 box.set_border_color(get_box, border_css)
  369.             else
  370.                 css := array.get(target_type, i) == 1 ? bull_ob_css : bear_ob_css
  371.                 box.set_border_color(get_box, css)
  372.  
  373.             box.set_bgcolor(get_box, css)
  374.         else
  375.             if style == 'Monochrome'
  376.                 css := array.get(target_type, i) == 1 ? color.new(#b2b5be, 80) : color.new(#5d606b, 80)
  377.             else
  378.                 css := array.get(target_type, i) == 1 ? ibull_ob_css : ibear_ob_css
  379.            
  380.             box.set_border_color(get_box, css)
  381.             box.set_bgcolor(get_box, css)
  382.        
  383. //Line Style function
  384. get_line_style(style) =>
  385.     out = switch style
  386.         '⎯⎯⎯'  => line.style_solid
  387.         '----' => line.style_dashed
  388.         '····' => line.style_dotted
  389.  
  390. //Set line/labels function for previous high/lows
  391. phl(h, l, tf, css)=>
  392.     var line high_line = line.new(na,na,na,na
  393.       , xloc = xloc.bar_time
  394.       , color = css
  395.       , style = get_line_style(pdhl_style))
  396.  
  397.     var label high_lbl = label.new(na,na
  398.       , xloc = xloc.bar_time
  399.       , text = str.format('P{0}H', tf)
  400.       , color = TRANSP_CSS
  401.       , textcolor = css
  402.       , size = size.small
  403.       , style = label.style_label_left)
  404.  
  405.     var line low_line = line.new(na,na,na,na
  406.       , xloc = xloc.bar_time
  407.       , color = css
  408.       , style = get_line_style(pdhl_style))
  409.  
  410.     var label low_lbl = label.new(na,na
  411.       , xloc = xloc.bar_time
  412.       , text = str.format('P{0}L', tf)
  413.       , color = TRANSP_CSS
  414.       , textcolor = css
  415.       , size = size.small
  416.       , style = label.style_label_left)
  417.  
  418.     hy = ta.valuewhen(h != h[1], h, 1)
  419.     hx = ta.valuewhen(h == high, time, 1)
  420.  
  421.     ly = ta.valuewhen(l != l[1], l, 1)
  422.     lx = ta.valuewhen(l == low, time, 1)
  423.  
  424.     if barstate.islast
  425.         ext = time + (time - time[1])*20
  426.  
  427.         //High
  428.         line.set_xy1(high_line, hx, hy)
  429.         line.set_xy2(high_line, ext, hy)
  430.  
  431.         label.set_xy(high_lbl, ext, hy)
  432.  
  433.         //Low
  434.         line.set_xy1(low_line, lx, ly)
  435.         line.set_xy2(low_line, ext, ly)
  436.  
  437.         label.set_xy(low_lbl, ext, ly)
  438.  
  439. //-----------------------------------------------------------------------------}
  440. //Global variables
  441. //-----------------------------------------------------------------------------{
  442. var trend = 0, var itrend = 0
  443.  
  444. var top_y = 0., var top_x = 0
  445. var btm_y = 0., var btm_x = 0
  446.  
  447. var itop_y = 0., var itop_x = 0
  448. var ibtm_y = 0., var ibtm_x = 0
  449.  
  450. var trail_up = high, var trail_dn = low
  451. var trail_up_x = 0,  var trail_dn_x = 0
  452.  
  453. var top_cross = true,  var btm_cross = true
  454. var itop_cross = true, var ibtm_cross = true
  455.  
  456. var txt_top = '',  var txt_btm = ''
  457.  
  458. //Alerts
  459. bull_choch_alert = false
  460. bull_bos_alert   = false
  461.  
  462. bear_choch_alert = false
  463. bear_bos_alert   = false
  464.  
  465. bull_ichoch_alert = false
  466. bull_ibos_alert   = false
  467.  
  468. bear_ichoch_alert = false
  469. bear_ibos_alert   = false
  470.  
  471. bull_iob_break = false
  472. bear_iob_break = false
  473.  
  474. bull_ob_break = false
  475. bear_ob_break = false
  476.  
  477. eqh_alert = false
  478. eql_alert = false
  479.  
  480. //Structure colors
  481. var bull_css = style == 'Monochrome' ? #b2b5be
  482.   : swing_bull_css
  483.  
  484. var bear_css = style == 'Monochrome' ? #b2b5be
  485.   : swing_bear_css
  486.  
  487. var ibull_css = style == 'Monochrome' ? #b2b5be
  488.   : swing_ibull_css
  489.  
  490. var ibear_css = style == 'Monochrome' ? #b2b5be
  491.   : swing_ibear_css
  492.  
  493. //Labels size
  494. var internal_structure_lbl_size = internal_structure_size == 'Tiny'
  495.   ? size.tiny
  496.   : internal_structure_size == 'Small'
  497.   ? size.small
  498.   : size.normal
  499.  
  500. var swing_structure_lbl_size = swing_structure_size == 'Tiny'
  501.   ? size.tiny
  502.   : swing_structure_size == 'Small'
  503.   ? size.small
  504.   : size.normal
  505.  
  506. var eqhl_lbl_size = eq_size == 'Tiny'
  507.   ? size.tiny
  508.   : eq_size == 'Small'
  509.   ? size.small
  510.   : size.normal
  511.  
  512. //Swings
  513. [top, btm] = swings(length)
  514.  
  515. [itop, ibtm] = swings(5)
  516.  
  517. //-----------------------------------------------------------------------------}
  518. //Pivot High
  519. //-----------------------------------------------------------------------------{
  520. var line extend_top = na
  521.  
  522. var label extend_top_lbl = label.new(na, na
  523.   , color = TRANSP_CSS
  524.   , textcolor = bear_css
  525.   , style = label.style_label_down
  526.   , size = size.tiny)
  527.  
  528. if top
  529.     top_cross := true
  530.     txt_top := top > top_y ? 'HH' : 'LH'
  531.  
  532.     if show_swings
  533.         top_lbl = label.new(n-length, top, txt_top
  534.           , color = TRANSP_CSS
  535.           , textcolor = bear_css
  536.           , style = label.style_label_down
  537.           , size = swing_structure_lbl_size)
  538.  
  539.         if mode == 'Present'
  540.             label.delete(top_lbl[1])
  541.  
  542.     //Extend recent top to last bar
  543.     line.delete(extend_top[1])
  544.     extend_top := line.new(n-length, top, n, top
  545.       , color = bear_css)
  546.  
  547.     top_y := top
  548.     top_x := n - length
  549.  
  550.     trail_up := top
  551.     trail_up_x := n - length
  552.  
  553. if itop
  554.     itop_cross := true
  555.  
  556.     itop_y := itop
  557.     itop_x := n - 5
  558.  
  559. //Trailing maximum
  560. trail_up := math.max(high, trail_up)
  561. trail_up_x := trail_up == high ? n : trail_up_x
  562.  
  563. //Set top extension label/line
  564. if barstate.islast and show_hl_swings
  565.     line.set_xy1(extend_top, trail_up_x, trail_up)
  566.     line.set_xy2(extend_top, n + 20, trail_up)
  567.  
  568.     label.set_x(extend_top_lbl, n + 20)
  569.     label.set_y(extend_top_lbl, trail_up)
  570.     label.set_text(extend_top_lbl, trend < 0 ? 'Strong High' : 'Weak High')
  571.  
  572. //-----------------------------------------------------------------------------}
  573. //Pivot Low
  574. //-----------------------------------------------------------------------------{
  575. var line extend_btm = na
  576.  
  577. var label extend_btm_lbl = label.new(na, na
  578.   , color = TRANSP_CSS
  579.   , textcolor = bull_css
  580.    ,
  581.   , size = size.tiny)
  582.  
  583. if btm
  584.     btm_cross := true
  585.     txt_btm := btm < btm_y ? 'LL' : 'HL'
  586.    
  587.     if show_swings
  588.         btm_lbl = label.new(n - length, btm, txt_btm
  589.           , color = TRANSP_CSS
  590.           , textcolor = bull_css
  591.            ,
  592.           , size = swing_structure_lbl_size)
  593.  
  594.         if mode == 'Present'
  595.             label.delete(btm_lbl[1])
  596.    
  597.     //Extend recent btm to last bar
  598.     line.delete(extend_btm[1])
  599.     extend_btm := line.new(n - length, btm, n, btm
  600.       , color = bull_css)
  601.  
  602.     btm_y := btm
  603.     btm_x := n-length
  604.  
  605.     trail_dn := btm
  606.     trail_dn_x := n-length
  607.  
  608. if ibtm
  609.     ibtm_cross := true
  610.  
  611.     ibtm_y := ibtm
  612.     ibtm_x := n - 5
  613.  
  614. //Trailing minimum
  615. trail_dn := math.min(low, trail_dn)
  616. trail_dn_x := trail_dn == low ? n : trail_dn_x
  617.  
  618. //Set btm extension label/line
  619. if barstate.islast and show_hl_swings
  620.     line.set_xy1(extend_btm, trail_dn_x, trail_dn)
  621.     line.set_xy2(extend_btm, n + 20, trail_dn)
  622.  
  623.     label.set_x(extend_btm_lbl, n + 20)
  624.     label.set_y(extend_btm_lbl, trail_dn)
  625.     label.set_text(extend_btm_lbl, trend > 0 ? 'Strong Low' : 'Weak Low')
  626.  
  627. //-----------------------------------------------------------------------------}
  628. //Order Blocks Arrays
  629. //-----------------------------------------------------------------------------{
  630. var iob_top = array.new_float(0)
  631. var iob_btm = array.new_float(0)
  632. var iob_left = array.new_int(0)
  633. var iob_type = array.new_int(0)
  634.  
  635. var ob_top = array.new_float(0)
  636. var ob_btm = array.new_float(0)
  637. var ob_left = array.new_int(0)
  638. var ob_type = array.new_int(0)
  639.  
  640. //-----------------------------------------------------------------------------}
  641. //Pivot High BOS/CHoCH
  642. //-----------------------------------------------------------------------------{
  643. //Filtering
  644. var bull_concordant = true
  645.  
  646. if ifilter_confluence
  647.     bull_concordant := high - math.max(close, open) > math.min(close, open - low)
  648.  
  649. //Detect internal bullish Structure
  650. if ta.crossover(close, itop_y) and itop_cross and top_y != itop_y and bull_concordant
  651.     bool choch = na
  652.    
  653.     if itrend < 0
  654.         choch := true
  655.         bull_ichoch_alert := true
  656.     else
  657.         bull_ibos_alert := true
  658.    
  659.     txt = choch ? 'CHoCH' : 'BOS'
  660.  
  661.     if show_internals
  662.         if show_ibull == 'All' or (show_ibull == 'BOS' and not choch) or (show_ibull == 'CHoCH' and choch)
  663.             display_Structure(itop_x, itop_y, txt, ibull_css, true, true, internal_structure_lbl_size)
  664.    
  665.     itop_cross := false
  666.     itrend := 1
  667.    
  668.     //Internal Order Block
  669.     if show_iob
  670.         ob_coord(false, itop_x, iob_top, iob_btm, iob_left, iob_type)
  671.  
  672. //Detect bullish Structure
  673. if ta.crossover(close, top_y) and top_cross
  674.     bool choch = na
  675.    
  676.     if trend < 0
  677.         choch := true
  678.         bull_choch_alert := true
  679.     else
  680.         bull_bos_alert := true
  681.  
  682.     txt = choch ? 'CHoCH' : 'BOS'
  683.    
  684.     if show_Structure
  685.         if show_bull == 'All' or (show_bull == 'BOS' and not choch) or (show_bull == 'CHoCH' and choch)
  686.             display_Structure(top_x, top_y, txt, bull_css, false, true, swing_structure_lbl_size)
  687.    
  688.     //Order Block
  689.     if show_ob
  690.         ob_coord(false, top_x, ob_top, ob_btm, ob_left, ob_type)
  691.  
  692.     top_cross := false
  693.     trend := 1
  694.  
  695. //-----------------------------------------------------------------------------}
  696. //Pivot Low BOS/CHoCH
  697. //-----------------------------------------------------------------------------{
  698.  var bear_c
  699.  
  700. if ifilter_confluence
  701.     bear_concordant := high - math.max(close, open) < math.min(close, open - low)
  702.  
  703. //Detect internal bearish Structure
  704. if ta.crossunder(close, ibtm_y) and ibtm_cross and btm_y != ibtm_y and bear_concordant
  705.     bool choch = false
  706.    
  707.     if itrend > 0
  708.         choch := true
  709.         bear_ichoch_alert := true
  710.     else
  711.         bear_ibos_alert := true
  712.    
  713.     txt = choch ? 'CHoCH' : 'BOS'
  714.  
  715.     if show_internals
  716.         if show_ibear == 'All' or (show_ibear == 'BOS' and not choch) or (show_ibear == 'CHoCH' and choch)
  717.             display_Structure(ibtm_x, ibtm_y, txt, ibear_css, true, false, internal_structure_lbl_size)
  718.    
  719.     ibtm_cross := false
  720.     itrend := -1
  721.    
  722.     //Internal Order Block
  723.     if show_iob
  724.         ob_coord(true, ibtm_x, iob_top, iob_btm, iob_left, iob_type)
  725.  
  726. //Detect bearish Structure
  727. if ta.crossunder(close, btm_y) and btm_cross
  728.     bool choch = na
  729.    
  730.     if trend > 0
  731.         choch := true
  732.         bear_choch_alert := true
  733.     else
  734.         bear_bos_alert := true
  735.  
  736.     txt = choch ? 'CHoCH' : 'BOS'
  737.    
  738.     if show_Structure
  739.         if show_bear == 'All' or (show_bear == 'BOS' and not choch) or (show_bear == 'CHoCH' and choch)
  740.             display_Structure(btm_x, btm_y, txt, bear_css, false, false, swing_structure_lbl_size)
  741.    
  742.     //Order Block
  743.     if show_ob
  744.         ob_coord(true, btm_x, ob_top, ob_btm, ob_left, ob_type)
  745.  
  746.     btm_cross := false
  747.     trend := -1
  748.  
  749. //-----------------------------------------------------------------------------}
  750. //Order Blocks
  751. //-----------------------------------------------------------------------------{
  752. //Set order blocks
  753. var iob_boxes = array.new_box(0)
  754. var ob_boxes = array.new_box(0)
  755.  
  756. //Delete internal order blocks box coordinates if top/bottom is broken
  757. for element in iob_type
  758.     index = array.indexof(iob_type, element)
  759.  
  760.     if close < array.get(iob_btm, index) and element == 1
  761.         array.remove(iob_top, index)
  762.         array.remove(iob_btm, index)
  763.         array.remove(iob_left, index)
  764.         array.remove(iob_type, index)
  765.         bull_iob_break := true
  766.  
  767.     else if close > array.get(iob_top, index) and element == -1
  768.         array.remove(iob_top, index)
  769.         array.remove(iob_btm, index)
  770.         array.remove(iob_left, index)
  771.         array.remove(iob_type, index)
  772.         bear_iob_break := true
  773.  
  774. //Delete internal order blocks box coordinates if top/bottom is broken
  775. for element in ob_type
  776.     index = array.indexof(ob_type, element)
  777.  
  778.     if close < array.get(ob_btm, index) and element == 1
  779.         array.remove(ob_top, index)
  780.         array.remove(ob_btm, index)
  781.         array.remove(ob_left, index)
  782.         array.remove(ob_type, index)
  783.         bull_ob_break := true
  784.  
  785.     else if close > array.get(ob_top, index) and element == -1
  786.         array.remove(ob_top, index)
  787.         array.remove(ob_btm, index)
  788.         array.remove(ob_left, index)
  789.         array.remove(ob_type, index)
  790.         bear_ob_break := true
  791.  
  792. iob_size = array.size(iob_type)
  793. ob_size = array.size(ob_type)
  794.  
  795. if barstate.isfirst
  796.     if show_iob
  797.         for i = 0 to iob_showlast-1
  798.             array.push(iob_boxes, box.new(na,na,na,na, xloc = xloc.bar_time))
  799.     if show_ob
  800.         for i = 0 to ob_showlast-1
  801.             array.push(ob_boxes, box.new(na,na,na,na, xloc = xloc.bar_time))
  802.  
  803. if iob_size > 0
  804.     if barstate.islast
  805.         display_ob(iob_boxes, iob_top, iob_btm, iob_left, iob_type, iob_showlast, false, iob_size)
  806.  
  807. if ob_size > 0
  808.     if barstate.islast
  809.         display_ob(ob_boxes, ob_top, ob_btm, ob_left, ob_type, ob_showlast, true, ob_size)
  810.  
  811. //-----------------------------------------------------------------------------}
  812. //EQH/EQL
  813. //-----------------------------------------------------------------------------{
  814. var eq_prev_top = 0.
  815. var eq_top_x = 0
  816.  
  817. var eq_prev_btm = 0.
  818. var eq_btm_x = 0
  819.  
  820. if show_eq
  821.     eq_top = ta.pivothigh(eq_len, eq_len)
  822.     eq_btm = ta.pivotlow(eq_len, eq_len)
  823.  
  824.     if eq_top
  825.         max = math.max(eq_top, eq_prev_top)
  826.         min = math.min(eq_top, eq_prev_top)
  827.        
  828.         if max < min + atr * eq_threshold
  829.             eqh_line = line.new(eq_top_x, eq_prev_top, n-eq_len, eq_top
  830.               , color = bear_css
  831.                ,
  832.  
  833.             eqh_lbl = label.new(int(math.avg(n-eq_len, eq_top_x)), eq_top, 'EQH'
  834.               , color = #00000000
  835.               , textcolor = bear_css
  836.                ,
  837.               , size = eqhl_lbl_size)
  838.  
  839.             if mode == 'Present'
  840.                 line.delete(eqh_line[1])
  841.                 label.delete(eqh_lbl[1])
  842.            
  843.             eqh_alert := true
  844.  
  845.         eq_prev_top := eq_top
  846.         eq_top_x := n-eq_len
  847.  
  848.     if eq_btm
  849.         max = math.max(eq_btm, eq_prev_btm)
  850.         min = math.min(eq_btm, eq_prev_btm)
  851.        
  852.         if min > max - atr * eq_threshold
  853.             eql_line = line.new(eq_btm_x, eq_prev_btm, n-eq_len, eq_btm
  854.               , color = bull_css
  855.               , style = line.style_dotted)
  856.  
  857.             eql_lbl = label.new(int(math.avg(n-eq_len, eq_btm_x)), eq_btm, 'EQL'
  858.               , color = #00000000
  859.               , textcolor = bull_css
  860.               , style = label.style_label_up
  861.               , size = eqhl_lbl_size)
  862.  
  863.             eql_alert := true
  864.  
  865.             if mode == 'Present'
  866.                 line.delete(eql_line[1])
  867.                 label.delete(eql_lbl[1])
  868.  
  869.         eq_prev_btm := eq_btm
  870.         eq_btm_x := n-eq_len
  871.  
  872. //-----------------------------------------------------------------------------}
  873. //Fair Value Gaps
  874. //-----------------------------------------------------------------------------{
  875. var bullish_fvg_max = array.new_box(0)
  876. var bullish_fvg_min = array.new_box(0)
  877.  
  878. var bearish_fvg_max = array.new_box(0)
  879. var bearish_fvg_min = array.new_box(0)
  880.  
  881. float bullish_fvg_avg = na
  882. float bearish_fvg_avg = na
  883.  
  884. bullish_fvg_cnd = false
  885. bearish_fvg_cnd = false
  886.  
  887. [src_c1, src_o1, src_h, src_l, src_h2, src_l2] =
  888.   request.security(syminfo.tickerid, fvg_tf, get_ohlc())
  889.  
  890. if show_fvg
  891.     delta_per = (src_c1 - src_o1) / src_o1 * 100
  892.  
  893.     change_tf = timeframe.change(fvg_tf)
  894.  
  895.     threshold = fvg_auto ? ta.cum(math.abs(change_tf ? delta_per : 0)) / n * 2
  896.       : 0
  897.  
  898.     //FVG conditions
  899.     bullish_fvg_cnd := src_l > src_h2
  900.       and src_c1 > src_h2
  901.       and delta_per > threshold
  902.       and change_tf
  903.  
  904.     bearish_fvg_cnd := src_h < src_l2
  905.       and src_c1 < src_l2
  906.       and -delta_per > threshold
  907.       and change_tf
  908.  
  909.     //FVG Areas
  910.     if bullish_fvg_cnd
  911.         array.unshift(bullish_fvg_max, box.new(n-1, src_l, n + fvg_extend, math.avg(src_l, src_h2)
  912.           , border_color = bull_fvg_css
  913.           , bgcolor = bull_fvg_css))
  914.        
  915.         array.unshift(bullish_fvg_min, box.new(n-1, math.avg(src_l, src_h2), n + fvg_extend, src_h2
  916.           , border_color = bull_fvg_css
  917.           , bgcolor = bull_fvg_css))
  918.    
  919.     if bearish_fvg_cnd
  920.         array.unshift(bearish_fvg_max, box.new(n-1, src_h, n + fvg_extend, math.avg(src_h, src_l2)
  921.           , border_color = bear_fvg_css
  922.           , bgcolor = bear_fvg_css))
  923.        
  924.         array.unshift(bearish_fvg_min, box.new(n-1, math.avg(src_h, src_l2), n + fvg_extend, src_l2
  925.           , border_color = bear_fvg_css
  926.           , bgcolor = bear_fvg_css))
  927.  
  928.     for bx in bullish_fvg_min
  929.         if low < box.get_bottom(bx)
  930.             box.delete(bx)
  931.             box.delete(array.get(bullish_fvg_max, array.indexof(bullish_fvg_min, bx)))
  932.    
  933.     for bx in bearish_fvg_max
  934.         if high > box.get_top(bx)
  935.             box.delete(bx)
  936.             box.delete(array.get(bearish_fvg_min, array.indexof(bearish_fvg_max, bx)))
  937.  
  938. //-----------------------------------------------------------------------------}
  939. //Previous day/week high/lows
  940. //-----------------------------------------------------------------------------{
  941. //Daily high/low
  942. [pdh, pdl] = request.security(syminfo.tickerid, 'D', hl()
  943.   , lookahead = barmerge.lookahead_on)
  944.  
  945. //Weekly high/low
  946. [pwh, pwl] = request.security(syminfo.tickerid, 'W', hl()
  947.   , lookahead = barmerge.lookahead_on)
  948.  
  949. //Monthly high/low
  950. [pmh, pml] = request.security(syminfo.tickerid, 'M', hl()
  951.   , lookahead = barmerge.lookahead_on)
  952.  
  953. //Display Daily
  954. if show_pdhl
  955.     phl(pdh, pdl, 'D', pdhl_css)
  956.  
  957. //Display Weekly
  958. if show_pwhl
  959.     phl(pwh, pwl, 'W', pwhl_css)
  960.    
  961. //Display Monthly
  962. if show_pmhl
  963.     phl(pmh, pml, 'M', pmhl_css)
  964.  
  965. //-----------------------------------------------------------------------------}
  966. //Premium/Discount/Equilibrium zones
  967. //-----------------------------------------------------------------------------{
  968. var premium = box.new(na, na, na, na
  969.   , bgcolor = color.new(premium_css, 80)
  970.   , border_color = na)
  971.  
  972. var premium_lbl = label.new(na, na
  973.   , text = 'Premium'
  974.   , color = TRANSP_CSS
  975.   , textcolor = premium_css
  976.   , style = label.style_label_down
  977.   , size = size.small)
  978.  
  979. var eq = box.new(na, na, na, na
  980.   , bgcolor = color.rgb(120, 123, 134, 80)
  981.   , border_color = na)
  982.  
  983. var eq_lbl = label.new(na, na
  984.   , text = 'Equilibrium'
  985.   , color = TRANSP_CSS
  986.   , textcolor = eq_css
  987.   , style = label.style_label_left
  988.   , size = size.small)
  989.  
  990. var discount = box.new(na, na, na, na
  991.   , bgcolor = color.new(discount_css, 80)
  992.   , border_color = na)
  993.  
  994. var discount_lbl = label.new(na, na
  995.   , text = 'Discount'
  996.   , color = TRANSP_CSS
  997.   , textcolor = discount_css
  998.   , style = label.style_label_up
  999.   , size = size.small)
  1000.  
  1001. //Show Premium/Discount Areas
  1002. if barstate.islast and show_sd
  1003.     avg = math.avg(trail_up, trail_dn)
  1004.  
  1005.     box.set_lefttop(premium, math.max(top_x, btm_x), trail_up)
  1006.     box.set_rightbottom(premium, n, .95 * trail_up + .05 * trail_dn)
  1007.  
  1008.     label.set_xy(premium_lbl, int(math.avg(math.max(top_x, btm_x), n)), trail_up)
  1009.  
  1010.     box.set_lefttop(eq, math.max(top_x, btm_x), .525 * trail_up + .475*trail_dn)
  1011.     box.set_rightbottom(eq, n, .525 * trail_dn + .475 * trail_up)
  1012.  
  1013.     label.set_xy(eq_lbl, n, avg)
  1014.    
  1015.     box.set_lefttop(discount, math.max(top_x, btm_x), .95 * trail_dn + .05 * trail_up)
  1016.     box.set_rightbottom(discount, n, trail_dn)
  1017.     label.set_xy(discount_lbl, int(math.avg(math.max(top_x, btm_x), n)), trail_dn)
  1018.  
  1019. //-----------------------------------------------------------------------------}
  1020. //Trend
  1021. //-----------------------------------------------------------------------------{
  1022. var color trend_css = na
  1023.  
  1024. if show_trend
  1025.     if style == 'Colored'
  1026.         trend_css := itrend == 1 ? bull_css : bear_css
  1027.     else if style == 'Monochrome'
  1028.         trend_css := itrend == 1 ? #b2b5be : #5d606b
  1029.  
  1030. plotcandle(open, high, low, close
  1031.   , color = trend_css
  1032.   , wickcolor = trend_css
  1033.   , bordercolor = trend_css
  1034.   , editable = false)
  1035.  
  1036. //-----------------------------------------------------------------------------}
  1037. //Alerts
  1038. //-----------------------------------------------------------------------------{
  1039. //Internal Structure
  1040. alertcondition(bull_ibos_alert, 'Internal Bullish BOS', 'Internal Bullish BOS formed')
  1041. alertcondition(bull_ichoch_alert, 'Internal Bullish CHoCH', 'Internal Bullish CHoCH formed')
  1042.  
  1043. alertcondition(bear_ibos_alert, 'Internal Bearish BOS', 'Internal Bearish BOS formed')
  1044. alertcondition(bear_ichoch_alert, 'Internal Bearish CHoCH', 'Internal Bearish CHoCH formed')
  1045.  
  1046. //Swing Structure
  1047. alertcondition(bull_bos_alert, 'Bullish BOS', 'Internal Bullish BOS formed')
  1048. alertcondition(bull_choch_alert, 'Bullish CHoCH', 'Internal Bullish CHoCH formed')
  1049.  
  1050. alertcondition(bear_bos_alert, 'Bearish BOS', 'Bearish BOS formed')
  1051. alertcondition(bear_choch_alert, 'Bearish CHoCH', 'Bearish CHoCH formed')
  1052.  
  1053. //order Blocks
  1054. alertcondition(bull_iob_break, 'Bullish Internal OB Breakout', 'Price broke bullish internal OB')
  1055. alertcondition(bear_iob_break, 'Bearish Internal OB Breakout', 'Price broke bearish internal OB')
  1056.  
  1057. alertcondition(bull_ob_break, 'Bullish Swing OB Breakout', 'Price broke bullish swing OB')
  1058. alertcondition(bear_ob_break, 'Bearish Swing OB Breakout', 'Price broke bearish swing OB')
  1059.  
  1060. //EQH/EQL
  1061. alertcondition(eqh_alert, 'Equal Highs', 'Equal highs detected')
  1062. alertcondition(eql_alert, 'Equal Lows', 'Equal lows detected')
  1063.  
  1064. //FVG
  1065. alertcondition(bullish_fvg_cnd, 'Bullish FVG', 'Bullish FVG formed')
  1066. alertcondition(bearish_fvg_cnd, 'Bearish FVG', 'Bearish FVG formed')
  1067.  
  1068. //-----------------------------------------------------------------------------}