Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © jjustingreyy
- //@version=5
- indicator('Relative Strength vs SPY', overlay=true)
- // === USER INPUTS ===
- i_textColor = input(color.new(#d1d4dc, 0), title='Text Color', group = "colors")
- i_posColor = input(color.rgb(71, 254, 163), title='Positive Color', group = "colors")
- i_negColor = input(color.rgb(255, 101, 98), title='Negative Color', group = "colors")
- i_tableFrameColor = input(color.new(#2b2e38, 0), title='Table Frame Color', group = "colors")
- i_headerRowColor = input(color.new(#2b2e38, 0), title='Header Row Color', group = "colors")
- i_rowBackgroundColor = input(color.rgb(0, 0, 0, 100), title='Cell Background Color', group = "colors")
- sym = input.symbol(title='Index/Ticker Compare', defval='SPY', group = "index/compare symbol")
- symbolName = syminfo.ticker(sym)
- ratioOneMo = input.float(title='One Month ROC', minval = 0, maxval = 1, defval = 0.3, group = 'relative strength ratios')
- 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.'
- ratioThreeMo = input.float(title='Three Month ROC', minval = 0, maxval = 1, defval = 0.3, group = 'relative strength ratios')
- ratioSixMo = input.float(title='Six Month ROC', minval = 0, maxval = 1, defval = 0.2, group = 'relative strength ratios')
- ratioTwelveMo = input.float(title='One Year ROC', minval = 0, maxval = 1, defval = 0.2, group = 'relative strength ratios')
- // Various Functions to calculate table cells
- f_rateOfreturn(_v1, _v2) => (_v1 - _v2) * 100 / math.abs(_v2)
- f_performance(sec, _barsBack) => request.security(sec, '1D', nz(ta.roc(close, _barsBack)))
- // request.security(sec, '1D', f_rateOfreturn(close, close[_barsBack]))
- recentClose(sec) => request.security(sec, '1D', close)
- lastYearClose(sec) => request.security(sec, '12M', close[1], lookahead=barmerge.lookahead_on)
- fiftyTwoHighDiff(sec) =>
- fiftyTwoHigh = request.security(sec, 'W', ta.highest(high, 52))
- request.security(sec, '1D', ((fiftyTwoHigh - close)/fiftyTwoHigh)*100)
- relStrength(sec) =>
- oneMORS = request.security(sec, 'D', nz(ta.roc(close, 21)))
- threeMORS = request.security(sec, 'D', nz(ta.roc(close, 63)))
- sixMORS = request.security(sec, 'D', nz(ta.roc(close, 126)))
- twelveMORS = request.security(sec, 'D', nz(ta.roc(close, 252)))
- ratioOneMo*oneMORS + ratioThreeMo*threeMORS + ratioSixMo*sixMORS + ratioTwelveMo*twelveMORS
- volatility(sec) => // Monthly Volatility in Stock Screener (also ADR)
- request.security(sec, 'D', ta.sma((high - low) / math.abs(low) * 100, 21))
- // === Fill Table Cells ===
- f_fillCell(_table, _column, _row, _value, _timeframe) =>
- _c_color = _value >= 0 ? i_posColor : i_negColor
- _cellText = str.tostring(_value, '0.0') + '%'
- table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=_c_color, width=6)
- f_fillCellVol(_table, _column, _row, _value) =>
- _cellText = str.tostring(_value, '0.0') + '%'
- table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=i_textColor, width=6)
- f_fillCellHeader(_table, _column, _row, _value) =>
- _cellText = _value
- table.cell(_table, _column, _row, _cellText, bgcolor=i_headerRowColor, text_color=i_textColor, width=8)
- f_fillCellText(_table, _column, _row, _value) =>
- _cellText = _value
- table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=i_textColor, width=6)
- f_fillCellFiftyTwo(_table, _column, _row, _value) =>
- _c_color = _value <= 35 ? i_posColor : i_negColor
- _cellText = str.tostring(_value, '0.0') + '%'
- table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=_c_color, width=10)
- f_fillCellRSComp(_table, _column, _row, _value) =>
- _c_color = _value >= 0 ? i_posColor : i_negColor
- _cellText = str.tostring(_value, '0.0') + ' %'
- table.cell(_table, _column, _row, _cellText, bgcolor=i_rowBackgroundColor, text_color=_c_color, width=8)
- // Define table
- var table perfTable = table.new(position.top_right, 4, 6, border_width=1, frame_width = 1, frame_color = i_tableFrameColor, border_color = i_tableFrameColor)
- // Draw table and assign values
- if barstate.islast
- f_fillCellHeader(perfTable, 0, 0, 'Performance')
- f_fillCellText(perfTable, 0, 1, 'YTD')
- f_fillCellText(perfTable, 0, 2, '1Y')
- f_fillCellText(perfTable, 0, 3, '6M')
- f_fillCellText(perfTable, 0, 4, '3M')
- f_fillCellText(perfTable, 0, 5, '1M')
- f_fillCellHeader(perfTable, 1, 0, syminfo.ticker)
- f_fillCell(perfTable, 1, 1, f_rateOfreturn(recentClose(syminfo.tickerid), lastYearClose(syminfo.tickerid)), 'YTD')
- f_fillCell(perfTable, 1, 2, f_performance(syminfo.tickerid, 252), '1Y')
- f_fillCell(perfTable, 1, 3, f_performance(syminfo.tickerid, 126), '6M')
- f_fillCell(perfTable, 1, 4, f_performance(syminfo.tickerid, 63), '3M')
- f_fillCell(perfTable, 1, 5, f_performance(syminfo.tickerid, 21), '1M')
- f_fillCellHeader(perfTable, 2, 0, symbolName)
- f_fillCell(perfTable, 2, 1, f_rateOfreturn(recentClose(sym), lastYearClose(sym)), 'YTD')
- f_fillCell(perfTable, 2, 2, f_performance(sym, 251), '1Y')
- f_fillCell(perfTable, 2, 3, f_performance(sym, 126), '6M')
- f_fillCell(perfTable, 2, 4, f_performance(sym, 63), '3M')
- f_fillCell(perfTable, 2, 5, f_performance(sym, 21), '1M')
- f_fillCellHeader(perfTable, 3, 0, 'ADR')
- f_fillCellVol(perfTable, 3, 1, volatility(syminfo.tickerid))
- f_fillCellHeader(perfTable, 3, 2, 'Off 52-wk High')
- f_fillCellFiftyTwo(perfTable, 3, 3, fiftyTwoHighDiff(syminfo.tickerid))
- f_fillCellHeader(perfTable, 3, 4, 'RS vs. ' + symbolName)
- f_fillCellRSComp(perfTable, 3, 5, (relStrength(syminfo.tickerid)-relStrength(sym))/math.abs(relStrength(sym)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement