0

What code to use to know if we want to print numbers on candle, such as if i want to plot 30 on 30'th candle, 7 on 7'th candle using index divisible in every timeframe.

novice in pine world, help would appriciate.

before i am using v_line with bar_index 30, but this will help clean up chart. here is what i have done.

//@version=5 indicator(title='candle count', overlay=true,max_labels_count=500)

text_color=input.color(color.blue,title="Text Color Input")

numBars = 0

t = time('D')

if t == t[1] numBars := nz(numBars[1]) + 1 numBars else numBars := 1 numBars

label.new(bar_index [30], high, str.tostring(numBars), textcolor=text_color, style=label.style_none)

but this will plot on all the candles i want it on only pirticular number candle.

1 Answer 1

0

You can use modulo operator in pine script - %. For example, bar_index % 30 == 0 will return true on each 30th bar, since the remainder of each 30th bar is equal to 0.

Then you can just add labels each time the condition is true:

//@version=5 indicator("My script", overlay = true) is_bar_divisible_by_30 = bar_index % 30 == 0 is_bar_divisible_by_7 = bar_index % 7 == 0 if is_bar_divisible_by_30 label.new(bar_index, high, "30", color = color.green, textcolor=color.white, style=label.style_label_down) if is_bar_divisible_by_7 label.new(bar_index, high * 1.1, "7", color = color.aqua, textcolor=color.white, style=label.style_label_down) 

This will work on every timeframe.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the comment, i want them only one time.
Which time? The first occurrence or the last?
only one time from current candle, 7 on 7'th and 30 on 30th.
The script runs on each bar, and every time it runs, the bar it runs on called "current bar". Do you mean the last bar of the chart? You are looking for the 7th bar before the last bar of the chart? Your question is about modulo operator
yes, i mean 30'th candle from 0 to 30, and only once in each TF,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.