Search code examples
pine-scriptalgorithmic-tradingtrading

Pine Script - How to get the high of current day, till current bar


I would like to get the high value of the current day. And the high value should be taken from previous bars of the current day.

I tried this code

security(syminfo.ticker, "D", high, lookahead=barmerge.lookahead_on)

But the problem is that it is taking the high value of the current day after all the current day's candles.

Thank you.


Solution

  • newDay = change(time("D")) != 0
    
    float todaysHigh = na
    
    if newDay
        todaysHigh := high
    else if high > todaysHigh[1]
        todaysHigh := high
    else
        todaysHigh := todaysHigh[1]
    

    if block can also be written as a ternary :

    todaysHigh := newDay ? high : high > todaysHigh[1] ? high : todaysHigh[1]