Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // Only working properly on ES, NQ, YM (ES1!, ESM2023, etc etc). Trouble with SPY due to odd behaviour counting premarket hours
- // © twingall
- //@version=5
- indicator("Pre-market Highs & Lows on RTH chart", overlay = true, max_bars_back = 5000)
- rthOpen = input.session('0930-0931', "RTH open (New York time)", options =['0930-0931','1030-1031','1130-1131', '0830-0831'])
- _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")
- showHighs = input.bool(true, "show pre-market high", inline ='1')
- showLows = input.bool(true, "show pre-market low", inline ='2')
- maxDayLines = input.int(3, minval = 1, title = "days of history to show")
- showLabels = input.bool(true, "labels", group = 'formatting', inline ='1')
- textCol = input.color(color.black, "||", group = 'formatting', inline ='1')
- textSize = input.string(size.normal, "size", options =[size.auto, size.tiny, size.small, size.normal, size.large, size.huge], group = 'formatting', inline ='1')
- 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")
- showLines = input.bool(true, "lines", group = 'formatting', inline ='2')
- lineCol = input.color(color.fuchsia, "||", group = 'formatting', inline ='2')
- lineWidth = input.int(1, "width", group = 'formatting', inline ='2')
- _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)")
- 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)")
- extChoice = input.bool(false, "extend lines fully to the right",group = 'formatting')
- string lineStyle = switch _lineStyle
- "arrow left" => line.style_arrow_left
- "solid" => line.style_solid
- "dotted" => line.style_dotted
- "dashed" => line.style_dashed
- atr = ta.atr(400)
- colorNone = color.new(color.white, 100)
- rthStartSess=time(timeframe.period, rthOpen, 'America/New_York')
- inRtSt = nz(rthStartSess)
- ETHextraTime = math.round(1000*3600*_hour) //pre-market time in ms 1000*3600*_hour
- barT = timeframe.in_seconds(timeframe.period)*1000
- barsETH = math.round(ETHextraTime/ barT)
- extendedTicker = ticker.modify(syminfo.ticker, session = session.extended)
- //condition to see if our chart is toggled to electronic trading hours (ETH) or regular trading hours (RTH)
- _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
- outOfHoursClose = ta.valuewhen(time==_7_00, close,0)
- bool isETH = nz(outOfHoursClose)
- 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
- 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
- var line hiLine = na
- var label hiLineLab =na
- var array<line> hiLineArr = array.new<line>(0)
- var array<label> hiLineLabArr = array.new<label>(0)
- var line loLine = na
- var label loLineLab =na
- var array<line> loLineArr = array.new<line>(0)
- var array<label> loLineLabArr = array.new<label>(0)
- if inRtSt and not inRtSt[1] and showHighs
- 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):
- 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)
- 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):
- 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)
- array.push(hiLineArr, hiLine)
- array.push(hiLineLabArr,hiLineLab)
- if array.size(hiLineArr)>maxDayLines
- line.delete(array.shift(hiLineArr))
- label.delete(array.shift(hiLineLabArr))
- if inRtSt and not inRtSt[1] and showLows
- 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):
- 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)
- 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):
- 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)
- array.push(loLineArr, loLine)
- array.push(loLineLabArr,loLineLab)
- if array.size(loLineArr)>maxDayLines
- line.delete(array.shift(loLineArr))
- label.delete(array.shift(loLineLabArr))
- ////////////////// 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
- 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")
- colNascent = input.color(color.blue, "Line Color: ", group = 'Plot Currently forming preMkt Highs/Lows', inline = '2')
- _lineStyle2 = input.string("dotted", "| Line style:", options =["arrow left" ,"solid", "dotted", "dashed"], group = 'Plot Currently forming preMkt Highs/Lows', inline ='2')
- inpRegTradHrs =input.session('0930-1600', 'regular session', group = 'Plot Currently forming preMkt Highs/Lows')
- regSess=time(timeframe.period, inpRegTradHrs, 'America/New_York')
- string lineStyle2 = switch _lineStyle2
- "arrow left" => line.style_arrow_left
- "solid" => line.style_solid
- "dotted" => line.style_dotted
- "dashed" => line.style_dashed
- inRegSess =not na(regSess)
- barsSinceRTHcloseETH = ta.barssince(inRegSess)
- //turns out can only retrieve ETH price info from RTH toggled chart by using request.security_lower_tf (request.security() won't work)
- timeDiff = timenow-last_bar_time
- extHighsArr = request.security_lower_tf(extendedTicker, timeframe.period, timeDiff>0?high:0)
- if timeDiff ==0
- array.clear(extHighsArr)
- highestOf = array.max(extHighsArr)
- extLowsArr = request.security_lower_tf(extendedTicker, timeframe.period, timeDiff>0?low:0)
- if timeDiff ==0
- array.clear(extLowsArr)
- lowestOf = array.min(extLowsArr)
- _t_extHcurrRTH = request.security(extendedTicker, timeframe.period, ta.highest(high, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1))
- _t_extHcurrETH = ta.highest(high, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1)
- 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'
- _t_extLcurrRTH = request.security(extendedTicker, timeframe.period, ta.lowest(low, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1))
- _t_extLcurrETH = ta.lowest(low, barsSinceRTHcloseETH>0?barsSinceRTHcloseETH:1)
- 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'
- var line hiLineCurr = na, var label hiLineLabCurr =na
- var line loLineCurr = na, var label loLineLabCurr =na
- if barstate.islast and showNascent and not inRegSess
- hiLineCurr:=isETH?line.new(bar_index, tickerExtHighestCurr, bar_index -barsSinceRTHcloseETH, tickerExtHighestCurr, style= lineStyle2, extend =extend.none,color =color.blue, width =lineWidth):
- line.new(bar_index, tickerExtHighestCurr,bar_index-5, tickerExtHighestCurr, style= line.style_dotted, extend =extChoice?extend.right:extend.none,color =color.blue, width =lineWidth)
- loLineCurr:=isETH?line.new(bar_index, tickerExtLowestCurr, bar_index -barsSinceRTHcloseETH, tickerExtLowestCurr, style= lineStyle2, extend =extend.none,color =color.blue, width =lineWidth):
- line.new(bar_index, tickerExtLowestCurr, bar_index-5, tickerExtLowestCurr, style= line.style_dotted, extend =extChoice?extend.right:extend.none,color =color.blue, width =lineWidth)
- 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):
- label.new(bar_index,tickerExtHighestCurr+(vertOffset*atr), text=showLabels?str.tostring(tickerExtHighestCurr):"", style = label.style_label_down,color = colorNone,textcolor =textCol, size =textSize)
- 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):
- label.new(bar_index,tickerExtLowestCurr-(vertOffset*atr), text=showLabels?str.tostring(tickerExtLowestCurr):"", style = label.style_label_up,color = colorNone,textcolor =textCol, size =textSize)
- if tickerExtHighestCurr!=tickerExtLowestCurr[1]
- line.delete(hiLineCurr[1])
- label.delete(hiLineLabCurr[1])
- line.delete(loLineCurr[1])
- label.delete(loLineLabCurr[1])
- if inRegSess
- line.delete(hiLineCurr)
- label.delete(hiLineLabCurr)
- line.delete(loLineCurr)
- label.delete(loLineLabCurr)
- ///****************** debugging
- // var Table = table.new(position.top_right, columns = 14, rows = 5, bgcolor = color.orange, border_width = 1)
- // if barstate.islast
- // table.cell(Table, 0, 0, "DEBUG TABLE", bgcolor=color.blue)
- // table.cell(Table, 1, 0, "_t_extHcurrRTH = "+str.tostring(_t_extHcurrRTH))
- // table.cell(Table, 1, 1, "timeDiff = " +str.tostring(timeDiff))
- // table.cell(Table, 0, 2, "lowestOf = " +str.tostring(lowestOf),bgcolor = color.yellow)
- // table.cell(Table, 3, 2, "tickerExtHighestCurr = " +str.tostring(tickerExtHighestCurr),bgcolor = color.green)
- // table.cell(Table, 0, 3, "highestOf = " +str.tostring(highestOf),bgcolor = color.green)//
Advertisement
Add Comment
Please, Sign In to add comment