pine-scriptpine-script-v5

Calculate no of bars to a date in future in pine script


From my formula, I have calculated a future time in UNIX format (Eg = 1748457000000).

My current UNIX time is time (eg = 1746157500000).

This difference is approximately 27.18 days (round to 27 days).

I want to calculate the number of bars in the future and not the days (as shown in the picture using the measure tool)

enter image description here


Solution

  • time() accepts a negative bars_back argument to retrieve the UNIX time up to the 500th bar in the future. You could iterate until the expected number of bars is found:

    int counter = 1
    while time("", -counter) < futureTime
        counter += 1
    log.info("The number of bars is: {0}", counter)