xmd79

SMART TRADING [Fixios]

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