Search code examples
pine-script-v5

Draw horizontal line at open price of a particular candle while chart is in different timeframe


I need to draw a horizontal line for each session till the end of the session. The line should start from a specific time after the session starts and the timeframe of this candle is 3 minute. But my chart will be in a different timeframe let's say 15 min or 30 min. Example

My chart timeframe is 15 minutes, and I need to draw a horizontal line at the open price of lets say 9:33 with candle timeframe 3 minute. What I got so far is given below

This will draw the horizontal line at the given time

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

var barCount = 0
count = ta.barssince(session.isfirstbar)
if session.islastbar
    barCount := count 

var openPrice = 0.0
openPrice := (hour== 9 and minute == 33) ? open : openPrice
DayOpen = request.security(syminfo.tickerid, "1", open)


plot(openPrice)

But this code will draw line only if the chart time is 3 minutes, if I switch to 15 min, then the above code won't draw the line.


Solution

  • //@version=5
    indicator("Help  openTF3m", overlay = true)
    
    var barCount = 0
    count = ta.barssince(session.isfirstbar)
    if session.islastbar
        barCount := count 
    
    var float openPrice = na
    //openPrice := (hour(time) == 9 and minute(time) == 33 ) ? begin : openPrice
    DayOpen = request.security(syminfo.tickerid, "1", open)
    
    array<float> begin = request.security_lower_tf(syminfo.tickerid, "1", open)
    int SizeArr = array.size(begin)
    int tt = timestamp(year, month, dayofmonth, 09, 33, 00)
    
    if time <= tt and tt <= (time_close - 1) and (timenow - time)/(60*1000) < 100000
        i = SizeArr - (time_close("", 0) - tt)/(60*1000)
        openPrice := array.get(begin, i)   
    
    plot(openPrice, "openPrice")
    

    Note that: The plots based on intrabar data may not appear on all available chart bars, as request.security_lower_tf() can only access up to the most recent 100,000 intrabars available from the requested context. When executing this function on a chart bar that doesn’t have accessible intrabar data, it will return an empty array.

    SPY on TF30m