pine-script

Pinescript only plots a few dates


how indicator shows on chart

I'm trying to mark specific dates on the chart as bullish or bearish based on an external signal.

In this script I supplied 9 dates, but only 3 were plotted. Why?

All the 9 dates in script below have price data bars in tradingview data.

This may not be the best way to import dates in bulk, however given Pinescript's lack of import function (outside of their own data source) this is the route I have chosen.

Thanks!

//@version=5
indicator("Custom Callout", overlay=true)

// Create separate arrays for each field
symbols = array.new_string(10)
dates = array.new_int(10)
labels = array.new_string(10)
colors = array.new_color(10)

// Populate the arrays
array.set(symbols, 0, "ZC1!")
array.set(dates, 0, 20241108)
array.set(labels, 0, "Bullish")
array.set(colors, 0, color.green)

array.set(symbols, 1, "ZC1!")
array.set(dates, 1, 20241011)
array.set(labels, 1, "Bullish")
array.set(colors, 1, color.green)

array.set(symbols, 2, "ZC1!")
array.set(dates, 2, 20240510)
array.set(labels, 2, "Bearish")
array.set(colors, 2, color.red)

array.set(symbols, 3, "ZC1!")
array.set(dates, 3, 20240411)
array.set(labels, 3, "Bullish")
array.set(colors, 3, color.green)

array.set(symbols, 4, "ZC1!")
array.set(dates, 4, 20240208)
array.set(labels, 4, "Bearish")
array.set(colors, 4, color.red)

array.set(symbols, 5, "ZC1!")
array.set(dates, 5, 20240112)
array.set(labels, 5, "Bearish")
array.set(colors, 5, color.red)

array.set(symbols, 6, "ZC1!")
array.set(dates, 6, 20231109)
array.set(labels, 6, "Bearish")
array.set(colors, 6, color.red)

array.set(symbols, 7, "ZC1!")
array.set(dates, 7, 20230811)
array.set(labels, 7, "Bullish")
array.set(colors, 7, color.green)

array.set(symbols, 8, "ZC1!")
array.set(dates, 8, 20230512)
array.set(labels, 8, "Bearish")
array.set(colors, 8, color.red)



// Convert the current bar's date to YYYYMMDD
current_date = year * 10000 + month * 100 + dayofmonth

// Plot the labels
for i = 0 to array.size(symbols) - 1
    if syminfo.ticker == array.get(symbols, i) and current_date == array.get(dates, i)
        label.new(bar_index, high, array.get(labels, i), style=label.style_label_down, color=array.get(colors, i))

Solution

  • It is because some of the dates do not exist.

    If you add the below two lines, you can see it.

    plotchar(current_date, "current_date", "", color=color.green)
    plotchar(current_date[1], "current_date[1]", "", color=color.red)
    

    It basically displays the current_date's current value in green and its previous value in red.

    Then let's have a look at the first item in your array.

    array.set(symbols, 0, "ZC1!")
    array.set(dates, 0, 20241108)
    array.set(labels, 0, "Bullish1")
    array.set(colors, 0, color.green)
    

    This is supposed to add a label when the date is 20241108. However, there is no bar for this.

    enter image description here

    If you go to, 11.11.2024, you can see that current_date is 20241110 (green). And its previous value is 20241107 (red). So, you see? It jumps from 07 to 10. There is no 08 (20241108).

    Edit:

    Variables like year, month, dayofmonth, will return the value on the time of the bar's open. When your ticker has an overnight session, where Monday session starts on Sunday, those variables will return a value that is lower by 1 than the day of the trading day - which is the case here.

    To fix that, you can use the time_close() function. This function will return the time of the current bar's close.

    You can fix your code as below and then everything should work.

    dayofmonth_fixed = dayofmonth(time_close("1D"))
    
    current_date = year * 10000 + month * 100 + dayofmonth_fixed
    

    Note: You may want to do the same trick for the year and month.

    enter image description here