Ok moving on, there is a TradingView indicator called a variable moving average that I would like to implement as a stand alone indicator and as the basis for other indicators.
I have sifted through and added the required syntax punctuation etc. but there is one embedded Pine reference that I'm struggling with at this point; the nz built in function as follows from Pine script language reference manual:
nz
Replaces NaN values with zeros (or given value) in a series.
nz(x, y) → integer
nz(x, y) → float
nz(x, y) → color
nz(x, y) → bool
nz(x, y) → series[integer]
nz(x, y) → series[bool]
nz(x, y) → series[color]
nz(x, y) → series
nz(x) → color
nz(x) → bool
nz(x) → integer
nz(x) → float
nz(x) → series[integer]
nz(x) → series[bool]
nz(x) → series[color]
nz(x) → series
EXAMPLE
nz(sma(close, 100))
RETURNS
Two args version: returns x if it's a valid (not NaN) number, otherwise y
One arg version: returns x if it's a valid (not NaN) number, otherwise 0
ARGUMENTS
x (series) Series of values to process.
y (float) Value that will be inserted instead of all NaN values in x series.
Original TradingView script:
src=close
len2 = input(10, title = "VMA 2 Length")
std2 = input(true, title = "Show Trend Direction")
bc2 = input(false, title = "Color bars based on Trend")
showVMA2 = input(true, title="Show VMA 2?")
k2 = 1.0/len2
pdm2 = max((src - src[1]), 0)
mdm2 = max((src[1] - src), 0)
pdmS2 = ((1 - k2)*nz(pdmS2[1]) + k2*pdm2)
mdmS2 = ((1 - k2)*nz(mdmS2[1]) + k2*mdm2)
s2 = pdmS2 + mdmS2
pdi2 = pdmS2/s2
mdi2 = mdmS2/s2
pdiS2 = ((1 - k2)*nz(pdiS2[1]) + k2*pdi2)
mdiS2 = ((1 - k2)*nz(mdiS2[1]) + k2*mdi2)
d2 = abs(pdiS2 - mdiS2)
s12 = pdiS2 + mdiS2
iS2 = ((1 - k2)*nz(iS2[1]) + k2*d2/s12)
hhv2 = highest(iS2, len2)
llv2 = lowest(iS2, len2)
d12 = hhv2 - llv2
vI2 = (iS2 - llv2)/d12
vma2 = (1 - k2*vI2)*nz(vma2[1]) + k2*vI2*src
//vam line colour
vmaL2=(vma2 > vma2[1]) ? lime : (vma2<vma2[1]) ? red : (vma2==vma2[1]) ? white : red
plot(showVMA2 and vma2 ? vma2 : na, color=std2 ? vmaL2 : aqua, style=line, linewidth=2, transp=0, title="VMA-1")
My efforts to convert script to BCFL:
{--------------- Variable Moving Average ---------------}
src:=close;
len := 10;
k := 1.0/len;
pdm := max((src - src,-1), 0);
mdm := max((src,-1 - src), 0);
pdmS := ((1 - k)*nz(pdmS,-1) + k*pdm);
mdmS := ((1 - k)*nz(mdmS,-1) + k*mdm);
s := pdmS + mdmS;
pdi := pdmS/s;
mdi := mdmS/s;
pdiS := ((1 - k)*nz(pdiS,-1) + k*pdi);
mdiS := ((1 - k)*nz(mdiS,-1) + k*mdi);
d := abs(pdiS - mdiS);
s1 := pdiS + mdiS;
iS := ((1 - k)*nz(iS,-1) + k*d/s1);
hhv := highest(iS, len);
llv := lowest(iS, len);
d1 := hhv - llv;
vI := (iS - llv)/d1;
vma := (1 - k*vI)*nz(vma,-1) + k*vI*src;
vma;
Take your time no hurry.