xmd79

Session Liquidity

Dec 15th, 2022
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.25 KB | None | 0 0
  1. // © Infinity_Trading_
  2. // Last Modified: 12/15/2022
  3.  
  4. //@version=5
  5. indicator(title="Session Liquidity", shorttitle="Session Liquidity", overlay=true)
  6.  
  7. // Description:
  8. //
  9. // The “Session Liquidity” TradingView indicator by Infinity Trading creates dynamic horizontal lines at the high and low points of a specified time span within the trading day.
  10. // This indicator gives the user control of three separate time spans so the user can dynamically see the highs and lows of their favorite daily time spans.
  11.  
  12. // Purpose:
  13. //
  14. // This indicator is similar to my TradingView indicator “Futures Exchange Sessions 3.0”.
  15. // In that indicator the user gets control of dynamic price boxes. For me, these boxes made it difficult to spot ICT’s Orderblocks.
  16. // So instead of boxes I made independently controllable lines and now I can spot ICT Orderblocks and easily identify Liquidity Pools.
  17.  
  18. // Special Notes:
  19. //
  20. // If the Line Start Time is within one candle of the Start Day Timezone in the Settings, then the line/box won’t display. So choose the previous timezone
  21. // Lines only display when the timeframe is <= 30 minute
  22.  
  23.  
  24. ///////////////////////////////////////////////////////////////////////////////// CONSTANTS
  25.  
  26. c_milliseconds_in_day = 24 * 60 * 60 * 1000
  27. c_tooltip_1 = "If the user increments/decrements this integer field and nothing happens, that is because the user is moving throught a weekend or holiday. Please continue incrementing/decrementing and after 2-3 clicks the user will see backgrounds/labels appear or disappear. The algorithm for Days Back is counting every 24-hour period which is why this behavior occurs."
  28. c_tooltip_2 = "To make the lines start over each day the code must know when to start the day. Since the user is entering times, a static start time won't work. Use midnight EST in the three timezone options as the starting point. If the users lines fits in the Asian Session then choose Asia/Shanghai. If the lines are within the London Session then choose Europe/London. And the same goes for the New York Session."
  29. c_tooltip_3 = "Please specify a start & end time within the same 'day' where the 'day' starts at midnight of the timezone selected by the user in the dropdown Start Day Timezone"
  30.  
  31.  
  32. ///////////////////////////////////////////////////////////////////////////////// INPUTS
  33.  
  34.  
  35. var string GDTB = "Limit Past Lines"
  36. i_display_days_back = input.int(defval=15, minval=1, maxval=80, title="Display Only 🔢 Days Back", group=GDTB, tooltip=c_tooltip_1)
  37.  
  38.  
  39. var string L0 = "First Session Lines"
  40. i_1st_session_high_line_toggle = input.bool(defval=true, title="High Line On/Off      ", group=L0, inline="high-1")
  41. i_1st_session_high_label_toggle = input.bool(defval=true, title="High Label On/Off", group=L0, inline="high-1")
  42. i_1st_session_low_line_toggle = input.bool(defval=true, title="Low Line On/Off      ", group=L0, inline="low-1")
  43. i_1st_session_low_label_toggle = input.bool(defval=true, title="Low Label On/Off", group=L0, inline="low-1")
  44. i_1st_session_start_day = input.string(defval="Asia/Shanghai", title="Line Start Day Timezone", options=["America/New_York", "Europe/London", "Asia/Shanghai"], group=L0, tooltip=c_tooltip_2)
  45. i_1st_session_time = input.session(defval='1900-0259', title="Line Start & End Times", group=L0, tooltip=c_tooltip_3)
  46. i_1st_session_text_char = input.string(defval="First Session", title="Text Style     ", group=L0, inline="text1")
  47. i_1st_session_text_color = input.color(defval=color.new(#131722, 0), title="", group=L0, inline="text1")
  48. i_1st_session_text_size = input.string(defval="Normal", title="", options=["Tiny", "Small", "Normal", "Large", "Huge", "Auto"], group=L0, inline="text1")
  49. i_1st_session_line_high_color = input.color(defval=color.new(#00bcd4, 0), title="Line Style - High  ", group=L0, inline="line-high1")
  50. i_1st_session_line_high_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=L0, inline="line-high1")
  51. i_1st_session_line_high_width = input.int(defval=2, title="", minval=1, maxval=20, group=L0, inline="line-high1")
  52. i_1st_session_line_low_color = input.color(defval=color.new(#00bcd4, 0), title="Line Style - Low  ", group=L0, inline="line-low1")
  53. i_1st_session_line_low_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=L0, inline="line-low1")
  54. i_1st_session_line_low_width = input.int(defval=2, title="", minval=1, maxval=20, group=L0, inline="line-low1")
  55.  
  56.  
  57. var string L2 = "Second Session Lines"
  58. i_2nd_session_high_line_toggle = input.bool(defval=true, title="High Line On/Off      ", group=L2, inline="high-2")
  59. i_2nd_session_high_label_toggle = input.bool(defval=true, title="High Label On/Off", group=L2, inline="high-2")
  60. i_2nd_session_low_line_toggle = input.bool(defval=true, title="Low Line On/Off      ", group=L2, inline="low-2")
  61. i_2nd_session_low_label_toggle = input.bool(defval=true, title="Low Label On/Off", group=L2, inline="low-2")
  62. i_2nd_session_start_day = input.string(defval="Europe/London", title="Line Start Day Timezone", options=["America/New_York", "Europe/London", "Asia/Shanghai"], group=L2, tooltip=c_tooltip_2)
  63. i_2nd_session_time = input.session(defval='0300-0929', title="Line Start & End Times", group=L2, tooltip=c_tooltip_3)
  64. i_2nd_session_text_char = input.string(defval="Second Session", title="Text Style     ", group=L2, inline="text2")
  65. i_2nd_session_text_color = input.color(defval=color.new(#131722, 0), title="", group=L2, inline="text2")
  66. i_2nd_session_text_size = input.string(defval="Normal", title="", options=["Tiny", "Small", "Normal", "Large", "Huge", "Auto"], group=L2, inline="text2")
  67. i_2nd_session_line_high_color = input.color(defval=color.new(#f2a03a, 0), title="Line Style - High  ", group=L2, inline="line-high2")
  68. i_2nd_session_line_high_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=L2, inline="line-high2")
  69. i_2nd_session_line_high_width = input.int(defval=2, title="", minval=1, maxval=20, group=L2, inline="line-high2")
  70. i_2nd_session_line_low_color = input.color(defval=color.new(#f2a03a, 0), title="Line Style - Low  ", group=L2, inline="line-low2")
  71. i_2nd_session_line_low_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=L2, inline="line-low2")
  72. i_2nd_session_line_low_width = input.int(defval=2, title="", minval=1, maxval=20, group=L2, inline="line-low2")
  73.  
  74.  
  75. var string L3 = "Third Session Lines"
  76. i_3rd_session_high_line_toggle = input.bool(defval=false, title="High Line On/Off      ", group=L3, inline="high-3")
  77. i_3rd_session_high_label_toggle = input.bool(defval=false, title="High Label On/Off", group=L3, inline="high-3")
  78. i_3rd_session_low_line_toggle = input.bool(defval=false, title="Low Line On/Off      ", group=L3, inline="low-3")
  79. i_3rd_session_low_label_toggle = input.bool(defval=false, title="Low Label On/Off", group=L3, inline="low-3")
  80. i_3rd_session_start_day = input.string(defval="America/New_York", title="Line Start Day Timezone", options=["America/New_York", "Europe/London", "Asia/Shanghai"], group=L3, tooltip=c_tooltip_2)
  81. i_3rd_session_time = input.session(defval='0930-1559', title="Line Start & End Times", group=L3, tooltip=c_tooltip_3)
  82. i_3rd_session_text_char = input.string(defval="Third Session", title="Text Style     ", group=L3, inline="text3")
  83. i_3rd_session_text_color = input.color(defval=color.new(#131722, 0), title="", group=L3, inline="text3")
  84. i_3rd_session_text_size = input.string(defval="Normal", title="", options=["Tiny", "Small", "Normal", "Large", "Huge", "Auto"], group=L3, inline="text3")
  85. i_3rd_session_line_high_color = input.color(defval=color.new(#467deb, 0), title="Line Style - High  ", group=L3, inline="line-high3")
  86. i_3rd_session_line_high_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=L3, inline="line-high3")
  87. i_3rd_session_line_high_width = input.int(defval=2, title="", minval=1, maxval=20, group=L3, inline="line-high3")
  88. i_3rd_session_line_low_color = input.color(defval=color.new(#467deb, 0), title="Line Style - Low  ", group=L3, inline="line-low3")
  89. i_3rd_session_line_low_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group=L3, inline="line-low3")
  90. i_3rd_session_line_low_width = input.int(defval=2, title="", minval=1, maxval=20, group=L3, inline="line-low3")
  91.  
  92.  
  93. //------------------------------------
  94. // VARIABLES
  95. //------------------------------------
  96.  
  97. // See if a new calendar day started on the intra-day time frame
  98. // with variable timezone
  99.  
  100. newDayFirstStartLine = dayofmonth(time, i_1st_session_start_day) != dayofmonth(time, i_1st_session_start_day)[1] and timeframe.isintraday
  101.  
  102. newDaySecondStartLine = dayofmonth(time, i_2nd_session_start_day) != dayofmonth(time, i_2nd_session_start_day)[1] and timeframe.isintraday
  103.  
  104. newDayThirdStartLine = dayofmonth(time, i_3rd_session_start_day) != dayofmonth(time, i_3rd_session_start_day)[1] and timeframe.isintraday
  105.  
  106.  
  107. ///////////////////////////////////////////////////////////////////////////////// DAYS BACK
  108.  
  109. // Find the last bar on the chart
  110. lastBarDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), hour(timenow), minute(timenow), second(timenow))
  111. // Find the current bar that is being processed
  112. thisBarDate = timestamp(year, month, dayofmonth, hour, minute, second)
  113. // Find the number of days between these two bars
  114. daysLeft = math.floor((lastBarDate - thisBarDate) / c_milliseconds_in_day)
  115. // Only display the number of days specified in Input Settings
  116. in_days_back_range = daysLeft < i_display_days_back
  117.  
  118.  
  119. ///////////////////////////////////////////////////////////////////////////////// HELPER FUNCTIONS
  120.  
  121.  
  122. // Function the converts string inputs to line style code
  123. line_style_function(input_var) =>
  124. switch input_var
  125. "dotted (┈)" => line.style_dotted
  126. "dashed (╌)" => line.style_dashed
  127. => line.style_solid
  128.  
  129.  
  130. // Function that converts string inputs to text size code
  131. text_size_function(string_size) =>
  132. switch string_size
  133. "Tiny" => size.tiny
  134. "Small" => size.small
  135. "Normal" => size.normal
  136. "Large" => size.large
  137. "Huge" => size.huge
  138. "Auto" => size.auto
  139.  
  140.  
  141.  
  142. ///////////////////////////////////////////////////////////////////////////////// MAIN FUNCTION
  143.  
  144. // Creates lines and labels accoriding to settings
  145. session_liquidity_lines_function(HIGH_LINE_TOGGLE, LOW_LINE_TOGGLE, HIGH_LABEL_TOGGLE, LOW_LABEL_TOGGLE, SESSION_TIME, DAYS_BACK, NEW_DAY, LINE_HIGH_COLOR, LINE_HIGH_STYLE, LINE_HIGH_WIDTH, LINE_LOW_COLOR, LINE_LOW_STYLE, LINE_LOW_WIDTH, TEXT_COLOR, TEXT_CHAR, TEXT_SIZE) =>
  146.  
  147. // Time period variables
  148. session_est_window = time(timeframe.period, SESSION_TIME, "America/New_York")
  149. // Parse start hour and start minutes from session string
  150. session_start_hour = str.substring(SESSION_TIME, 0, 2)
  151. session_start_minutes = str.substring(SESSION_TIME, 2, 4)
  152. // Use time hour and minute to create boolean for time
  153. session_start_time = (hour(time, "America/New_York") == str.tonumber(session_start_hour)) and (minute(time) == str.tonumber(session_start_minutes))
  154.  
  155. // Line Style functions
  156. session_line_high_style = line_style_function(LINE_HIGH_STYLE)
  157. session_line_low_style = line_style_function(LINE_LOW_STYLE)
  158. // Text size function
  159. label_text_size = text_size_function(TEXT_SIZE)
  160.  
  161. // Instantiate variables
  162. var float session_high = 0.0
  163. var float session_low = 0.0
  164. var line session_line_low = na
  165. var line session_line_high = na
  166. var label session_label_low = na
  167. var label session_label_high = na
  168. var float price_session_line_low = na
  169. var float price_session_line_high = na
  170.  
  171. standardized_calc = ta.stdev(close, 10) / 2
  172.  
  173. // Only display the specified number of days back
  174. if DAYS_BACK
  175.  
  176. // Toggle NY Session box on/off
  177. // Only display when timeframe is 30 minutes or less
  178. if timeframe.isminutes and timeframe.multiplier < 60
  179.  
  180. // If a new day starts, create initial lines
  181. if NEW_DAY
  182.  
  183. if HIGH_LINE_TOGGLE
  184. session_line_high := line.new(x1=na, y1=na, x2=na, y2=na, color=LINE_HIGH_COLOR, style=session_line_high_style, width=LINE_HIGH_WIDTH)
  185. if LOW_LINE_TOGGLE
  186. session_line_low := line.new(x1=na, y1=na, x2=na, y2=na, color=LINE_LOW_COLOR, style=session_line_low_style, width=LINE_LOW_WIDTH)
  187.  
  188. // Dummy variable to trick pine script. Both sides of the conditional must return the same object type
  189. pine_script = 5
  190.  
  191. // If not new day, then update the highest high and lowest low values and update the lines
  192. else
  193.  
  194. // When time cross the start time, create the line with updated highs and lows for the first time
  195. if session_start_time
  196. session_high := high
  197. session_low := low
  198.  
  199. if HIGH_LINE_TOGGLE
  200. // Update high line with initial x's and y's
  201. line.set_x1(session_line_high, bar_index)
  202. line.set_y1(session_line_high, session_high)
  203. line.set_x2(session_line_high, bar_index + 1)
  204. line.set_y2(session_line_high, session_high)
  205.  
  206. if LOW_LINE_TOGGLE
  207. // Update low line with initial x's and y's
  208. line.set_x1(session_line_low, bar_index)
  209. line.set_y1(session_line_low, session_low)
  210. line.set_x2(session_line_low, bar_index + 1)
  211. line.set_y2(session_line_low, session_low)
  212.  
  213. // At every new candle during specified time span, update y1, x2, y2 values to move the lines up or down and to the right
  214. if session_est_window
  215. session_high := math.max(session_high, high)
  216. session_low := math.min(session_low, low)
  217.  
  218. if HIGH_LINE_TOGGLE
  219. // Don't need to update x1
  220. line.set_y1(session_line_high, session_high)
  221. line.set_x2(session_line_high, bar_index + 1)
  222. line.set_y2(session_line_high, session_high)
  223.  
  224. if LOW_LINE_TOGGLE
  225. // Don't need to update x1
  226. line.set_y1(session_line_low, session_low)
  227. line.set_x2(session_line_low, bar_index + 1)
  228. line.set_y2(session_line_low, session_low)
  229.  
  230. // Draw label only for most recent line
  231. // Calculation to center label on line
  232. x_middle = (line.get_x2(session_line_low) - line.get_x1(session_line_low)) / 2
  233.  
  234. if HIGH_LABEL_TOGGLE
  235. price_session_line_high := line.get_price(session_line_high, bar_index)
  236. session_label_high := label.new(x=bar_index - x_middle, y=price_session_line_high, text=TEXT_CHAR, xloc=xloc.bar_index, color=color.new(#131722, 100), textcolor=TEXT_COLOR, tooltip=str.tostring(price_session_line_high), size = label_text_size)
  237. // Delete all pasts labels except the most recent one
  238. label.delete(session_label_high[1])
  239.  
  240. if LOW_LABEL_TOGGLE
  241. // Get y value (price) of midnight open line
  242. price_session_line_low := line.get_price(session_line_low, bar_index)
  243. session_label_low := label.new(x=bar_index - x_middle, y=price_session_line_low - standardized_calc, text=TEXT_CHAR, xloc=xloc.bar_index, color=color.new(#131722, 100), textcolor=TEXT_COLOR, tooltip=str.tostring(price_session_line_low), size = label_text_size)
  244. // Delete all pasts labels except the most recent one
  245. label.delete(session_label_low[1])
  246.  
  247. // Dummy variable to trick pine script. Both sides of the conditional must return the same object type
  248. pine_script = 6
  249.  
  250.  
  251.  
  252. //------------------------------------
  253. // 1st SESSION LIQUIDITY
  254. //------------------------------------
  255.  
  256.  
  257. session_liquidity_lines_function(i_1st_session_high_line_toggle, i_1st_session_low_line_toggle, i_1st_session_high_label_toggle, i_1st_session_low_label_toggle, i_1st_session_time, in_days_back_range, newDayFirstStartLine, i_1st_session_line_high_color, i_1st_session_line_high_style, i_1st_session_line_high_width, i_1st_session_line_low_color, i_1st_session_line_low_style, i_1st_session_line_low_width, i_1st_session_text_color, i_1st_session_text_char, i_1st_session_text_size)
  258.  
  259.  
  260. //------------------------------------
  261. // 2nd SESSION LIQUIDITY
  262. //------------------------------------
  263.  
  264.  
  265. session_liquidity_lines_function(i_2nd_session_high_line_toggle, i_2nd_session_low_line_toggle, i_2nd_session_high_label_toggle, i_2nd_session_low_label_toggle, i_2nd_session_time, in_days_back_range, newDaySecondStartLine, i_2nd_session_line_high_color, i_2nd_session_line_high_style, i_2nd_session_line_high_width, i_2nd_session_line_low_color, i_2nd_session_line_low_style, i_2nd_session_line_low_width, i_2nd_session_text_color, i_2nd_session_text_char, i_2nd_session_text_size)
  266.  
  267.  
  268. //------------------------------------
  269. // 3rd SESSION LIQUIDITY
  270. //------------------------------------
  271.  
  272.  
  273. session_liquidity_lines_function(i_3rd_session_high_line_toggle, i_3rd_session_low_line_toggle, i_3rd_session_high_label_toggle, i_3rd_session_low_label_toggle, i_3rd_session_time, in_days_back_range, newDayThirdStartLine, i_3rd_session_line_high_color, i_3rd_session_line_high_style, i_3rd_session_line_high_width, i_3rd_session_line_low_color, i_3rd_session_line_low_style, i_3rd_session_line_low_width, i_3rd_session_text_color, i_3rd_session_text_char, i_3rd_session_text_size)
  274.  
  275.  
  276. /////////////////////////////////////////////////////////////////////////////////
  277.  
Advertisement
Add Comment
Please, Sign In to add comment