Search code examples
pine-scriptpine-script-v5pine-script-v4

Draw trendline from start of day to end of day using specific start price and close price


Can someone please help me modify below code to use market close time instead of bar_index+1 which can also help me omit openLine.set line as well

//@version=5
indicator("My script", overlay = true)
newDay = ta.change(time('D'))
var line openLine = na
if newDay
    openLine := line.new(bar_index, 2534.05, bar_index+1, 2481.90)
openLine.set_x2(bar_index)

In above code instead of bar_index+1 i want to specify end of day time for eg:- indian market close at 15:25pm so i can avoid openLine.set as well


Solution

  • Get the timestamp of the close bar using the timestamp() function.

    timestamp(year, month, day, hour, minute, second) → simple int
    

    Then use xloc=xloc.bar_time in your line.new() so it will use times instead of bar indices.

    Then you can pass time to the x1 argument and the return value of the timestamp() to the x2.