xmd79

Session TPO Profile

Apr 30th, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © noop42
  3.  
  4. //@version=5
  5. indicator("Session TPO Profile", overlay=true, max_bars_back=5000, max_boxes_count = 500, max_lines_count = 500)
  6.  
  7. session = input.session("0930-1600", title="Session calculation", group="Session configuration")
  8.  
  9. tick_size = input.int(25, "Ticks number per tpo", group="Global Options") * syminfo.mintick
  10. round_values = input.bool(true, "Round TPO levels on Tick number", group="Global Options")
  11. letters_size = input.string("Small", options=["Normal", "Small", "Tiny"], title="TPO letters size", group="Global Options")
  12. print_as_bars = input.bool(false, "Display TPO as bars", group="Global Options")
  13. extend_levels = input.bool(true, "Extend yesterday's levels", group="Global Options")
  14. show_va_background = input.bool(true, "Show Market Profile calculation area background", group="Global Options")
  15. va_bg_color=input.color(#2962ff10, "Value area background color", group="Global Options")
  16. show_histogram = input.bool(true, "Show TPO's", group="Global Options")
  17. tpo_va_color = input.color(color.white, "TPO Color (in VA)", group="Global Options")
  18. tpo_nova_color = input.color(color.gray, "TPO Color (out of VA)", group="Global Options")
  19. limits_color=input.color(color.black, "High and Low colors", group="Highs & Lows")
  20. show_high_low = input.bool(false, "Show high/low levels", group="Highs & Lows")
  21.  
  22. poc_col = input.color(#b58900, "POC color", group="POC Options")
  23. show_poc = input.bool(true, "Show POC as line", group="POC Options")
  24. show_initial_balance = input.bool(true, "Show Initial Balance", group="IB")
  25. ib_color = input.color(#38f40e, "IB Color", group="IB")
  26.  
  27. va_line_color = input.color(#2962ff, "Value area lines color", group="Value area options")
  28. show_va_lines = input.bool(true, "Show value areas as lines",group="Value area options")
  29. va_percent = input.int(68, "Value area percent",group="Value area options") / 100
  30.  
  31. show_singleprints = input.bool(true, "Highlight Single prints", group="Single prints")
  32. sp_col = input.color(#ec407a, "Single prints color", group="Single prints")
  33. extend_sp = input.bool(false, "Extend single prints", group="Single prints")
  34. sp_ext_trans = input.int(80, "Extended single prints transparency", group="Single prints", minval=0, maxval=100)
  35. open_color = input.color(color.yellow, "Opening price color", group="Open/Close")
  36. close_color = input.color(#ff0000, "Closing price color", group="Open/Close")
  37.  
  38. tpo_spacing = input.int(0, "TPO Spacing")
  39.  
  40. inSession(sess) => na(time(timeframe.period, sess)) == false
  41. sess = inSession(session)
  42. session_over = sess[1] and not sess
  43. session_start = sess and not sess[1]
  44.  
  45. letters = input.string("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@$€£{}[]()*+-/=%&?!", "TPO letters")
  46.  
  47. letters_size_value = letters_size == "Small" ? size.small : letters_size == "Normal" ? size.normal : size.tiny
  48.  
  49. bars_nb = 0
  50.  
  51. var line topline = na
  52. var line botline = na
  53. var line vah_line = na
  54. var line val_line = na
  55. var line poc = na
  56.  
  57. //var max_score = 0
  58. var poc_price = 0.0
  59. var bars_score = array.new<int>(bars_nb)
  60. var profile_bars = array.new<box>(bars_nb)
  61. var letter_bars = array.new<string>(bars_nb)
  62. var vah_offset = 0
  63. var val_offset = 0
  64.  
  65. spaces_string(n) =>
  66. r = ""
  67. if n > 0
  68. for i=0 to n
  69. r += " "
  70. r
  71.  
  72. find_h(x) =>
  73. h = high
  74. for i=0 to x
  75. if high[i] > h
  76. h := high[i]
  77. h
  78. find_l(x) =>
  79. l = low
  80. for i=0 to x
  81. if low[i] < l
  82. l := low[i]
  83. l
  84. count_tpo(x, y) =>
  85. r = 0
  86. for i=x to y
  87. r += array.get(bars_score,i)
  88. r
  89.  
  90. biggest_tpo(x, y) =>
  91. t1 = array.get(bars_score, x)
  92. t2 = array.get(bars_score, y)
  93. t1 >= t2 ? "h": "l"
  94.  
  95. var session_candles_count = 0
  96. var poc_position = 0
  97. var total_tpo = 0
  98. var open_price = open
  99.  
  100. if session_start
  101. session_candles_count := 0
  102. open_price := open
  103. //if candle_offset == candles_nb -1
  104. if session_over
  105. ext = extend_levels ? extend.right: extend.none
  106. if extend_levels
  107. line.set_extend(vah_line, extend.none)
  108. line.set_extend(val_line, extend.none)
  109. line.set_extend(poc, extend.none)
  110. line.set_extend(topline, extend.none)
  111. line.set_extend(botline, extend.none)
  112.  
  113. max_score = 0
  114. total_tpo := 0
  115. h = find_h(session_candles_count)
  116. highest=h
  117. l = find_l(session_candles_count)
  118. bars_nb := int((h-l) / tick_size)
  119. if round_values
  120. h := h+tick_size - (h % tick_size)
  121. l := l - (l % tick_size)
  122. box_width = (h-l) / bars_nb
  123.  
  124. if show_poc
  125. poc := line.new(bar_index-session_candles_count, 0.0, bar_index, 0.0, color=poc_col, width=2, extend=ext)
  126. if show_high_low
  127. topline := line.new(bar_index-session_candles_count, h, bar_index, h, color=limits_color, extend=ext)
  128. botline := line.new(bar_index-session_candles_count, l, bar_index, l, color=limits_color, extend=ext)
  129.  
  130. // TPO calculation
  131. for i=0 to bars_nb-1
  132. tpo_letters = ""
  133. score = 0
  134. for x=0 to session_candles_count
  135. in_bar = (high[x] <= h and high[x] >= h-box_width) or (low[x] <= h and low[x] >= h-box_width) or (high[x] > h and low[x] < h-box_width)
  136. if in_bar
  137. score += 1
  138. if x <= session_candles_count and session_candles_count <= str.length(letters) -1
  139. tpo_letters := str.substring(letters, session_candles_count-(x), (session_candles_count - (x-1)))+spaces_string(tpo_spacing)+tpo_letters
  140.  
  141. if score >= max_score
  142. max_score := score
  143. poc_price := h - (box_width/2)
  144. poc_position := i
  145.  
  146. line.set_y1(poc, poc_price)
  147. line.set_y2(poc, poc_price)
  148. if print_as_bars
  149. tpo_letters := ""
  150. array.insert(profile_bars,i, box.new(bar_index-session_candles_count, h, bar_index-(session_candles_count-score), h-box_width, text=tpo_letters, text_size=letters_size_value, text_color=tpo_va_color, text_halign=text.align_left, bgcolor=#00000000, border_color=#00000000))
  151. array.insert(bars_score, i, score)
  152. h -= box_width
  153. total_tpo += score
  154.  
  155. // VA Calculation
  156. vah_price = 0.0
  157. val_price = 0.0
  158. vah_offset := poc_position > 0 ? poc_position -1 : poc_position
  159. val_offset := poc_position < bars_nb -1 ? poc_position +1 : poc_position
  160.  
  161. for i=0 to bars_nb-1
  162. d = vah_offset == 0 ? "l" : val_offset == bars_nb-1 ? "h" : biggest_tpo(vah_offset, val_offset)
  163. if d == "h"
  164. vah_offset := vah_offset > 0 ? vah_offset -1 : vah_offset
  165. else
  166. val_offset := val_offset < bars_nb-1 ? val_offset +1 : val_offset
  167. if count_tpo(vah_offset, val_offset) / total_tpo >= va_percent
  168. break
  169.  
  170. vah_price := box.get_top(array.get(profile_bars, vah_offset))-(box_width/2)
  171. val_price := box.get_bottom(array.get(profile_bars, val_offset))+(box_width/2)
  172.  
  173. // Set tpo colors
  174. for i=0 to bars_nb-1
  175. bar_col = #00000000
  176. text_col = (i > vah_offset and i < val_offset ? tpo_va_color : tpo_nova_color)
  177. text_col := val_offset == i or vah_offset == i ? va_line_color : poc_position == i ? poc_col : (array.get(bars_score, i) == 1 and show_singleprints) ? sp_col : text_col
  178. if not show_histogram
  179. text_col := #00000000
  180. if print_as_bars
  181. if not extend_sp
  182. box.set_border_color(array.get(profile_bars, i), text_col)
  183. box.set_bgcolor(array.get(profile_bars, i), text_col)
  184. if extend_sp and (array.get(bars_score, i) == 1 and show_singleprints)
  185. extended_sp_color = color.new(sp_col, transp=sp_ext_trans)
  186. box.set_right(array.get(profile_bars, i), bar_index)
  187. box.set_bgcolor(array.get(profile_bars, i), extended_sp_color)
  188.  
  189. box.set_text_color(array.get(profile_bars, i), text_col)
  190.  
  191. //plot vah/val
  192. if show_va_lines
  193. vah_line := line.new(bar_index-session_candles_count, vah_price, bar_index, vah_price, color=va_line_color, extend=ext)
  194. val_line := line.new(bar_index-session_candles_count, val_price, bar_index, val_price, color=va_line_color, extend=ext)
  195.  
  196. label.new(bar_index-session_candles_count-1, open_price, text="🢒", style=label.style_none, textcolor=open_color)
  197. label.new(bar_index-session_candles_count-1, close, text="x", style=label.style_none, textcolor=close_color)
  198.  
  199. line.new(bar_index-session_candles_count-1, open_price, bar_index-session_candles_count, open_price, color=open_color, width=3)
  200. line.new(bar_index-session_candles_count-1, close, bar_index-session_candles_count, close, color=close_color, width=3)
  201.  
  202. ibh = high[session_candles_count] > high[session_candles_count-1] ? high[session_candles_count] : high[session_candles_count-1]
  203. ibl = low[session_candles_count] < low[session_candles_count-1] ? low[session_candles_count] : low[session_candles_count-1]
  204. line.new(bar_index-session_candles_count, ibh, bar_index-session_candles_count, ibl, color=ib_color, width=2)
  205. if show_va_background
  206. box.new(bar_index-session_candles_count, vah_price, bar_index, val_price, bgcolor=va_bg_color, border_color=#00000000)
  207. label.new(bar_index-(session_candles_count/2), highest,text=str.tostring(total_tpo)+" TPO\n", style=label.style_none, textcolor=tpo_va_color, size=size.small)
  208.  
  209. session_candles_count += 1
  210.  
Add Comment
Please, Sign In to add comment