Search code examples
pine-scriptpine-script-v5

accessing past dates high / low that are not visible


I am attempting to write a script to draw horizontal lines from past highs. For example, say I wanted to draw a horizontal line on the high of the first Monday of the last 5 months. Also, would like to not have to have all these months in view on the chart at the time.

I currently have an array of days I can iterate through, but looking at the docs, it does not appear there is a way to get the high / low of a security on a specific past date.

In version 5 (the only version I have worked with) using request.security does not work in a loop, and even if it did, it does not appear to allow to get security high on a given timestamp but only at a candle point.

This seems like a pretty simple thing to do, but the docs do not show me anything that resembles what I need.

I feel like I may be going about it wrong. Pine script is an interesting time based language that I am not familiar with yet. Any ideas out there?


Solution

  • How does pine script work?

    When a Pine script is loaded on a chart it executes once on each historical bar using the available OHLCV (open, high, low, close, volume) values for each bar

    source

    This means if you want to get the "past" highs, you'll have to mark the current highs meeting your criteria for the future. Think about it as working real-time only as if you would have done it the last n months continuously though. Then you'll receive your testing results all at once on the latest bar.

    The following example would run on each bar on execution up to the latest and would plot horizontal lines on monday high values producing the result you'd expect.

    ...
    
    mHigh = ta.highest(high, timeframe.in_seconds("1D") / timeframe.in_seconds())[1] // one possibility to get high for monday
    if timeframe.change("1D") and dayofweek == dayofweek.tuesday
        line.new(bar_index - 1, mHigh, bar_index, mHigh, extend = extend.right) // HINT: hline() won't work
    ...