Phase-Accumulation Adaptive RSX w/ Expanded Source Types [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(title="Phase-Accumulation Adaptive RSX w/ Expanded Source Types [Loxx]", shorttitle="PAARSXEST [Loxx]", overlay=false, max_bars_back = 3000)
import loxx/loxxexpandedsourcetypes/3
import loxx/loxxrsx/1
greencolor = #2DD204
redcolor = #D2042D 

lightgreencolor = #96E881 
lightredcolor =  #DF4F6C

calcComp(src, period)=>
    out = (
         (0.0962 * src +
         0.5769 * src[2] -
         0.5769 * src[4] -
         0.0962 * src[6]) * (0.075 * src[1] + 0.54))
    out
    
_prout(src, mult, filt)=>
    Smooth = 0., Detrender = 0.
    I1 = 0., Q1 = 0., Period = 0., DeltaPhase = 0., InstPeriod = 0.
    PhaseSum = 0., Phase = 0., _period = 0.
    Smooth := bar_index > 5 ? (4 * src + 3 * nz(src[1]) + 2 * nz(src[2]) + nz(src[3])) / 10 : Smooth
    ts = calcComp(Smooth, Period)
    Detrender := bar_index > 5 ? ts : Detrender

    qs = calcComp(Detrender, Period)
    Q1 := bar_index > 5 ? qs : Q1
    I1 := bar_index > 5 ? nz(Detrender[3]) : I1
    
    I1 := .15 * I1 + .85 * nz(I1[1])
    Q1 := .15 * Q1 + .85 * nz(Q1[1])
    
    Phase := nz(Phase[1])
    Phase := math.abs(I1) > 0 ? 180.0 / math.pi * math.atan(math.abs(Q1 / I1)) : Phase
    
    Phase := I1 < 0 and Q1 > 0 ? 180 - Phase : Phase
    Phase := I1 < 0 and Q1 < 0 ? 180 + Phase : Phase
    Phase := I1 > 0 and Q1 < 0 ? 360 - Phase : Phase
    
    DeltaPhase := nz(Phase[1]) - Phase
    DeltaPhase := nz(Phase[1]) < 90 and Phase > 270 ? 360 + nz(Phase[1]) - Phase : DeltaPhase
    DeltaPhase := math.max(math.min(DeltaPhase, 60), 7)

    InstPeriod := nz(InstPeriod[1])
    PhaseSum := 0
    count = 0
    
    while (PhaseSum < mult * 360 and count < 4500)
        PhaseSum += nz(DeltaPhase[count])
        count := count + 1
    
    InstPeriod := count > 0 ? count : InstPeriod

    alpha = 2.0 / (1.0 + math.max(filt, 1))
    _period := nz(_period[1]) + alpha * (InstPeriod - nz(_period[1]))
    _period := math.floor(_period) < 1 ? 1 : math.floor(_period)
    math.floor(_period)
    
smthtype = input.string("Kaufman", "Heiken-Ashi Better Smoothing", options = ["AMA", "T3", "Kaufman"], group=  "Basic Settings")

srcoption = 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)"])

regcycles = input.float(1, title = "EMA PA Cycles", group=  "Basic Settings")
regfilter = input.float(0, title = "EMA PA Filter", group=  "Basic Settings")

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")

colorbars = input.bool(false, "Color bars?", group = "UI Options")

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)

float src = switch srcoption
	"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
	
int fout = math.floor(_prout(src, regcycles, regfilter))
val = loxxrsx.rsx(src, fout)

colorout = val > val[2] ? greencolor : val < val[2] ? redcolor : color.gray

plot(50, color = bar_index % 2 ? color.gray : na)

plot(val, color = colorout, linewidth = 2)
barcolor(colorbars ? colorout : na)

goLong = ta.crossover(val, val[1]) 
goShort = ta.crossunder(val, val[1])

alertcondition(goLong, title = "Long", message = "Phase Accumulation, EMA w/ Expanded Source Types [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title = "Short", message = "Phase Accumulation, EMA w/ Expanded Source Types [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

























