I wrote an Indicator in Pincescript, which shows seperator lines for the weekdays and also for the weekend. Additional to that, its possible to show the weekday names.
So far this works finde, but when I combine it together with the rest of the planned indicator, it gets slowly. And it's also not necessary to have all the lines, it would also be better for a "clean chart" to reduce them.
So my plan is to reduce the amount of printed lines. Usually for this reason there is the "max_lines_count", but in this case I can't use it, because the other part of the indicator is also drawing lines and boxes.
For this reason I already set two constants, "i_DaySepLookBack" and "i_WeekdSepLookBack". The function which draws the horizontal lines is this one:
f_vline(BarIndex, Color, LineStyle, LineWidth, LookBack) =>
low_ = hl2 - (syminfo.mintick) * i_MMMlength
high_ = hl2 + (syminfo.mintick) * i_MMMlength
line.new(time, low_, time, high_, xloc.bar_time, extend=extend.none, color=Color, style=LineStyle, width=LineWidth)
Can somebody help me/push me in the right direction, on how I have to rebuild this function to reduce the amount of printed lines?
Here is the Code of the whole Indicator:
//@version=5
indicator("Show Week and Day Seperator", overlay=true, max_lines_count=500, max_boxes_count=500)
// Constants and One-Time-Init Vars {
transpLine = 0
i_Seplength = 800
i_DaySepLookBack = 5
i_WeekSepLookBack = 5
// }
// Inputs {
grpWeekLine = "================== Week Seperator Lines =================="
i_user_week_start = input.string(title='Week Separator on Day', defval='Sunday', options=['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], group=grpWeekLine)
i_user_week_toggle = input(title='Show Week Separator Line', defval=true, inline="Weekly Line", group=grpWeekLine)
i_weeklyLineCl =input.color(color.new(color.blue, transpLine), "", inline="Weekly Line", group=grpWeekLine)
i_weekLineWidth = input.int(title='Week Line Width', defval=4, minval=0, maxval=20, step=1, group=grpWeekLine)
i_user_week_line_style = input.string(title='Week Line Style', defval='dashed', options=['solid', 'dashed', 'dotted'], group=grpWeekLine)
grpDaykLine = "=================== Day Seperator Lines ==================="
i_user_day_toggle = input(title='Show Day Separator Line', defval=true, inline="Day Line", group=grpDaykLine)
i_daylyLineCl =input.color(color.new(color.blue, transpLine), "", inline="Day Line", group=grpDaykLine)
i_dayLineWidth = input.int(title='Day Line Width', defval=4, minval=0, maxval=20, step=1, group=grpDaykLine)
i_user_day_line_style = input.string(title='Day Line Style', defval='dashed', options=['solid', 'dashed', 'dotted'], group=grpDaykLine)
// Alternative Lines using backcolours
grpWeekDayLineAdditional = "================== Additional Settings =================="
i_user_show_on_1hour = input(title='Show Day Separator and Days of Week on 1 hour Chart', defval=false, group=grpWeekDayLineAdditional)
i_user_show_on_updaily = input(title='Show Week Separator up to the Daily Chart', defval=false, group=grpWeekDayLineAdditional)
i_showDOW = input.bool(true, title='Show days of week', inline="i_ShowDow", group=grpWeekDayLineAdditional)
i_user_dow_color = input.color(color.new(color.blue, transpLine), "", inline="i_ShowDow", group=grpWeekDayLineAdditional)
i_isShortDayLabel = input.bool(false, "Use short names for days of week (Mon, Tue...)", group=grpWeekDayLineAdditional)
// }
week_line_style = i_user_week_line_style == 'solid' ? line.style_solid : i_user_week_line_style == 'dashed' ? line.style_dashed : i_user_week_line_style == 'dotted' ? line.style_dotted : line.style_solid
day_line_style = i_user_day_line_style == 'solid' ? line.style_solid : i_user_day_line_style == 'dashed' ? line.style_dashed : i_user_day_line_style == 'dotted' ? line.style_dotted : line.style_solid
tickerExchangeOffset = 5
int new_day_start_time = 17
int dayLabelStartTime = 1
week_start_day = i_user_week_start == 'Sunday' ? dayofweek.sunday : i_user_week_start == 'Monday' ? dayofweek.monday : i_user_week_start == 'Tuesday' ? dayofweek.tuesday : i_user_week_start == 'Wednesday' ? dayofweek.wednesday : i_user_week_start == 'Thursday' ? dayofweek.thursday : i_user_week_start == 'Friday' ? dayofweek.friday : i_user_week_start == 'Saturday' ? dayofweek.saturday : dayofweek.sunday
// Functions {
f_vline(BarIndex, Color, LineStyle, LineWidth, LookBack) =>
low_ = hl2 - (syminfo.mintick) * i_Seplength
high_ = hl2 + (syminfo.mintick) * i_Seplength
line.new(time, low_, time, high_, xloc.bar_time, extend=extend.none, color=Color, style=LineStyle, width=LineWidth)
// }
// Calcutations {
if syminfo.timezone == 'Etc/UTC'
new_day_start_time += tickerExchangeOffset
dayLabelStartTime += tickerExchangeOffset
dayLabelStartTime
// Add the start of week line
isNewWeek() =>
dayofweek == week_start_day ? 1 : 0
isStartTime() =>
hour == new_day_start_time and minute == 0 ? 1 : 0
isValidDaySeparatorResolution() =>
timeframe.isdwm == true or timeframe.period == '60' and not i_user_show_on_1hour or timeframe.in_seconds() >= timeframe.in_seconds("120") ? 0 : 1
isValidWeekSeparatorResolution() =>
i_user_show_on_updaily and timeframe.isdwm == false ? 1 : not i_user_show_on_updaily and timeframe.in_seconds() <= timeframe.in_seconds("59") ? 1 : 0
isValidDaySeparator = isValidDaySeparatorResolution()
isValidDayTextSeparator = isValidDaySeparatorResolution()
isValidWeekSeparator = isValidWeekSeparatorResolution()
if isValidWeekSeparator and i_user_week_toggle and isNewWeek() == 1 and isStartTime() == 1
f_vline(bar_index, i_weeklyLineCl, week_line_style, i_weekLineWidth, i_WeekSepLookBack)
// Add daily separator
isNewDay() =>
dayofweek != week_start_day ? 1 : 0
if isValidDaySeparator and i_user_day_toggle and isNewDay() == 1 and isStartTime() == 1
f_vline(bar_index, i_daylyLineCl, day_line_style, i_dayLineWidth, i_DaySepLookBack)
// Display the days of week
dot_color = color.new(color.silver, 100)
dowtext_color = color.new(color.silver, 0)
plotshape((not i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and not i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.monday : false, text='Monday', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((not i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and not i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.tuesday : false, text='Tuesday', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((not i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and not i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.wednesday : false, text='Wednesday', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((not i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and not i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.thursday : false, text='Thursday', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((not i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and not i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.friday : false, text='Friday', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.monday : false, text='Mon', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.tuesday : false, text='Tue', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.wednesday : false, text='Wed', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.thursday : false, text='Thu', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
plotshape((i_isShortDayLabel and i_showDOW or i_showDOW and timeframe.period == '60' and i_user_show_on_1hour and i_isShortDayLabel) and isValidDayTextSeparator ? hour == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.friday : false, text='Fri', color=dot_color, offset=8, style=shape.circle, location=location.bottom, textcolor=i_user_dow_color, size=size.tiny, editable=false)
// }
// Plots {
// }
You should record all the lines you create in an array (see array.new_line https://www.tradingview.com/pine-script-reference/v5/#fun_array{dot}new_line).
Then you will be able to delete old lines directly with the information recorded in your array.