 Fiat Currency and Gold Indices (FGXY) Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @ esistturnt
// Credit to @loxx for the stock DXY in pinescript


//@version=5
indicator("Fiat Currency and Gold Indices (FGXY) Candles",shorttitle="FGXY",overlay = false,timeframe='', timeframe_gaps = true)


transparency = input.int(0,'Transparency')
greencolor   = color.new(input.color(#ACFB00,'Up Color'),transparency)
redcolor     = color.new(input.color(#FF0000,'Down Color'),transparency)
//Default: Gold Index
str = input.string("CAD","(3 Letter Prefix: XAU,USD,CAD,EUR etc.)",group='Main Currency')

pair1 =input.string("EUR",'Currency Pair #1 (3 Letter Prefix)',group="Pair 1")
pair2 =input.string("JPY",'Currency Pair #2 (3 Letter Prefix)',group="Pair 2")
pair3 =input.string("GBP",'Currency Pair #3 (3 Letter Prefix)',group="Pair 3")
pair4 =input.string("CAD",'Currency Pair #4 (3 Letter Prefix)',group="Pair 4")
pair5 =input.string("SEK",'Currency Pair #5 (3 Letter Prefix)',group="Pair 5")
pair6 =input.string("CHF",'Currency Pair #6 (3 Letter Prefix)',group="Pair 6")

var string EUR = str == "USD" ?  pair1+"USD": str == "XAU"?  str + pair1       :  str != "EUR" ? str + pair1  :   pair1 + "USD"// ""   + str   :
var string JPY = str == "USD" ? "USD"+pair2 : str == "XAU"?  str + pair2       :  str != "JPY" ? str + pair2  :  "USD"  + pair2 // str + "JPY"  :
var string GBP = str == "USD" ?  pair3+"USD": str == "XAU"?  str + pair3       :  str != "GBP" ? str + pair3  :   pair3 + "USD" // ""   + str   :
var string CAD = str == "USD" ? "USD"+pair4 : str == "XAU"?  str + pair4       :  str != "CAD" ? str + pair4  :  "USD"  + pair4 // str  + ""     
var string SEK = str == "USD" ? "USD"+pair5 : str == "XAU"?  "XAUSEK1630GMTSEK":  str != "SEK" ? str + pair5  :  "USD"  + pair5 // st   + ""     
var string CHF = str == "USD" ? "USD"+pair6 : str == "XAU"?  str  + pair6      :  str != "CHF" ? str + pair6  :  "USD"  + pair6 // str  + ""     

//Default DXY Weights
sym1 = EUR      ,     weight1 = input.float(0.576,group='Pair 1')
sym2 = JPY      ,     weight2 = input.float(0.136,group='Pair 2')
sym3 = GBP      ,     weight3 = input.float(0.119,group='Pair 3')
sym4 = CAD      ,     weight4 = input.float(0.091,group='Pair 4')
sym5 = SEK      ,     weight5 = input.float(0.042,group='Pair 5')
sym6 = CHF      ,     weight6 = input.float(0.036,group='Pair 6')

//OHLC
eurc = request.security(sym1, timeframe.period, nz(close,close[1])) 
euro = request.security(sym1, timeframe.period, nz(open ,open [1])) 
eurh = request.security(sym1, timeframe.period, nz(high ,high [1])) 
eurl = request.security(sym1, timeframe.period, nz(low  ,low  [1])) 

jpyc = request.security(sym2, timeframe.period, nz(close,close[1])) 
jpyo = request.security(sym2, timeframe.period, nz(open ,open [1])) 
jpyh = request.security(sym2, timeframe.period, nz(high ,high [1])) 
jpyl = request.security(sym2, timeframe.period, nz(low  ,low  [1])) 

gbpc = request.security(sym3, timeframe.period, nz(close,close[1])) 
gbpo = request.security(sym3, timeframe.period, nz(open ,open [1])) 
gbph = request.security(sym3, timeframe.period, nz(high ,high [1])) 
gbpl = request.security(sym3, timeframe.period, nz(low  ,low  [1])) 

cadc = request.security(sym4, timeframe.period, nz(close,close[1])) 
cado = request.security(sym4, timeframe.period, nz(open ,open [1])) 
cadh = request.security(sym4, timeframe.period, nz(high ,high [1])) 
cadl = request.security(sym4, timeframe.period, nz(low  ,low  [1])) 

sekc = request.security(sym5, timeframe.period, nz(close,close[1])) 
seko = request.security(sym5, timeframe.period, nz(open ,open [1])) 
sekh = request.security(sym5, timeframe.period, nz(high ,high [1])) 
sekl = request.security(sym5, timeframe.period, nz(low  ,low  [1])) 

chfc = request.security(sym6, timeframe.period, nz(close,close[1])) 
chfo = request.security(sym6, timeframe.period, nz(open ,open [1])) 
chfh = request.security(sym6, timeframe.period, nz(high ,high [1])) 
chfl = request.security(sym6, timeframe.period, nz(low  ,low  [1]))
g=str=="XAU"?1:-1

//Apply DXY formula to OHLC values 
dxyClose = 50.14348112 * math.pow(eurc, g*weight1) * math.pow(jpyc, weight2) *
  math.pow(gbpc, -1*weight3)                  * math.pow(cadc, weight4) *
  math.pow(sekc, weight5)                  * math.pow(chfc, weight6)
dxyOpen = 50.14348112 * math.pow(euro, g*weight1) * math.pow(jpyo, weight2) *
  math.pow(gbpo, -1*weight3)                  * math.pow(cado, weight4) *
  math.pow(seko, weight5)                  * math.pow(chfo, weight6)
dxyHigh = 50.14348112 * math.pow(eurh, g*weight1) * math.pow(jpyh, weight2) *
  math.pow(gbph, -1*weight3)                  * math.pow(cadh, weight4) *
  math.pow(sekh, weight5)                  * math.pow(chfh, weight6)
dxyLow =  50.14348112 * math.pow(eurl, g*weight1) * math.pow(jpyl, weight2) *
  math.pow(gbpl, -1*weight3)                  * math.pow(cadl, weight4) *
  math.pow(sekl, weight5)                  * math.pow(chfl, weight6)
color= dxyOpen < dxyClose ? greencolor : redcolor
//Plot
plotcandle(dxyOpen, dxyHigh, dxyLow, dxyClose, title='FGXY', color =color,bordercolor=color, wickcolor=color)