RSI Precision Trend Candles [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("RSI Precision Trend Candles [Loxx]", 
     shorttitle='RSIPTC [Loxx]', 
     overlay = true, 
     timeframe="", 
     timeframe_gaps = true)
import loxx/loxxexpandedsourcetypes/3

greencolor = #2DD204
redcolor = #D2042D

_ptrend(float _high, float _low, float _close, int _period, float _sense)=>
    t_close = _close
    t_range = _high - _low
    t_avgr = t_range
    t_trend = 0., t_avgd = 0., t_avgu = 0., t_minc = 0.,t_maxc = 0.
    for i = 1 to _period
        t_avgr += nz(t_range)
    t_avgr /= _period
    t_avgr *= _sense
    t_trend := nz(t_trend[1])
    t_avgd := nz(t_avgd[1])
    t_avgu := nz(t_avgu[1])
    t_minc := nz(t_minc[1])
    t_maxc := nz(t_maxc[1])
    switch int(nz(t_trend[1])) 
        0=>
            if (_close > nz(t_avgu[1])) 
                t_minc := _close
                t_avgd := _close - t_avgr
                t_trend := 1
            if (_close < nz(t_avgd[1])) 
                t_maxc := _close
                t_avgu := _close + t_avgr
                t_trend := -1
        1=>
            t_avgd := nz(t_minc[1]) - t_avgr
            if (_close > nz(t_minc[1]))
                t_minc := _close
            if (_close < nz(t_avgd[1]))
                t_maxc := _close
                t_avgu := _close + t_avgr
                t_trend := -1
        -1=>
            t_avgu := t_maxc[1] + t_avgr
            if (_close < nz(t_maxc[1]))
                t_maxc := _close
            if (_close > nz(t_avgu[1]))
                t_minc := _close
                t_avgd := _close - t_avgr
                t_trend := 1
    t_trend
    
smthtype = input.string("Kaufman", "Heikin-Ashi Better Caculation Type", options = ["AMA", "T3", "Kaufman"], group = "Basic Settings")

srcin = input.string("Close", "Source", group= "Basic Settings", 
     options = 
     ["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average", "Average Median Body", "Trend Biased", "Trend Biased (Extreme)", 
     "HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend Biased (Extreme)",
     "HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical", "HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB Trend Biased (Extreme)"])

rsiper = input.int(14, "RSI Period", group = "Basic Settings")
rsihiloper = input.int(5, "RSI HiLo Period" , group = "Basic Settings")
avgper = input.int(30, "Average Period" , group = "Basic Settings")
sense = input.float(3, "sense" , group = "Basic Settings")

showsignals = input.bool(false, "Show signals?", group = "UI Options")

kfl=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End", group = "Moving Average Inputs")
ksl=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End", group = "Moving Average Inputs")
amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group = "Moving Average Inputs")
amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group = "Moving Average Inputs")

haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
hahigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
halow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
hamedian = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hl2)
hatypical = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlc3)
haweighted = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlcc4)
haaverage = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, ohlc4)

src = switch srcin
	"Close" => loxxexpandedsourcetypes.rclose()
	"Open" => loxxexpandedsourcetypes.ropen()
	"High" => loxxexpandedsourcetypes.rhigh()
	"Low" => loxxexpandedsourcetypes.rlow()
	"Median" => loxxexpandedsourcetypes.rmedian()
	"Typical" => loxxexpandedsourcetypes.rtypical()
	"Weighted" => loxxexpandedsourcetypes.rweighted()
	"Average" => loxxexpandedsourcetypes.raverage()
    "Average Median Body" => loxxexpandedsourcetypes.ravemedbody()
	"Trend Biased" => loxxexpandedsourcetypes.rtrendb()
	"Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
	"HA Close" => loxxexpandedsourcetypes.haclose(haclose)
	"HA Open" => loxxexpandedsourcetypes.haopen(haopen)
	"HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
	"HA Low" => loxxexpandedsourcetypes.halow(halow)
	"HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
	"HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
	"HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
	"HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
    "HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
	"HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
	"HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendbext(haclose, haopen, hahigh, halow)
	"HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
	"HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
	"HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
	"HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
	"HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
	"HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
	"HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
	"HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
    "HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased (Extreme)" => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
	=> haclose
	
rsi = ta.rsi(src, rsiper)
rhigh = ta.highest(rsi, rsihiloper)  
rlow  = ta.lowest(rsi, rsihiloper)  

trend = _ptrend(rhigh, rlow, rsi,  avgper, sense)

barcolor(trend == -1 ? redcolor : trend == 1 ?  greencolor : color.gray)

goLong = trend == 1  and (trend[1] == -1 or trend[1] == 0)
goShort = trend == -1  and (trend[1] == 1 or trend[1] == 0)

plotshape(goLong and showsignals, title = "Long", color = color.yellow, textcolor = color.yellow, text = "L", style = shape.triangleup, location = location.belowbar, size = size.tiny)
plotshape(goShort and showsignals, title = "Short", color = color.fuchsia, textcolor = color.fuchsia, text = "S", style = shape.triangledown, location = location.abovebar, size = size.tiny)

alertcondition(goLong, title="Long", message="RSI Precision Trend Candles [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title="Short", message="RSI Precision Trend Candles [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")



