SavingFace

BTC Monthly Positive and Negative Averages

Oct 31st, 2024 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | Cryptocurrency | 0 0
  1. //@version=5
  2. indicator("Monthly Positive and Negative Averages with Monthly Open Line", overlay=true)
  3. //==x77x My Other Indicators @ https://pastebin.com/u/SavingFace
  4.  
  5. // Function to get the month abbreviation
  6. f_get_month_name(month_num) =>
  7. month_name = switch month_num
  8. 1 => "Jan"
  9. 2 => "Feb"
  10. 3 => "Mar"
  11. 4 => "Apr"
  12. 5 => "May"
  13. 6 => "Jun"
  14. 7 => "Jul"
  15. 8 => "Aug"
  16. 9 => "Sep"
  17. 10 => "Oct"
  18. 11 => "Nov"
  19. 12 => "Dec"
  20. => "Unknown"
  21.  
  22. // Define average positive and negative returns for each month
  23. month_avg_positive = switch month(time)
  24. 1 => 39.63 // January
  25. 2 => 22.75 // February
  26. 3 => 42.47 // March
  27. 4 => 27.84 // April
  28. 5 => 30.65 // May
  29. 6 => 15.61 // June
  30. 7 => 14.83 // July
  31. 8 => 28.09 // August
  32. 9 => 4.90 // September
  33. 10 => 27.48 // October
  34. 11 => 23.79 // November
  35. 12 => 38.89 // December
  36. => na // Default
  37.  
  38. month_avg_negative = switch month(time)
  39. 1 => -16.68 // January
  40. 2 => -19.82 // February
  41. 3 => -15.63 // March
  42. 4 => -7.82 // April
  43. 5 => -14.77 // May
  44. 6 => -16.31 // June
  45. 7 => -6.99 // July
  46. 8 => -11.42 // August
  47. 9 => -8.10 // September
  48. 10 => -8.39 // October
  49. 11 => -19.30 // November
  50. 12 => -15.11 // December
  51. => na // Default
  52.  
  53. // Capture the open price on the first bar of each month
  54. var float monthly_open_price = na
  55. var float avg_positive_level = na
  56. var float avg_negative_level = na
  57.  
  58. var line monthly_open_line = na
  59. var line avg_positive_line = na
  60. var line avg_negative_line = na
  61. var label monthly_open_label = na
  62. var label avg_positive_label = na
  63. var label avg_negative_label = na
  64.  
  65. // Delete labels only at the start of a new month
  66. if (ta.change(month(time)))
  67. monthly_open_price := open
  68. avg_positive_level := monthly_open_price * (1 + month_avg_positive / 100)
  69. avg_negative_level := monthly_open_price * (1 + month_avg_negative / 100)
  70.  
  71. // Delete the previous month's lines and labels when a new month starts
  72. line.delete(monthly_open_line)
  73. line.delete(avg_positive_line)
  74. line.delete(avg_negative_line)
  75. label.delete(monthly_open_label)
  76. label.delete(avg_positive_label)
  77. label.delete(avg_negative_label)
  78.  
  79. // Create new lines for the monthly open, average positive, and average negative levels
  80. monthly_open_line := line.new(x1=bar_index, y1=monthly_open_price, x2=bar_index + 1, y2=monthly_open_price, color=#ffebba, width=1)
  81. avg_positive_line := line.new(x1=bar_index, y1=avg_positive_level, x2=bar_index + 1, y2=avg_positive_level, color=#5d606b, width=2)
  82. avg_negative_line := line.new(x1=bar_index, y1=avg_negative_level, x2=bar_index + 1, y2=avg_negative_level, color=#b13e00, width=2)
  83.  
  84. // Create labels for the lines at the start of the line
  85. 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)
  86. 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)
  87. 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)
  88.  
  89. // Extend the existing lines to the current bar for the current month
  90. if (not na(monthly_open_price))
  91. line.set_xy2(monthly_open_line, bar_index, monthly_open_price)
  92. if (not na(avg_positive_level))
  93. line.set_xy2(avg_positive_line, bar_index, avg_positive_level)
  94. if (not na(avg_negative_level))
  95. line.set_xy2(avg_negative_line, bar_index, avg_negative_level)
  96.  
  97. // Update the labels with the current percentage change and ensure they stay positioned correctly
  98. label.set_text(monthly_open_label, "Monthly Open\n" + str.tostring((close - monthly_open_price) / monthly_open_price * 100, "#.##") + "%")
  99. label.set_text(avg_positive_label, f_get_month_name(month(time)) + " +Avg: " + str.tostring(month_avg_positive, "#.##") + "%")
  100. label.set_text(avg_negative_label, f_get_month_name(month(time)) + " -Avg: " + str.tostring(month_avg_negative, "#.##") + "%")
  101.  
Advertisement
Add Comment
Please, Sign In to add comment