Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("Monthly Positive and Negative Averages with Monthly Open Line", overlay=true)
- //==x77x My Other Indicators @ https://pastebin.com/u/SavingFace
- // Function to get the month abbreviation
- f_get_month_name(month_num) =>
- month_name = switch month_num
- 1 => "Jan"
- 2 => "Feb"
- 3 => "Mar"
- 4 => "Apr"
- 5 => "May"
- 6 => "Jun"
- 7 => "Jul"
- 8 => "Aug"
- 9 => "Sep"
- 10 => "Oct"
- 11 => "Nov"
- 12 => "Dec"
- => "Unknown"
- // Define average positive and negative returns for each month
- month_avg_positive = switch month(time)
- 1 => 39.63 // January
- 2 => 22.75 // February
- 3 => 42.47 // March
- 4 => 27.84 // April
- 5 => 30.65 // May
- 6 => 15.61 // June
- 7 => 14.83 // July
- 8 => 28.09 // August
- 9 => 4.90 // September
- 10 => 27.48 // October
- 11 => 23.79 // November
- 12 => 38.89 // December
- => na // Default
- month_avg_negative = switch month(time)
- 1 => -16.68 // January
- 2 => -19.82 // February
- 3 => -15.63 // March
- 4 => -7.82 // April
- 5 => -14.77 // May
- 6 => -16.31 // June
- 7 => -6.99 // July
- 8 => -11.42 // August
- 9 => -8.10 // September
- 10 => -8.39 // October
- 11 => -19.30 // November
- 12 => -15.11 // December
- => na // Default
- // Capture the open price on the first bar of each month
- var float monthly_open_price = na
- var float avg_positive_level = na
- var float avg_negative_level = na
- var line monthly_open_line = na
- var line avg_positive_line = na
- var line avg_negative_line = na
- var label monthly_open_label = na
- var label avg_positive_label = na
- var label avg_negative_label = na
- // Delete labels only at the start of a new month
- if (ta.change(month(time)))
- monthly_open_price := open
- avg_positive_level := monthly_open_price * (1 + month_avg_positive / 100)
- avg_negative_level := monthly_open_price * (1 + month_avg_negative / 100)
- // Delete the previous month's lines and labels when a new month starts
- line.delete(monthly_open_line)
- line.delete(avg_positive_line)
- line.delete(avg_negative_line)
- label.delete(monthly_open_label)
- label.delete(avg_positive_label)
- label.delete(avg_negative_label)
- // Create new lines for the monthly open, average positive, and average negative levels
- monthly_open_line := line.new(x1=bar_index, y1=monthly_open_price, x2=bar_index + 1, y2=monthly_open_price, color=#ffebba, width=1)
- avg_positive_line := line.new(x1=bar_index, y1=avg_positive_level, x2=bar_index + 1, y2=avg_positive_level, color=#5d606b, width=2)
- avg_negative_line := line.new(x1=bar_index, y1=avg_negative_level, x2=bar_index + 1, y2=avg_negative_level, color=#b13e00, width=2)
- // Create labels for the lines at the start of the line
- monthly_open_label := label.new(x=bar_index, y=monthly_open_price, text="Monthly Open\n" + str.tostring((close - monthly_open_price) / monthly_open_price * 100, "#.##") + "%", style=label.style_label_right, color=#ffebba, textcolor=#252525)
- avg_positive_label := label.new(x=bar_index, y=avg_positive_level, text=f_get_month_name(month(time)) + " +Avg: " + str.tostring(month_avg_positive, "#.##") + "%", style=label.style_label_right, color=#5d606b, textcolor=color.white)
- avg_negative_label := label.new(x=bar_index, y=avg_negative_level, text=f_get_month_name(month(time)) + " -Avg: " + str.tostring(month_avg_negative, "#.##") + "%", style=label.style_label_right, color=#b13e00, textcolor=color.white)
- // Extend the existing lines to the current bar for the current month
- if (not na(monthly_open_price))
- line.set_xy2(monthly_open_line, bar_index, monthly_open_price)
- if (not na(avg_positive_level))
- line.set_xy2(avg_positive_line, bar_index, avg_positive_level)
- if (not na(avg_negative_level))
- line.set_xy2(avg_negative_line, bar_index, avg_negative_level)
- // Update the labels with the current percentage change and ensure they stay positioned correctly
- label.set_text(monthly_open_label, "Monthly Open\n" + str.tostring((close - monthly_open_price) / monthly_open_price * 100, "#.##") + "%")
- label.set_text(avg_positive_label, f_get_month_name(month(time)) + " +Avg: " + str.tostring(month_avg_positive, "#.##") + "%")
- label.set_text(avg_negative_label, f_get_month_name(month(time)) + " -Avg: " + str.tostring(month_avg_negative, "#.##") + "%")
Advertisement
Add Comment
Please, Sign In to add comment