Heiko Fuchs
Heiko Fuchs

Reputation: 3

Tradingview Pine Script - variable in array to plot current ticker

I am new to pine script, just wanted to change a script which plots currency strengths, I want it to only plot the 2 Lines from the pair in the current chart.

This is what I tried so far

.........................
avgUSD = (USDEUR + USDGBP + USDJPY + USDNZD + USDCAD + USDCHF + USDAUD) / 7
avgEUR = (EURNZD + EURGBP + EURJPY + EURUSD + EURCAD + EURCHF + EURAUD) / 7
avgJPY = (JPYEUR + JPYGBP + JPYNZD + JPYUSD + JPYCAD + JPYCHF + JPYAUD) / 7
avgCAD = (CADEUR + CADGBP + CADJPY + CADUSD + CADNZD + CADCHF + CADAUD) / 7
avgCHF = (CHFEUR + CHFGBP + CHFJPY + CHFUSD + CHFCAD + CHFNZD + CHFAUD) / 7
avgAUD = (AUDEUR + AUDGBP + AUDJPY + AUDUSD + AUDCAD + AUDCHF + AUDNZD) / 7
avgNZD = (NZDEUR + NZDGBP + NZDJPY + NZDUSD + NZDCAD + NZDCHF + NZDAUD) / 7
avgGBP = (GBPEUR + GBPJPY + GBPUSD + GBPCAD + GBPCHF + GBPAUD + GBPNZD) / 7

cName = (syminfo.root)
avgLeft = 0.0
avgRight = 0.0
cLeft = ""
cRight = ""

if cName == "CADCHF"
    avgLeft = avgCAD
    cLeft = "color=colCAD"
    avgRight = avgCHF
    cRight = "color=colCHF"
else
    avgLeft = avgAUD
    cLeft = "color=colAUD"
    avgRight = avgJPY
    cRight = "color=colJPY"
    


plot(avgLeft, title="NZD % Change", color=colNZD)
plot(avgRight, title="EUR % Change", color=colEUR)

where cName is the Ticker and I just would check with if clause which ticker it is and therefor assign the right values to the variables (string manipulation would be much less code, but couldnt get that to work either)

avgLeft and avgRight are the variables which I then would plot , but its not working, I get flat lines or "undeclared identifier" error if I do not set avgRight avgLeft to 0.0 at the beginning

It should just assign the the right average to my avgLeft avgRight variables, but it seems my if clause does nothing

PS: title and color are only there for testing, I know they are not matching

Thanks

Upvotes: 0

Views: 434

Answers (1)

rumpypumpydumpy
rumpypumpydumpy

Reputation: 3833

Within your code those variable assignments are only within the local scope of the if statements. Because they are written with only a = those variables are created and assigned those values as their initial value, and only within the scope of the if statement. You need to use reassign := instead to assign those values to the global variables.

if cName == "CADCHF"
    avgLeft := avgCAD
    cLeft := "color=colCAD"
    avgRight := avgCHF
    cRight := "color=colCHF"

Upvotes: 0

Related Questions