xmd79

AltSessions

Apr 30th, 2023
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.42 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. // © KryptoGostek aka MrCode X AltSignals.io Enjoy =)
  3.  
  4. // This script is a modification of boitoki's session script. Include some fixes, new features and options.
  5.  
  6. //@version=5
  7.  
  8. indicator('AltSessions', overlay=true, max_boxes_count=220, max_labels_count=400, max_lines_count=400, max_bars_back=550)
  9.  
  10. ///////////////
  11. // Groups
  12. ///////////////
  13. g0 = 'SETTINGS'
  14. g1 = '♯1 SESSION'
  15. g2 = '♯2 SESSION'
  16. g3 = '♯3 SESSION'
  17. g4 = 'SESSION BOX STYLE'
  18. g5 = 'DATA LABELS SHOW & STYLE'
  19. g6 = 'INFOBOX SHOW & STYLE'
  20. g7 = 'MIN/MAX SHOW & STYLE'
  21. g81 = 'SESSION #1'
  22. g82 = 'SESSION #2'
  23. g83 = 'SESSION #3'
  24. g9 = 'NY MIDNIGHT OPEN'
  25.  
  26. ///////////////
  27. // Defined
  28. ///////////////
  29. show = true
  30. pips = syminfo.mintick * 10
  31. max_bars = 500
  32.  
  33. option_yes = 'Yes'
  34. option_no = 'No'
  35.  
  36. fmt_price = '{0,number,#.#####}'
  37. fmt_pips = '{0,number,#.#}'
  38.  
  39. icon_separator = ' • '
  40. empty_character = ' ' // U+2003
  41.  
  42. c_none = color.new(color.black, 100)
  43.  
  44. valLab_offset = 3 // number of bars added to x2 as an offset for value label
  45. label_NYMO_offset = 15 // number of bars added to x as an offset for NYMO label
  46.  
  47. var float mo = na
  48. mo := ta.valuewhen(ta.change(time("D","0000-0000","GMT-5")), open, 0) // NY Midnight Open
  49.  
  50. ///////////////
  51. // Functions
  52. ///////////////
  53. f_get_started (_session) => na(_session[1]) and _session // previous bar wasn't in session, current one is in session time
  54.  
  55. f_get_ended (_session) => na(_session) and _session[1] // previous bar was in session time, current one isn't in session time
  56.  
  57. f_inSession (_session, _timezone) => na(time(timeframe.period, _session + ":1234567", _timezone)) == false // function for detecting if current bar falls within specified custom time period (session)
  58.  
  59. f_get_period (_session, _start, _lookback) => // check i bars back if they are in session or not, if the first bar of session is found - break, the result is session duration in bars
  60. result = math.max(_start, 1)
  61. for i = result to _lookback
  62. if f_get_started(_session)
  63. result := 1
  64. break
  65. else if na(_session[i+1]) and _session[i]
  66. result := i+1
  67. break
  68. result
  69.  
  70. f_get_label_position (_y) => // set label position according to inputs data
  71. switch _y
  72. 'top' => label.style_label_lower_left
  73. 'bottom' => label.style_label_upper_left
  74.  
  75. f_get_day (n) => // set day name
  76. switch n
  77. 1 => 'Sun'
  78. 2 => 'Mon'
  79. 3 => 'Tue'
  80. 4 => 'Wed'
  81. 5 => 'Thu'
  82. 6 => 'Fri'
  83. 7 => 'Sat'
  84.  
  85. ///////////////
  86. // Inputs
  87. ///////////////
  88.  
  89. // Timezone & history switch
  90. i_tz = input.string(title="🕑 Timezone", defval="GMT", options=["GMT-10","GMT-9","GMT-8","GMT-7","GMT-6","GMT-5","GMT-4","GMT-3","GMT-2","GMT-1","GMT","GMT+1","GMT+2","GMT+3","GMT+3:30","GMT+4","GMT+5","GMT+5:30","GMT+6","GMT+7","GMT+8","GMT+9","GMT+10","GMT+10:30","GMT+11","GMT+12","GMT+13","GMT+13:45"], group=g0)
  91. i_show_history = input.string(option_no, 'Show history', options=[option_yes, option_no], group=g0) == option_yes
  92. i_encapsulate = input.bool(true, 'Encapsulate the last session candle', group=g0)
  93. sess_offset = i_encapsulate ? 0 : 1 // (should be 1 or 0) subtract number of candles from end of session to encapsulate last session candle or not
  94. i_lookback = 12 * 60 // 720 lookback period for calculation of session bars number - max session duration 12h on 1m chart, 6h on 30sec etc.
  95.  
  96. // Sessions
  97. i_show_sess1 = input.bool(true, title="Session", inline="asia", group=g81)
  98. i_sess1_label = input.string('Tokyo',':', inline="asia", group=g81)
  99. i_sess1 = input.session(title=" ", defval="0100-0400", inline="asia", group=g81)
  100. i_sess1_bgCol = input.color(color.new(color.yellow,0), title="color", inline="asia", group=g81)
  101. i_sess1_minCol = input.color(color.rgb(207, 205, 66), title="Min", inline="asia", group=g81)
  102. i_sess1_maxCol = input.color(color.rgb(207, 205, 66), title="Max", inline="asia", group=g81)
  103. i_sess1_minmax = input.bool(true, title="Show Min/Max lines", inline="asia_mm", group=g81)
  104. i_sess1_extend = input.bool(false, 'Extend Min/Max lines', inline="asia_mm", group=g81)
  105.  
  106. i_show_sess2 = input.bool(true, title="Session", inline="euro", group=g82)
  107. i_sess2_label = input.string('London',':', inline="euro", group=g82)
  108. i_sess2 = input.session(title=" ", defval="0700-1100", inline="euro", group=g82)
  109. i_sess2_bgCol = input.color(color.new(color.blue,0), title="color", inline="euro", group=g82)
  110. i_sess2_minCol = input.color(color.rgb(42, 129, 215), title="Min", inline="euro", group=g82)
  111. i_sess2_maxCol = input.color(color.rgb(42, 129, 215), title="Max", inline="euro", group=g82)
  112. i_sess2_minmax = input.bool(true, title="Show Min/Max lines", inline="euro_mm", group=g82)
  113. i_sess2_extend = input.bool(false, 'Extend Min/Max lines', inline="euro_mm", group=g82)
  114.  
  115. i_show_sess3 = input.bool(true, title="Session", inline="america", group=g83)
  116. i_sess3_label = input.string('New York',':', inline="america", group=g83)
  117. i_sess3 = input.session(title=" ", defval="1430-2000", inline="america", group=g83)
  118. i_sess3_bgCol = input.color(color.new(color.red,0), title="color", inline="america", group=g83)
  119. i_sess3_minCol = input.color(color.rgb(200, 75, 75), title="Min", inline="america", group=g83)
  120. i_sess3_maxCol = input.color(color.rgb(200, 75, 75), title="Max", inline="america", group=g83)
  121. i_sess3_minmax = input.bool(true, title="Show Min/Max lines", inline="america_mm", group=g83)
  122. i_sess3_extend = input.bool(false, 'Extend Min/Max lines', inline="america_mm", group=g83)
  123.  
  124. // Session Box Show & Styles
  125. i_sess_border_style = input.string(line.style_dashed, 'Border Style', options=[line.style_solid, line.style_dotted, line.style_dashed], inline='sess_box_style', group=g4)
  126. i_sess_border_width = input.int(1, 'width', minval=0, inline='sess_box_style', group=g4)
  127. i_sess_bgopacity = input.int(94, 'Background Opacity (100 = no color)', minval=0, maxval=100, step=1, group=g4)
  128.  
  129. // Session MinMax Show & Styles
  130. i_minmax_lineStyle = input.string(line.style_solid, title="Line Style", options=[line.style_solid, line.style_dotted, line.style_dashed], inline="line_style", group=g7)
  131. i_minmax_lineWidth = input.int(2, title="width", inline="line_style", group=g7)
  132. i_show_minmax_lines = input.bool(true, title="Toggle showing min/max lines for all sessions", group=g7)
  133. i_show_minmax_val = input.bool(true, title="Toggle showing min/max values for all sessions", group=g7)
  134.  
  135. // Data Label
  136. i_label_show = input.bool(true, 'Show labels', inline='label_show', group=g5) and show
  137. i_label_size = str.lower(input.string('Normal', '', options=['Auto', 'Tiny', 'Small', 'Normal', 'Large', 'Huge'], inline='label_show', group=g5))
  138. i_label_position_y = str.lower(input.string('Top', '', options=['Top', 'Bottom'], inline='label_show', group=g5))
  139. i_label_position = f_get_label_position(i_label_position_y)
  140. i_label_format_name = input.bool(true, 'Name', inline='label_format', group=g5)
  141. i_label_format_day = input.bool(false, 'Day', inline='label_format', group=g5)
  142. i_label_format_price = input.bool(false, 'Price range', inline='label_format', group=g5)
  143. i_label_format_pips = input.bool(false, 'Pips range', inline='label_format', group=g5)
  144.  
  145. // NY Midnight Open
  146. i_mo = input.bool(true, 'Midnight Open', inline="mo", group=g9)
  147. i_moCol = input.color(color.rgb(216, 68, 226), title="color", inline="mo", group=g9)
  148. i_mo_lineStyle = input.string(line.style_dotted, 'Line Style', options=[line.style_solid, line.style_dotted, line.style_dashed], inline='mo_style', group=g9)
  149. i_mo_lineWidth = input.int(2, title="width", inline="mo_style", group=g9)
  150. i_mo_label = input.bool(true, 'Show price label', inline="mo_extend", group=g9)
  151. i_mo_extend = input.bool(false, 'Extend line', inline="mo_extend", group=g9)
  152.  
  153. // InfoBox table
  154. i_table_InfoBox_show = input.bool(true, 'Show InfoBox', group=g6)
  155. i_tableCol = input.color(color.new(color.blue, 0), title="Background", inline="sessionBox", group=g6)
  156. i_tableCell_txtCol = input.color(color.new(color.white,0), title="Text", inline="sessionBox", group=g6)
  157.  
  158. // ------------------------
  159. // Drawing labels
  160. // ------------------------
  161. f_render_dataLabel (_show, _session, _is_started, _color, _top, _bottom, _text, _delete_history) =>
  162. var label my_label = na
  163. var int start_time = na
  164.  
  165. v_position_y = (i_label_position_y == 'top') ? _top : _bottom
  166. v_label = array.new_string()
  167. v_chg = _top - _bottom // price range
  168.  
  169. if _is_started
  170. start_time := time
  171.  
  172. if i_label_format_name and not na(_text) // session name
  173. array.push(v_label, _text)
  174.  
  175. if i_label_format_day
  176. array.push(v_label, f_get_day(dayofweek(start_time, i_tz))) // day name
  177.  
  178. if i_label_format_price
  179. array.push(v_label, str.format(fmt_price, v_chg)) // price range
  180.  
  181. if i_label_format_pips
  182. array.push(v_label, str.format(fmt_pips, v_chg / pips) + ' pips') // pips range
  183.  
  184. if _show
  185. if _is_started // check if bar is the firts bar of session
  186. my_label := label.new(time, v_position_y, array.join(v_label, icon_separator), textcolor=_color, color=c_none, size=i_label_size, style=i_label_position, xloc=xloc.bar_time, text_font_family=font.family_monospace) // create label
  187.  
  188. if _delete_history // delete history label according to input
  189. label.delete(my_label[1])
  190.  
  191. if _session // if in session,change y parameter and update text
  192. label.set_y(my_label, v_position_y)
  193. label.set_text(my_label, array.join(v_label, icon_separator))
  194.  
  195. f_drawValLabel (_show, _session, _is_started, _is_ended, _x, _y, _textColor, _delete_history) => // Function to draw labels with min/max values
  196. var label level_label = na
  197.  
  198. if _show
  199. if _is_started
  200. level_label := label.new(_x+valLab_offset, _y, '    ' + str.tostring(_y), xloc.bar_index, yloc.price, #00000000, label.style_none, _textColor, textalign=text.align_left, text_font_family=font.family_monospace)
  201.  
  202. if _delete_history // delete history label according to input
  203. label.delete(level_label[1])
  204.  
  205. else if _session
  206. label.set_x(level_label, _x+valLab_offset)
  207. label.set_y(level_label, _y)
  208. label.set_text(level_label, str.tostring(_y))
  209.  
  210. else if _is_ended
  211. label.set_x(level_label, bar_index-sess_offset)
  212.  
  213. // ------------------------
  214. // Drawing session boxes
  215. // ------------------------
  216. f_render_session (_show, _session, _is_started, _is_ended, _color, _top, _bottom, _extend, _delete_history) =>
  217. var box my_box = na
  218.  
  219. x0_1 = ta.valuewhen(na(_session[1]) and _session, bar_index, 1) // barindex on the previous session start
  220. x0_2 = ta.valuewhen(na(_session) and _session[1], bar_index, 0) // barindex on the last session end
  221. var x1 = 0
  222. var x2 = 0
  223. var session_open = 0.0
  224. var session_high = 0.0
  225. var session_low = 0.0
  226.  
  227. if _show
  228. if _is_started
  229. diff = math.abs(x0_2 - x0_1)-sess_offset // previous session duration in bars
  230. x1 := bar_index
  231. x2 := bar_index + (math.min(diff, max_bars))
  232. my_box := box.new(x1, _top, x2, _bottom, _color, i_sess_border_width, i_sess_border_style, bgcolor=color.new(_color, i_sess_bgopacity))
  233.  
  234. session_open := open
  235. session_high := _top
  236. session_low := _bottom
  237.  
  238. // if _extend //_is_extend
  239. // box.set_extend(my_box, extend.right)
  240.  
  241. if _delete_history
  242. box.delete(my_box[1])
  243.  
  244. else if _session
  245. box.set_top(my_box, _top)
  246. box.set_bottom(my_box, _bottom)
  247.  
  248. session_high := _top
  249. session_low := _bottom
  250.  
  251. else if _is_ended
  252. session_open := na
  253. box.set_right(my_box, bar_index-sess_offset)
  254.  
  255. [x1, x2, session_open, session_high, session_low]
  256.  
  257. // ------------------------
  258. // Drawing lines
  259. // ------------------------
  260. f_drawLine (_show ,_session, _is_started, _is_ended, _x1, _x2, _y1, _color, _extend, _delete_history) =>
  261. var line lineMinMax = na
  262.  
  263. if _show
  264. if _is_started
  265. lineMinMax := line.new(_x1, _y1, _x2, _y1, xloc = xloc.bar_index, extend=_delete_history and _extend ?extend.right:extend.none, color=_color, style=i_minmax_lineStyle, width=i_minmax_lineWidth)
  266.  
  267. if _delete_history // delete history label according to input
  268. line.delete(lineMinMax[1])
  269.  
  270. else if _session
  271. line.set_y1(lineMinMax, _y1)
  272. line.set_y2(lineMinMax, _y1)
  273.  
  274. else if _is_ended
  275. line.set_x2(lineMinMax, bar_index-sess_offset)
  276.  
  277. // ------------------------
  278. // Drawing NY midnigh open
  279. // ------------------------
  280.  
  281. f_drawNYMO() =>
  282. var line line_NYMO = na
  283. var label label_NYMO = na
  284. var int x1 = bar_index
  285. var int x2 = bar_index
  286. float y1 = mo
  287.  
  288. delete_history = (not i_show_history) or i_mo_extend
  289.  
  290. if mo[1] != mo
  291. x1 := bar_index
  292. x2 := bar_index
  293. line_NYMO := line.new(x1, mo, x2, mo, xloc = xloc.bar_index, extend=delete_history and i_mo_extend ?extend.right:extend.none, color=i_moCol, style=i_mo_lineStyle, width=i_mo_lineWidth)
  294.  
  295. if i_mo_label
  296. label_NYMO := label.new(x2+label_NYMO_offset, mo, str.tostring(mo) + ' NY Midnight Open', xloc.bar_index, yloc.price, #FFFFFF, label.style_none, i_moCol, textalign=text.align_left, text_font_family=font.family_monospace)
  297.  
  298. if delete_history // delete history line according to input
  299. line.delete(line_NYMO[1])
  300. label.delete(label_NYMO[1])
  301.  
  302. else if mo == mo[1]
  303. line.set_x2(line_NYMO, bar_index)
  304. label.set_x(label_NYMO, bar_index+label_NYMO_offset)
  305.  
  306. // ------------------------
  307. // Drawing
  308. // ------------------------
  309. draw (_show, _session, _bgcolor, _show_minmax, _maxcol, _mincol, _label, _extend, _lookback) =>
  310.  
  311. is_started = f_get_started(_session) // check if bar is the first bar of session
  312. is_ended = f_get_ended(_session) // check if bar is the first bar after the session
  313.  
  314. max = f_get_period(_session, 1, _lookback) // get session duration in bars // is_started ? 1 : is_started[1] ? 2 :
  315. top = ta.highest(high, max) // get top of the session
  316. bottom = ta.lowest(low, max) // get bottom of the session
  317.  
  318. delete_history = (not i_show_history) or _extend // check if history drawings and labels should be deleted, showing only the last ones
  319.  
  320. [x1, x2, _open, _high, _low] = f_render_session(_show, _session, is_started, is_ended, _bgcolor, top, bottom, _extend, delete_history) // draw a session box
  321.  
  322. if i_label_show
  323. f_render_dataLabel(_show, _session, is_started, _bgcolor, top, bottom, _label, delete_history) // draw a session label if needed
  324.  
  325. if i_show_minmax_lines and _show_minmax
  326. f_drawLine(_show, _session, is_started, is_ended, x1, x2, _high, _maxcol, _extend, delete_history)
  327. f_drawLine(_show, _session, is_started, is_ended, x1, x2, _low, _mincol, _extend, delete_history)
  328.  
  329. if i_show_minmax_val
  330. f_drawValLabel(_show, _session, is_started, is_ended, x2, _high, _maxcol, delete_history)
  331. f_drawValLabel(_show, _session, is_started, is_ended, x2, _low, _mincol, delete_history)
  332.  
  333. [_session, _open, _high, _low, max] // return session, open, top, low
  334.  
  335. ///////////////////
  336. // Calculating
  337. ///////////////////
  338. int sess1 = time(timeframe.period, i_sess1, i_tz) // convert session to timezone from input
  339. int sess2 = time(timeframe.period, i_sess2, i_tz)
  340. int sess3 = time(timeframe.period, i_sess3, i_tz)
  341.  
  342. ///////////////////
  343. // Plotting
  344. ///////////////////
  345. [is_sess1, sess1_open, sess1_high, sess1_low, sess1_max] = draw(i_show_sess1, sess1, i_sess1_bgCol, i_sess1_minmax, i_sess1_maxCol, i_sess1_minCol, i_sess1_label, i_sess1_extend, i_lookback) // draw session box and label
  346. [is_sess2, sess2_open, sess2_high, sess2_low, sess2_max] = draw(i_show_sess2, sess2, i_sess2_bgCol, i_sess2_minmax, i_sess2_maxCol, i_sess2_minCol, i_sess2_label, i_sess2_extend, i_lookback)
  347. [is_sess3, sess3_open, sess3_high, sess3_low, sess3_max] = draw(i_show_sess3, sess3, i_sess3_bgCol, i_sess3_minmax, i_sess3_maxCol, i_sess3_minCol, i_sess3_label, i_sess3_extend, i_lookback)
  348.  
  349. // Create table - InfoBox
  350. var table infoBoxTable = table.new(position.top_right, 1, 1, border_width = 3)
  351.  
  352. is_asia_sess = f_inSession(i_sess1, i_tz) and i_show_sess1
  353. is_euro_sess = f_inSession(i_sess2, i_tz) and i_show_sess2
  354. is_america_sess = f_inSession(i_sess3, i_tz) and i_show_sess3
  355.  
  356. cell_text = is_america_sess and is_asia_sess and is_euro_sess ? i_sess1_label + "\n" + i_sess2_label + "\n" + i_sess3_label :
  357. is_america_sess and is_asia_sess ? i_sess3_label + "\n" + i_sess1_label :
  358. is_asia_sess and is_euro_sess ? i_sess1_label + "\n" + i_sess2_label :
  359. is_euro_sess and is_america_sess ? i_sess2_label + "\n" + i_sess3_label :
  360. is_america_sess ? i_sess3_label : is_asia_sess ? i_sess1_label : is_euro_sess ? i_sess2_label : "None"
  361.  
  362. f_fillCell(_table, _column, _row, _text) =>
  363. table.cell(_table, _column, _row, _text, bgcolor = i_tableCol, text_color = i_tableCell_txtCol, width = 6)
  364.  
  365. if barstate.islast and i_table_InfoBox_show
  366. f_fillCell(infoBoxTable, 0, 0, cell_text)
  367.  
  368. // Draw NY Midnight Open line
  369. if i_mo
  370. f_drawNYMO()
Advertisement
Add Comment
Please, Sign In to add comment