jimfred
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: July 2nd, 2021
Contact: TradingView Profile

Add date to label

I am trying to add the date to labels on every significant pivot on the chart. For example, the script PIVOT HILO prints the hi/lo value of the pivot on a label above or below the candle pivot. But I want to also include the date on the label.

Anyone have an idea how to grab the date, for this purpose?

Thanks...

CanYouCatchMe01
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: June 28th, 2021

Re: Add date to label

You can do this.
Image

I get the year as an integer and convert it to a string and then add the text on the label. I was not able to get the day. There is a variable called dayofmonth, but that seems to be another thing.

code:

Code: Select all

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

//@version=4
study("date pivot points", overlay=true)
prd = input(defval = 20, title="Pivot Point Period", minval = 10, maxval = 50)

float ph = na, float pl = na //If there is no pivit points. Default value
ph := pivothigh(prd, prd)
pl := pivotlow(prd, prd)

date = tostring(year) + "-" + tostring(month)

if (ph) //If 20 days ago was a pivot point
    label.new(bar_index-prd, ph, date, yloc=yloc.abovebar, color=color.green) //NOTE: bar_index-prd, becuse it occured 20 days ago
if (pl)
    label.new(bar_index-prd, pl, date, yloc=yloc.belowbar, color=color.red)

Return to “Pine Script Q&A”