`Indicator of quarterly lines “Quarter & lines”.
Quarters of the year.
Horizontal lines of the quarters.
-
:
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 -
:
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))
Dmitriy Dim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.