//@version=5
indicator("KusKus Starlight w/ cross", shorttitle = "Kus", 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
RangePeriod = input.int(30, title="Range Periods")
PriceSmoothing = input.float(0.3, title="Price Smoothing")
IndexSmoothing = input.float(0.3, title="Index Smoothing")

// Set fixed values if needed
if useFixedValue
    if assetType == "forex" and timeFrame == "1d"
        RangePeriod := 9
        PriceSmoothing := 0.8
        IndexSmoothing := 0.7
    else if assetType == "forex" and timeFrame == "4h"
        RangePeriod := 21
        PriceSmoothing := 0.5
        IndexSmoothing := 0.5
    else if assetType == "crypto" and timeFrame == "1d"
        RangePeriod := 38
        PriceSmoothing := 0.4
        IndexSmoothing := 0.2
    else if assetType == "crypto" and timeFrame == "4h"
        RangePeriod := 9
        PriceSmoothing := 0.1
        IndexSmoothing := 0.5
    else if assetType == "metals" and timeFrame == "1d"
        RangePeriod := 9
        PriceSmoothing := 0.3
        IndexSmoothing := 0.9
    else if assetType == "metals" and timeFrame == "4h"
        RangePeriod := 40
        PriceSmoothing := 0.1
        IndexSmoothing := 0.1
    else if assetType == "stocks" and timeFrame == "1d"
        RangePeriod := 6
        PriceSmoothing := 0.7
        IndexSmoothing := 0.8
    else if assetType == "stocks" and timeFrame == "4h"
        RangePeriod := 19
        PriceSmoothing := 0.9
        IndexSmoothing := 0.9

// Calculation
highestHigh = ta.highest(high, RangePeriod)
lowestLow = ta.lowest(low, RangePeriod)
greatestRange = (highestHigh - lowestLow)

// Calculate midPrice
midPrice = (high + low) / 2

// Prevent division by zero in price location calculation
priceLocation = greatestRange != 0 ? 2 * ((midPrice - lowestLow) / greatestRange) - 1 : 0

// Initialize buffers
var float extMapBuffer = na
var float extMapBuffer1 = na

// Smoothing of price location
extMapBuffer := PriceSmoothing * nz(extMapBuffer[1]) + (1 - PriceSmoothing) * priceLocation
smoothedLocation = math.max(math.min(extMapBuffer, 0.99), -0.99)

// Fisher location
fishIndex = math.log((1 + smoothedLocation) / (1 - smoothedLocation))

// Smoothing of Fisher location
extMapBuffer1 := IndexSmoothing * nz(extMapBuffer1[1]) + (1 - IndexSmoothing) * fishIndex
smoothedFish = extMapBuffer1

// Plot the smoothed Fisher index as a histogram
plot(smoothedFish, color=smoothedFish > 0 ? color.green : color.red, linewidth=3, style=plot.style_histogram)

// Crossover and crossunder detection for smoothedFish
fishCrossover = ta.crossover(smoothedFish, 0)
fishCrossunder = ta.crossunder(smoothedFish, 0)

// Plot shapes for crossover and crossunder
plotshape(fishCrossover, title="Fish Crossover", location=location.bottom, color=color.green, style=shape.triangleup, size=size.tiny)
plotshape(fishCrossunder, title="Fish Crossunder", location=location.bottom, color=color.red, style=shape.triangledown, size=size.tiny)