Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.1 kB
17
Indexable
Never
//@version=5

indicator("Supertrended RSI", overlay = true, timeframe = "")

// Input parameters
relativeStrengthIndexLength = input.int(14, title="RSI Length", group="RSI Settings")
smoothingLength = input.int(21, title="RSI Smoothing Length", group="RSI Settings")
rsiInputSource = input.source(close, title="RSI Source", group="RSI Settings")
isSmoothed = input.bool(false, "Smooth RSI?", group="RSI Settings")

movingAverageLength = input.int(14, title="MA Length", group="MA Settings", display = display.data_window)
movingAverageType = input.string("HMA", title="MA Type", options=["SMA", "HMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings", display = display.data_window)
showMovingAverage = input.bool(true, "Show RSI MA?", group="MA Settings")

trendFactor = input.float(0.8, title="Factor", group="Super Trend Settings")
averageTrueRangeLength = input.int(10, title="ATR Length", group="Super Trend Settings")

colorUp = input.color(#00ffbb, title="Up Color")
colorDown = input.color(#ff1100, title="Down Color")

// MA Selection
calculateMA(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "HMA" => ta.hma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

// Function to calculate ATR
calculateATR(source, atrLength) =>
    highestHigh = ta.highest(source, atrLength)
    lowestLow = ta.lowest(source, atrLength)
    trueRange = na(highestHigh[1]) ? highestHigh - lowestLow : math.max(highestHigh - lowestLow, math.abs(highestHigh - source[1]), math.abs(lowestLow - source[1]))
    ta.rma(trueRange, atrLength)

// Function to calculate Supertrend
calculateSupertrend(factor, atrPeriod, source) =>
    priceSource = source
    atr = calculateATR(source, atrPeriod)
    upperBand = priceSource + factor * atr
    lowerBand = priceSource - factor * atr
    prevLowerBand = nz(lowerBand[1])
    prevUpperBand = nz(upperBand[1])
    lowerBand := lowerBand > prevLowerBand or source[1] < prevLowerBand ? lowerBand : prevLowerBand
    upperBand := upperBand < prevUpperBand or source[1] > prevUpperBand ? upperBand : prevUpperBand
    int trendDirection = na
    float supertrendValue = na
    prevSupertrend = supertrendValue[1]
    if na(atr[1])
        trendDirection := 1
    else if prevSupertrend == prevUpperBand
        trendDirection := source > upperBand ? -1 : 1
    else
        trendDirection := source < lowerBand ? 1 : -1
    supertrendValue := trendDirection == -1 ? lowerBand : upperBand
    [supertrendValue, trendDirection]

// Calculating RSI
rsiValue = isSmoothed ? ta.hma(ta.rsi(rsiInputSource, relativeStrengthIndexLength), smoothingLength) : ta.rsi(rsiInputSource, relativeStrengthIndexLength)
rsiMovingAverage = calculateMA(rsiValue, movingAverageLength, movingAverageType)

// Calculating Supertrend based on RSI values
[rsiSupertrend, trendDirection] = calculateSupertrend(trendFactor, averageTrueRangeLength, rsiValue)


// Coloring bars based on Supertrend
barcolor(trendDirection == 1 ? color.new(colorUp, 90) : trendDirection == -1 ? color.new(colorDown, 90) : na)

// Char plotting for crossover and crossunder
plotshape(ta.crossover(rsiSupertrend, rsiValue) and rsiSupertrend > 70 ? 85 : na, style=shape.triangledown, location=location.abovebar, color=colorDown, size=size.tiny, title="Crossover Down")
plotshape(ta.crossunder(rsiSupertrend, rsiValue) and rsiSupertrend < 30 ? 15 : na, style=shape.triangleup, location=location.belowbar, color=colorUp, size=size.tiny, title="Crossunder Up")

//Alerts
alertcondition(ta.crossover(rsiValue, rsiSupertrend), "RSI Bullish Supertrend")
alertcondition(ta.crossunder(rsiValue, rsiSupertrend), "RSI Bearish Supertrend")
alertcondition(ta.crossover(rsiSupertrend, rsiValue) and rsiSupertrend > 70, "RSI Bullish Reversal")
alertcondition(ta.crossunder(rsiSupertrend, rsiValue) and rsiSupertrend < 30, "RSI Bearish Reversal")
Leave a Comment