PA-Adaptive TRIX Log [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("PA-Adaptive TRIX Log [Loxx]", 
     overlay = false, 
     shorttitle='PAATL [Loxx]',
     timeframe="",
     timeframe_gaps=true)
     
import loxx/loxxpaaspecial/1

SM02 = 'Signal'
SM03 = 'Middle Crosses'


_iLWMA(src, per)=>
    lwma = src, workLwma = src
    sumw = per, sum = per * src
    for k = 1 to per - 1 
        weight = per - k
        sumw += weight
        sum += weight * nz(workLwma[k])   
    lwma := (sum/sumw)
    lwma

_iRMA(src, per) =>
    rma = src
    rma := na(rma[1]) ? src : (src - nz(rma[1])) * (1/per) + nz(rma[1])
    rma

_iEMA(src, per) =>
    ema = src
    ema := na(ema[1]) ? src : (src - nz(ema[1])) * (2 / (per + 1)) + nz(ema[1])
    ema
    
_iSMA(src, per)=>
    avg = src, k = 1, workSma = src
    while k < per
        avg += nz(workSma[k])
        k += 1
    out = avg/k
    out


greencolor = #2DD204
redcolor = #D2042D 

    
srcin = input.source(close, "Source", group = "Basic Settings")
sigper = input.int(9, "Signal Period", group = "Basic Settings")
type = input.string("SMA", "Signal Smoothing Type", options = ["EMA", "WMA", "RMA", "SMA"], group = "Basic Settings")

fregcycles = input.float(1., title = "PA Cycles", group=  "Phase Accumulation Cycle Settings")
fregfilter = input.float(0., title = "PA Filter", group=  "Phase Accumulation Cycle Settings")

sigtype = input.string(SM03, "Signal type", options = [SM02, SM03], group = "Signal Settings")

colorbars = input.bool(true, "Color bars?", group = "UI Options")
showsignals = input.bool(true, "Show signals?", group= "UI Options")

variant(type, src, len) =>
    sig = 0.0
    if type == "SMA"
        sig := _iSMA(src, len) 
    else if type == "EMA"
        sig := _iEMA(src, len) 
    else if type == "WMA"
        sig := _iLWMA(src, len)   
    else if type == "RMA"
        sig := _iRMA(src, len)  
    sig
    
int flen = math.floor(loxxpaaspecial.paa(srcin, fregcycles, fregfilter))
flen := flen < 1 ? 1 : flen

src = math.log(srcin)

workTrix1 = _iEMA(src, flen)
workTrix2 = _iEMA(workTrix1, flen)
workTrix3 = _iEMA(workTrix2, flen)

val = 10000 * (workTrix3 - nz(workTrix3[1])) / nz(workTrix3[1])

sig = variant(type, val, sigper)
mid = 0.

state = 0.
if sigtype == SM02
    if (val < sig) 
        state :=-1
    if (val > sig) 
        state := 1
else if sigtype == SM03
    if (val < mid) 
        state :=-1
    if (val > mid)
        state := 1

colorout = state == 1 ? greencolor : state == -1 ? redcolor : color.gray
plot(val, "TRIX Log", color = colorout, linewidth = 3)
plot(sig, "Signal", color = color.white, linewidth = 1)

plot(mid, color = bar_index % 2 ? color.gray : na)
barcolor(colorbars ? colorout : na)

goLong = sigtype == SM02 ? ta.crossover(val, sig) : ta.crossover(val, mid)
goShort = sigtype == SM02 ? ta.crossunder(val, sig) : ta.crossunder(val, mid)

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

alertcondition(goLong, title="Long", message="PA-Adaptive TRIX Log [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title="Short", message="PA-Adaptive TRIX Log [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

