0

I keep receiving the following error. I don't understand because the variable is declared. Any suggestions? The script is simply trying to highlight the background based on conditional statements regarding the ticker VIX. Thanks!

Error: Undeclared identifier vix_highlight

study(title="VIX Momo", shorttitle="Vix Momo", overlay=false)

//VIX Momo Highlight 
vix_ticker = 'VIX'

smaValue_30_VIX = sma(close, 30) smaValue_50_VIX = sma(close, 50) smaValue_200_VIX = sma(close, 200)

vix_30sma = security(vix_ticker, 'D', smaValue_30_VIX) vix_50sma = security(vix_ticker, 'D', smaValue_50_VIX) vix_200sma = security(vix_ticker, 'D', smaValue_200_VIX)

timeFrame_VIX = input(title="Other time frame", type=resolution,
     defval="180")

smaClose_VIX = security(vix_ticker, timeFrame_VIX, sma(close,200))

threehr_price_VIX = security(vix_ticker, timeFrame_VIX, close)

red_VIX = (vix_30sma > vix_50sma) and (threehr_price_VIX >= smaClose_VIX) and (close >= vix_50sma)

vix_highlight = bgcolor(red_VIX ? #eb4034 : na, transp = 80)

plot(vix_highlight)
prime90
  • 889
  • 2
  • 14
  • 26

1 Answers1

2

The function bgcolor does not return any value, it only changes the background color. In this form, the script works.

//@version=4

study(title="Help (VIX Momo)", shorttitle="Vix Momo", overlay=false)

//VIX Momo Highlight 
vix_ticker = 'VIX'

// smaValue_30_VIX = sma(close, 30) 
// smaValue_50_VIX = sma(close, 50) 
// smaValue_200_VIX = sma(close, 200)

// vix_30sma = security(vix_ticker, 'D', smaValue_30_VIX) 
// vix_50sma = security(vix_ticker, 'D', smaValue_50_VIX) 
// vix_200sma = security(vix_ticker, 'D', smaValue_200_VIX)

vix_30sma = security(vix_ticker, 'D', sma(close, 30)) 
vix_50sma = security(vix_ticker, 'D', sma(close, 50)) 
//vix_200sma = security(vix_ticker, 'D', sma(close, 200))

timeFrame_VIX = input(title="Other time frame", type=input.resolution, defval="180")

smaClose_VIX = security(vix_ticker, timeFrame_VIX, sma(close,200))

threehr_price_VIX = security(vix_ticker, timeFrame_VIX, close)

red_VIX = (vix_30sma > vix_50sma) and (threehr_price_VIX >= smaClose_VIX) and (close >= vix_50sma)

bgcolor(red_VIX ? #eb4034 : na, transp = 80)

//plot(vix_highlight)
AnyDozer
  • 2,236
  • 2
  • 5
  • 22
  • It only works on the VIX ticker. Is it possible to plot the results of bgcolor, using VIX data, on all charts you pull up? That's why I was trying to use plot because I'm trying to get the VIX highlight to display on all my charts so I don't need to look at the VIX chart. Thanks. – prime90 Jan 31 '21 at 15:47
  • Yes, it is possible. It is best to write a function that will check the necessary conditions, and then request the results of this function for the ticker `VIX` via the function `security`. – AnyDozer Jan 31 '21 at 16:09
  • Ok, I'll give it a try and post a new question if I need help. Thank's. – prime90 Jan 31 '21 at 16:38
  • Take a look [here](https://stackoverflow.com/questions/65858641/how-to-apply-formula-to-several-securities#comment116461761_65858641). You can do something like this. – AnyDozer Jan 31 '21 at 18:27