SavingFace

Cycle MA + Deviations

Sep 29th, 2024
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | Cryptocurrency | 0 0
  1. //@version=5
  2. indicator("Cycle MA + Deviations", overlay=true)
  3.  
  4. // Input for moving average type
  5. ma_type = input.string("VWMA", title="Moving Average Type", options=["SMA", "EMA", "WMA", "HMA", "VWMA", "RMA", "ALMA"])
  6.  
  7. // Input for the length of the moving average
  8. ma_length = 1460
  9.  
  10. // Function to select the chosen moving average type
  11. f_ma(source, length) =>
  12. switch ma_type
  13. "SMA" => ta.sma(source, length)
  14. "EMA" => ta.ema(source, length)
  15. "WMA" => ta.wma(source, length)
  16. "HMA" => ta.hma(source, length)
  17. "VWMA" => ta.vwma(source, length)
  18. "RMA" => ta.rma(source, length)
  19. "ALMA" => ta.alma(source, length, 0.85, 6)
  20.  
  21. // Calculate the selected moving average with the user-defined length
  22. selected_ma = f_ma(close, ma_length)
  23.  
  24. // Deviation levels (percentages)
  25. deviation_up_50 = selected_ma * 1.50
  26. deviation_up_100 = selected_ma * 2.00
  27. deviation_up_200 = selected_ma * 3.00
  28. deviation_up_300 = selected_ma * 4.00
  29. deviation_up_400 = selected_ma * 5.00
  30. deviation_down_25 = selected_ma * 0.75
  31.  
  32. // Plot the selected moving average
  33. plot(selected_ma, color=#ff0000, linewidth=2, title="Selected Moving Average")
  34.  
  35. // Plot the deviation bands
  36. plot(deviation_up_400, color=#5d606b, linewidth=1, title="+400% Deviation")
  37. plot(deviation_up_300, color=#5d606b, linewidth=1, title="+300% Deviation")
  38. plot(deviation_up_200, color=#5d606b, linewidth=1, title="+200% Deviation")
  39. plot(deviation_up_100, color=#5d606b, linewidth=1, title="+100% Deviation")
  40. plot(deviation_up_50, color=#5d606b, linewidth=1, title="+50% Deviation")
  41. plot(deviation_down_25, color=#ff5a00, linewidth=1, title="-25% Deviation")
  42.  
  43. // Table position selection
  44. table_position = input.string("Bottom Right", title="Table Position", options=["Top Right", "Middle Right", "Bottom Right"])
  45.  
  46. // Table layout selection
  47. table_layout = input.string("Vertical", title="Table Layout", options=["Vertical", "Horizontal"])
  48.  
  49. // Table coordinates based on selected position
  50. var label_position = switch table_position
  51. "Top Right" => position.top_right
  52. "Middle Right" => position.middle_right
  53. "Bottom Right" => position.bottom_right
  54.  
  55. // Create the table with dynamic size based on layout
  56. var table deviation_table = table.new(label_position, table_layout == "Vertical" ? 7 : 7, table_layout == "Vertical" ? 7 : 7, bgcolor=#363a45, border_width=1)
  57.  
  58. // Update the table with the current prices of each plot
  59. if (barstate.islast)
  60. if (table_layout == "Vertical")
  61. table.cell(deviation_table, 0, 0, " Cycle MA: " + str.tostring(selected_ma, "#.##"), text_color=color.white)
  62. table.cell(deviation_table, 0, 1, "+400%: " + str.tostring(deviation_up_400, "#.##"), text_color=color.white)
  63. table.cell(deviation_table, 0, 2, "+300%: " + str.tostring(deviation_up_300, "#.##"), text_color=color.white)
  64. table.cell(deviation_table, 0, 3, "+200%: " + str.tostring(deviation_up_200, "#.##"), text_color=color.white)
  65. table.cell(deviation_table, 0, 4, "+100%: " + str.tostring(deviation_up_100, "#.##"), text_color=color.white)
  66. table.cell(deviation_table, 0, 5, "+50%: " + str.tostring(deviation_up_50, "#.##"), text_color=color.white)
  67. table.cell(deviation_table, 0, 6, "-25%: " + str.tostring(deviation_down_25, "#.##"), text_color=color.white)
  68. else
  69. table.cell(deviation_table, 0, 0, "Cycle MA: " + str.tostring(selected_ma, "#.##"), text_color=color.white)
  70. table.cell(deviation_table, 1, 0, "+400%: " + str.tostring(deviation_up_400, "#.##"), text_color=color.white)
  71. table.cell(deviation_table, 2, 0, "+300%: " + str.tostring(deviation_up_300, "#.##"), text_color=color.white)
  72. table.cell(deviation_table, 3, 0, "+200%: " + str.tostring(deviation_up_200, "#.##"), text_color=color.white)
  73. table.cell(deviation_table, 4, 0, "+100%: " + str.tostring(deviation_up_100, "#.##"), text_color=color.white)
  74. table.cell(deviation_table, 5, 0, "+50%: " + str.tostring(deviation_up_50, "#.##"), text_color=color.white)
  75. table.cell(deviation_table, 6, 0, "-25%: " + str.tostring(deviation_down_25, "#.##"), text_color=color.white)
  76.  
Advertisement
Add Comment
Please, Sign In to add comment