Relative Strength/Weakness Arrows
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © seaneholmes

//@version=5

indicator(shorttitle='RRS/S', title='Real Relative Sector Strength Arrows', overlay=true)

var CURRENT_COLOR = #d1d4dc
var BASELINE_COLOR = #b2b5be

colorCurrent = input.color(CURRENT_COLOR, title='Current') 
baseline = input.symbol('SPY', title='Baseline', inline='baseline')
colorBaseline = input.color(BASELINE_COLOR, title='', inline='baseline')
src = input(close, title='Source')
periodPC= input.int(12, minval=1, title='Price Chg. Period')
periodATR = input.int(12, minval=1, title='ATR Period')

// ===== Sector Symbol Definitions =====
// Note:
// - As of 2022-08-29
// - A symbol may belong in multiple sectors

// Communication Services
var sectorName1 = 'Comms'
var sector1 = 'XLC'
var sectorSymbols1 = 'ATVI, CHTR, CMCSA, DIS, DISH, EA, META, FOX, FOXA, GOOG, GOOGL, IPG, LUMN, LYV, MTCH, NFLX, NWS, NWSA, OMC, PARA, T, TMUS, TTWO, TWTR, VZ, WBD'
var color1 = #ffc000



// ===== Sector Inputs =====

var SECTOR_GROUP_NAME = 'Sectors (Check Each to Always Show)'
var SECTOR_SHOW_NAME = ''

showAll = input.bool(false, title='Show All', group=SECTOR_GROUP_NAME)

c1 = input.color(color1, title='1', group=SECTOR_GROUP_NAME, inline='S1A')
n1 = input.string(sectorName1, title='', group=SECTOR_GROUP_NAME, inline='S1A')
s1 = input.symbol(sector1 , title='', group=SECTOR_GROUP_NAME, inline='S1B')
symbols1 = input.string(sectorSymbols1, title='', group=SECTOR_GROUP_NAME, inline='S1B')
show1 = input.bool(false, title=SECTOR_SHOW_NAME, group=SECTOR_GROUP_NAME, inline='S1B')

// ===== Utility Functions =====

isSymbolInSector(string sectorSymbols) =>
    // replace all commas, pluses and minuses with whitespace then split by whitespace
    symbols = str.split(str.replace_all(str.replace_all(str.replace_all(sectorSymbols, ',', ' '), '+', ' '), '-', ' '), ' ')
    array.indexof(symbols, syminfo.ticker) >= 0

// ===== RRS =====
// NOTE:
// - RRS is the powerIndex of the current symbol minus the powerIndex of the baseline/sector
// - A higher (lower) powerIndex to the baseline/sector means relative strength (weakness)
// - Because we're plotting the symbol, baseline and sector together, we show each powerIndex instead of RRS

calcPowerIndex(series float src, series float atr) =>
    symbolRollingMove = src - src[periodPC]
    symbolPowerIndex = symbolRollingMove / atr[1]

// current + baseline
powerIndex = calcPowerIndex(close, ta.atr(periodATR))
[src0, atr0] = request.security(symbol=baseline,  timeframe="", expression=[src, ta.atr(periodATR)])
powerIndex0 = calcPowerIndex(src0, atr0)

// sectors
var plot1  = str.length(s1) > 0 and (showAll or show1  or isSymbolInSector(symbols1))
[src1,  atr1 ] = request.security(symbol=s1,  timeframe="", expression=[src, ta.atr(periodATR)])
powerIndex1  = calcPowerIndex(plot1  ? src1  : na, plot1  ? atr1  : na)


// ===== Visuals (Sector Tags) =====

// sectors
var string[] activeSectors = array.new_string()
var color[] activeColors = array.new_color()
if barstate.isfirst
    if plot1
        array.push(activeSectors, str.length(n1) > 0 ? n1 : syminfo.ticker(s1))
        array.push(activeColors, c1)



var sectorTag = table.new(position=position.bottom_right, columns=array.size(activeSectors), rows=1, border_width=0)
if barstate.isfirst
    int i = 0
    while i < array.size(activeSectors)
        table.cell(sectorTag, column=i, row=0, text=array.get(activeSectors, i), text_color=array.get(activeColors, i))
        i := i+1


//BarColor Plot: 
 
long1 = powerIndex > powerIndex0 and powerIndex1 > powerIndex0 and powerIndex > powerIndex1 and isSymbolInSector(symbols1)
short1 = powerIndex < powerIndex0 and powerIndex1 < powerIndex0 and powerIndex < powerIndex1 and isSymbolInSector(symbols1)
 
vwap = ta.vwap
vwapup = close > ta.vwap
vwapdown = close < ta.vwap




plotshape((long1) and vwapup, "Long", shape.triangleup, location.belowbar, color.green)
plotshape((short1) and vwapdown, "Short", shape.triangledown, location.abovebar, color.red)





