本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// ____ __ ___ ________ ___________ ___________ __ ____ ___
// / __ )/ / / | / ____/ //_/ ____/ |/_ __< / // / / __ |__ \
// / __ / / / /| |/ / / ,< / / / /| | / / / / // /_/ / / __/ /
// / /_/ / /___/ ___ / /___/ /| / /___/ ___ |/ / / /__ __/ /_/ / __/
// /_____/_____/_/ |_\____/_/ |_\____/_/ |_/_/ /_/ /_/ \____/____/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © blackcat1402
//@version=4
study(title="[blackcat] L2 Ehlers Enhanced Signal to Noise Indicator", shorttitle="BCL2EESNR")
//Inputs
Price = input(hl2, title = "Source")
// Vars:
Smooth = 0.00
Detrender = 0.00
I1 = 0.00
Q1 = 0.00
jI = 0.00
jQ = 0.00
I2 = 0.00
Q2 = 0.00
Re = 0.00
Im = 0.00
Period = 0.00
SmoothPeriod = 0.00
I3 = 0.00
Q3 = 0.00
Signal = 0.00
Noise = 0.00
SNR = 0.00
pi = 2 * asin(1)
//Hilbert Transform
Smooth := bar_index >5 ? (4*Price + 3*nz(Price[1]) + 2*nz(Price[2]) + nz(Price[3]) ) / 10 : Smooth
Detrender := bar_index >5 ? (.0962*Smooth + .5769*nz(Smooth[2]) - .5769*nz(Smooth[4]) - .0962*nz(Smooth[6])) *( .075*nz(Period[1]) + .54) : Detrender
//Compute InPhase and Quadrature components
Q1 := bar_index >5 ? ( .0962*Detrender + .5769*nz(Detrender[2]) -.5769*nz(Detrender[4]) - .0962*nz(Detrender[6]) ) *( .075*nz(Period[1]) + .54) : Q1
I1 := bar_index >5 ? nz(Detrender[3]) : I1
//Advance the phase of I1 and Q1 by 90 degrees
jI := (.0962*I1 + .5769*nz(I1[2]) - .5769*nz(I1[4]) - .0962*nz(I1[6]) ) * ( .075*nz(Period[1]) + .54)
jQ := (.0962*Q1 + .5769*nz(Q1[2]) - .5769*nz(Q1[4]) - .0962*nz(Q1[6]) ) * ( .075*nz(Period[1]) + .54)
//Phasor addition for 3 bar averaging
I2 := I1 - jQ
Q2 := Q1 + jI
//Smooth the I and Q components before applying the discriminator
I2 := .2*I2 + .8*nz(I2[1])
Q2 := .2*Q2 + .8*nz(Q2[1])
//Homodyne Discriminator
Re := I2*nz(I2[1]) + Q2*nz(Q2[1])
Im := I2*nz(Q2[1]) - Q2*nz(I2[1])
Re := .2*Re + .8*nz(Re[1])
Im := .2*Im + .8*nz(Im[1])
Period := Im != 0 and Re != 0 ? 2*pi / atan(Im/Re) : Period
Period := Period > 1.5*nz(Period[1]) ? 1.5*nz(Period[1]) : Period
Period := Period < .67*nz(Period[1]) ? .67*nz(Period[1]) : Period
//Limit Period to be within the bounds of 6 bar and 50 bar cycles
Period := Period < 6 ? 6 : Period
Period := Period > 50 ? 50 : Period
Period := .2*Period + .8*nz(Period[1])
SmoothPeriod := .33*Period + .67*nz(SmoothPeriod[1])
Q3 := .5* (Smooth - nz(Smooth[2])) * (.1759*SmoothPeriod + .4607)
I3 := 0
var len = ceil(SmoothPeriod/2)
for count = 0 to len - 1
I3 := I3 + nz(Q3[count])
I3 := 1.57*I3 / len
Signal := I3*I3 + Q3*Q3
Noise := .1* (high - low) * (high - low) * .25 + .9*nz(Noise[1])
SNR := Noise != 0 and Signal != 0 ? .33* (10*log (Signal/Noise) /log (10) ) + .67*nz(SNR[1]) : SNR
plot1 = plot(SNR, "SNR", color=color.yellow, linewidth=3)
plot2 = plot(6, "Ref", color=color.fuchsia, linewidth=3)
fill(plot1, plot2, color=SNR>6?color.yellow:color.fuchsia,transp=50)