//@version=5
indicator("StableFx w/ cross", overlay=false)

// Input settings
useFixedValue = input.bool(false, title="Use Fixed Value?")
assetType = input.string("forex", title="Asset Type", options=["forex", "crypto", "metals", "stocks"])
timeFrame = input.string("1d", title="Time Frame", options=["4h", "1d"])

// Default input parameters
CCI = input.int(34, title="CCI Length")
p = input.int(6, title="Smoothing 'p'")
period = input.int(61, title="MA Length")
method = input.string("0 - Simple", title="MA Type", options=["0 - Simple", "1 - Exponential", "2 - Smoothed", "3 - Linear Weighted"])

// Set fixed values if needed
if useFixedValue
    if assetType == "forex" and timeFrame == "1d"
        CCI := 13
        p := 17
        period := 27
        method := "0 - Simple"
    else if assetType == "forex" and timeFrame == "4h"
        CCI := 25
        p := 2
        period := 18
        method := "0 - Simple"
    else if assetType == "crypto" and timeFrame == "1d"
        CCI := 50
        p := 1
        period := 48
        method := "0 - Simple"
    else if assetType == "crypto" and timeFrame == "4h"
        CCI := 25
        p := 3
        period := 26
        method := "3 - Linear Weighted"
    else if assetType == "metals" and timeFrame == "1d"
        CCI := 14
        p := 15
        period := 5
        method := "1 - Exponential"
    else if assetType == "metals" and timeFrame == "4h"
        CCI := 100
        p := 1
        period := 53
        method := "2 - Smoothed"
    else if assetType == "stocks" and timeFrame == "1d"
        CCI := 51
        p := 17
        period := 71
        method := "1 - Exponential"
    else if assetType == "stocks" and timeFrame == "4h"
        CCI := 29
        p := 15
        period := 5
        method := "1 - Exponential"

// CCI Calculation
cci_src = hlc3
cci_val = ta.cci(cci_src, CCI)

// Weights
c1 = 0.5
c2 = 0.5
c3 = 0.5

// Apply weights to CCI values
wts = array.new_float(p, 0)
for i = 0 to p - 1
    weighted_val = cci_val[i] * c1 + cci_val[i] * c2 - cci_val[i] * c3
    array.push(wts, weighted_val)

// Sum the max and min values in the array
cci_wt = array.max(wts) + array.min(wts)

// Select MA type based on 'method'
ma = switch method
    "0 - Simple" => ta.sma(cci_wt, period)
    "1 - Exponential" => ta.ema(cci_wt, period)
    "2 - Smoothed" => ta.rma(cci_wt, period)
    "3 - Linear Weighted" => ta.linreg(cci_wt, period, 0)

// Plot CCI Weighted Value
plot(cci_wt, title="StableFX", color=color.green)

// Plot Moving Average
plot(ma, title="Moving Average", color=color.red)

// Crossover detection between CCI Weighted Value and Moving Average
crossOver = ta.crossover(cci_wt, ma)
crossUnder = ta.crossunder(cci_wt, ma)

// Plot shapes for crossovers
plotshape(crossOver, title="Crossover Up", location=location.bottom, color=color.green, style=shape.triangleup, size=size.tiny)
plotshape(crossUnder, title="Crossover Down", location=location.bottom, color=color.red, style=shape.triangledown, size=size.tiny)
