Advertisement
xmd79

StratifyTrade]Price Action Volumetric Order Blocks & Trend Strength

Feb 12th, 2024
694
3
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.62 KB | None | 3 0
  1.  
  2. //@version=5
  3. indicator("<*[StratifyTrade]Price Action Volumetric Order Blocks & Trend Strength*>","*[StratifyTrade]Price Action Volumetric Order Blocks & Trend Strength*", overlay = true, max_bars_back = 5000, max_boxes_count = 500, max_labels_count = 500, max_lines_count = 500)
  4.  
  5. const color colup = #089981
  6. const color coldn = #f23645
  7.  
  8. const string tm = "[Length] Use Length to adjust cordinate of the orderblocks\n[Full] Use whole candle body"
  9. const string tn = "Mitigation method for when to trigger order blocks"
  10. const string tj = "Order block Metrics text size"
  11. const string ta = 'Display internal buy & sell activity'
  12. const string ts = 'Show Last number of orderblocks'
  13. const string gv = "Volumetric Order Blocks"
  14.  
  15. obshow = input.bool (true , "Show Last" , ts, '1', gv)
  16. oblast = input.int (5 , "" , 0, 50, 1 , inline = '1', group = gv)
  17. obupcs = input.color (color.new(colup, 90), "" , inline = '1', group = gv)
  18. obdncs = input.color (color.new(coldn, 90), "" , inline = '1', group = gv)
  19. obshowactivity = input.bool (true , "Show Buy/Sell Activity         ", ta, '2', gv)
  20. obactup = input.color (color.new(colup, 50), "" , inline = '2', group = gv)
  21. obactdn = input.color (color.new(coldn, 50), "" , inline = '2', group = gv)
  22. obmode = input.string("Length" , "Construction " , ["Length", "Full"], tm, '3', gv)
  23. len = input.int (5 , "" , 1, 20, 1 , inline = '3', group = gv)
  24. obmiti = input.string("Close" , "Mitigation Method" , ["Close", "Wick", "Avg"], tn, group = gv)
  25. obtxt = input.string("Normal" , "Metric Size" , ["Tiny", "Small", "Normal", "Large", "Huge"], tj, group = gv)
  26. showmetric = input.bool (true , "Show Metrics" , group = gv)
  27. showline = input.bool (true , "Show Mid-Line" , group = gv)
  28. overlap = input.bool (true , "Hide Overlap" , group = gv, tooltip = "Most recent order block will be preserved")
  29.  
  30. blcreated = input.bool(false , "Bullish OB Formed      " , inline = "Formed" , group = "ANY ALERT")
  31. brcreated = input.bool(false , "Bearish OB Formed" , inline = "Formed" , group = "ANY ALERT")
  32. blmitigated = input.bool(false , "Bullish OB Mitigated   " , inline = "Mitigated" , group = "ANY ALERT")
  33. brmitigated = input.bool(false , "Bearish OB Mitigated" , inline = "Mitigated" , group = "ANY ALERT")
  34. blinside = input.bool(false , "Price Inside Bullish OB" , inline = "Inside" , group = "ANY ALERT")
  35. brinside = input.bool(false , "Price Inside Bearish OB" , inline = "Inside" , group = "ANY ALERT")
  36.  
  37.  
  38.  
  39. type bar
  40. float o = open
  41. float h = high
  42. float l = low
  43. float c = close
  44. float v = volume
  45. int i = bar_index
  46. int t = time
  47.  
  48. type ob
  49. float top
  50. float btm
  51. float avg
  52. int loc
  53. color css
  54. float vol
  55. int dir
  56. int move
  57. int blPOS
  58. int brPOS
  59. int xlocbl
  60. int xlocbr
  61.  
  62. type alert
  63. bool created = false
  64. bool inside = false
  65. bool mitigated = false
  66.  
  67. type cross
  68. bool reset = false
  69.  
  70.  
  71. bar b = bar .new()
  72. alert blal = alert.new()
  73. alert bral = alert.new()
  74.  
  75. var cross blIS = cross.new()
  76. var cross brIS = cross.new()
  77.  
  78.  
  79. method txSz(string s) =>
  80. out = switch s
  81. "Tiny" => size.tiny
  82. "Small" => size.small
  83. "Normal" => size.normal
  84. "Large" => size.large
  85. "Huge" => size.huge
  86. out
  87.  
  88.  
  89. method display(ob id, ob[] full, int i) =>
  90.  
  91. box.new (top = id.top, bottom = id.btm, left = id.loc, right = b.t , border_color = na, bgcolor = id.css, xloc = xloc.bar_time)
  92. box.new (top = id.top, bottom = id.btm, left = b.t , right = b.t + 1 , border_color = na, bgcolor = id.css, xloc = xloc.bar_time, extend = extend.right)
  93.  
  94. if obshowactivity
  95.  
  96. box.new(top = id.top, bottom = id.avg, left = id.loc, right = id.xlocbl, border_color = na, bgcolor = obactup, xloc = xloc.bar_time)
  97. box.new(top = id.avg, bottom = id.btm, left = id.loc, right = id.xlocbr, border_color = na, bgcolor = obactdn, xloc = xloc.bar_time)
  98.  
  99. if showline
  100.  
  101. line.new(
  102. x1 = id.loc
  103. , x2 = b.t
  104. , y1 = id.avg
  105. , y2 = id.avg
  106. , color = color.new(id.css, 0)
  107. , xloc = xloc.bar_time
  108. , style = line.style_dashed
  109. )
  110.  
  111.  
  112. if showmetric
  113.  
  114. if i == math.min(oblast - 1, full.size() - 1)
  115.  
  116. float tV = 0
  117. float[] dV = array.new<float>()
  118.  
  119. seq = math.min(oblast - 1, full.size() - 1)
  120.  
  121. for j = 0 to seq
  122.  
  123. cV = full.get(j)
  124.  
  125. tV += cV.vol
  126.  
  127. if j == seq
  128.  
  129. for y = 0 to seq
  130.  
  131. dV.push(
  132. math.floor(
  133. (full.get(y).vol / tV) * 100)
  134. )
  135.  
  136. id = full.get(y)
  137.  
  138. label.new(
  139. b.i + 1
  140. , id.avg
  141. , textcolor = color.new(id.css, 0)
  142. , style = label.style_label_left
  143. , size = obtxt.txSz()
  144. , color = #ffffff00
  145. , text =
  146. str.tostring(
  147. math.round(full.get(y).vol, 3), format = format.volume) + " (" + str.tostring(dV.get(y)) + "%)"
  148. )
  149.  
  150.  
  151. method overlap(ob[] id) =>
  152.  
  153. if id.size() > 1
  154.  
  155. for i = id.size() - 1 to 1
  156.  
  157. stuff = id.get(i)
  158. current = id.get(0)
  159.  
  160. switch
  161.  
  162. stuff.btm > current.btm and stuff.btm < current.top => id.remove(i)
  163. stuff.top < current.top and stuff.btm > current.btm => id.remove(i)
  164. stuff.top > current.top and stuff.btm < current.btm => id.remove(i)
  165. stuff.top < current.top and stuff.top > current.btm => id.remove(i)
  166.  
  167.  
  168. method umt(ob metric) =>
  169.  
  170. switch metric.dir
  171.  
  172. 1 =>
  173.  
  174. switch metric.move
  175.  
  176. 1 => metric.blPOS := metric.blPOS + 1, metric.move := 2
  177. 2 => metric.blPOS := metric.blPOS + 1, metric.move := 3
  178. 3 => metric.brPOS := metric.brPOS + 1, metric.move := 1
  179.  
  180. -1 =>
  181.  
  182. switch metric.move
  183.  
  184. 1 => metric.brPOS := metric.brPOS + 1, metric.move := 2
  185. 2 => metric.brPOS := metric.brPOS + 1, metric.move := 3
  186. 3 => metric.blPOS := metric.blPOS + 1, metric.move := 1
  187.  
  188. if (b.t - b.t[1]) == (b.t[1] - b.t[2])
  189.  
  190. metric.xlocbl := metric.loc + (b.t - b.t[1]) * metric.blPOS
  191. metric.xlocbr := metric.loc + (b.t - b.t[1]) * metric.brPOS
  192.  
  193.  
  194. fnOB() =>
  195.  
  196. var ob[] blob = array.new<ob>()
  197. var ob[] brob = array.new<ob>()
  198.  
  199. var int dir = 0
  200.  
  201. up = ta.highest ( len )
  202. dn = ta.lowest ( len )
  203. pv = ta.pivothigh(b.v, len, len)
  204.  
  205. dir := b.h[len] > up ? -1 : b.l[len] < dn ? 1 : dir[1]
  206.  
  207. atr = ta.atr(len)
  208.  
  209. btmP = obmode == "Length" ? (b.h[len] - 1 * atr[len]) < b.l[len] ? b.l[len] : (b.h[len] - 1 * atr[len]) : b.l[len]
  210.  
  211. topP = obmode == "Length" ? (b.l[len] + 1 * atr[len]) > b.h[len] ? b.h[len] : (b.l[len] + 1 * atr[len]) : b.h[len]
  212.  
  213. if pv and dir == 1
  214.  
  215. blob.unshift(
  216. ob.new(
  217. topP
  218. , b.l[len]
  219. , math.avg(topP, b.l[len])
  220. , b.t[len]
  221. , obupcs
  222. , b.v[len]
  223. , b.c[len] > b.o[len] ? 1 : -1
  224. , 1
  225. , 0
  226. , 0
  227. , b.t[len]
  228. )
  229. )
  230.  
  231. blal.created := true
  232. blIS.reset := false
  233.  
  234. if pv and dir == -1
  235.  
  236. brob.unshift(
  237. ob.new(
  238. b.h[len]
  239. , btmP
  240. , math.avg(btmP, b.h[len])
  241. , b.t[len]
  242. , obdncs
  243. , b.v[len]
  244. , b.c[len] > b.o[len] ? 1 : -1
  245. , 1
  246. , 0
  247. , 0
  248. , b.t[len]
  249. )
  250. )
  251.  
  252. bral.created := true
  253. brIS.reset := false
  254.  
  255. if blob.size() > 0 and barstate.isconfirmed
  256.  
  257. for [i, ob] in blob
  258.  
  259. for j = 0 to len - 1
  260.  
  261. if obmiti == "Close" ? math.min(b.c[j], b.o[j]) < ob.btm : obmiti == "Wick" ? b.l < ob.btm : obmiti == "Avg" ? b.l < ob.avg : na
  262.  
  263. blob.remove(i)
  264. blal.mitigated := true
  265. break
  266.  
  267. if brob.size() > 0 and barstate.isconfirmed
  268.  
  269. for[i, ob] in brob
  270.  
  271. for j = 0 to len - 1
  272.  
  273. if obmiti == "Close" ? math.max(b.c[j], b.o[j]) > ob.top : obmiti == "Wick" ? b.h > ob.top : obmiti == "Avg" ? b.h > ob.avg : na
  274.  
  275. brob.remove(i)
  276. bral.mitigated := true
  277. break
  278.  
  279. if blob.size() > 0
  280.  
  281. for [i, metric] in blob
  282.  
  283. metric.umt()
  284.  
  285. if brob.size() > 0
  286.  
  287. for [i, metric] in brob
  288.  
  289. metric.umt()
  290.  
  291. if overlap
  292.  
  293. blob.overlap()
  294. brob.overlap()
  295.  
  296. if barstate.isconfirmed
  297.  
  298. if blob.size() > 0
  299.  
  300. ob = blob.get(0)
  301.  
  302. if low < ob.top and blIS.reset == false
  303. blal.inside := true
  304. blIS.reset := true
  305.  
  306. if brob.size() > 0
  307.  
  308. ob = brob.get(0)
  309.  
  310. if high > ob.btm and brIS.reset == false
  311. bral.inside := true
  312. brIS.reset := true
  313.  
  314. if barstate.islast
  315.  
  316. for bx in box.all
  317. bx.delete()
  318.  
  319. for ln in line.all
  320. ln.delete()
  321.  
  322. for lb in label.all
  323. lb.delete()
  324.  
  325. if blob.size() > 0
  326. for i = 0 to math.min(oblast - 1, blob.size() - 1)
  327. blob.get(i).display(blob, i)
  328.  
  329. if brob.size() > 0
  330. for i = 0 to math.min(oblast - 1, brob.size() - 1)
  331. brob.get(i).display(brob, i)
  332.  
  333.  
  334. if obshow
  335. fnOB()
  336.  
  337.  
  338. if blinside and blal.inside
  339. alert("Price Inside Bullish OB")
  340.  
  341. if blcreated and blal.created
  342. alert("Bullish OB Formed")
  343.  
  344. if blmitigated and blal.mitigated
  345. alert("Bullish OB Mitigated")
  346.  
  347.  
  348. if brinside and bral.inside
  349. alert("Price Inside Bearish OB")
  350.  
  351. if brcreated and bral.created
  352. alert("Bearish OB Formed")
  353.  
  354. if brmitigated and bral.mitigated
  355. alert("Bearish OB Mitigated")
  356.  
  357.  
  358. //indicator("Trend Strength Gauge", 'HSMA',overlay = true)
  359. // Inputs
  360. length = input(9, "HSMA Length")
  361. color_up = input.color(color.teal, "Color Up", group = "Theme")
  362. color_dn = input.color(color.white, "Color Down", group = "Theme")
  363.  
  364. // Hma
  365. wma = ta.wma(close, length)
  366. wma1 = ta.wma(close, length)
  367. a = 3 * wma - 2 * wma1
  368.  
  369. // Sma
  370. a1 = ta.sma(close, length)
  371.  
  372. // Extract the difference (HMA - SMA)
  373. diff = a - a1
  374.  
  375. // Table Function
  376. printTable(txt, col, row, color, txt1, col1, row1, color1) => var table t = table.new(position.bottom_center, 60, 3),
  377. table.cell(t, col, row, txt, bgcolor = color),
  378. table.cell(t, col1, row1, txt1, bgcolor = color1, text_color = color.white)
  379.  
  380. // Normalize
  381. x = diff
  382. length_ = 100
  383. xMax = ta.highest(x, length_)
  384. xMin = ta.lowest(x, length_)
  385. range_ = xMax - xMin
  386. y = x / range_
  387.  
  388. // Convertation searies float to searies int with round()
  389. g = math.round(y*40)
  390.  
  391. // Plot
  392. color1 = color.from_gradient(g, -2, 2, color_dn, color_up)
  393. p1 = plot(a, 'HEMA', color1, 2)
  394. p2 = plot(a1, 'SMA', color1, 2)
  395. fill(p1, p2, color = color1)
  396.  
  397. // Convertation negative values to positive
  398. g := g < 0 ? g *- 1 : g
  399.  
  400. // Plot trend strength gauge
  401. if barstate.islast
  402. for i = 1 to 40
  403. color_ = math.round(y*40) < 0 ? color_dn : color_up
  404. color = color.from_gradient(i, 1, 50, color.rgb(0, 137, 123, 100), color_)
  405. printTable("", i, 1, color, "V", g, 1, color.rgb(255, 255, 255, 100))
  406.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement