xmd79

Fusion: Forex sessions with daylight savings

Feb 2nd, 2022
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.45 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. // © Koalems
  3.  
  4. // @version=5
  5.  
  6. // ***************************************************************************************************
  7. //
  8. //
  9. // CREDITS
  10. //
  11. //
  12. // ***************************************************************************************************
  13. // Blended my own sessions indicator and "FX Market Sessions" by boidoki
  14.  
  15. // ***************************************************************************************************
  16. //
  17. //
  18. // CONSTANTS
  19. //
  20. //
  21. // ***************************************************************************************************
  22. var colorTransparent = color.new(color.white, 100)
  23. var maxBoxes = 500
  24. var maxLabels = 500
  25.  
  26. // ***************************************************************************************************
  27. //
  28. //
  29. // TYPE
  30. //
  31. //
  32. // ***************************************************************************************************
  33. indicator(
  34. title = 'Fusion: Forex sessions with daylight savings',
  35. shorttitle = 'Fusion: Fx Sessions',
  36. max_boxes_count = maxBoxes,
  37. max_labels_count = maxLabels,
  38. overlay = true)
  39.  
  40. // ***************************************************************************************************
  41. //
  42. //
  43. // INPUTS
  44. //
  45. //
  46. // ***************************************************************************************************
  47. TooltipTimezones = 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones'
  48.  
  49. // ***************************************************************************************************
  50. // 7000 Inputs - Indicator: Session
  51. // ***************************************************************************************************
  52.  
  53. Group7000 = '7000 Show'
  54. sessionsShow = input.bool(true, group=Group7000, title='Sessions')
  55. sessionsShowOnMainChart = input.bool(true, group=Group7000, title='  On main chart', tooltip="If you are on the main chart then make sure this is checked.\n\nWhen you want to use this separate from your main chart ucheck it.\n\nOn the main chart we can show:\n* The legend\n* Boxes\n* Labels\n* Breakout check area\n* Start & end labels\n\nOn any other chart we can show:\n* The legend\n* Horizontal bars representing the sessions.\n\nAll objects are color coded and the legend shows the color codes.")
  56.  
  57. sessShow = sessionsShow and timeframe.isintraday and timeframe.multiplier <= 60 and timeframe.multiplier >= 3
  58.  
  59. sessShowBox = input.bool(true, group=Group7000, title='  Boxes') and sessShow and sessionsShowOnMainChart
  60. sessShowLabel = input.bool(true, group=Group7000, title='  Labels') and sessShow and sessionsShowOnMainChart
  61. sessShowOpeningRange = input.bool(false, group=Group7000, title='  Breakout check area') and sessShow and sessionsShowOnMainChart
  62. sessShowSessionEnds = input.bool(false, group=Group7000, title='  Start/end labels') and sessShow and sessionsShowOnMainChart
  63. sessLedgendShow = input.bool(false, group=Group7000, title="  Show legend") and sessShow
  64. sessLedgendTransparency = input.bool(true, group=Group7000, title="    Legend match translucency")
  65.  
  66. sessFrankfurtColor = input.color(#7e57c2, group=Group7000, inline="SRS1", title="Color")
  67. sessFrankfurtShow = input.bool(true, group=Group7000, inline="SRS1", title="Frankfurt")
  68. sessLondonColor = input.color(#4caf50, group=Group7000, inline="SRS2", title='Color')
  69. sessLondonShow = input.bool(true, group=Group7000, inline="SRS2", title="London")
  70. sessNewYorkColor = input.color(#f44336, group=Group7000, inline="SRS3", title="Color")
  71. sessNewYorkShow = input.bool(true, group=Group7000, inline="SRS3", title="New York")
  72. sessSydneyColor = input.color(#00bcd4, group=Group7000, inline="SRS4", title="Color")
  73. sessSydneyShow = input.bool(true, group=Group7000, inline="SRS4", title="Sydney")
  74. sessTokyoColor = input.color(#f57f17, group=Group7000, inline="SRS5", title="Color")
  75. sessTokyoShow = input.bool(true, group=Group7000, inline="SRS5", title="Tokyo")
  76.  
  77. sessTransparency = input.int(45, group=Group7000, title="Transparency")
  78. sessLineWidth = input.int(20, group=Group7000, title="Line width")
  79. sessLedgendLocation = input.string("Bottom Left", group=Group7000, title="Legend location", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"])
  80.  
  81. // ***************************************************************************************************
  82. // Inputs - Indicator: Session - DETAILS
  83. // ***************************************************************************************************
  84.  
  85. // When using Timezones the form GMT+n does not take daylight savings into account however the form
  86. // "America/Phoenix" does. This is why our input for the timezone variables like "sessSydneyTz" are
  87. // strings like "Australia/Sydney", i.e., we don't have to worry about daylight savings ourselves.
  88.  
  89. Group7020 = '7020 Session: Frankfurt'
  90. sessFrankfurtLabel = input.string('Frankfurt', group=Group7020, title='Label')
  91. sessFrankfurtTz = input.string('Europe/Berlin', group=Group7020, title='Timezone', tooltip=TooltipTimezones)
  92. sessFrankfurtPeriod = input.session('0800-1601', group=Group7020, title='Period')
  93.  
  94. Group7030 = '7030 Session: London'
  95. sessLondonLabel = input.string('London', group=Group7030, title='Label')
  96. sessLondonTz = input.string('Europe/London', group=Group7030, title='Timezone', tooltip=TooltipTimezones)
  97. sessLondonPeriod = input.session('0800-1601', group=Group7030, title='Period')
  98.  
  99. Group7040 = '7040 Session: New York'
  100. sessNewYorkLabel = input.string('New York', group=Group7040, title='Label')
  101. sessNewYorkTz = input.string('America/New_York', group=Group7040, title='Timezone', tooltip=TooltipTimezones)
  102. sessNewYorkPeriod = input.session('0800-1601', group=Group7040, title='Period')
  103.  
  104. Group7050 = '7050 Session: Sydney'
  105. sessSydneyLabel = input.string('Sydney', group=Group7050, title='Label')
  106. sessSydneyTz = input.string('Australia/Sydney', group=Group7050, title='Timezone', tooltip=TooltipTimezones)
  107. sessSydneyPeriod = input.session('0800-1601', group=Group7050, title='Period')
  108.  
  109. Group7060 = '7060 Session: Tokyo'
  110. sessTokyoLabel = input.string('Tokyo', group=Group7060, title='Label')
  111. sessTokyoTz = input.string('Asia/Tokyo', group=Group7060, title='Timezone', tooltip=TooltipTimezones)
  112. sessTokyoPeriod = input.session('0800-1601', group=Group7060, title='Period')
  113.  
  114. Group7070 = '7070 Session: Show & Styles'
  115. sessRectLineStyle = input.string(line.style_dashed, group=Group7070, title='Line style', options=[line.style_solid, line.style_dotted, line.style_dashed])
  116. sessRectLineWidth = input.int(1, group=Group7070, minval=1, title='Line width')
  117. sessLabelSize = input.string(size.normal, group=Group7070, title='Label size', options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge])
  118. sessShowBgColor = input.bool(true, group=Group7070, title='Background colors')
  119. sessRectBgOpacity = input.int(94, group=Group7070, minval=0, maxval=100, step=1, title='Background opacity')
  120.  
  121. Group7080 = '7080 Session: Opening Range'
  122. sessLookBackOrMins = input.int(30, group=Group7080, minval=1, step=1, title='Breakout - start gap', tooltip='Minutes')
  123.  
  124. // ***************************************************************************************************
  125. //
  126. //
  127. // INDICATOR
  128. //
  129. //
  130. // ***************************************************************************************************
  131.  
  132. // Constants
  133. var sessLookbackMins = 12 * 60
  134. var sessLookbackOrBars = math.floor(sessLookBackOrMins / timeframe.multiplier)
  135.  
  136. // Functions
  137. IsInSession(session) =>
  138. IsInSession = not na(session)
  139. IsInSession
  140.  
  141. TableLocation(loc) =>
  142. loc == 'Top Left' ? position.top_left :
  143. loc == 'Top Center' ? position.top_center :
  144. loc == 'Top Right' ? position.top_right :
  145. loc == 'Middle Left' ? position.middle_left :
  146. loc == 'Middle Center' ? position.middle_center :
  147. loc == 'Middle Right' ? position.middle_right :
  148. loc == 'Bottom Left' ? position.bottom_left :
  149. loc == 'Bottom Center' ? position.bottom_center :
  150. loc == 'Bottom Right' ? position.bottom_right : na
  151.  
  152. CreateSessionRect(sessionTime, sessionDuration, labelText, col, sessShow) =>
  153. bool result = false
  154. bool shouldBeDelete = false
  155. bool is_inSession = IsInSession(sessionTime)
  156.  
  157. if is_inSession
  158. result := true
  159. int minsLen = math.round(sessionDuration / timeframe.multiplier)
  160. int begin_index = 0
  161.  
  162. for i = 1 to minsLen by 1
  163. if not na(sessionTime[i])
  164. begin_index += 1
  165. begin_index
  166.  
  167. int pR = bar_index
  168. int pL = bar_index - begin_index
  169.  
  170. float pT = 0
  171. float pB = high
  172. for i = 1 to begin_index by 1
  173. pT := math.max(high[i], pT)
  174. pB := math.min(low[i], pB)
  175. pB
  176.  
  177. float range_1 = pT - pB
  178.  
  179. var label line_label = na
  180. var box my_box = na
  181.  
  182. if sessShowBox
  183. if not is_inSession[1]
  184. my_box := switch col
  185. sessFrankfurtColor => box.new(pR, pT, pL, pB, border_color=col, border_width=sessRectLineWidth, border_style=sessRectLineStyle, bgcolor=color.new(sessFrankfurtColor, 99))
  186. sessLondonColor => box.new(pR, pT, pL, pB, border_color=col, border_width=sessRectLineWidth, border_style=sessRectLineStyle, bgcolor=color.new(sessLondonColor, 99))
  187. sessNewYorkColor => box.new(pR, pT, pL, pB, border_color=col, border_width=sessRectLineWidth, border_style=sessRectLineStyle, bgcolor=color.new(sessNewYorkColor, 99))
  188. sessSydneyColor => box.new(pR, pT, pL, pB, border_color=col, border_width=sessRectLineWidth, border_style=sessRectLineStyle, bgcolor=color.new(sessSydneyColor, 99))
  189. sessTokyoColor => box.new(pR, pT, pL, pB, border_color=col, border_width=sessRectLineWidth, border_style=sessRectLineStyle, bgcolor=color.new(sessTokyoColor, 99))
  190. => na
  191.  
  192. if sessShow and is_inSession
  193. box.set_left(my_box, pL)
  194. box.set_right(my_box, pR)
  195. box.set_top(my_box, pT)
  196. box.set_bottom(my_box, pB)
  197.  
  198. if shouldBeDelete
  199. box.delete(my_box[1])
  200.  
  201. if sessShow and sessShowLabel
  202. if not is_inSession[1]
  203. line_label := label.new(pL, pT, labelText, textcolor=col, color=colorTransparent, style=label.style_none, size=sessLabelSize, textalign=text.align_left)
  204. line_label
  205.  
  206. if is_inSession
  207. label.set_xy(line_label, pL, pT)
  208.  
  209. if shouldBeDelete
  210. label.delete(line_label[1])
  211. result
  212.  
  213. SetSessionHL(times, timesOrCalc) =>
  214. float h = na
  215. float l = na
  216. h := times ? h[1] : na
  217. l := times ? l[1] : na
  218.  
  219. if timesOrCalc
  220. for i = 0 to sessLookbackOrBars by 1
  221. h := i == 0 ? high : math.max(high[i], h)
  222. l := i == 0 ? low : math.min(low[i], l)
  223. [h, l]
  224.  
  225. MakeBreakoutCheckAreaBox(h, l, condition) =>
  226. box b = na
  227. if condition
  228. b := box.new(
  229. bar_index, h,
  230. bar_index, l,
  231. border_color = color.new(color.silver, 50),
  232. border_width = 1,
  233. border_style = line.style_solid,
  234. bgcolor = color.new(color.silver, 94))
  235. b
  236.  
  237. AdjustBreakoutCheckAreaBox(B, h) =>
  238. if not na(h)
  239. box.set_right(B, bar_index)
  240.  
  241. // **********************************************************************
  242. // MAIN
  243. // **********************************************************************
  244. sessFrankfurtTimes = time(timeframe.period, sessFrankfurtPeriod, sessFrankfurtTz)
  245. sessLondonTimes = time(timeframe.period, sessLondonPeriod, sessLondonTz)
  246. sessNewYorkTimes = time(timeframe.period, sessNewYorkPeriod, sessNewYorkTz)
  247. sessSydneyTimes = time(timeframe.period, sessSydneyPeriod, sessSydneyTz)
  248. sessTokyoTimes = time(timeframe.period, sessTokyoPeriod, sessTokyoTz)
  249.  
  250. sessFrankfurtTimesOrCalc = sessFrankfurtTimes[sessLookbackOrBars] and na(sessFrankfurtTimes[sessLookbackOrBars + 1])
  251. sessLondonTimesOrCalc = sessLondonTimes[sessLookbackOrBars] and na(sessLondonTimes[sessLookbackOrBars + 1])
  252. sessNewYorkTimesOrCalc = sessNewYorkTimes[sessLookbackOrBars] and na(sessNewYorkTimes[sessLookbackOrBars + 1])
  253. sessSydneyTimesOrCalc = sessSydneyTimes[sessLookbackOrBars] and na(sessSydneyTimes[sessLookbackOrBars + 1])
  254. sessTokyoTimesOrCalc = sessTokyoTimes[sessLookbackOrBars] and na(sessTokyoTimes[sessLookbackOrBars + 1])
  255.  
  256. [sessFrankfurt_h, sessFrankfurt_l] = SetSessionHL(sessFrankfurtTimes, sessFrankfurtTimesOrCalc)
  257. [sessLondon_h, sessLondon_l] = SetSessionHL(sessLondonTimes, sessLondonTimesOrCalc)
  258. [sessNewYork_h, sessNewYork_l] = SetSessionHL(sessNewYorkTimes, sessNewYorkTimesOrCalc)
  259. [sessSydney_h, sessSydney_l] = SetSessionHL(sessSydneyTimes, sessSydneyTimesOrCalc)
  260. [sessTokyo_h, sessTokyo_l] = SetSessionHL(sessTokyoTimes, sessTokyoTimesOrCalc)
  261.  
  262. sessFrankfurtStarted = sessFrankfurtTimes and na(sessFrankfurtTimes[1])
  263. sessLondonStarted = sessLondonTimes and na(sessLondonTimes[1])
  264. sessNewYorkStarted = sessNewYorkTimes and na(sessNewYorkTimes[1])
  265. sessSydneyStarted = sessSydneyTimes and na(sessSydneyTimes[1])
  266. sessTokyoStarted = sessTokyoTimes and na(sessTokyoTimes[1])
  267. sessFrankfurtEnded = na(sessFrankfurtTimes) and sessFrankfurtTimes[1]
  268. sessLondonEnded = na(sessLondonTimes) and sessLondonTimes[1]
  269. sessNewYorkEnded = na(sessNewYorkTimes) and sessNewYorkTimes[1]
  270. sessSydneyEnded = na(sessSydneyTimes) and sessSydneyTimes[1]
  271. sessTokyoEnded = na(sessTokyoTimes) and sessTokyoTimes[1]
  272.  
  273. // Session end
  274. sessASessionEnded = sessFrankfurtEnded or sessLondonEnded or sessNewYorkEnded or sessSydneyEnded or sessTokyoEnded
  275.  
  276. // We are in a session
  277. sessInSession = not na(sessFrankfurtTimes) or not na(sessLondonTimes) or not na(sessNewYorkTimes) or not na(sessSydneyTimes) or not na(sessTokyoTimes)
  278.  
  279. // ***************************************************************************************************
  280. //
  281. //
  282. // SHOW
  283. //
  284. //
  285. // ***************************************************************************************************
  286. if sessLedgendShow
  287. var infoPos = TableLocation(sessLedgendLocation)
  288. var info = table.new(
  289. infoPos,
  290. 5, 1,
  291. frame_color = color.gray, frame_width = 1,
  292. border_color = color.black, border_width = 1)
  293. var transl = sessLedgendTransparency ? sessTransparency : 0
  294.  
  295. table.cell(info, 0, 0, 'Frankfurt', text_color=color.white, text_size=size.small, text_halign=text.align_left, bgcolor=color.new(sessFrankfurtColor , transl))
  296. table.cell(info, 1, 0, 'London' , text_color=color.white, text_size=size.small, text_halign=text.align_left, bgcolor=color.new(sessLondonColor , transl))
  297. table.cell(info, 2, 0, 'New York' , text_color=color.white, text_size=size.small, text_halign=text.align_left, bgcolor=color.new(sessNewYorkColor , transl))
  298. table.cell(info, 3, 0, 'Sydney' , text_color=color.white, text_size=size.small, text_halign=text.align_left, bgcolor=color.new(sessSydneyColor , transl))
  299. table.cell(info, 4, 0, 'Tokyo' , text_color=color.white, text_size=size.small, text_halign=text.align_left, bgcolor=color.new(sessTokyoColor , transl))
  300.  
  301. // ***************************************************************************************************
  302. // SHOW - On main chart
  303. // ***************************************************************************************************
  304. CreateSessionRect(sessFrankfurtTimes, sessLookbackMins, sessFrankfurtLabel, sessFrankfurtColor, sessShow and sessFrankfurtShow)
  305. CreateSessionRect(sessLondonTimes, sessLookbackMins, sessLondonLabel, sessLondonColor, sessShow and sessLondonShow)
  306. CreateSessionRect(sessNewYorkTimes, sessLookbackMins, sessNewYorkLabel, sessNewYorkColor, sessShow and sessNewYorkShow)
  307. CreateSessionRect(sessSydneyTimes, sessLookbackMins, sessSydneyLabel, sessSydneyColor, sessShow and sessSydneyShow)
  308. CreateSessionRect(sessTokyoTimes, sessLookbackMins, sessTokyoLabel, sessTokyoColor, sessShow and sessTokyoShow)
  309.  
  310. if sessShowOpeningRange
  311. sessFrankfurtBox = MakeBreakoutCheckAreaBox(sessFrankfurt_h, sessFrankfurt_l, sessFrankfurtShow and sessFrankfurtTimes and sessFrankfurtTimesOrCalc)
  312. sessLondonBox = MakeBreakoutCheckAreaBox(sessLondon_h, sessLondon_l, sessLondonShow and sessLondonTimes and sessLondonTimesOrCalc)
  313. sessNewYorkBox = MakeBreakoutCheckAreaBox(sessNewYork_h, sessNewYork_l, sessNewYorkShow and sessNewYorkTimes and sessNewYorkTimesOrCalc)
  314. sessSydneyBox = MakeBreakoutCheckAreaBox(sessSydney_h, sessSydney_l, sessSydneyShow and sessSydneyTimes and sessSydneyTimesOrCalc)
  315. sessTokyoBox = MakeBreakoutCheckAreaBox(sessTokyo_h, sessTokyo_l, sessTokyoShow and sessTokyoTimes and sessTokyoTimesOrCalc)
  316.  
  317. AdjustBreakoutCheckAreaBox(sessFrankfurtBox, sessFrankfurt_h)
  318. AdjustBreakoutCheckAreaBox(sessLondonBox, sessLondon_h)
  319. AdjustBreakoutCheckAreaBox(sessNewYorkBox, sessNewYork_h)
  320. AdjustBreakoutCheckAreaBox(sessSydneyBox, sessSydney_h)
  321. AdjustBreakoutCheckAreaBox(sessTokyoBox, sessTokyo_h)
  322.  
  323. plotshape(sessShowSessionEnds and sessFrankfurtStarted and sessFrankfurtShow, color=color.new(sessFrankfurtColor, 45), text='Start', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session start: Frankfurt')
  324. plotshape(sessShowSessionEnds and sessLondonStarted and sessLondonShow, color=color.new(sessLondonColor, 45), text='Start', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session start: London')
  325. plotshape(sessShowSessionEnds and sessNewYorkStarted and sessNewYorkShow, color=color.new(sessNewYorkColor, 45), text='Start', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session start: New York')
  326. plotshape(sessShowSessionEnds and sessSydneyStarted and sessSydneyShow, color=color.new(sessSydneyColor, 45), text='Start', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session start: Sydney')
  327. plotshape(sessShowSessionEnds and sessTokyoStarted and sessTokyoShow, color=color.new(sessTokyoColor, 45), text='Start', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session start: Tokyo')
  328.  
  329. plotshape(sessShowSessionEnds and sessFrankfurtEnded and sessFrankfurtShow, color=color.new(sessFrankfurtColor, 45), text='End', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session end: Frankfurt')
  330. plotshape(sessShowSessionEnds and sessLondonEnded and sessLondonShow, color=color.new(sessLondonColor, 45), text='End', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session end: London')
  331. plotshape(sessShowSessionEnds and sessNewYorkEnded and sessNewYorkShow, color=color.new(sessNewYorkColor, 45), text='End', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session end: New York')
  332. plotshape(sessShowSessionEnds and sessSydneyEnded and sessSydneyShow, color=color.new(sessSydneyColor, 45), text='End', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session end: Sydney')
  333. plotshape(sessShowSessionEnds and sessTokyoEnded and sessTokyoShow, color=color.new(sessTokyoColor, 45), text='End', style=shape.labeldown, location=location.bottom, size=size.small, textcolor=color.white, title='Session end: Tokyo')
  334.  
  335. // ***************************************************************************************************
  336. // SHOW - Off main chart
  337. // ***************************************************************************************************
  338. offMainChart = not sessionsShowOnMainChart and sessShow
  339.  
  340. plot(sessFrankfurtShow and offMainChart ? (sessFrankfurtTimes ? 10 : na) : na, style=plot.style_linebr, linewidth=sessLineWidth, color=color.new(sessFrankfurtColor, sessTransparency), title="Frankfurt")
  341. plot(sessLondonShow and offMainChart ? (sessLondonTimes ? 8 : na) : na, style=plot.style_linebr, linewidth=sessLineWidth, color=color.new(sessLondonColor , sessTransparency), title="London")
  342. plot(sessNewYorkShow and offMainChart ? (sessNewYorkTimes ? 6 : na) : na, style=plot.style_linebr, linewidth=sessLineWidth, color=color.new(sessNewYorkColor, sessTransparency), title="New York")
  343. plot(sessSydneyShow and offMainChart ? (sessSydneyTimes ? 4 : na) : na, style=plot.style_linebr, linewidth=sessLineWidth, color=color.new(sessSydneyColor , sessTransparency), title="Sydney")
  344. plot(sessTokyoShow and offMainChart ? (sessTokyoTimes ? 2 : na) : na, style=plot.style_linebr, linewidth=sessLineWidth, color=color.new(sessTokyoColor , sessTransparency), title="Tokyo")
  345.  
Advertisement
Add Comment
Please, Sign In to add comment