Search code examples
pine-scriptpine-script-v5tradingview-api

Code in Pinescript V5 in Tradingview check for Open=Low condition at one bar later


I am trying to build a code in Pinescript V5 in Tradingview which check for a Open=Low condition one bar later. For example if Market open time is 9:15 AM, the code should fire only once at 9:16 AM to check if the previous bar (the bar at 9:15 AM) had Open=Low condition met

I could only frame till this part

//@version=5
indicator("Testing Alert on Open=Low", overlay=true)

var bool isLow_high = na
var bool alerted = false

// Market open time (adjust as per your market's opening time)
marketOpenTime = timestamp(year, month, dayofmonth, 09, 15)

// Detect if it's a new trading day
isNewDay = dayofmonth != dayofmonth[1]

Solution

  • You can write your conditions for the first bar of the session and check that with the history reference operator [] on the next bar to see if it was true.

    Please note that, this will only work on the 1-min timeframe.

    //@version=5
    indicator("My script", overlay=true)
    
    marketOpenTime = (timestamp(year, month, dayofmonth, 09, 30) == time)
    condition = (low == open)
    
    was_condition_met_at_open = marketOpenTime[1] and condition[1]
    
    plotshape(was_condition_met_at_open, "S", shape.triangledown, size=size.normal)
    

    enter image description here

    I had to change the start time to 09:30 as that's when the new day starts for this example.