'CUSTOM RELATIVE between two securities

there is a Study to build a RELATIVE between two symbols that is like this:

`

study("RELATIVE STRENGTH", shorttitle="RS") 
a = tickerid 
b = input("SPY", type=symbol) 
as = security(a, period, close) 
bs = security(b, period, close) 
plot(as/bs, title="RS", color=blue)

`

This works fine to do relatives with SPY. But I would like to make the divisor customizable in Pine. for example, depending on the sector of the active security, I would like to choose in PINE the appropriate sector ETF(I have tried using if...else). I am new to programming in PINE, and I cannot make it . I have looked for a simmilar formula but I do not find it. This is what I have done and it says: "Mismatched input 'b' expecting 'end of line without line continuation'

`

//@version=2

study("RELATIVE STRENGTH", shorttitle="RS") 
a = ticker    // or tickerid ???
//b = input("SPX500USD", type=symbol) 

if a == "AAPL"  
     b := input("XLK", type=symbol) 
     
    else if a == "AMZN"
        b := input("XLY", type=symbol) 
    
    else
        b := input("SPY", type=symbol) 

as = security(a, period, close) 
bs = security(b, period, close) 
plot(as/bs, title="RS", color=blue)

`

thank you for your help



Solution 1:[1]

Finally I was able to solve it . Just add this lines to the @e2e4 code

myLabel = label.new(bar_index[0],secA/secB ,"/"+str.tostring(b), color = color.white, style = label.style_label_left, textcolor = color.blue, size=size.normal )

label.delete(myLabel[1])

Solution 2:[2]

The inputs in Pine Script could not change during the runtime and should be known at the compilation stage. Moreover, the request.security() function accepts only a simple form of strings for the symbol= argument, hence the mutable b variable in your example is not compatible with the security functions.

Instead of input, that could not be changed from the Pine script during the runtime, you can use the ternary ?: operator and hardcore the values for b to make it compatible with the security calls. The script below uses the Pine Script version 5 compiler directive:

//@version=5

indicator("RELATIVE STRENGTH", shorttitle="RS") 
ticker = syminfo.ticker
var string b = ticker == "AAPL" ? "XLK" : ticker == "AMZN" ? "XLY" : "SPY"

secA = request.security(ticker, timeframe.period, close) 
secB = request.security(b, timeframe.period, close) 
plot(secA/secB, title="RS", color=color.blue)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wai Ha Lee
Solution 2 e2e4