1

Guys I need help to add labels in script. Now the indicator is showing labels for today only.

How do we enable labels for all previous days? When we tried using plotchar parameter, labels showing correctly for all previous days. But, I don't want to use the plotchart in this script. Which code need to use see previous day labels?

Here is the code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pbghosh

//@version=5
indicator(title='Daily CPR', shorttitle='D-CPR', overlay=true, format=format.price, precision=2)

blue = color.blue
fuchsia = color.fuchsia
white = color.white

var color_TC = input.color(defval=color.new(blue, 0), title='TC', group='CPR')
var color_CP = input.color(defval=color.new(fuchsia, 0), title='CP', group='CPR')
var color_BC = input.color(defval=color.new(blue, 0), title='BC', group='CPR')
var color_R1 = input.color(defval=color.new(#FF4000,0), title='R1', group='CPR')
var color_R2 = input.color(defval=color.new(#FF4000 ,0), title='R2', group='CPR')
var color_R3 = input.color(defval=color.new(#FF4000, 0), title='R3', group='CPR')
var color_R4 = input.color(defval=color.new(#FF4000, 0), title='R4', group='CPR')
var color_R5 = input.color(defval=color.new(#FF4000,0), title='R5', group='CPR')
var color_S1 = input.color(defval=color.new(#FF4000,0), title='S1', group='CPR')
var color_S2 = input.color(defval=color.new(#FF4000, 0), title='S2', group='CPR')
var color_S3 = input.color(defval=color.new(#FF4000, 0), title='S3', group='CPR')
var color_S4 = input.color(defval=color.new(#FF4000,0), title='S4', group='CPR')
var color_S5 = input.color(defval=color.new(#FF4000,0), title='S5', group='CPR')
var color_PDH = input.color(defval=color.new(white, 0), title='PDH', group='CPR')
var color_PDL = input.color(defval=color.new(white, 0), title='PDL', group='CPR')


h = request.security(syminfo.tickerid, 'D', high[1], lookahead=barmerge.lookahead_on)
l = request.security(syminfo.tickerid, 'D', low[1], lookahead=barmerge.lookahead_on)
c = request.security(syminfo.tickerid, 'D', close[1], lookahead=barmerge.lookahead_on)

showlabel = true
drawlabel(level, title, color) => label.delete(label.new(bar_index, level, title + " " + "(" + str.tostring(level, format.mintick) + ")", color=#00000000, style=label.style_label_left, textcolor=color)[1])
        
notHigherTimeframe = timeframe.period != '30' and timeframe.period != '45' and timeframe.period != '60' and timeframe.period != '120' and timeframe.period != '180' and timeframe.period != '240' and timeframe.period != 'D' and timeframe.period != 'W' and timeframe.period != 'M'

calculatePivot() =>
    (h + l + c) / 3

calculateBC() =>
    (h + l) / 2

calculateTC() =>
    calculatePivot() - calculateBC() + calculatePivot()

truncate(number, decimals) =>
    factor = math.pow(10, decimals)
    int(number * factor) / factor
    //round(number * 10) / 10

pivot = calculatePivot()
tc = calculateTC()
bc = calculateBC()

if tc < bc
    t = tc
    tc := bc
    bc := t
    bc

r1Level = truncate(2 * pivot - l, 2)
r2Level = truncate(pivot + h - l, 2)
r3Level = truncate(h + 2 * (pivot - l), 2)
r4Level = truncate(h + 3 * (pivot - l), 2)
r5Level = truncate(h + 4 * (pivot - l), 2)
s1Level = truncate(2 * pivot - h, 2)
s2Level = truncate(pivot - (h - l), 2)
s3Level = truncate(l - 2 * (h - pivot), 2)
s4Level = truncate(l - 3 * (h - pivot), 2)
s5Level = truncate(l - 4 * (h - pivot), 2)

plot(series=notHigherTimeframe ? truncate(tc, 2) : na, title='TC', color=color.new(color_TC, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? truncate(pivot, 2) : na, title='CP', color=color.new(color_CP, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? truncate(bc, 2) : na, title='BC', color=color.new(color_BC, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? r1Level : na, title='R1', color=color.new(color_R1, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? r2Level : na, title='R2', color=color.new(color_R2, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? r3Level : na, title='R3', color=color.new(color_R3, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? r4Level : na, title='R4', color=color.new(color_R4, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? r5Level : na, title='R5', color=color.new(color_R5, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? s1Level : na, title='S1', color=color.new(color_S1, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? s2Level : na, title='S2', color=color.new(color_S2, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? s3Level : na, title='S3', color=color.new(color_S3, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? s4Level : na, title='S4', color=color.new(color_S4, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? s5Level : na, title='S5', color=color.new(color_S5, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? h : na, title='PDH', color=color.new(color_PDH, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)
plot(series=notHigherTimeframe ? l : na, title='PDL', color=color.new(color_PDL, pivot[1] != pivot and notHigherTimeframe ? 100 : 0), style=plot.style_line, linewidth=1)

if showlabel and timeframe.isintraday
    drawlabel(truncate(tc, 2), title='TC', color=color_TC)
    drawlabel(truncate(pivot, 2), title='CP', color=color_CP)
    drawlabel(truncate(bc, 2), title='BC', color=color_BC)
    drawlabel(r1Level, title='R1', color=color_R1)
    drawlabel(r2Level, title='R2', color=color_R2)
    drawlabel(r3Level, title='R3', color=color_R3)
    drawlabel(r4Level, title='R4', color=color_R4)
    drawlabel(r5Level, title='R5', color=color_R5)
    drawlabel(s1Level, title='S1', color=color_S1)
    drawlabel(s2Level, title='S2', color=color_S2)
    drawlabel(s3Level, title='S3', color=color_S3)
    drawlabel(s4Level, title='S4', color=color_S4)
    drawlabel(s5Level, title='S5', color=color_S5)
    drawlabel(h, title='PDH', color=color_PDH)
    drawlabel(l, title='PDL', color=color_PDL)
cryptoweeknd
  • 37
  • 1
  • 8

1 Answers1

1
  1. label.delete(...[1]) you're always deleting the previous bar labels. So remove/refactor it
  2. set max_labels_count = 500 in your indicator definition. By that you'll get the most possible labels displayed on chart

/////////////////////////
EDIT after further clarification in comments
/////////////////////////

  1. max_lines_count = 500 extend your indicator definition also by that
  2. extend the conditional to add your labels as follows:
if showlabel and timeframe.isintraday and hour == 0 and minute == 0

Why?
Because like I said, if you look closely everything was there for you (after removing delete()) but since you printed on each bar (every 5min-15min resp.) the max limit was reached before you could count back just a couple of days. With this addition you only add one label once per pivot line instead of every 5 minute or so. Since you're importing your data daily that's why I chose 00:00 for setting the label just once.

elod008
  • 1,227
  • 1
  • 5
  • 15
  • Sorry for the delay. My internet went down. When I change the `label.delete` (line 33) to this `label.new(bar_index, level, title + " " + "(" + str.tostring(level, format.mintick) + ")", color=#00000000, style=label.style_label_left, textcolor=color) ` it didn't showing previous labels, but current labels look like [this.](https://i.postimg.cc/d1s23CMd/Untitled-1.jpg) – cryptoweeknd Sep 04 '22 at 22:44
  • 1
    I tried your code once again and on a 4h chart you should see pivots for at least 4 bars. Make sure your indicator(..., max_labels_count = 500) is as high as you need/possible, since you are drawing multiple labels on a single bar which means that the drawings on the bar are very limited. Again, I copied your code from above, set the "max_labels_count=500", removed delete() and chose 4h so that I'm on intraday and I see 5 bars with labels on eg.: BTCUSDT. If this result is not what you like you'll have to refactor your code. – elod008 Sep 05 '22 at 07:15
  • I'm using 1min to 15min chart for trading. Not bigger time frame. I use `max_labels_count` [too.](https://i.postimg.cc/v87tp6Pf/code.jpg) Here is the my 4h [result.](https://i.postimg.cc/3RF0b2Kw/4h.jpg) There is no lines. – cryptoweeknd Sep 05 '22 at 08:30
  • 1
    Labels are there on your "4th result" like you wanted. So that's checked. I'd suggest adding "max_lines_count = 500" too after max_labels_count in your indicator() definition. I've just tried your code again, on 15min chart. There are several lines through several bars. So if you obey your "notHigherTimeframe" condition, you have to see your plots too. – elod008 Sep 05 '22 at 09:14
  • I need the labels on the end of the line only like [this.](https://i.postimg.cc/763x0v80/plotchar.jpg) That script is used `plotchar()` function. The posted script here have `drawlabel` function. When we delete `label.delete` and add `max_labels_count=500, max_lines_count=500` it will add labels on every [candles.](https://i.postimg.cc/qRh150M6/candle-label.jpg) I don't want that. Only want one label that is end of the line like [this](https://i.postimg.cc/763x0v80/plotchar.jpg) or like [this](https://i.postimg.cc/NMDjZhJY/meant.jpg) – cryptoweeknd Sep 05 '22 at 17:46
  • mate, after adding `if showlabel and timeframe.isintraday and hour == 0 and minute == 0` it is showing. but, still have some issues. `1)` it's showing 4days labels only (even used max_count=5000). `2)` on 4th day, some of the labels not showing - TC, CP, BC, R1, R2 R3 [screenshot.](https://i.postimg.cc/xCwBXy22/Untitled-1.jpg). `3)` Not working on Indian Indices [NIFTY](https://www.tradingview.com/symbols/NSE-NIFTY/) and [BANKNIFTY](https://www.tradingview.com/symbols/NSE-BANKNIFTY/) – cryptoweeknd Sep 08 '22 at 21:26
  • 1
    I hear you. But you need to understand this: there is a maximum limit for displaying labels and that is 500 bars back in pine script. Not one more. Look at your screenshot how many bars you're viewing. Of course the lower your time frame the less days you're going to see. Because one bar does not represent a day per se. If you're on 1min you won't even get a full day out... With 15min you'll see the max possible ~4 days (3+ some hours). That really ALL you can achieve with your preferences in pine script. Exactly what you image is unfortunatelly not possible. – elod008 Sep 09 '22 at 06:38
  • Thanks mate. Now I understand. Please check the new question I raised. – cryptoweeknd Sep 09 '22 at 07:21