Wave
2024-11-27

//@version=3
study(title="WaveTrend Oscillator  ", shorttitle="WT_Krypt", precision=3)

PI=3.14159265359

dropn(src, n) =>
    na(src ) ? na : src

EhlersSuperSmoother(src, lower) =>
	a1 = exp(-PI * sqrt(2) / lower)
	coeff2 = 2 * a1 * cos(sqrt(2) * PI / lower)
	coeff3 = -pow(a1, 2)
	coeff1 = (1 - coeff2 - coeff3) / 2
	filt = 0.0
	filt := nz(coeff1 * (src + nz(src , src)) + coeff2 * filt  + coeff3 * nz(filt , filt ), src)

// Replaces built-in EMA function for better sensitivity in the beginning
xema(src, len) =>
    mult = 2.0 / (len + 1.0)
    res = na
    res := mult * src + (1.0 - mult) * nz(res , src)

EhlersEmaSmoother(sig, smoothK, smoothP) =>
    EhlersSuperSmoother(xema(sig, smoothK), smoothP)

// step function (assigns values if series evaluates to True 
// and keeps them until a new True result is seen)
step(xs, vals) =>
    res = na
    res := nz(xs ? vals : res , vals)

// try to detect monotonous increase in step function
casc(xs, start, while) =>
    res = na
    res := abs(nz((start and xs > xs  and (not res ) and while) ? xs - xs  : (xs >= xs  and while ? res  + xs - xs  : 0.0)))

// try to detect monotonous decrease in step function
cdesc(xs, start, while) =>
    res = na
    res := abs(nz((start and xs < xs  and (not res ) and while) ? xs  - xs : (xs <= xs  and while ? res  + xs  - xs : 0.0)))

findhidden(price, allbottoms, alltops, bottomsteps, topsteps, backtrack) =>
    pricebottoms = step(allbottoms, lowest(price, backtrack))
    pricetops = step(alltops, highest(price, backtrack))
    hbull = sqrt(cdesc(bottomsteps, true, true) * casc(pricebottoms, true, true))
    hbear = sqrt(casc(topsteps, true, true) * cdesc(pricetops, true, true))
     

n1 = input(10, title="Channel Length", minval=1)
n2 = input(21, title="Average Length", minval=1)
smoothing = input(4, title="Signal Smoothing", minval=0)
maperiod = input(4, title="MA Period", minval=1)
ob = input(0.75, title="Overbought Level", minval=0.0)
os = input(-0.75, title="Oversold Level", maxval=-0.0)
showhist = input(true, title="Histogram")
showsignals = input(true, title="Buy/Sell Signals")
src = input(close, type=source, title="Source")

price = log(dropn(src, 1))

delta = price - xema(price, n1)
d = xema(abs(delta), n1)
ci = ((d == 0) ? 0 : (delta / d))
sig = EhlersEmaSmoother(ci, n2, smoothing)
ma = sma(sig, maperiod)

// create plots
plot(sig, color=#0094ff, style=line, title="Signal", transp=0)
plot(ma, color=#ff6a00, style=line, title="SMA", transp=0)

lineBuyBottom = plot(os, color=orange, title="Oversold Level")
lineSellTop = plot(ob, color=#00bb00, title="Overbought Level")

lineBuy = plot(series=(sig < os ? sig : os), transp=100, title="n/a", editable=false)
lineSell = plot(series=(sig > ob ? sig : ob), transp=100, title="n/a", editable=false)

fill(plot1=lineBuy, plot2=lineBuyBottom, color=orange, transp=50, title="Oversold area", transp=0)
fill(plot1=lineSellTop, plot2=lineSell, color=#00bb00, transp=50, title="Overbought area")

plot(showhist ? sig-ma : na, color=sig > ma ? #5fc8ff : #ff9900, style=area, transp=80, title="Histogram")

// buy/sell signals

midpoint = (ob + os) / 2
exoff = (ob - midpoint) / 7
ploff = (ob - midpoint) / 8
hiddenupper = midpoint + 0.61803398875
hiddenlower = midpoint - 0.61803398875

// filter out false positives by adding sum of signal and MA at last 5 pips
reqpips = 5
sumsig = sum(sig, reqpips)
summa = sum(ma, reqpips)
allbottoms = showsignals and ma < ob and crossover(sig, ma) and sumsig  < summa 
alltops = showsignals and ma > os and crossunder(sig, ma) and sumsig  > summa 

// bullish and bearish conditions
bottomsteps = step(allbottoms, ma)
topsteps = step(alltops, ma)

  = findhidden(price, allbottoms, alltops, bottomsteps, topsteps, reqpips)

possiblebull = casc(bottomsteps, bottomsteps  < os or (hiddenbull  > hiddenbear ), sig < ob)
possiblebear = cdesc(topsteps, topsteps  > ob or (hiddenbear  > hiddenbull ), sig > os)
bullishtrend = possiblebull / (1.0 + possiblebear)
bearishtrend = possiblebear / (1.0 + possiblebull)

buysignals = allbottoms and ((ma < midpoint and (sig  < os - exoff or bullishtrend > hiddenbear)) or (ma < hiddenupper and hiddenbull > bearishtrend))
sellsignals = alltops and ((ma > midpoint and (sig  > ob + exoff or bearishtrend > hiddenbull)) or (ma > hiddenlower and hiddenbear > bullishtrend))

plot(buysignals ? sig  - ploff : na, style=circles, color=#008fff, linewidth=3, title="Buy Signal", transp=0)
plot(sellsignals ? sig  + ploff : na, style=circles, color=#ff0000, linewidth=3, title="Sell Signal", transp=0)
