Reputation: 1
I have written a Pine script that displays the number of each bar/candle and of selected pivot points.
However, the number at the pivot points differs from that of the respective bar, although I use the same variable. In my opinion, the bar number should be the same as the pivot point number, but it isn't.
This is incomprehensible to me. Is there a way to correct this?
(Hint: I need the correct pivot point number in order to process it further in the script).
//@version=5
indicator('(_) number of bars and pivot points', overlay=true, max_labels_count = 500)
// --- input ---------------------
lenPivot = input.int(defval = 28, title = 'Pivot lenghts', minval = 1)
// --- pivot ---------------------
pivotH = ta.pivothigh(high, lenPivot, lenPivot)
pivotL = ta.pivotlow(low, lenPivot, lenPivot)
// --- count ---------------------
countDays() =>
DoW = dayofweek
na(DoW[1]) or DoW != DoW[1]
var counter = 1
if countDays()
counter := 1
else
counter += 1
// ----- labels ------------------
LabelBar = label.new(bar_index, 0, text = str.tostring(counter), yloc = yloc.belowbar,
style = label.style_none, textcolor = color.orange, size = size.normal)
if not na(pivotH)
LabelPivotH = label.new(bar_index[lenPivot], pivotH, text = str.tostring(counter),
color = color.new(color.red, 050), style = label.style_label_down,
textcolor = color.black, size = size.small)
if not na(pivotL)
LabelPivotL = label.new(bar_index[lenPivot], pivotL, text = str.tostring(counter),
color = color.new(color.green, 050), style = label.style_label_up,
textcolor = color.black, size = size.small)
My goal: Display the number of bars at the respective pivot point, calculated back from the last bar (bar_index). This can easily be calculated by: last bar number minus pivot point bar number. However, this assumes that the pivot point bar number is correct - which is not the case with my script.
Upvotes: 0
Views: 122
Reputation: 116
countDays()
is a function that returns true whenever it detects a day change.
counter
is a variable with a default value of 1, incremented by one on every bar. Every time a day change is detected, its value is reset to 1.
Underneath each candle, you print the current value of counter
. I see no issues with this label. Given that you print the current value of counter
under each candle, the labels under the candles will show the correct value of counter
.
The problem is with the labels you print for pivot high/low detections. Pivot detection is a lagging exercise, which means pivotH
or pivotL
will only have a value other than na
once lenPivot
many bars have formed since the bar marking the pivot. Therefore, when you print the pivot high/low labels, you must print them in the past (lenPivot
candles to the left).
But when you print a label in the past, you also need to ensure you use a historical value of counter
; the value of the counter
variable was at lenPivot
bars ago.
Therefore, instead of printing the current value of counter
:
text = str.tostring(counter)
You should print its value lenPivot
candles ago:
text = str.tostring(counter[lenPivot])
Here is the corrected version of your script.
//@version=5
indicator('(_) number of bars and pivot points', overlay=true, max_labels_count = 500)
// --- input ---------------------
lenPivot = input.int(defval = 28, title = 'Pivot lenghts', minval = 1)
// --- pivot ---------------------
pivotH = ta.pivothigh(high, lenPivot, lenPivot)
pivotL = ta.pivotlow(low, lenPivot, lenPivot)
// --- count ---------------------
countDays() =>
DoW = dayofweek
na(DoW[1]) or DoW != DoW[1]
var counter = 1
if countDays()
counter := 1
else
counter += 1
// ----- labels ------------------
LabelBar = label.new(bar_index, 0, text = str.tostring(counter), yloc = yloc.belowbar,
style = label.style_none, textcolor = color.orange, size = size.normal)
if not na(pivotH)
LabelPivotH = label.new(bar_index[lenPivot], pivotH, text = str.tostring(counter[lenPivot]),
color = color.new(color.red, 050), style = label.style_label_down,
textcolor = color.black, size = size.small)
if not na(pivotL)
LabelPivotL = label.new(bar_index[lenPivot], pivotL, text = str.tostring(counter[lenPivot]),
color = color.new(color.green, 050), style = label.style_label_up,
textcolor = color.black, size = size.small)
Upvotes: 0