Pine Script 5. How to draw lines (High, Low) for individual quarters of the year?

`Indicator of quarterly lines “Quarter & lines”.
Quarters of the year.
Horizontal lines of the quarters.

  1. :
    Lines of Conditional Quarters in a year:
    Day. Month – Day. Month
    1st pair of lines (High, Low): on Friday of the 2nd week. 03 – 5. 06
    2nd pair of lines (High, Low): on Friday of the 2nd week. 06 – 4. 09
    3rd pair of lines (High, Low): on Friday of the 2nd week. 09 – 4. 12
    4th pair of lines (High, Low): on Friday of the 2nd week. 12 – 6. 03

  2. :
    Horizontal lines of the block:

(above the values ​​on the graph, 2 px:)
High – yellow color (default)

(below the values ​​on the graph, 2 px: )
Low – color.navy (default)`

This is what I got, but it’s not quite what I needed.

//@version=5
indicator("Quarter & lines", overlay=true) //, max_lines_count=450)

//---- I ------------------------------------------------------------------------------------

//
//   Setting the colors for the High and Low lines
//
color clr_High = input.color(color.yellow, "Color High")
color clr_Low = input.color(color.navy, "Color Low")



//---- II ------------------------------------------------------------------------------------


// 
//   Function to get the date of the second Friday of the month
// 
second_friday(y, m) =>
    first_day_of_month = timestamp(y, m, 1)
    first_day_of_month_dow = dayofweek(first_day_of_month)
    second_friday_day = 1 + (5 - first_day_of_month_dow + 7) % 7 + 7
    timestamp(y, m, second_friday_day)

// 
//   Defining quarter time intervals
// 
sec_friday_1 = second_friday(year, 3)
Q_1_start = sec_friday_1
Q_1_end = timestamp(year, 6, 5, 23, 59)

sec_friday_2 = second_friday(year, 6)
Q_2_start = sec_friday_2
Q_2_end = timestamp(year, 9, 4, 23, 59)

sec_friday_3 = second_friday(year, 9)
Q_3_start = sec_friday_3
Q_3_end = timestamp(year, 12, 4, 23, 59)

sec_friday_4 = second_friday(year, 12)
Q_3_start = sec_friday_4
Q_4_end = timestamp(year + 1, 3, 6, 23, 59)

// 
//   Function for getting bar index by time
// 
F_Get_BarIndexAtTime(_time) =>
    var int barIndex = na
    if (_time <= time and (_time > time[1] or bar_index == 0))
        barIndex := bar_index
    barIndex

// 
//   Getting bar indexes for each quarter
// 
inBar_q1_start = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_1_start))
inBar_q1_end   = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_1_end))

inBar_q2_start = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_2_start))
inBar_q2_end   = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_2_end))

inBar_q3_start = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_3_start))
inBar_q3_end   = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_3_end))

inBar_q4_start = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_3_start))
inBar_q4_end   = request.security(syminfo.tickerid, "D", F_Get_BarIndexAtTime(Q_4_end))

// 
//   Function for determining High and Low for a quarter
// 
F_Get_High_Low(  startMonth, startDay,   endMonth, endDay  ) =>
    var float highPrice = na
    var float lowPrice = na
    if (month == startMonth and dayofmonth == startDay)
        highPrice := high
        lowPrice := low
    if (month >= startMonth and month <= endMonth)
        if (month == endMonth and dayofmonth > endDay)
            na
        else
            highPrice := math.max(highPrice, high)
            lowPrice := math.min(lowPrice, low)
    [highPrice, lowPrice]

// 
//  Definition of quarterly periods -     [highPrice, lowPrice]
// 
[q1_High,  q1_Low] = F_Get_High_Low( 3, dayofmonth(Q_1_start),   6, dayofmonth(Q_1_end) )
[q2_High, q2_Low] = F_Get_High_Low( 6, dayofmonth(Q_2_start),   9, dayofmonth(Q_2_end) )
[q3_High, q3_Low] = F_Get_High_Low( 9, dayofmonth(Q_3_start),   12, dayofmonth(Q_3_end) )
[q4_High, q4_Low] = F_Get_High_Low( 12, dayofmonth(Q_3_start),   3, dayofmonth(Q_4_end) )

// 
//   Drawing lines and marks
// 
if not na(inBar_q1_start) and not na(inBar_q1_end) and not na(q1_High) and not na(q1_Low)
    line.new(inBar_q1_start, q1_High, inBar_q1_end, q1_High, width=2, color=clr_High)
    line.new(inBar_q1_start, q1_Low, inBar_q1_end, q1_Low, width=2, color=clr_Low)

if not na(inBar_q2_start) and not na(inBar_q2_end) and not na(q2_High) and not na(q2_Low)
    line.new(inBar_q2_start, q2_High, inBar_q2_end, q2_High, width=2, color=clr_High)
    line.new(inBar_q2_start, q2_Low, inBar_q2_end, q2_Low, width=2, color=clr_Low)

if not na(inBar_q3_start) and not na(inBar_q3_end) and not na(q3_High) and not na(q3_Low)
    line.new(inBar_q3_start, q3_High, inBar_q3_end, q3_High, width=2, color=clr_High)
    line.new(inBar_q3_start, q3_Low, inBar_q3_end, q3_Low, width=2, color=clr_Low)

if not na(inBar_q4_start) and not na(inBar_q4_end) and not na(q4_High) and not na(q4_Low)
    line.new(inBar_q4_start, q4_High, inBar_q4_end, q4_High, width=2, color=clr_High)
    line.new(inBar_q4_start, q4_Low, inBar_q4_end, q4_Low, width=2, color=clr_Low)

// 
//   Information output
// 
// label.new(bar_index, high, text="Q1 start=" + str.tostring(inBar_q1_start) + " end=" + str.tostring(inBar_q1_end) + " High=" + str.tostring(q1_High) + " Low=" + str.tostring(q1_Low))
// label.new(bar_index, high * 0.95, text="Q2 start=" + str.tostring(inBar_q2_start) + " end=" + str.tostring(inBar_q2_end) + " High=" + str.tostring(q2_High) + " Low=" + str.tostring(q2_Low))
// label.new(bar_index, high * 0.9, text="Q3 start=" + str.tostring(inBar_q3_start) + " end=" + str.tostring(inBar_q3_end) + " High=" + str.tostring(q3_High) + " Low=" + str.tostring(q3_Low))
// label.new(bar_index, high * 0.85, text="Q4 start=" + str.tostring(inBar_q4_start) + " end=" + str.tostring(inBar_q4_end) + " High=" + str.tostring(q4_High) + " Low=" + str.tostring(q4_Low))

New contributor

Dmitriy Dim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật