Rick911
Rick911

Reputation: 1

Need help using line.new to get x1 value

I made a script to draw premarket high using the plot() function but I'd like to use the line.new() function to make a cleaner indicator on the chart.

This is my original indicator with the plot() that is working.

//@version=4

study("PreMarket High", shorttitle="preH", overlay=true)

// Get the premarket high value 
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", type=input.integer)
ending_minute = input(defval=30, title="Ending Minute", type=input.integer)

day_high = 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
   
else
    day_high := day_high[1]
   
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

// Draw premarket high on chart
plot(day_high, style=plot.style_line, color=#ffeb3b, linewidth=1, trackprice = true, offset = -9999, title="PreHigh")

Now I've been testing using the line.new function like this

//@version=4

study("PreMarket High", shorttitle="preH", overlay=true)

// Get the premarket high value 
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", type=input.integer)
ending_minute = input(defval=30, title="Ending Minute", type=input.integer)

day_high = 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
   
else
    day_high := day_high[1]
   
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

// Draw premarket high on chart

var line l = line.new(bar_index[20], day_high, bar_index, day_high, color=color.lime, width=2, extend=extend.right)
line.set_x1(l, bar_index[20])
line.set_x2(l, bar_index)
line.set_y1(l, day_high)
line.set_y2(l, day_high)

I've used an arbitrary value of bar_index[20] for x1 to test and the indicator seems to be working but I'd like to be able to get the bar_index of the premarket high candle.

Is there a way to get the bar_index value of the variable 'day_high' since it's the candle I'd like to start the line from?

I've tried using barssince but it's not working and I am stuck. Should I be using xloc instead?

Thanks!

Upvotes: 0

Views: 582

Answers (1)

Rick911
Rick911

Reputation: 1

Figured it out using the timestamp function.

// Today's Session Start timestamp
y = year(timenow)
m = month(timenow)
d = dayofmonth(timenow)

// Start & End time for Today
start = timestamp(y, m, d, 07, 00)
end = start + 43200000

var line l_high = line.new(start, day_high, end, day_high, color=color.yellow, width=1, xloc=xloc.bar_time)
line.set_x1(l_high, start)
line.set_x2(l_high, end)
line.set_y1(l_high, day_high)
line.set_y2(l_high, day_high)

Upvotes: 0

Related Questions