xmd79

Pre-market Highs & Lows on RTH chart

May 22nd, 2023
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 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. // Only working properly on ES, NQ, YM (ES1!, ESM2023, etc etc). Trouble with SPY due to odd behaviour counting premarket hours
  3. // © twingall
  4.  
  5. //@version=5
  6. indicator("Pre-market Highs & Lows on RTH chart", overlay = true, max_bars_back = 5000)
  7.  
  8. rthOpen = input.session('0930-0931', "RTH open (New York time)", options =['0930-0931','1030-1031','1130-1131', '0830-0831'])
  9.  
  10. _hour = input.float(16, "Pre-market: 'bar hours' back from RTH open", minval =1, step =0.25, tooltip ="This is to define your pre-market session in which the highest high and lowest low will be identified\n\n0.25 = 15mins")
  11. showHighs = input.bool(true, "show pre-market high", inline ='1')
  12. showLows = input.bool(true, "show pre-market low", inline ='2')
  13. maxDayLines = input.int(3, minval = 1, title = "days of history to show")
  14. showLabels = input.bool(true, "labels", group = 'formatting', inline ='1')
  15. textCol = input.color(color.black, "||", group = 'formatting', inline ='1')
  16. textSize = input.string(size.normal, "size", options =[size.auto, size.tiny, size.small, size.normal, size.large, size.huge], group = 'formatting', inline ='1')
  17. vertOffset = input.int(0, minval =0, title="Vertical Offset", group = 'formatting', inline ='1', tooltip ="ATR multiple to Raise high price labels and Lower low price labels from the line\n\nFor cases where labels print over price and cannot be easily read")
  18. showLines = input.bool(true, "lines", group = 'formatting', inline ='2')
  19. lineCol = input.color(color.fuchsia, "||", group = 'formatting', inline ='2')
  20. lineWidth = input.int(1, "width", group = 'formatting', inline ='2')
  21. _lineStyle = input.string("dotted", "linestyle", options =["arrow left" ,"solid", "dotted", "dashed"], group = 'formatting', inline ='2', tooltip ="only applies when chart is toggles to ETH(electronic trading hours)")
  22. rthLineLen = input.int(5, minval=0, title= "Line length (bars back from RTH open)", group = 'formatting', tooltip ="only applies when chart is toggled to RTH(regular trading hours)")
  23. extChoice = input.bool(false, "extend lines fully to the right",group = 'formatting')
  24.  
  25. string lineStyle = switch _lineStyle
  26. "arrow left" => line.style_arrow_left
  27. "solid" => line.style_solid
  28. "dotted" => line.style_dotted
  29. "dashed" => line.style_dashed
  30.  
  31. atr = ta.atr(400)
  32. colorNone = color.new(color.white, 100)
  33. rthStartSess=time(timeframe.period, rthOpen, 'America/New_York')
  34.  
  35. inRtSt = nz(rthStartSess)
  36.  
  37. ETHextraTime = math.round(1000*3600*_hour) //pre-market time in ms 1000*3600*_hour
  38.  
  39. barT = timeframe.in_seconds(timeframe.period)*1000
  40. barsETH = math.round(ETHextraTime/ barT)
  41. extendedTicker = ticker.modify(syminfo.ticker, session = session.extended)
  42.  
  43. //condition to see if our chart is toggled to electronic trading hours (ETH) or regular trading hours (RTH)
  44. _7_00= timestamp('America/New_York', year, month, dayofmonth, 07, 00, 00) //arbitrary pre-market time used for the 'is RTH or is ETH' condition
  45. outOfHoursClose = ta.valuewhen(time==_7_00, close,0)
  46. bool isETH = nz(outOfHoursClose)
  47.  
  48. tickerExtHighest = request.security(extendedTicker, timeframe.period, ta.highest(high, barsETH)[1]) // use [1], so it doesn't count 9:30 high as highest pre-market
  49. tickerExtLowest = request.security(extendedTicker, timeframe.period, ta.lowest(low, barsETH)[1]) // use [1], so it doesn't count 9:30 low as lowest pre-market
  50.  
  51. var line hiLine = na
  52. var label hiLineLab =na
  53. var array<line> hiLineArr = array.new<line>(0)
  54. var array<label> hiLineLabArr = array.new<label>(0)
  55.  
  56. var line loLine = na
  57. var label loLineLab =na
  58. var array<line> loLineArr = array.new<line>(0)
  59. var array<label> loLineLabArr = array.new<label>(0)
  60.  
  61. if inRtSt and not inRtSt[1] and showHighs
  62. hiLine:=isETH?line.new(time-ETHextraTime, tickerExtHighest, time, tickerExtHighest, xloc=xloc.bar_time, style= lineStyle, extend =extChoice?extend.right:extend.none,color =showLines?lineCol:colorNone, width =lineWidth):
  63. line.new(time[rthLineLen], tickerExtHighest,time, tickerExtHighest, xloc=xloc.bar_time, style= line.style_dotted, extend =extChoice?extend.right:extend.none,color =showLines?lineCol:colorNone, width =lineWidth)
  64. hiLineLab:=isETH?label.new(time-ETHextraTime/2,tickerExtHighest+(vertOffset*atr), text=showLabels?str.tostring(tickerExtHighest):"", style = label.style_label_down,color = colorNone,textcolor =textCol, xloc=xloc.bar_time, size =textSize):
  65. label.new(time[1],tickerExtHighest+(vertOffset*atr), text=showLabels?str.tostring(tickerExtHighest):"", style = label.style_label_down,color = colorNone,textcolor =textCol, xloc=xloc.bar_time, size =textSize)
  66. array.push(hiLineArr, hiLine)
  67. array.push(hiLineLabArr,hiLineLab)
  68. if array.size(hiLineArr)>maxDayLines
  69. line.delete(array.shift(hiLineArr))
  70. label.delete(array.shift(hiLineLabArr))
  71.  
  72. if inRtSt and not inRtSt[1] and showLows
  73. loLine:=isETH?line.new(time-ETHextraTime, tickerExtLowest, time, tickerExtLowest, xloc=xloc.bar_time, style= lineStyle, extend =extChoice?extend.right:extend.none,color =showLines?lineCol:colorNone, width =lineWidth):
  74. line.new(time[rthLineLen], tickerExtLowest, time, tickerExtLowest, xloc=xloc.bar_time, style= line.style_dotted, extend =extChoice?extend.right:extend.none,color =showLines?lineCol:colorNone, width =lineWidth)
  75. loLineLab:=isETH?label.new(time-ETHextraTime/2,tickerExtLowest-(vertOffset*atr), text=showLabels?str.tostring(tickerExtLowest):"", style = label.style_label_up,color = colorNone, textcolor =textCol, xloc=xloc.bar_time, size =textSize):
  76. label.new(time[1],tickerExtLowest-(vertOffset*atr), text=showLabels?str.tostring(tickerExtLowest):"", style = label.style_label_up,color = colorNone,textcolor =textCol, xloc=xloc.bar_time, size =textSize)
  77. array.push(loLineArr, loLine)
  78. array.push(loLineLabArr,loLineLab)
  79. if array.size(loLineArr)>maxDayLines
  80. line.delete(array.shift(loLineArr))
  81. label.delete(array.shift(loLineLabArr))
  82.  
  83. ////////////////// Nascent preMkt highs and lows : this will only show outside of regular trading hours, but should display on RTH chart dynamically through the overnight session
  84. showNascent = input.bool(true, "Plot Nascent pre-mkt H/L", group = 'Plot Currently forming preMkt Highs/Lows', inline = '1', tooltip = "Only applies outside of Regular trading hours\n\nshows currently forming, dynamic Highest/Lowest of preMkt as we progress through the overnight session\n\nWorks with the historical lines, when the we get to opening bell, these lines disappear and are replaced by the original lines")
  85. colNascent = input.color(color.blue, "Line Color: ", group = 'Plot Currently forming preMkt Highs/Lows', inline = '2')
  86. _lineStyle2 = input.string("dotted", "| Line style:", options =["arrow left" ,"solid", "dotted", "dashed"], group = 'Plot Currently forming preMkt Highs/Lows', inline ='2')
  87. inpRegTradHrs =input.session('0930-1600', 'regular session', group = 'Plot Currently forming preMkt Highs/Lows')
  88. regSess=time(timeframe.period, inpRegTradHrs, 'America/New_York')
  89. string lineStyle2 = switch _lineStyle2
  90. "arrow left" => line.style_arrow_left
  91. "solid" => line.style_solid
  92. "dotted" => line.style_dotted
  93. "dashed" => line.style_dashed
  94.  
  95. inRegSess =not na(regSess)
  96. barsSinceRTHcloseETH = ta.barssince(inRegSess)
  97. //turns out can only retrieve ETH price info from RTH toggled chart by using request.security_lower_tf (request.security() won't work)
  98. timeDiff = timenow-last_bar_time
  99. extHighsArr = request.security_lower_tf(extendedTicker, timeframe.period, timeDiff>0?high:0)
  100. if timeDiff ==0
  101. array.clear(extHighsArr)
  102. highestOf = array.max(extHighsArr)
  103. extLowsArr = request.security_lower_tf(extendedTicker, timeframe.period, timeDiff>0?low:0)
  104. if timeDiff ==0
  105. array.clear(extLowsArr)
  106. lowestOf = array.min(extLowsArr)
  107.  
  108. _t_extHcurrRTH = request.security(extendedTicker, timeframe.period, ta.highest(high, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1))
  109. _t_extHcurrETH = ta.highest(high, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1)
  110. tickerExtHighestCurr = isETH? _t_extHcurrETH:math.max((timeDiff>0?highestOf:_t_extHcurrRTH),_t_extHcurrRTH) //math.max: account for the short period between 4pm 'close' and last bar of RTH 'close'
  111.  
  112. _t_extLcurrRTH = request.security(extendedTicker, timeframe.period, ta.lowest(low, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1))
  113. _t_extLcurrETH = ta.lowest(low, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1)
  114. tickerExtLowestCurr = isETH? _t_extLcurrETH: math.min((timeDiff>0?lowestOf:_t_extLcurrRTH),_t_extLcurrRTH) //math.min: account for the short period between 4pm 'close' and last bar of RTH 'close'
  115.  
  116. var line hiLineCurr = na, var label hiLineLabCurr =na
  117. var line loLineCurr = na, var label loLineLabCurr =na
  118.  
  119. if barstate.islast and showNascent and not inRegSess
  120. hiLineCurr:=isETH?line.new(bar_index, tickerExtHighestCurr, bar_index -barsSinceRTHcloseETH, tickerExtHighestCurr, style= lineStyle2, extend =extend.none,color =color.blue, width =lineWidth):
  121. line.new(bar_index, tickerExtHighestCurr,bar_index-5, tickerExtHighestCurr, style= line.style_dotted, extend =extChoice?extend.right:extend.none,color =color.blue, width =lineWidth)
  122. loLineCurr:=isETH?line.new(bar_index, tickerExtLowestCurr, bar_index -barsSinceRTHcloseETH, tickerExtLowestCurr, style= lineStyle2, extend =extend.none,color =color.blue, width =lineWidth):
  123. line.new(bar_index, tickerExtLowestCurr, bar_index-5, tickerExtLowestCurr, style= line.style_dotted, extend =extChoice?extend.right:extend.none,color =color.blue, width =lineWidth)
  124. hiLineLabCurr:=isETH?label.new(bar_index,tickerExtHighestCurr+(vertOffset*atr), text=showLabels?str.tostring(tickerExtHighestCurr):"", style = label.style_label_down,color = colorNone,textcolor =textCol, size =textSize):
  125. label.new(bar_index,tickerExtHighestCurr+(vertOffset*atr), text=showLabels?str.tostring(tickerExtHighestCurr):"", style = label.style_label_down,color = colorNone,textcolor =textCol, size =textSize)
  126. loLineLabCurr:=isETH?label.new(bar_index, tickerExtLowestCurr-(vertOffset*atr), text=showLabels?str.tostring(tickerExtLowestCurr):"", style = label.style_label_up,color = colorNone,textcolor =textCol, size =textSize):
  127. label.new(bar_index,tickerExtLowestCurr-(vertOffset*atr), text=showLabels?str.tostring(tickerExtLowestCurr):"", style = label.style_label_up,color = colorNone,textcolor =textCol, size =textSize)
  128. if tickerExtHighestCurr!=tickerExtLowestCurr[1]
  129. line.delete(hiLineCurr[1])
  130. label.delete(hiLineLabCurr[1])
  131. line.delete(loLineCurr[1])
  132. label.delete(loLineLabCurr[1])
  133.  
  134. if inRegSess
  135. line.delete(hiLineCurr)
  136. label.delete(hiLineLabCurr)
  137. line.delete(loLineCurr)
  138. label.delete(loLineLabCurr)
  139.  
  140. ///****************** debugging
  141.  
  142. // var Table = table.new(position.top_right, columns = 14, rows = 5, bgcolor = color.orange, border_width = 1)
  143. // if barstate.islast
  144. // table.cell(Table, 0, 0, "DEBUG TABLE", bgcolor=color.blue)
  145. // table.cell(Table, 1, 0, "_t_extHcurrRTH = "+str.tostring(_t_extHcurrRTH))
  146. // table.cell(Table, 1, 1, "timeDiff = " +str.tostring(timeDiff))
  147. // table.cell(Table, 0, 2, "lowestOf = " +str.tostring(lowestOf),bgcolor = color.yellow)
  148. // table.cell(Table, 3, 2, "tickerExtHighestCurr = " +str.tostring(tickerExtHighestCurr),bgcolor = color.green)
  149. // table.cell(Table, 0, 3, "highestOf = " +str.tostring(highestOf),bgcolor = color.green)//
Advertisement
Add Comment
Please, Sign In to add comment