xmd79

YoD - Price Action Pack (Version 2) (Swing)

Dec 15th, 2023
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.15 KB | None | 0 0
  1.  
  2. //@version=5
  3. indicator("YoD - Price Action Pack (Version 2) (Swing)", "YoD - Price Action Pack (Version 2)(Swing)"
  4. , overlay = true)
  5.  
  6. //-----------------------------------------------------------------------------{
  7. //Constants
  8. //-----------------------------------------------------------------------------{
  9. color TRANSP_CSS = #ffffff00
  10.  
  11. //-----------------------------------------------------------------------------{
  12.  
  13.  
  14.  
  15. //"Moving Average Band & Momentum Based Candles
  16. //----------------------------------------{
  17. len = input(title='SSL Length', defval=3000, group = "Moving Average Band & Momentum Based Candles")
  18. maType = input.string(title='SSL Type', defval='TMA',group = "Moving Average Band & Momentum Based Candles", options=['SMA','FRMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'HMA', 'THMA', 'EHMA', 'JMA', 'Kijun v2', 'EDSMA', 'McGinley','ALMA','ALHLSMA'])
  19. blength = input.int(100, minval=1, title = "Basis Length" ,group = "Moving Average Band & Momentum Based Candles")
  20. bType = input.string(title='Basis Type', defval='MF',group = "Moving Average Band & Momentum Based Candles", options=['SMA','FRMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'HMA', 'THMA', 'EHMA', 'JMA', 'Kijun v2', 'EDSMA', 'McGinley','ALMA','ALHLSMA'])
  21. src = close
  22. fibratio = input(defval=8, title='Fib Multiplier', group = "Moving Average Band & Momentum Based Candles")
  23. jurik_phase = input(1, "JMA Phase", group = "Moving Average Band & Momentum Based Candles")
  24. jurik_power = input(1, "JMA Power", group = "Moving Average Band & Momentum Based Candles")
  25. kidiv = input(1, "Kijun Mod Divider", group = "Moving Average Band & Momentum Based Candles")
  26. volatility_lookback = input(1000, "VAMA Lookback Voloatility", group = "Moving Average Band & Momentum Based Candles")
  27. ssfLength = input(20, "EDSMA Smoother Fileter Length", group = "Moving Average Band & Momentum Based Candles")
  28. ssfPoles = input(2, "EDSMA Smoother Poles Length", group = "Moving Average Band & Momentum Based Candles")
  29. offset = input.float(title="ALMA Offset", defval=0.85, group = "Moving Average Band & Momentum Based Candles")
  30. sigma = input.float(title="ALMA Sigma", defval=6, group = "Moving Average Band & Momentum Based Candles")
  31.  
  32.  
  33.  
  34.  
  35. //----------------------------------------}
  36. //Market Structure
  37. //----------------------------------------{
  38. show_Structure = input(true, 'Show Structure'
  39. , group = 'Market Structure')
  40.  
  41. //Bull Structure
  42. show_bull = input.string('All', 'Bullish Structure'
  43. , options = ['All', 'BOS', 'CHoCH']
  44. , inline = 'bull'
  45. , group = 'Market Structure')
  46.  
  47. swing_bull_css = input(#00c3ff, ''
  48. , inline = 'bull'
  49. , group = 'Market Structure')
  50.  
  51. //Bear Structure
  52. show_bear = input.string('All', 'Bearish Structure'
  53. , options = ['All', 'BOS', 'CHoCH']
  54. , inline = 'bear'
  55. , group = 'Market Structure')
  56.  
  57. swing_bear_css = input(#ff0062, ''
  58. , inline = 'bear'
  59. , group = 'Market Structure')
  60.  
  61. swing_structure_size = input.string('Small', 'Label Size'
  62. , options = ['Tiny', 'Small', 'Normal']
  63. , group = 'Market Structure')
  64.  
  65. length = input.int(100, 'Swing Trend Length'
  66. , minval = 10
  67. , group = 'Market Structure')
  68.  
  69. show_hl_swings = false
  70.  
  71. ilength = 50
  72.  
  73. show_swings = input(true, 'Show HH/LH/HL/LL'
  74. , inline = 'swings'
  75. , group = 'Market Structure')
  76.  
  77. //----------------------------------------}
  78. //Order Blocks
  79. //----------------------------------------{
  80. swinglength = input.int(10, title = 'Swing Length', group = 'Order Blocks', minval = 1, maxval = 100)
  81. box_width = input.float(15, title = 'OB Width', group = 'Order Blocks', minval = 1, maxval = 30, step = 0.5)
  82. supply_color = input.color(color.new(#ff0062,70), title = 'Bearish OB', group = 'Order Blocks', inline = '3')
  83. demand_color = input.color(color.new(#00c3ff,70), title = 'Bullish OB', group = 'Order Blocks', inline = '3')
  84. supply_outline_color = color.new(#ff0062,100)
  85. demand_outline_color = color.new(#ff0062,100)
  86. bos_label_color = color.rgb(255, 255, 255, 50)
  87. poi_label_color = color.rgb(255, 255, 255, 50)
  88. swing_type_color = color.black
  89. show_price_action_labels = false
  90. history_of_demand_to_keep = 5
  91.  
  92.  
  93.  
  94. mode = 'Historical'
  95. style = 'Colored'
  96. show_trend = false
  97. show_internals = false
  98. show_ibull = 'All'
  99. swing_ibull_css = #ff0062
  100. show_ibear = 'All'
  101. swing_ibear_css = #000000
  102. ifilter_confluence = false
  103. internal_structure_size = 'Tiny'
  104.  
  105. // ~~ Inputs {
  106. fibonacci = input.float(0.382,"Fibonacci Level  ",options=[0.236,0.382,0.50,0.618,0.786], group="Fib Settings")
  107. custom = false
  108. customfib = 0.5
  109. bull = color.lime
  110. bear = color.red
  111. barcol = false
  112. structure = false
  113. sen = 5
  114. s_bull = color.rgb(203, 68, 140)
  115. s_bear = color.rgb(95, 75, 188)
  116. Channel = false
  117. mult = 3.5
  118. max_col = color.rgb(203, 68, 140)
  119. min_col = color.rgb(95, 75, 188)
  120. levels = input.bool(true,"Fibonacci Levels",inline="level", group="Fib Settings")
  121. lvl_lab = input.bool(true,"Labels",inline="level", group="Fib Settings")
  122. line_s = "Solid"
  123. width = 2
  124. info = false
  125. Level_0236 = input.color(#F44336,"0.236",inline="fibcol", group="Fib Settings")
  126. Level_0382 = input.color(#81C784,"0.382",inline="fibcol", group="Fib Settings")
  127. Level_050 = input.color(#ff0062,"0.50",inline="fibcol", group="Fib Settings")
  128. Level_0618 = input.color(#009688,"0.618",inline="fibcol", group="Fib Settings")
  129. Level_0786 = input.color(#64B5F6,"0.786",inline="fibcol", group="Fib Settings")
  130. Level_0 = input.color(#5f5f5f,"0",inline="fibcol", group="Fib Settings")
  131. line_style = switch line_s
  132. "Solid" => line.style_solid
  133. "Dotted"=> line.style_dotted
  134. "Dashed"=> line.style_dashed
  135. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  136.  
  137. // ~~ Variables {
  138. b = bar_index
  139. var pos = 0
  140. var hi = high
  141. var lo = low
  142. var hloc = b
  143. var lloc = b
  144. var retrace = 0.0
  145. var t = string(na)
  146. fib = custom?customfib:fibonacci
  147. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  148.  
  149.  
  150.  
  151. // ~~ Track high/low and calculate fibonacci level {
  152. if pos>=0
  153. if high<retrace
  154. pos := -1
  155. lo := low
  156. lloc := time
  157. retrace := lo+(hi-lo)*fib
  158. t := str.tostring(dayofmonth)+"/"+str.tostring(month)+"/"+str.tostring(year)+" "
  159. if structure
  160. label.new(math.round(math.avg(hloc,lloc)),retrace[1],"▼",xloc.bar_time,
  161. color=color(na),style=label.style_label_up,textcolor=bear)
  162.  
  163. else if high>hi
  164. hi := high
  165. hloc := time
  166. retrace := hi-(hi-lo)*fib
  167. if structure and hi[1]==hi[sen]
  168. pos += 1
  169. line.new(hloc[1],hi[1],hloc,hi[1],xloc.bar_time,color=s_bear)
  170. if pos<=0
  171. if low>retrace
  172. pos := 1
  173. hi := high
  174. hloc := time
  175. retrace := hi-(hi-lo)*fib
  176. t := str.tostring(dayofmonth)+"/"+str.tostring(month)+"/"+str.tostring(year)+" "
  177. if structure
  178. label.new(math.round(math.avg(hloc,lloc)),retrace[1],"▲",xloc.bar_time,
  179. color=color(na),style=label.style_label_down,textcolor=bull)
  180.  
  181. else if low<lo
  182. lo := low
  183. lloc := time
  184. retrace := lo+(hi-lo)*fib
  185. if structure and lo[1]==lo[sen]
  186. pos := pos-1
  187. line.new(lloc[1],lo[1],lloc,lo[1],xloc.bar_time,color=s_bull)
  188. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  189.  
  190. // ~~ Levels {
  191. if levels or lvl_lab
  192. var lines = array.new<line>(7)
  193. var labels= array.new<label>(7)
  194. var lvls = array.from(0,0.236,0.382,0.50,0.618,0.786,1)
  195. var cols = array.from(Level_0,Level_0236,Level_0382,Level_050,Level_0618,Level_0786,Level_0)
  196. //Create fibonacci level lines
  197. if na(lines.get(0))
  198. for [i,c] in cols
  199. lines.pop().delete()
  200. labels.pop().delete()
  201. lines.unshift(line.new(na,na,na,na,xloc.bar_time,color=c,style=line_style,width=width))
  202. labels.unshift(label.new(na,na,str.tostring(lvls.get(i)*100,format.percent),
  203. color=color(na),style=label.style_label_left,textcolor=c))
  204. lines.push(line.new(na,na,na,na,xloc.bar_time,color=cols.get(0),style=line_style,width=width))
  205. //Update xy
  206. for [i,lvl] in lvls
  207. if levels
  208. lines.get(i).set_xy1(lvl==0?pos<0?hloc:lloc:pos>0?hloc:lloc,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
  209. lines.get(i).set_xy2(timenow,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
  210. if lvl_lab
  211. labels.get(i).set_xy(b,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
  212. if levels
  213. lines.get(7).set_xy1(pos<0?hloc:lloc,pos<0?hi:lo)
  214. lines.get(7).set_xy2(pos>0?hloc:lloc,pos<0?lo:hi)
  215. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  216. get2PoleSSF(src, length) =>
  217. PI = 2 * math.asin(1)
  218. arg = math.sqrt(2) * PI / length
  219. a1 = math.exp(-arg)
  220. b1 = 2 * a1 * math.cos(arg)
  221. c2 = b1
  222. c3 = -math.pow(a1, 2)
  223. c1 = 1 - c2 - c3
  224.  
  225. ssf = 0.0
  226. ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
  227. ssf
  228.  
  229. get3PoleSSF(src, length) =>
  230. PI = 2 * math.asin(1)
  231.  
  232. arg = PI / length
  233. a1 = math.exp(-arg)
  234. b1 = 2 * a1 * math.cos(1.738 * arg)
  235. c1 = math.pow(a1, 2)
  236.  
  237. coef2 = b1 + c1
  238. coef3 = -(c1 + b1 * c1)
  239. coef4 = math.pow(c1, 2)
  240. coef1 = 1 - coef2 - coef3 - coef4
  241.  
  242. ssf = 0.0
  243. ssf := coef1 * src + coef2 * nz(ssf[1]) + coef3 * nz(ssf[2]) + coef4 * nz(ssf[3])
  244. ssf
  245.  
  246. beta = 0.8
  247. feedback = false
  248. z = 0.5
  249. pow=10
  250.  
  251. ma(type, src, len) =>
  252. float result = 0
  253. if type == 'TMA'
  254. result := ta.sma(ta.sma(src, math.ceil(len / 2)), math.floor(len / 2) + 1)
  255. result
  256. if type == 'MF'
  257. ts = 0.
  258. b = 0.
  259. c = 0.
  260. os = 0.
  261. //----
  262. alpha = 2 / (len + 1)
  263. a = feedback ? z * src + (1 - z) * nz(ts[1], src) : src
  264. //----
  265. b := a > alpha * a + (1 - alpha) * nz(b[1], a) ? a : alpha * a + (1 - alpha) * nz(b[1], a)
  266. c := a < alpha * a + (1 - alpha) * nz(c[1], a) ? a : alpha * a + (1 - alpha) * nz(c[1], a)
  267. os := a == b ? 1 : a == c ? 0 : os[1]
  268. //----
  269. upper = beta * b + (1 - beta) * c
  270. lower = beta * c + (1 - beta) * b
  271. ts := os * upper + (1 - os) * lower
  272. result := ts
  273. result
  274. if type == 'LSMA'
  275. result := ta.linreg(src, len, 1)
  276. result
  277. if type == 'SMA' // Simple
  278. result := ta.sma(src, len)
  279. result
  280. if type == 'EMA' // Exponential
  281. result := ta.ema(src, len)
  282. result
  283. if type == 'DEMA' // Double Exponential
  284. e = ta.ema(src, len)
  285. result := 2 * e - ta.ema(e, len)
  286. result
  287. if type == 'TEMA' // Triple Exponential
  288. e = ta.ema(src, len)
  289. result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
  290. result
  291. if type == 'WMA' // Weighted
  292. result := ta.wma(src, len)
  293. result
  294. if type == 'VAMA' // Volatility Adjusted
  295. /// Copyright © 2019 to present, Joris Duyck (JD)
  296. mid = ta.ema(src, len)
  297. dev = (src - mid) * 1.25
  298. vol_up = ta.highest(dev, volatility_lookback)
  299. vol_down = ta.lowest(dev, volatility_lookback)
  300. result := mid + math.avg(vol_up, vol_down)
  301. result
  302. if type == 'ALHLSMA'
  303. A = ta.alma(src, len, 0.85, 6)
  304. B = ta.wma(2*ta.wma(src, len/2)-ta.wma(src, len), math.floor(math.sqrt(len)))
  305. C = ta.linreg(src, len, 0)
  306. alhlsma = (A + B + C) / 3
  307. result := alhlsma
  308. result
  309. if type == 'HMA' // Hull
  310. result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
  311. result
  312. if type == 'JMA' // Jurik
  313. /// Copyright © 2018 Alex Orekhov (everget)
  314. /// Copyright © 2017 Jurik Research and Consulting.
  315. phaseRatio = jurik_phase < -100 ? 0.5 : jurik_phase > 100 ? 2.5 : jurik_phase / 100 + 1.5
  316. beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2)
  317. alpha = math.pow(beta, jurik_power)
  318. jma = 0.0
  319. e0 = 0.0
  320. e0 := (1 - alpha) * src + alpha * nz(e0[1])
  321. e1 = 0.0
  322. e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
  323. e2 = 0.0
  324. e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
  325. jma := e2 + nz(jma[1])
  326. result := jma
  327. result
  328. if type == 'Kijun v2'
  329. kijun = math.avg(ta.lowest(len), ta.highest(len)) //, (open + close)/2)
  330. conversionLine = math.avg(ta.lowest(len / kidiv), ta.highest(len / kidiv))
  331. delta = (kijun + conversionLine) / 2
  332. result := delta
  333. result
  334. if type == 'McGinley'
  335. mg = 0.0
  336. mg := na(mg[1]) ? ta.ema(src, len) : mg[1] + (src - mg[1]) / (len * math.pow(src / mg[1], 4))
  337. result := mg
  338. result
  339. if type == 'FRMA'
  340. LMA = ta.rma(src, len)
  341. HMA = ta.rma(src, len)
  342. avg = ta.atr(len)
  343. lr3 = avg * 0.236
  344. hr3 = avg * 0.236
  345. topl = LMA + hr3
  346. bottl = HMA - lr3
  347. fma = (topl + bottl) / 2.00
  348. result := fma
  349. result
  350. if type == 'THMA'
  351. thma = ta.ema(2 * ta.ema(src, len / 2) - ta.ema(src, len), math.round(math.sqrt(len)))
  352. result := thma
  353. result
  354. if type == 'EHMA'
  355. ehma = ta.wma(ta.wma(src, len / 3) * 3 - ta.wma(src, len / 2) - ta.wma(src, len), len)
  356. result := ehma
  357. result
  358. if type == 'ALMA'
  359. alma = ta.alma(src, len, offset, sigma)
  360. result := alma
  361. result
  362. if type == 'EDSMA'
  363.  
  364. zeros = src - nz(src[2])
  365. avgZeros = (zeros + zeros[1]) / 2
  366.  
  367. // Ehlers Super Smoother Filter
  368. ssf = ssfPoles == 2 ? get2PoleSSF(avgZeros, ssfLength) : get3PoleSSF(avgZeros, ssfLength)
  369.  
  370. // Rescale filter in terms of Standard Deviations
  371. stdev = ta.stdev(ssf, len)
  372. scaledFilter = stdev != 0 ? ssf / stdev : 0
  373.  
  374. alpha = 5 * math.abs(scaledFilter) / len
  375.  
  376. edsma = 0.0
  377. edsma := alpha * src + (1 - alpha) * nz(edsma[1])
  378. result := edsma
  379. result
  380. result
  381.  
  382. ///Keltner Baseline Channel
  383. s2 = (ma(bType, close, blength) - ma(maType, close, len))
  384. c_color = s2 <= 0 ? #ff0062 : #00c3ff
  385. basis = ma(bType, close, blength)
  386. Keltma = ma(maType, close, len)
  387. avg = ta.atr(len)
  388. lr3 = avg * fibratio
  389. hr3 = avg * fibratio
  390. topl = Keltma + hr3
  391. bottl = Keltma - lr3
  392. fhma = (topl + bottl) / 2
  393. fill_color = color.rgb(255, 255, 255, 95)
  394. f3 = plot(Keltma, title='Median MA', color = c_color, linewidth = 2)
  395. t3 = plot(topl, title='Top MA', color = #00c3ff, linewidth = 2)
  396. b3 = plot(bottl, title='Bottom MA', color= #ff0062 , linewidth = 2)
  397. plot(basis, "Basis", color=c_color)
  398. plotcandle(open, high, low, close, color=c_color, wickcolor = c_color, bordercolor = c_color, title = "Momentum Candles")
  399. fill(t3, b3, color=fill_color, title="Background")
  400.  
  401.  
  402.  
  403. // FUNCTION TO ADD NEW AND REMOVE LAST IN ARRAY
  404. f_array_add_pop(array, new_value_to_add) =>
  405. array.unshift(array, new_value_to_add)
  406. array.pop(array)
  407.  
  408. // FUNCTION SWING H & L LABELS
  409. f_sh_sl_labels(array, swing_type) =>
  410.  
  411. var string label_text = na
  412. if swing_type == 1
  413. if array.get(array, 0) >= array.get(array, 1)
  414. label_text := 'HH'
  415. else
  416. label_text := 'LH'
  417. label.new(bar_index - swinglength, array.get(array,0), text = label_text, style=label.style_label_down, textcolor = swing_type_color, color = color.new(swing_type_color, 100), size = size.tiny)
  418.  
  419. else if swing_type == -1
  420. if array.get(array, 0) >= array.get(array, 1)
  421. label_text := 'HL'
  422. else
  423. label_text := 'LL'
  424. label.new(bar_index - swinglength, array.get(array,0), text = label_text, style=label.style_label_up, textcolor = swing_type_color, color = color.new(swing_type_color, 100), size = size.tiny)
  425.  
  426. // FUNCTION MAKE SURE SUPPLY ISNT OVERLAPPING
  427. f_check_overlapping(new_poi, box_array, atr) =>
  428.  
  429. atr_threshold = atr * 2
  430. okay_to_draw = true
  431.  
  432. for i = 0 to array.size(box_array) - 1
  433. top = box.get_top(array.get(box_array, i))
  434. bottom = box.get_bottom(array.get(box_array, i))
  435. poi = (top + bottom) / 2
  436.  
  437. upper_boundary = poi + atr_threshold
  438. lower_boundary = poi - atr_threshold
  439.  
  440. if new_poi >= lower_boundary and new_poi <= upper_boundary
  441. okay_to_draw := false
  442. break
  443. else
  444. okay_to_draw := true
  445. okay_to_draw
  446.  
  447.  
  448. // FUNCTION TO DRAW SUPPLY OR DEMAND ZONE
  449. f_supply_demand(value_array, bn_array, box_array, label_array, box_type, atr) =>
  450.  
  451. atr_buffer = atr * (box_width / 10)
  452. box_left = array.get(bn_array, 0)
  453. box_right = bar_index
  454.  
  455. var float box_top = 0.00
  456. var float box_bottom = 0.00
  457. var float poi = 0.00
  458.  
  459.  
  460. if box_type == 1
  461. box_top := array.get(value_array, 0)
  462. box_bottom := box_top - atr_buffer
  463. poi := (box_top + box_bottom) / 2
  464. else if box_type == -1
  465. box_bottom := array.get(value_array, 0)
  466. box_top := box_bottom + atr_buffer
  467. poi := (box_top + box_bottom) / 2
  468.  
  469. okay_to_draw = f_check_overlapping(poi, box_array, atr)
  470. // okay_to_draw = true
  471.  
  472. //delete oldest box, and then create a new box and add it to the array
  473. if box_type == 1 and okay_to_draw
  474. box.delete( array.get(box_array, array.size(box_array) - 1) )
  475. f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = supply_outline_color,
  476. bgcolor = supply_color, extend = extend.right, text = 'Bearish OB', text_halign = text.align_center, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
  477.  
  478. box.delete( array.get(label_array, array.size(label_array) - 1) )
  479. f_array_add_pop(label_array, box.new( left = box_left, top = poi, right = box_right, bottom = poi, border_color = color.new(poi_label_color,90),
  480. bgcolor = color.new(poi_label_color,90), extend = extend.right, text = '', text_halign = text.align_left, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
  481.  
  482. else if box_type == -1 and okay_to_draw
  483. box.delete( array.get(box_array, array.size(box_array) - 1) )
  484. f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = demand_outline_color,
  485. bgcolor = demand_color, extend = extend.right, text = 'Bullish OB', text_halign = text.align_center, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
  486.  
  487. box.delete( array.get(label_array, array.size(label_array) - 1) )
  488. f_array_add_pop(label_array, box.new( left = box_left, top = poi, right = box_right, bottom = poi, border_color = color.new(poi_label_color,90),
  489. bgcolor = color.new(poi_label_color,90), extend = extend.right, text = '', text_halign = text.align_left, text_valign = text.align_center, text_color = poi_label_color, text_size = size.small, xloc = xloc.bar_index))
  490.  
  491.  
  492. // FUNCTION TO CHANGE SUPPLY/DEMAND TO A BOS IF BROKEN
  493. f_sd_to_bos(box_array, bos_array, label_array, zone_type) =>
  494.  
  495. if zone_type == 1
  496. for i = 0 to array.size(box_array) - 1
  497. level_to_break = box.get_top(array.get(box_array,i))
  498. // if ta.crossover(close, level_to_break)
  499. if close >= level_to_break
  500. copied_box = box.copy(array.get(box_array,i))
  501. f_array_add_pop(bos_array, copied_box)
  502. mid = (box.get_top(array.get(box_array,i)) + box.get_bottom(array.get(box_array,i))) / 2
  503. box.set_top(array.get(bos_array,0), mid)
  504. box.set_bottom(array.get(bos_array,0), mid)
  505. box.set_extend( array.get(bos_array,0), extend.none)
  506. box.set_right( array.get(bos_array,0), bar_index)
  507. box.set_text( array.get(bos_array,0), '' )
  508. box.set_text_color( array.get(bos_array,0), bos_label_color)
  509. box.set_text_size( array.get(bos_array,0), size.small)
  510. box.set_text_halign( array.get(bos_array,0), text.align_center)
  511. box.set_text_valign( array.get(bos_array,0), text.align_center)
  512. box.delete(array.get(box_array, i))
  513. box.delete(array.get(label_array, i))
  514.  
  515.  
  516. if zone_type == -1
  517. for i = 0 to array.size(box_array) - 1
  518. level_to_break = box.get_bottom(array.get(box_array,i))
  519. // if ta.crossunder(close, level_to_break)
  520. if close <= level_to_break
  521. copied_box = box.copy(array.get(box_array,i))
  522. f_array_add_pop(bos_array, copied_box)
  523. mid = (box.get_top(array.get(box_array,i)) + box.get_bottom(array.get(box_array,i))) / 2
  524. box.set_top(array.get(bos_array,0), mid)
  525. box.set_bottom(array.get(bos_array,0), mid)
  526. box.set_extend( array.get(bos_array,0), extend.none)
  527. box.set_right( array.get(bos_array,0), bar_index)
  528. box.set_text( array.get(bos_array,0), '' )
  529. box.set_text_color( array.get(bos_array,0), bos_label_color)
  530. box.set_text_size( array.get(bos_array,0), size.small)
  531. box.set_text_halign( array.get(bos_array,0), text.align_center)
  532. box.set_text_valign( array.get(bos_array,0), text.align_center)
  533. box.delete(array.get(box_array, i))
  534. box.delete(array.get(label_array, i))
  535.  
  536.  
  537.  
  538. // FUNCTION MANAGE CURRENT BOXES BY CHANGING ENDPOINT
  539. f_extend_box_endpoint(box_array) =>
  540.  
  541. for i = 0 to array.size(box_array) - 1
  542. box.set_right(array.get(box_array, i), bar_index + 100)
  543.  
  544. //
  545. //CALCULATIONS
  546. //
  547.  
  548. // CALCULATE ATR
  549. atr = ta.atr(50)
  550.  
  551. // CALCULATE SWING HIGHS & SWING LOWS
  552. swing_high = ta.pivothigh(high, swinglength, swinglength)
  553. swing_low = ta.pivotlow(low, swinglength, swinglength)
  554.  
  555. // ARRAYS FOR SWING H/L & BN
  556. var swing_high_values = array.new_float(5,0.00)
  557. var swing_low_values = array.new_float(5,0.00)
  558.  
  559. var swing_high_bns = array.new_int(5,0)
  560. var swing_low_bns = array.new_int(5,0)
  561.  
  562. // ARRAYS FOR SUPPLY / DEMAND
  563. var current_supply_box = array.new_box(history_of_demand_to_keep, na)
  564. var current_demand_box = array.new_box(history_of_demand_to_keep, na)
  565.  
  566. // ARRAYS FOR SUPPLY / DEMAND POI LABELS
  567. var current_supply_poi = array.new_box(history_of_demand_to_keep, na)
  568. var current_demand_poi = array.new_box(history_of_demand_to_keep, na)
  569.  
  570. // ARRAYS FOR BOS
  571. var supply_bos = array.new_box(5, na)
  572. var demand_bos = array.new_box(5, na)
  573. //
  574. //END CALCULATIONS
  575. //
  576.  
  577. // NEW SWING HIGH
  578. if not na(swing_high)
  579.  
  580. //MANAGE SWING HIGH VALUES
  581. f_array_add_pop(swing_high_values, swing_high)
  582. f_array_add_pop(swing_high_bns, bar_index[swinglength])
  583. if show_price_action_labels
  584. f_sh_sl_labels(swing_high_values, 1)
  585.  
  586. f_supply_demand(swing_high_values, swing_high_bns, current_supply_box, current_supply_poi, 1, atr)
  587.  
  588. // NEW SWING LOW
  589. else if not na(swing_low)
  590.  
  591. //MANAGE SWING LOW VALUES
  592. f_array_add_pop(swing_low_values, swing_low)
  593. f_array_add_pop(swing_low_bns, bar_index[swinglength])
  594. if show_price_action_labels
  595. f_sh_sl_labels(swing_low_values, -1)
  596.  
  597. f_supply_demand(swing_low_values, swing_low_bns, current_demand_box, current_demand_poi, -1, atr)
  598.  
  599.  
  600. f_sd_to_bos(current_supply_box, supply_bos, current_supply_poi, 1)
  601. f_sd_to_bos(current_demand_box, demand_bos, current_demand_poi, -1)
  602.  
  603. f_extend_box_endpoint(current_supply_box)
  604. f_extend_box_endpoint(current_demand_box)
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611. //-----------------------------------------------------------------------------}
  612. //Functions
  613. //-----------------------------------------------------------------------------{
  614. n = bar_index
  615.  
  616.  
  617. cmean_range = ta.cum(high - low) / n
  618.  
  619. //HL Output function
  620. hl() => [high, low]
  621.  
  622. //Get ohlc values function
  623. get_ohlc()=> [close[1], open[1], high, low, high[2], low[2]]
  624.  
  625. //Display Structure function
  626. display_Structure(x, y, txt, css, dashed, down, lbl_size)=>
  627. structure_line = line.new(x, y, n, y
  628. , color = css
  629. , style = dashed ? line.style_dashed : line.style_solid)
  630.  
  631. structure_lbl = label.new(int(math.avg(x, n)), y, txt
  632. , color = TRANSP_CSS
  633. , textcolor = css
  634. , style = down ? label.style_label_down : label.style_label_up
  635. , size = lbl_size)
  636.  
  637. if mode == 'Present'
  638. line.delete(structure_line[1])
  639. label.delete(structure_lbl[1])
  640.  
  641. //Swings detection/measurements
  642. swings(len)=>
  643. var os = 0
  644.  
  645. upper = ta.highest(len)
  646. lower = ta.lowest(len)
  647.  
  648. os := high[len] > upper ? 0 : low[len] < lower ? 1 : os[1]
  649.  
  650. top = os == 0 and os[1] != 0 ? high[len] : 0
  651. btm = os == 1 and os[1] != 1 ? low[len] : 0
  652.  
  653. [top, btm]
  654.  
  655.  
  656.  
  657. //-----------------------------------------------------------------------------}
  658. //Global variables
  659. //-----------------------------------------------------------------------------{
  660. var trend = 0, var itrend = 0
  661.  
  662. var top_y = 0., var top_x = 0
  663. var btm_y = 0., var btm_x = 0
  664.  
  665. var itop_y = 0., var itop_x = 0
  666. var ibtm_y = 0., var ibtm_x = 0
  667.  
  668. var trail_up = high, var trail_dn = low
  669. var trail_up_x = 0, var trail_dn_x = 0
  670.  
  671. var top_cross = true, var btm_cross = true
  672. var itop_cross = true, var ibtm_cross = true
  673.  
  674. var txt_top = '', var txt_btm = ''
  675.  
  676. //Alerts
  677. bull_choch_alert = false
  678. bull_bos_alert = false
  679.  
  680. bear_choch_alert = false
  681. bear_bos_alert = false
  682.  
  683. bull_ichoch_alert = false
  684. bull_ibos_alert = false
  685.  
  686. bear_ichoch_alert = false
  687. bear_ibos_alert = false
  688.  
  689. bull_iob_break = false
  690. bear_iob_break = false
  691.  
  692. bull_ob_break = false
  693. bear_ob_break = false
  694.  
  695. eqh_alert = false
  696. eql_alert = false
  697.  
  698. //Structure colors
  699. var bull_css = style == 'Monochrome' ? #b2b5be
  700. : swing_bull_css
  701.  
  702. var bear_css = style == 'Monochrome' ? #b2b5be
  703. : swing_bear_css
  704.  
  705. var ibull_css = style == 'Monochrome' ? #b2b5be
  706. : swing_ibull_css
  707.  
  708. var ibear_css = style == 'Monochrome' ? #b2b5be
  709. : swing_ibear_css
  710.  
  711. //Labels size
  712. var internal_structure_lbl_size = internal_structure_size == 'Tiny'
  713. ? size.tiny
  714. : internal_structure_size == 'Small'
  715. ? size.small
  716. : size.normal
  717.  
  718. var swing_structure_lbl_size = swing_structure_size == 'Tiny'
  719. ? size.tiny
  720. : swing_structure_size == 'Small'
  721. ? size.small
  722. : size.normal
  723.  
  724.  
  725.  
  726. //Swings
  727. [top, btm] = swings(length)
  728.  
  729. [itop, ibtm] = swings(ilength)
  730.  
  731. //-----------------------------------------------------------------------------}
  732. //Pivot High
  733. //-----------------------------------------------------------------------------{
  734. var line extend_top = na
  735.  
  736. var label extend_top_lbl = label.new(na, na
  737. , color = TRANSP_CSS
  738. , textcolor = bear_css
  739. , style = label.style_label_down
  740. , size = size.tiny)
  741.  
  742. if top
  743. top_cross := true
  744. txt_top := top > top_y ? 'HH' : 'LH'
  745.  
  746. if show_swings
  747. top_lbl = label.new(n-length, top, txt_top
  748. , color = TRANSP_CSS
  749. , textcolor = bear_css
  750. , style = label.style_label_down
  751. , size = swing_structure_lbl_size)
  752.  
  753. if mode == 'Present'
  754. label.delete(top_lbl[1])
  755.  
  756. //Extend recent top to last bar
  757. line.delete(extend_top[1])
  758. extend_top := line.new(n-length, top, n, top
  759. , color = bear_css)
  760.  
  761. top_y := top
  762. top_x := n - length
  763.  
  764. trail_up := top
  765. trail_up_x := n - length
  766.  
  767. if itop
  768. itop_cross := true
  769.  
  770. itop_y := itop
  771. itop_x := n - 5
  772.  
  773. //Trailing maximum
  774. trail_up := math.max(high, trail_up)
  775. trail_up_x := trail_up == high ? n : trail_up_x
  776.  
  777. //Set top extension label/line
  778. if barstate.islast and show_hl_swings
  779. line.set_xy1(extend_top, trail_up_x, trail_up)
  780. line.set_xy2(extend_top, n + 20, trail_up)
  781.  
  782. label.set_x(extend_top_lbl, n + 20)
  783. label.set_y(extend_top_lbl, trail_up)
  784. label.set_text(extend_top_lbl, trend < 0 ? 'Strong High' : 'Weak High')
  785.  
  786. //-----------------------------------------------------------------------------}
  787. //Pivot Low
  788. //-----------------------------------------------------------------------------{
  789. var line extend_btm = na
  790.  
  791. var label extend_btm_lbl = label.new(na, na
  792. , color = TRANSP_CSS
  793. , textcolor = bull_css
  794. , style = label.style_label_up
  795. , size = size.tiny)
  796.  
  797. if btm
  798. btm_cross := true
  799. txt_btm := btm < btm_y ? 'LL' : 'HL'
  800.  
  801. if show_swings
  802. btm_lbl = label.new(n - length, btm, txt_btm
  803. , color = TRANSP_CSS
  804. , textcolor = bull_css
  805. , style = label.style_label_up
  806. , size = swing_structure_lbl_size)
  807.  
  808. if mode == 'Present'
  809. label.delete(btm_lbl[1])
  810.  
  811. //Extend recent btm to last bar
  812. line.delete(extend_btm[1])
  813. extend_btm := line.new(n - length, btm, n, btm
  814. , color = bull_css)
  815.  
  816. btm_y := btm
  817. btm_x := n-length
  818.  
  819. trail_dn := btm
  820. trail_dn_x := n-length
  821.  
  822. if ibtm
  823. ibtm_cross := true
  824.  
  825. ibtm_y := ibtm
  826. ibtm_x := n - 5
  827.  
  828. //Trailing minimum
  829. trail_dn := math.min(low, trail_dn)
  830. trail_dn_x := trail_dn == low ? n : trail_dn_x
  831.  
  832. //Set btm extension label/line
  833. if barstate.islast and show_hl_swings
  834. line.set_xy1(extend_btm, trail_dn_x, trail_dn)
  835. line.set_xy2(extend_btm, n + 20, trail_dn)
  836.  
  837. label.set_x(extend_btm_lbl, n + 20)
  838. label.set_y(extend_btm_lbl, trail_dn)
  839. label.set_text(extend_btm_lbl, trend > 0 ? 'Strong Low' : 'Weak Low')
  840.  
  841. //-----------------------------------------------------------------------------}
  842. //Order Blocks Arrays
  843. //-----------------------------------------------------------------------------{
  844. var iob_top = array.new_float(0)
  845. var iob_btm = array.new_float(0)
  846. var iob_left = array.new_int(0)
  847. var iob_type = array.new_int(0)
  848.  
  849. var ob_top = array.new_float(0)
  850. var ob_btm = array.new_float(0)
  851. var ob_left = array.new_int(0)
  852. var ob_type = array.new_int(0)
  853.  
  854. //-----------------------------------------------------------------------------}
  855. //Pivot High BOS/CHoCH
  856. //-----------------------------------------------------------------------------{
  857. //Filtering
  858. var bull_concordant = true
  859.  
  860. if ifilter_confluence
  861. bull_concordant := high - math.max(close, open) > math.min(close, open - low)
  862.  
  863. //Detect internal bullish Structure
  864. if ta.crossover(close, itop_y) and itop_cross and top_y != itop_y and bull_concordant
  865. bool choch = na
  866.  
  867. if itrend < 0
  868. choch := true
  869. bull_ichoch_alert := true
  870. else
  871. bull_ibos_alert := true
  872.  
  873. txt = choch ? 'CHoCH' : 'BOS'
  874.  
  875. if show_internals
  876. if show_ibull == 'All' or (show_ibull == 'BOS' and not choch) or (show_ibull == 'CHoCH' and choch)
  877. display_Structure(itop_x, itop_y, txt, ibull_css, true, true, internal_structure_lbl_size)
  878.  
  879. itop_cross := false
  880. itrend := 1
  881.  
  882.  
  883. //Detect bullish Structure
  884. if ta.crossover(close, top_y) and top_cross
  885. bool choch = na
  886.  
  887. if trend < 0
  888. choch := true
  889. bull_choch_alert := true
  890. else
  891. bull_bos_alert := true
  892.  
  893. txt = choch ? 'CHoCH' : 'BOS'
  894.  
  895. if show_Structure
  896. if show_bull == 'All' or (show_bull == 'BOS' and not choch) or (show_bull == 'CHoCH' and choch)
  897. display_Structure(top_x, top_y, txt, bull_css, false, true, swing_structure_lbl_size)
  898.  
  899.  
  900. top_cross := false
  901. trend := 1
  902.  
  903. //-----------------------------------------------------------------------------}
  904. //Pivot Low BOS/CHoCH
  905. //-----------------------------------------------------------------------------{
  906. var bear_concordant = true
  907.  
  908. if ifilter_confluence
  909. bear_concordant := high - math.max(close, open) < math.min(close, open - low)
  910.  
  911. //Detect internal bearish Structure
  912. if ta.crossunder(close, ibtm_y) and ibtm_cross and btm_y != ibtm_y and bear_concordant
  913. bool choch = false
  914.  
  915. if itrend > 0
  916. choch := true
  917. bear_ichoch_alert := true
  918. else
  919. bear_ibos_alert := true
  920.  
  921. txt = choch ? 'CHoCH' : 'BOS'
  922.  
  923. if show_internals
  924. if show_ibear == 'All' or (show_ibear == 'BOS' and not choch) or (show_ibear == 'CHoCH' and choch)
  925. display_Structure(ibtm_x, ibtm_y, txt, ibear_css, true, false, internal_structure_lbl_size)
  926.  
  927. ibtm_cross := false
  928. itrend := -1
  929.  
  930.  
  931.  
  932. //Detect bearish Structure
  933. if ta.crossunder(close, btm_y) and btm_cross
  934. bool choch = na
  935.  
  936. if trend > 0
  937. choch := true
  938. bear_choch_alert := true
  939. else
  940. bear_bos_alert := true
  941.  
  942. txt = choch ? 'CHoCH' : 'BOS'
  943.  
  944. if show_Structure
  945. if show_bear == 'All' or (show_bear == 'BOS' and not choch) or (show_bear == 'CHoCH' and choch)
  946. display_Structure(btm_x, btm_y, txt, bear_css, false, false, swing_structure_lbl_size)
  947.  
  948.  
  949. btm_cross := false
  950. trend := -1
  951.  
  952.  
  953. //-----------------------------------------------------------------------------}
  954. //Fair Value Gaps
  955. //-----------------------------------------------------------------------------{
  956. var bullish_fvg_max = array.new_box(0)
  957. var bullish_fvg_min = array.new_box(0)
  958.  
  959. var bearish_fvg_max = array.new_box(0)
  960. var bearish_fvg_min = array.new_box(0)
  961.  
  962. float bullish_fvg_avg = na
  963. float bearish_fvg_avg = na
  964.  
  965. bullish_fvg_cnd = false
  966. bearish_fvg_cnd = false
  967.  
  968. //Trend
  969. //-----------------------------------------------------------------------------{
  970. var color trend_css = na
  971.  
  972. if show_trend
  973. if style == 'Colored'
  974. trend_css := itrend == 1 ? bull_css : bear_css
  975. else if style == 'Monochrome'
  976. trend_css := itrend == 1 ? #b2b5be : #5d606b
  977.  
  978. plotcandle(open, high, low, close
  979. , color = trend_css
  980. , wickcolor = trend_css
  981. , bordercolor = trend_css
  982. , editable = false)
  983.  
  984.  
  985. filter(float src, int len) =>
  986. var float filter = na
  987. filter := ta.cum((src + (src[1] * 2) + (src[2] * 2) + src[3])/6)
  988. (filter - filter[len])/len
  989.  
  990. rsi(src, len) =>
  991. rsi = ta.rsi(filter(src, 1), len)
  992. f = -math.pow(math.abs(math.abs(rsi - 50) - 50), 1 + math.pow(len / 14, 0.618) - 1) / math.pow(50, math.pow(len / 14, 0.618) - 1) + 50
  993. rsia = if rsi > 50
  994. f + 50
  995. else
  996. -f + 50
  997. rsia
  998.  
  999.  
  1000.  
  1001. LL = ta.crossover(basis, topl) and s2 >= 0
  1002.  
  1003. SS = ta.crossunder(basis, bottl) and s2 <= 0
  1004.  
  1005.  
  1006. if (LL)
  1007. trend := 1
  1008. else if (SS)
  1009. trend := -1
  1010.  
  1011. var lastSignal = 0
  1012.  
  1013. longCondition = trend == 1 and LL and lastSignal != 1
  1014. shortCondition = trend == -1 and SS and lastSignal != -1
  1015.  
  1016.  
  1017. if (longCondition)
  1018. lastSignal := 1
  1019. alert("Open Long")
  1020. if (shortCondition)
  1021. lastSignal := -1
  1022. alert("Open Short")
  1023.  
  1024. if close > topl and (bull_choch_alert or bull_bos_alert)
  1025. alert("Bullish Choc/Bos in a long. Consider adding to your position")
  1026. if close < bottl and (bull_choch_alert or bull_bos_alert)
  1027. alert("Bullish Choc/Bos in a short. Consider closing your position")
  1028.  
  1029. if close > topl and (bear_choch_alert or bear_bos_alert)
  1030. alert("Bearish Choc/Bos in a long. Consider closing your position")
  1031. if close < bottl and (bear_choch_alert or bear_bos_alert)
  1032. alert("Bullish Choc/Bos in a short. Consider adding to your position")
  1033.  
  1034. plotshape(shortCondition, location=location.abovebar, style=shape.labeldown, color= #ff0062, size=size.tiny, text='Sell', textcolor=color.new(#ffffff, 0))
  1035. plotshape(longCondition, location=location.belowbar, style=shape.labelup, color= #00c3ff, size=size.tiny, text='Buy', textcolor=color.new(#000000, 0))
  1036. plotcandle(open, high, low, close, color=c_color, wickcolor = c_color, bordercolor = c_color, title = "Momentum Candles")
  1037.  
  1038. //alertcondition(longCondition, title='**Long**')
  1039. //alertcondition(shortCondition, title='**Short**')
  1040.  
  1041. //alertcondition(bull_bos_alert, 'Bullish BOS', 'Internal Bullish BOS formed')
  1042. //alertcondition(bull_choch_alert, 'Bullish CHoCH', 'Internal Bullish CHoCH formed')
  1043.  
  1044. //alertcondition(bear_bos_alert, 'Bearish BOS', 'Bearish BOS formed')
  1045. //alertcondition(bear_choch_alert, 'Bearish CHoCH', 'Bearish CHoCH formed')
  1046.  
Add Comment
Please, Sign In to add comment