Carlos Romero
Carlos Romero

Reputation: 13

Pre-market Open Pine Script Ver5

I 've been starting to dabble in writing some scripts for my day trading. I am currently stuck on how to get the premarket open price to stay as that. Currently what I have it shows the open then it follows the price action to the high side. It end up showing the PM high when the regular trading hrs start at 0930. Any help on this would be greatly appreciated!

//@version=5

indicator("testCYD PreMarket High/Low Mid", shorttitle="CYD PM H/L Mid", overlay=true)
t = time("1440", "0000-0930") 

is_first = na(t[1]) and not na(t) or t[1] < t

ending_hour = input(defval=9, title="Ending Hour")
ending_minute = input(defval=29, title="Ending Minute")

day_high = float(na)
day_low = float(na)

if is_first and barstate.isnew and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
    day_high := high
    day_low := low
    day_low
else
    day_high := day_high[1]
    day_low := day_low[1]
    day_low

if high > day_high and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
    day_high := high
    day_high

if low < day_low and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
    day_low := low
    day_low


plot(day_high, style=plot.style_line, color=#009688, linewidth=2, title="PM High")
plot((day_low + day_high) / 2, color=#fff9c4, linewidth=2, title="PM Mid")
plot(day_low, style=plot.style_line, color=#009688, linewidth=2, title="PM Low")


/////////////////
//Pre-Market Open

PMOTime = input.session('0400-0929:1234567', "Session", group="PreMarket Open")
PMOStyle = input.string ("Dashed", "Line Style", options=["Solid", "Dotted", "Dashed"], group="PreMarket Open")
PMOColor = input.color (#09b1d3, group="PreMarket Open")


tPMO = time          ("1", PMOTime)

_PMOStyle   = PMOStyle ==    "Solid" ? line.style_solid : PMOStyle == "Dotted" ? line.style_dotted : line.style_dashed

var line lne = na
var openPMO  = 0.0

if tPMO
    if not tPMO[1]
        openPMO  := open
    else
        openPMO := math.max(open, openPMO)
if openPMO != openPMO[1]
    if barstate.isconfirmed
        line.set_x2(lne, tPMO)
        lne := line.new(tPMO, openPMO, last_bar_time + 14400000/2, openPMO, xloc.bar_time, extend.none, PMOColor, _PMOStyle, 2)

//===========================


Upvotes: 0

Views: 1205

Answers (1)

beeholder
beeholder

Reputation: 1699

Currently what I have it shows the open then it follows the price action to the high side.

This seems to be caused by the following line: openPMO := math.max(open, openPMO). On each bar, you change the value of the openPMO if the current open is higher than the value that's saved into openPMO.

If I understood it correctly and your goal is just to have the pre-market open saved into a variable, then you can accomplish it easier like this:

//@version=5
indicator("My script", overlay = true)

var float premarketOpen = na
isFirstPremarketBar = session.ispremarket and not session.ispremarket[1]

if isFirstPremarketBar
    premarketOpen := open

plot(premarketOpen)

Upvotes: 0

Related Questions