Pips-Stepped PDFMA [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("Pips-Stepped PDFMA [Loxx]",
     shorttitle='PSPDFMA [Loxx]', 
     overlay = true, 
     timeframe="", 
     timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D

_calcBaseUnit() =>
    bool isForexSymbol = syminfo.type == "forex"
    bool isYenPair = syminfo.currency == "JPY"
    float out = isForexSymbol ? isYenPair ? 0.01 : 0.0001 : syminfo.mintick
    out
    
_stepSizeCalc(type, per, sense, size)=>
    float stma = 0.
    float ATR0 = 0.
    float ATRmax = -1e10
    float ATRmin = 1e10
    float out = 0
    float alfa = 0
    if (size == 0) 
        float avgrng = 0
        float Weight = 0
        for i = per-1 to 0 
            if (type == "SMA")
                alfa := 1.0
            else
                alfa := 1.0 * (per - i) / per
            avgrng += alfa * (nz(high[i]) - nz(low[i]))
            Weight += alfa
        ATR0 := avgrng / Weight
        if (ATR0 > ATRmax) 
            ATRmax := ATR0
        if (ATR0 < ATRmin) 
            ATRmin := ATR0
        out := math.round(0.5 * sense * (ATRmax + ATRmin) / _calcBaseUnit())
    else
        out := sense * size
    out

_pdf(x, vari, mean)=>
    out = (1.0/math.sqrt(2 * math.pi * math.pow(vari,2)) * math.exp(-math.pow(x-mean, 2)/(2 * math.pow(vari, 2))))
    out

_pdfma(src, per, vari, mean)=>
    maxx = 3.5
    step = math.pi/(per-1)
    coeff = array.new_float(per, 0)
    for k = 0 to per - 1
        array.set(coeff, k, _pdf(k * step, vari, mean * math.pi))
    sumw = array.get(coeff, 0)
    sum  = array.get(coeff, 0) * src
    for k = 1 to per -1  
        weight = nz(array.get(coeff, k)) * src
        sumw += weight 
        sum += weight * nz(src)
    out = sum/sumw
    out

per = input.int(10, "Period", group = "Basic Settings") 
MA_Mode = input.string("SMA", "MA Type", options = ["SMA", "LWMA"], group = "Basic Settings")
Percentage = input.float(0, "Percentage", step = 0.1, group = "Basic Settings", tooltip = "Percentage of Up/Down Moving")
stepsize = input.int(20, "Step size", group = "Basic Settings") 
sense = input.float(5, "Sensivity", step = 0.1, minval = 0.1, group = "Basic Settings") 
vari = input.float(1, "Variance", step = 0.1, minval = 0.1, group = "Basic Settings") 
mean = input.float(0, "Mean", step = 0.1, minval = -1, maxval = 1, group = "Basic Settings")
HighLow = input.bool(false, "High/Low mode?", group = "Basic Settings") 

colorbars = input.bool(false, "Color bars?", group= "UI Options")
flat = input.bool(false, "Flat-level colroing?", group= "UI Options")
showSigs = input.bool(false, "Show signals?", group= "UI Options")

size = _stepSizeCalc(MA_Mode, per, sense, stepsize)
smax = 0., smin = 0.

if HighLow
    smax := _pdfma(low, per, vari, mean)  + 2.0 * size * _calcBaseUnit()
    smin := _pdfma(high, per, vari, mean)  - 2.0 * size * _calcBaseUnit()
else 
    smax := _pdfma(close, per, vari, mean)  + 2.0 * size * _calcBaseUnit()
    smin := _pdfma(close, per, vari, mean)  - 2.0 * size * _calcBaseUnit()
    
trend = 0., out = 0.

trend := trend[1]

if (close > smax[1]) 
    trend := 1
if (close < smin[1]) 
    trend := -1

if (trend > 0) 
    if (smin < smin[1]) 
        smin := smin[1]
    out := smin + size * _calcBaseUnit()
else 
    if (smax > smax[1])
        smax := smax[1]
    out := smax - size * _calcBaseUnit()

val = out + Percentage / 100.0 * stepsize * _calcBaseUnit()
colorout = val > val[1] ? greencolor : val < val[1] ? redcolor : color.gray

goLong_pre = ta.crossover(val, val[1])
goShort_pre = ta.crossunder(val, val[1])

contSwitch = 0
contSwitch := nz(contSwitch[1])
contSwitch := goLong_pre ? 1 : goShort_pre ? -1 : contSwitch

goLong = goLong_pre and ta.change(contSwitch)
goShort = goShort_pre and ta.change(contSwitch)

plot(val,"Pips-Stepped PDFMA", color = flat ? colorout : (contSwitch == 1  ? greencolor : redcolor), linewidth = 3)

barcolor(colorbars ? flat ? colorout : (contSwitch == 1  ? greencolor : redcolor) : na)

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

alertcondition(goLong, title="Long", message="Pips-Stepped PDFMA [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title="Short", message="Pips-Stepped PDFMA [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")




