Search code examples
tradingpine-script

Geting indicator value for other timeframe


I am using pine-script to plot the distance from the open to daily Average True Range on an intraday chart. However, when i use an intraday chart the value for atr is not calculated with daily values

d_open = security(tickerid, "D", open)

atr_l1 = d_open - vatr
atrLow =plot(title='atr_l1', series=atr_l1, style=circles, color=lime)

This code plots the ATR based on selected time frame

I would like to access the daily ATR regardless to selected timeframe

dayAtr10() => atr(10)
dailyAtr = security(tickerid, "D", dayAtr10())

Thanks for any tips


Solution

  • You need to pass all daily calculations to the security() call, and use security() in a way that will not repaint. See the PineCoders How to avoid repainting when using security() - PineCoders FAQ indicator for an explanation on how to use security() while avoiding repainting.

    This script shows both repainting and non-repainting methods of using security(). If you leave it on a chart for a while, you will see the discrepancies between both.

    //@version=3
    study("", "", true)
    atrGap = open - atr(10)
    d_openGap = security(tickerid, "D", atrGap)
    plot(d_openGap, "d_openGap", red)
    d_openGapNoRepaint = security(tickerid, "D", atrGap[1], lookahead = barmerge.lookahead_on)
    plot(d_openGapNoRepaint, "d_openGap", green)
    

    enter image description here