Brisbane Queensland Australia
Posts: 220 since Aug 2017
Thanks Given: 167
Thanks Received: 380
seveera
Hi All
I came across an indicator in Tradingview. This is based on
moving average (ma)
This indicator as lag value calculated based on the MA ; the code is below (please see line:2)
*************************
Line:1 - MA3 = WMA (Close, 100)
Line:2 - MA2lagFilter = exp(20*log(MA3 / MA3[1]))*MA3
Line:3 - MA1 = wma(close,3)
q1=plot(MA3, "MOV AVG PLOT", color= MA3>MA3[1]?lime:red, transp=1, linewidth=3 )
q2=plot(filter?MA2lagFilter:na, "LAG FILTER", color= black, transp=1, linewidth=2 )
q3=plot(MA1, "3 PERIOD WMA", color= black, transp=50, linewidth=1 )
*************************
When I try to write a similar code in AFL, it doesn't give the same output as Tradingview. (please note i'm not a coder)
Looking for help or guidance, not sure where I'm going wrong.
can someone please write this code for
ninjatrader -7
Have attached an image of the indicator from Tradingview
Here is the actual correct pinescript code , no-one has any hope of translating that code above with many errors in it
study("CHK TimeSeriesLagReductionFilter[MULTI MA]", overlay=true)
//-----
//Moving Average Types
almaIn = 'ALMA: Arnaud Legoux'
emaIn = 'EMA'
etmaIn = 'ETMA: Triangular (Exponential)'
ssmaIn = 'SSMA Super Smoothed'
hullIn = 'Hull MA'
mgIn = 'McGinley Dynamic'
rmaIn = 'SMMA -/RMA/Wilders: Smoothed'
smaIn = 'SMA '
tmaIn = 'TRIMA: Triangular'
wmaIn = 'WMA'
vwmaIn = 'EVWMA : ElasticVolWMA'
////-----
//General Input
maType = input(wmaIn, "Moving Average Type",
options=[almaIn, ssmaIn ,emaIn, etmaIn, hullIn,
mgIn, vwmaIn, rmaIn, smaIn,tmaIn,wmaIn])
periodd = input(100, "MA Length input", minval=1)
lagReduce = input(20.0, title="Lag Reduction input", minval=1)
filter=input(true, title="Plot Time Series Lag Reduction Filter?")
//-----
//MA & Misc. Input
offAl = 0.85//input(0.85, "ALMA: Offset", step=0.05)
sigmaAl = 6 //FOR ALMA
//alpha = 0.7 //// FOR T3 TILSON
src = close
//-----
//MA Function Declaration
Alma(_src, _len) =>
alma = alma(_src, _len, offAl, sigmaAl)
//// ETMA EXPONENTIAL TRIANGULAR MOV AVG
//// from https://www.tradingview.com/script/UQAv1U0c-MA-Study-Different-Types-and-More-NeoButane/
//// however chnaged ema to wma
eTma(_src, _len) =>
tma = ema(ema(_src, _len), _len)
Hull(_src, _len) =>
hullma = wma(2*wma(_src, _len/2)-wma(_src, _len), round(sqrt(_len)))
Mg(_src, _len) =>
mg = 0.0
mg := na(mg[1]) ? ema(_src, _len) : mg[1] + (_src - mg[1]) / (_len * pow(_src/mg[1], 4))
Tma(_src, _len) =>
tma = sma(sma(_src, _len), _len)
Wild(_src, _len) =>
wild = 0.0
alpha = 1 / _len
wild := alpha * _src + (1.0 - alpha) * nz(wild[1], _src)
// SUPER SMOOTHED MOV AVG////////
//// SSMA SuperSmoother filter © 2013 John F. Ehlers, https://www.tradingview.com/script/3e6VWRq7-Multi-MA-Ribbon/
ssma(_src, _len) =>
if (_len>0)
a1=exp(-1.414*3.14159 / _len)
b1=2*a1*cos(1.414*3.14159 /_len)
c2=b1
c3=(-a1)*a1
c1=1 - c2 - c3
v9=0.0
v9:=c1*(_src + nz(_src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
v9
else
na
// EVWMA ELASTIC VOL WEIGHTED MOV AVG////////
func_evwma(_src, _len) =>
volumeSum = sum(volume, _len)
evwma3 = 0.0
evwma3 := ((volumeSum - volume) * nz(evwma3[1]) + volume * _src) / volumeSum
evwma3
ma(_type, _src, _len) =>
_type == almaIn ? Alma(_src, _len) :
_type == emaIn ? ema(_src, _len) :
_type == ssmaIn ? ssma(_src, _len) :
_type == etmaIn ? eTma(_src, _len) :
_type == mgIn ? Mg(_src, _len) :
_type == rmaIn ? rma(_src, _len) :
_type == smaIn ? sma(_src, _len) :
_type == tmaIn ? Tma(_src, _len) :
_type == vwmaIn ? func_evwma(_src, _len) :
_type == hullIn ? Hull(_src, _len) :
_type == wmaIn ? wma(_src, _len) : na
MA3 = ma(maType, src, periodd)
MA2lagFilter = exp(lagReduce*log(MA3 / MA3[1]))*MA3
MA1 = wma(close,3)
q1=plot(MA3, "MOV AVG PLOT", color= MA3>MA3[1]?lime:red, transp=1, linewidth=3 )
q2=plot(filter?MA2lagFilter:na, "LAG FILTER", color= black, transp=1, linewidth=2 )
q3=plot(MA1, "3 PERIOD WMA", color= black, transp=50, linewidth=1 )
/// LIME #00FF00 // #fd1206 RED
fill(q3, q2, MA1 > MA2lagFilter ? #00FF00 : #fd1206, title="FILL BETWEEN CANDLE AND LAG", transp=10)
fill(q2, q1, MA3 > MA2lagFilter ?#fd1206 : #00FF00, title="FILL BETWEEN LAG AND MOV AVG", transp=70)