CCFp (Complex Common Frames percent) ,Currency Strength
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kita3bb

//@version=4
study("CCFp (Complex Common Frames percent)","CCFp",false,format.price,4)

// —————————————————————Inputs———————————————————————————————————————

show_only_pair = input(false,'Show Only Pair On Chart')
mode      = input('3:LWMA','MA_Method', options = ['0:SMA','1:EMA','2:SMMA','3:LWMA'] )
src       = input(ohlc4,"Price",input.source)

fastlen   = input( 3,"Fast",minval = 1, maxval = 50)
slowlen   = input( 5,"Slow",minval = 2, maxval = 100)

prefix = syminfo.prefix
sym1 = syminfo.basecurrency
sym2 = syminfo.currency

tf        = timeframe.period
int time_frame = tf=='1' ? 1 :tf=='5' ? 5: tf=='15' ? 15 :tf=='30' ? 30 :tf == '60'? 60 :tf=='240'?240:tf == 'D'? 1440: tf == 'W' ? 10080:tf == 'M' ? 43200:na


usd   = input(true,'USD')
eur   = input(true,'EUR')
gbp   = input(true,'GBP')
aud   = input(true,'AUD')
nzd   = input(true,'NZD')
cad   = input(true,'CAD')
chf   = input(true,'CHF')
jpy   = input(true,'JPY')


color_usd = input(color.green,'Color_USD',input.color)
color_eur = input(color.blue,'Color_EUR',input.color)
color_gbp = input(color.red,'Color_GBP',input.color)
color_chf = input(color.gray,'Color_CHF',input.color)
color_jpy = input(color.maroon,'Color_JPY',input.color)
color_aud = input(color.orange,'Color_AUD',input.color)
color_cad = input(color.purple,'Color_CAD',input.color)
color_nzd = input(color.teal,'Color_NZD',input.color)


 
 
// —————————————————— data —————————————————————————————

eurusd = security(prefix + ":EURUSD",tf,src,lookahead=barmerge.lookahead_off)
gbpusd = security(prefix + ":GBPUSD",tf,src,lookahead=barmerge.lookahead_off)
audusd = security(prefix + ":AUDUSD",tf,src,lookahead=barmerge.lookahead_off)
nzdusd = security(prefix + ":NZDUSD",tf,src,lookahead=barmerge.lookahead_off)
usdcad = security(prefix + ":USDCAD",tf,src,lookahead=barmerge.lookahead_off)
usdchf = security(prefix + ":USDCHF",tf,src,lookahead=barmerge.lookahead_off)
usdjpy = security(prefix + ":USDJPY",tf,src,lookahead=barmerge.lookahead_off)

// ———————————— Calculations  ———————————————————————————


/// LWMA Method
lwma(src,period) =>
    float p = na
    float sum = 0
    float divider = period * (period + 1) / 2
    for i = 0 to period-1
        p := src[i] * (period - i)
        sum := sum + p
    sum / divider

//SMMA Method
smma(src,period) =>
    ret_val = 0.0
    sma_1 = sma(src, period)
    ret_val := na(ret_val[1]) ? sma_1 : (ret_val[1] * (period - 1) + src) / period
    
    ret_val

//calc ma
ma(source,method,period,time_frame) =>
    float ret_val = 0
    k = 1
    if method == '0:SMA'
        if time_frame <= 1
            ret_val := ret_val + sma(source,period*k)
            k += 5
        if time_frame <= 5
            ret_val := ret_val + sma(source,period*k)
            k += 3
        if time_frame <= 15
            ret_val := ret_val + sma(source,period*k)
            k += 2
        if time_frame <= 30
            ret_val := ret_val + sma(source,period*k)
            k += 2
        if time_frame <= 60
            ret_val := ret_val + sma(source,period*k)
            k += 4
        if time_frame <= 240
            ret_val := ret_val + sma(source,period*k)
            k += 6
        if time_frame <= 1440
            ret_val := ret_val + sma(source,period*k)
            k += 4
        if time_frame <= 10080
            ret_val := ret_val + sma(source,period*k)
            k += 4
        if time_frame <= 43200
            ret_val := ret_val + sma(source,period*k)
            
    if method == '1:EMA'
        if time_frame <= 1
            ret_val := ret_val + ema(source,period*k)
            k += 5
        if time_frame <= 5
            ret_val := ret_val + ema(source,period*k)
            k += 3
        if time_frame <= 15
            ret_val := ret_val + ema(source,period*k)
            k += 2
        if time_frame <= 30
            ret_val := ret_val + ema(source,period*k)
            k += 2
        if time_frame <= 60
            ret_val := ret_val + ema(source,period*k)
            k += 4
        if time_frame <= 240
            ret_val := ret_val + ema(source,period*k)
            k += 6
        if time_frame <= 1440
            ret_val := ret_val + ema(source,period*k)
            k += 4
        if time_frame <= 10080
            ret_val := ret_val + ema(source,period*k)
            k += 4
        if time_frame <= 43200
            ret_val := ret_val + ema(source,period*k)

    if method == '2:SMMA'
        if time_frame <= 1
            ret_val := ret_val + smma(source,period*k)
            k += 5
        if time_frame <= 5
            ret_val := ret_val + smma(source,period*k)
            k += 3
        if time_frame <= 15
            ret_val := ret_val + smma(source,period*k)
            k += 2
        if time_frame <= 30
            ret_val := ret_val + smma(source,period*k)
            k += 2
        if time_frame <= 60
            ret_val := ret_val + smma(source,period*k)
            k += 4
        if time_frame <= 240
            ret_val := ret_val + smma(source,period*k)
            k += 6
        if time_frame <= 1440
            ret_val := ret_val + smma(source,period*k)
            k += 4
        if time_frame <= 10080
            ret_val := ret_val + smma(source,period*k)
            k += 4
        if time_frame <= 43200
            ret_val := ret_val + smma(source,period*k)
 
    if method == '3:LWMA'
        if time_frame <= 1
            ret_val := ret_val + lwma(source,period*k)
            k += 5
        if time_frame <= 5
            ret_val := ret_val + lwma(source,period*k)
            k += 3
        if time_frame <= 15
            ret_val := ret_val + lwma(source,period*k)
            k += 2
        if time_frame <= 30
            ret_val := ret_val + lwma(source,period*k)
            k += 2
        if time_frame <= 60
            ret_val := ret_val + lwma(source,period*k)
            k += 4
        if time_frame <= 240
            ret_val := ret_val + lwma(source,period*k)
            k += 6
        if time_frame <= 1440
            ret_val := ret_val + lwma(source,period*k)
            k += 4
        if time_frame <= 10080
            ret_val := ret_val + lwma(source,period*k)
            k += 4
        if time_frame <= 43200
            ret_val := ret_val + lwma(source,period*k)
        
    ret_val

// CCFP index
CalcCCFP(ticker,eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) =>
    float ccfp_idx = 0.0

    if ticker == 'USD'
        if eur
            ccfp_idx += eurusd_slow / eurusd_fast - 1
        if gbp
            ccfp_idx += gbpusd_slow / gbpusd_fast - 1
        if aud
            ccfp_idx += audusd_slow / audusd_fast - 1
        if nzd
            ccfp_idx += nzdusd_slow / nzdusd_fast - 1
        if chf
            ccfp_idx += usdchf_fast / usdchf_slow - 1
        if cad
            ccfp_idx += usdcad_fast / usdcad_slow - 1
        if jpy
            ccfp_idx += usdjpy_fast / usdjpy_slow - 1
            
    else if ticker == 'EUR'
        if usd
            ccfp_idx += eurusd_fast / eurusd_slow - 1
        if gbp
            ccfp_idx += (eurusd_fast / gbpusd_fast) / (eurusd_slow/gbpusd_slow) - 1
        if aud
            ccfp_idx += (eurusd_fast / audusd_fast) / (eurusd_slow/audusd_slow) - 1
        if nzd
            ccfp_idx += (eurusd_fast / nzdusd_fast) / (eurusd_slow/nzdusd_slow) - 1
        if chf
            ccfp_idx += (eurusd_fast * usdchf_fast) / (eurusd_slow * usdchf_slow) - 1
        if cad
            ccfp_idx += (eurusd_fast * usdcad_fast) / (eurusd_slow * usdcad_slow) - 1
        if jpy
            ccfp_idx += (eurusd_fast * usdjpy_fast) / (eurusd_slow * usdjpy_slow) - 1
    
    else if ticker == 'GBP'
        if usd
            ccfp_idx += gbpusd_fast / gbpusd_slow - 1
        if eur
            ccfp_idx += (eurusd_slow / gbpusd_slow) / (eurusd_fast / gbpusd_fast) - 1
        if aud
            ccfp_idx += (gbpusd_fast / audusd_fast) / (gbpusd_slow / audusd_slow) - 1
        if nzd
            ccfp_idx += (gbpusd_fast / nzdusd_fast) / (gbpusd_slow / nzdusd_slow) - 1
        if chf
            ccfp_idx += (gbpusd_fast * usdchf_fast) / (gbpusd_slow * usdchf_slow) - 1
        if cad
            ccfp_idx += (gbpusd_fast * usdcad_fast) / (gbpusd_slow * usdcad_slow) - 1
        if jpy
            ccfp_idx += (gbpusd_fast * usdjpy_fast) / (gbpusd_slow * usdjpy_slow) - 1
    
    else if ticker == 'AUD'
        if usd
            ccfp_idx += audusd_fast / audusd_slow - 1
        if eur
            ccfp_idx += (eurusd_slow / audusd_slow) / (eurusd_fast / audusd_fast) - 1
        if gbp
            ccfp_idx += (gbpusd_slow / audusd_slow) / (gbpusd_fast / audusd_fast) - 1
        if nzd
            ccfp_idx += (audusd_fast / nzdusd_fast) / (audusd_slow / nzdusd_slow) - 1
        if chf
            ccfp_idx += (audusd_fast * usdchf_fast) / (audusd_slow * usdchf_slow) - 1
        if cad
            ccfp_idx += (audusd_fast * usdcad_fast) / (audusd_slow * usdcad_slow) - 1
        if jpy
            ccfp_idx += (audusd_fast * usdjpy_fast) / (audusd_slow * usdjpy_slow) - 1
    
    else if ticker == 'NZD'
        if usd
            ccfp_idx += nzdusd_fast / nzdusd_slow - 1
        if eur
            ccfp_idx += (eurusd_slow / nzdusd_slow) / (eurusd_fast / nzdusd_fast) - 1
        if gbp
            ccfp_idx += (gbpusd_slow / nzdusd_slow) / (gbpusd_fast / nzdusd_fast) - 1
        if aud
            ccfp_idx += (audusd_slow / nzdusd_slow) / (audusd_fast / nzdusd_fast) - 1
        if chf
            ccfp_idx += (nzdusd_fast * usdchf_fast) / (nzdusd_slow * usdchf_slow) - 1
        if cad
            ccfp_idx += (nzdusd_fast * usdcad_fast) / (nzdusd_slow * usdcad_slow) - 1
        if jpy
            ccfp_idx += (nzdusd_fast * usdjpy_fast) / (nzdusd_slow * usdjpy_slow) - 1
    
    else if ticker == 'CAD'
        if usd
            ccfp_idx += usdcad_slow / usdcad_fast - 1
        if eur
            ccfp_idx += (eurusd_slow * usdcad_slow) / (eurusd_fast * usdcad_fast) - 1
        if gbp
            ccfp_idx += (gbpusd_slow * usdcad_slow) / (gbpusd_fast * usdcad_fast) - 1
        if aud
            ccfp_idx += (audusd_slow * usdcad_slow) / (audusd_fast * usdcad_fast) - 1
        if nzd
            ccfp_idx += (nzdusd_slow * usdcad_slow) / (nzdusd_fast * usdcad_fast) - 1
        if chf
            ccfp_idx += (usdchf_fast / usdcad_fast) / (usdchf_slow / usdcad_slow) - 1
        if jpy
            ccfp_idx += (usdjpy_fast / usdcad_fast) / (usdjpy_slow / usdcad_slow) - 1
    
    else if ticker == 'CHF'
        if usd
            ccfp_idx += usdchf_slow / usdchf_fast - 1
        if eur
            ccfp_idx += (eurusd_slow * usdchf_slow) / (eurusd_fast * usdchf_fast) - 1
        if gbp
            ccfp_idx += (gbpusd_slow * usdchf_slow) / (gbpusd_fast * usdchf_fast) - 1
        if aud
            ccfp_idx += (audusd_slow * usdchf_slow) / (audusd_fast * usdchf_fast) - 1
        if nzd
            ccfp_idx += (nzdusd_slow * usdchf_slow) / (nzdusd_fast * usdchf_fast) - 1
        if cad
            ccfp_idx += (usdchf_slow / usdcad_slow) / (usdchf_fast / usdcad_fast) - 1
        if jpy
            ccfp_idx += (usdjpy_fast / usdchf_fast) / (usdjpy_slow / usdchf_slow) - 1
    
    else if ticker == 'JPY'
        if usd
            ccfp_idx += usdjpy_slow / usdjpy_fast - 1
        if eur
            ccfp_idx += (eurusd_slow * usdjpy_slow) / (eurusd_fast * usdjpy_fast) - 1
        if gbp
            ccfp_idx += (gbpusd_slow * usdjpy_slow) / (gbpusd_fast * usdjpy_fast) - 1
        if aud
            ccfp_idx += (audusd_slow * usdjpy_slow) / (audusd_fast * usdjpy_fast) - 1
        if nzd
            ccfp_idx += (nzdusd_slow * usdjpy_slow) / (nzdusd_fast * usdjpy_fast) - 1
        if cad
            ccfp_idx += (usdjpy_slow / usdcad_slow) / (usdjpy_fast / usdcad_fast) - 1
        if chf
            ccfp_idx += (usdjpy_slow / usdchf_slow) / (usdjpy_fast / usdchf_fast) - 1
    
        
    ccfp_idx

show_usd = show_only_pair ? (sym1 == 'USD' or sym2 == 'USD') and usd :usd
show_eur = show_only_pair ? (sym1 == 'EUR' or sym2 == 'EUR') and eur :eur
show_gbp = show_only_pair ? (sym1 == 'GBP' or sym2 == 'GBP') and gbp :gbp
show_aud = show_only_pair ? (sym1 == 'AUD' or sym2 == 'AUD') and aud :aud
show_nzd = show_only_pair ? (sym1 == 'NZD' or sym2 == 'NZD') and nzd :nzd
show_cad = show_only_pair ? (sym1 == 'CAD' or sym2 == 'CAD') and cad :cad
show_chf = show_only_pair ? (sym1 == 'CHF' or sym2 == 'CHF') and chf :chf
show_jpy = show_only_pair ? (sym1 == 'JPY' or sym2 == 'JPY') and jpy :jpy

eurusd_slow = eur ? ma(eurusd,mode,slowlen,time_frame) : na
eurusd_fast = eur ? ma(eurusd,mode,fastlen,time_frame) : na
gbpusd_slow = gbp ? ma(gbpusd,mode,slowlen,time_frame) : na
gbpusd_fast = gbp ? ma(gbpusd,mode,fastlen,time_frame) : na
audusd_slow = aud ? ma(audusd,mode,slowlen,time_frame) : na
audusd_fast = aud ? ma(audusd,mode,fastlen,time_frame) : na
nzdusd_slow = nzd ? ma(nzdusd,mode,slowlen,time_frame) : na
nzdusd_fast = nzd ? ma(nzdusd,mode,fastlen,time_frame) : na
usdchf_slow = chf ? ma(usdchf,mode,slowlen,time_frame) : na
usdchf_fast = chf ? ma(usdchf,mode,fastlen,time_frame) : na
usdcad_slow = cad ? ma(usdcad,mode,slowlen,time_frame) : na
usdcad_fast = cad ? ma(usdcad,mode,fastlen,time_frame) : na
usdjpy_slow = jpy ? ma(usdjpy,mode,slowlen,time_frame) : na
usdjpy_fast = jpy ? ma(usdjpy,mode,fastlen,time_frame) : na

USD = show_usd ? CalcCCFP('USD',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na
EUR = show_eur ? CalcCCFP('EUR',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na
GBP = show_gbp ? CalcCCFP('GBP',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na
AUD = show_aud ? CalcCCFP('AUD',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na
NZD = show_nzd ? CalcCCFP('NZD',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na
CAD = show_cad ? CalcCCFP('CAD',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na
CHF = show_chf ? CalcCCFP('CHF',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na
JPY = show_jpy ? CalcCCFP('JPY',eurusd,gbpusd,audusd,nzdusd,usdchf,usdcad,usdjpy,usd,eur,gbp,aud,nzd,chf,cad,jpy,eurusd_slow,eurusd_fast,gbpusd_slow,gbpusd_fast,audusd_slow,audusd_fast,nzdusd_slow,nzdusd_fast,usdchf_slow,usdchf_fast,usdcad_slow,usdcad_fast,usdjpy_slow,usdjpy_fast) : na

// ————— Plots --------//


plot(USD,title = 'USD',color = color_usd,linewidth= 2, style = plot.style_line,transp = 20)
plot(EUR,title = 'EUR',color = color_eur,linewidth= 2, style = plot.style_line,transp = 20)
plot(GBP,title = 'GBP',color = color_gbp,linewidth= 2, style = plot.style_line,transp = 20)
plot(AUD,title = 'AUD',color = color_aud,linewidth= 2, style = plot.style_line,transp = 20)
plot(NZD,title = 'NZD',color = color_nzd,linewidth= 2, style = plot.style_line,transp = 20)
plot(CAD,title = 'CAD',color = color_cad,linewidth= 2, style = plot.style_line,transp = 20)
plot(CHF,title = 'CHF',color = color_chf,linewidth= 2, style = plot.style_line,transp = 20)
plot(JPY,title = 'JPY',color = color_jpy,linewidth= 2, style = plot.style_line,transp = 20)


// --------Label------------//

style = label.style_label_left
size = size.small


var usd_label = label.new(bar_index, na, 'USD', color=color_usd,   style=style, textcolor=color.white, size=size)
var eur_label = label.new(bar_index, na, 'EUR', color=color_eur,   style=style, textcolor=color.white, size=size)
var gbp_label = label.new(bar_index, na, 'GBP', color=color_gbp,   style=style, textcolor=color.white, size=size)
var aud_label = label.new(bar_index, na, 'AUD', color=color_aud,   style=style, textcolor=color.white, size=size)
var nzd_label = label.new(bar_index, na, 'NZD', color=color_nzd,   style=style, textcolor=color.white, size=size)
var cad_label = label.new(bar_index, na, 'CAD', color=color_cad,   style=style, textcolor=color.white, size=size)
var chf_label = label.new(bar_index, na, 'CHF', color=color_chf,   style=style, textcolor=color.white, size=size)
var jpy_label = label.new(bar_index, na, 'JPY', color=color_jpy,   style=style, textcolor=color.white, size=size)


if barstate.islast
    if show_usd
        label.set_xy(usd_label,bar_index + 3, USD)
    if show_eur
        label.set_xy(eur_label,bar_index + 3, EUR)
    if show_gbp
        label.set_xy(gbp_label,bar_index + 3, GBP)
    if show_aud
        label.set_xy(aud_label,bar_index + 3, AUD)
    if show_nzd
        label.set_xy(nzd_label,bar_index + 3, NZD)
    if show_cad
        label.set_xy(cad_label,bar_index + 3, CAD)
    if show_chf
        label.set_xy(chf_label,bar_index + 3, CHF)
    if show_jpy
        label.set_xy(jpy_label,bar_index + 3, JPY)
    
