Plot background depending on Index EMA 10 and EMA 20
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MrMoneyMaker81

//@version=5
indicator(title="Index MA", overlay=true)
//Get the prefix of the stock. 
stock_prefix = syminfo.prefix

//Check which index should be used for the indicator. Uncheck below line if you need to debug. If no match is found indicator will use IXIC as reference. 
//label.new(bar_index, high, syminfo.prefix) //For debug to get the prifix of the ticker, add en else if with "prefix" below.  

index_ticker = if(stock_prefix == "OMXSTO")
    "OMXSPI"
else if(stock_prefix == "OSL_DLY")
    "OBX"
else if(stock_prefix == "NGM")
    "OMXSPI"
else if(stock_prefix == "OMXHEX")
    "OMXHPI"
else if(stock_prefix == "OMXCOP")
    "OMXCPI"
else
    "IXIC"

//Calculated the EMA10 and EMA20 for the index. 
index = request.security(index_ticker, timeframe.period, close)
indexSMA10=ta.ema(index, 10)
indexSMA10_old=indexSMA10[1]
indexSMA20=ta.ema(index, 20)
indexSMA20_old=indexSMA20[1]

//Plot Index EMA10 and EMA20 but it's transparent as defalut.  
plot(indexSMA10, color=color.new(color.white,100), title = "IndexSMA10")
plot(indexSMA20, color=color.new(color.white,100), title = "IndexSMA20")

//Plot the different backgrounds. Description is in the title of each plot. 
bgcolor((indexSMA10 < indexSMA10_old and indexSMA20 < indexSMA20_old and indexSMA10 < indexSMA20) and timeframe.period =='D' ? color.new(color.red,50) : na, title = "MA10 < MA20 & MA10/MA20 down")
bgcolor((indexSMA10 > indexSMA10_old and indexSMA20 > indexSMA20_old and indexSMA10 > indexSMA20) and timeframe.period =='D' ? color.new(color.green,50) : na, title = "MA10 > MA20 & MA10/MA20 up")
bgcolor((indexSMA10 < indexSMA10_old and indexSMA20 < indexSMA20_old and indexSMA10 > indexSMA20) and timeframe.period =='D' ? color.new(color.orange,70) : na, title = "MA10 > MA20 & MA10/MA20 down")
bgcolor((indexSMA10 > indexSMA10_old and indexSMA20 > indexSMA20_old and indexSMA10 < indexSMA20) and timeframe.period =='D' ? color.new(color.green,70) : na, title = "MA10 < MA20 & MA10/MA20 up")
bgcolor((indexSMA10 < indexSMA10_old and indexSMA20 > indexSMA20_old and indexSMA10 > indexSMA20) and timeframe.period =='D' ? color.new(color.orange,50) : na, title = "MA10 > MA20 & MA10 down MA20 up")
bgcolor((indexSMA10 > indexSMA10_old and indexSMA20 < indexSMA20_old and indexSMA10 < indexSMA20) and timeframe.period =='D' ? color.new(#00ff0a,50) : na, title = "MA10 < MA20 & MA10 up MA20 down")