Advertisement
Guest User

Untitled

a guest
May 17th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © jjustingreyy
  3. //@version=5
  4. indicator('Relative Strength vs SPY', overlay=true)
  5. // === USER INPUTS ===
  6. i_textColor = input(color.new(#d1d4dc, 0), title='Text Color', group = "colors")
  7. i_posColor = input(color.rgb(71, 254, 163), title='Positive Color', group = "colors")
  8. i_negColor = input(color.rgb(255, 101, 98), title='Negative Color', group = "colors")
  9. i_tableFrameColor = input(color.new(#2b2e38, 0), title='Table Frame Color', group = "colors")
  10. i_headerRowColor = input(color.new(#2b2e38, 0), title='Header Row Color', group = "colors")
  11. i_rowBackgroundColor = input(color.rgb(0, 0, 0, 100), title='Cell Background Color', group = "colors")
  12. sym = input.symbol(title='Index/Ticker Compare', defval='SPY', group = "index/compare symbol")
  13. symbolName = syminfo.ticker(sym)
  14. ratioOneMo = input.float(title='One Month ROC', minval = 0, maxval = 1, defval = 0.3, group = 'relative strength ratios')
  15. tooltip = 'Choose what percentage of each period\'s rate of change goes into the average. Total should equal 1.0. This will calculate the average strength of each ticker using these weights and display the difference (in multiples) between them and the indexwhich can be used to see how strong your ticker is vs. the index.'
  16. ratioThreeMo = input.float(title='Three Month ROC', minval = 0, maxval = 1, defval = 0.3, group = 'relative strength ratios')
  17. ratioSixMo = input.float(title='Six Month ROC', minval = 0, maxval = 1, defval = 0.2, group = 'relative strength ratios')
  18. ratioTwelveMo = input.float(title='One Year ROC', minval = 0, maxval = 1, defval = 0.2, group = 'relative strength ratios')
  19. // Various Functions to calculate table cells
  20. f_rateOfreturn(_v1, _v2) => (_v1 - _v2) * 100 / math.abs(_v2)
  21. f_performance(sec, _barsBack) => request.security(sec, '1D', nz(ta.roc(close, _barsBack)))
  22. // request.security(sec, '1D', f_rateOfreturn(close, close[_barsBack]))
  23. recentClose(sec) => request.security(sec, '1D', close)
  24. lastYearClose(sec) => request.security(sec, '12M', close[1], lookahead=barmerge.lookahead_on)
  25. fiftyTwoHighDiff(sec) =>
  26. fiftyTwoHigh = request.security(sec, 'W', ta.highest(high, 52))
  27. request.security(sec, '1D', ((fiftyTwoHigh - close)/fiftyTwoHigh)*100)
  28.  
  29. relStrength(sec) =>
  30. oneMORS = request.security(sec, 'D', nz(ta.roc(close, 21)))
  31. threeMORS = request.security(sec, 'D', nz(ta.roc(close, 63)))
  32. sixMORS = request.security(sec, 'D', nz(ta.roc(close, 126)))
  33. twelveMORS = request.security(sec, 'D', nz(ta.roc(close, 252)))
  34. ratioOneMo*oneMORS + ratioThreeMo*threeMORS + ratioSixMo*sixMORS + ratioTwelveMo*twelveMORS
  35. volatility(sec) => // Monthly Volatility in Stock Screener (also ADR)
  36. request.security(sec, 'D', ta.sma((high - low) / math.abs(low) * 100, 21))
  37. // === Fill Table Cells ===
  38. f_fillCell(_table, _column, _row, _value, _timeframe) =>
  39. _c_color = _value >= 0 ? i_posColor : i_negColor
  40. _cellText = str.tostring(_value, '0.0') + '%'
  41. table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=_c_color, width=6)
  42. f_fillCellVol(_table, _column, _row, _value) =>
  43. _cellText = str.tostring(_value, '0.0') + '%'
  44. table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=i_textColor, width=6)
  45. f_fillCellHeader(_table, _column, _row, _value) =>
  46. _cellText = _value
  47. table.cell(_table, _column, _row, _cellText, bgcolor=i_headerRowColor, text_color=i_textColor, width=8)
  48. f_fillCellText(_table, _column, _row, _value) =>
  49. _cellText = _value
  50. table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=i_textColor, width=6)
  51.  
  52. f_fillCellFiftyTwo(_table, _column, _row, _value) =>
  53. _c_color = _value <= 35 ? i_posColor : i_negColor
  54. _cellText = str.tostring(_value, '0.0') + '%'
  55. table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=_c_color, width=10)
  56. f_fillCellRSComp(_table, _column, _row, _value) =>
  57. _c_color = _value >= 0 ? i_posColor : i_negColor
  58. _cellText = str.tostring(_value, '0.0') + ' %'
  59. table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=_c_color, width=8)
  60. // Define table
  61. var table perfTable = table.new(position.top_right, 4, 6, border_width=1, frame_width = 1, frame_color = i_tableFrameColor, border_color = i_tableFrameColor)
  62. // Draw table and assign values
  63. if barstate.islast
  64. f_fillCellHeader(perfTable, 0, 0, 'Performance')
  65. f_fillCellText(perfTable, 0, 1, 'YTD')
  66. f_fillCellText(perfTable, 0, 2, '1Y')
  67. f_fillCellText(perfTable, 0, 3, '6M')
  68. f_fillCellText(perfTable, 0, 4, '3M')
  69. f_fillCellText(perfTable, 0, 5, '1M')
  70. f_fillCellHeader(perfTable, 1, 0, syminfo.ticker)
  71. f_fillCell(perfTable, 1, 1, f_rateOfreturn(recentClose(syminfo.tickerid), lastYearClose(syminfo.tickerid)), 'YTD')
  72. f_fillCell(perfTable, 1, 2, f_performance(syminfo.tickerid, 252), '1Y')
  73. f_fillCell(perfTable, 1, 3, f_performance(syminfo.tickerid, 126), '6M')
  74. f_fillCell(perfTable, 1, 4, f_performance(syminfo.tickerid, 63), '3M')
  75. f_fillCell(perfTable, 1, 5, f_performance(syminfo.tickerid, 21), '1M')
  76. f_fillCellHeader(perfTable, 2, 0, symbolName)
  77. f_fillCell(perfTable, 2, 1, f_rateOfreturn(recentClose(sym), lastYearClose(sym)), 'YTD')
  78. f_fillCell(perfTable, 2, 2, f_performance(sym, 251), '1Y')
  79. f_fillCell(perfTable, 2, 3, f_performance(sym, 126), '6M')
  80. f_fillCell(perfTable, 2, 4, f_performance(sym, 63), '3M')
  81. f_fillCell(perfTable, 2, 5, f_performance(sym, 21), '1M')
  82. f_fillCellHeader(perfTable, 3, 0, 'ADR')
  83. f_fillCellVol(perfTable, 3, 1, volatility(syminfo.tickerid))
  84. f_fillCellHeader(perfTable, 3, 2, 'Off 52-wk High')
  85. f_fillCellFiftyTwo(perfTable, 3, 3, fiftyTwoHighDiff(syminfo.tickerid))
  86. f_fillCellHeader(perfTable, 3, 4, 'RS vs. ' + symbolName)
  87. f_fillCellRSComp(perfTable, 3, 5, (relStrength(syminfo.tickerid)-relStrength(sym))/math.abs(relStrength(sym)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement