//@version=5
indicator("Tilson T3 crossFixed", overlay=true)

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

// Default parameters
length = input.int(7, title="Length", minval=1)
b = input.float(0.7, title="Factor")
priceType = input.string("Close", title="Price", options=["Close", "Open", "High", "Low", "Median", "Typical", "Weighted"])

// Set fixed values based on asset type and timeframe
if useFixedValue
    if assetType == "forex" and timeFrame == "1d"
        length := 23
        b := 1.13
        priceType := "Close"
    else if assetType == "forex" and timeFrame == "4h"
        length := 15
        b := 1.98
        priceType := "Close"
    else if assetType == "crypto" and timeFrame == "1d"
        length := 26
        b := 1.98
        priceType := "Close"
    else if assetType == "crypto" and timeFrame == "4h"
        length := 66
        b := 1.10
        priceType := "Median"
    else if assetType == "metals" and timeFrame == "1d"
        length := 12
        b := 0.08
        priceType := "Open"
    else if assetType == "metals" and timeFrame == "4h"
        length := 2
        b := 1.05
        priceType := "Low"
    else if assetType == "stocks" and timeFrame == "1d"
        length := 61
        b := 0.81
        priceType := "Typical"
    else if assetType == "stocks" and timeFrame == "4h"
        length := 70
        b := 1.35
        priceType := "Close"

// Reduce 'b' by 1
b := b - 1

// Select the source price based on priceType
src = switch priceType
    "Close" => close
    "Open" => open
    "High" => high
    "Low" => low
    "Median" => hl2
    "Typical" => hlc3
    "Weighted" => ohlc4
    => close

// T3 Calculation
c1 = -b * b * b
c2 = 3 * b * b + 3 * b * b * b
c3 = -6 * b * b - 3 * b - 3 * b * b * b
c4 = 1 + 3 * b + b * b * b + 3 * b * b

t3 = c1 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(src, length), length), length), length), length), length) +
     c2 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(src, length), length), length), length), length) +
     c3 * ta.ema(ta.ema(ta.ema(ta.ema(src, length), length), length), length) +
     c4 * ta.ema(ta.ema(ta.ema(src, length), length), length)

// Plot the T3 line
plot(t3, color=color.new(color.aqua, 0), title="T3 Line", linewidth=2)

// Crossover detection and plotshape
crossOver = ta.crossover(src, t3)
crossUnder = ta.crossunder(src, t3)

// Plot shapes for crossover and crossunder, always at the bottom
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)
