Conversion request – Adaptive Zero Lag EMA v2
Forums › ProRealTime English forum › ProBuilder support › Conversion request – Adaptive Zero Lag EMA v2
- This topic has 0 replies, 1 voice, and was last updated 5 years ago by AlgoAlex.
Viewing 1 post (of 1 total)
-
-
05/11/2019 at 10:55 PM #98250
Strategy seen here:
https://www.tradingview.com/script/Q7h83i1t-Adaptive-Zero-Lag-EMA-v2/
I am interested in the indicators used, but this maths is far behind my possibilities…
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129//@version=3strategy(title="Adaptive Zero Lag EMA v2", shorttitle="AZLEMA", overlay = true, initial_capital=1000, currency="USD", commission_type=strategy.commission.cash_per_contract, commission_value=0.00006833, slippage = 5, pyramiding=1, calc_on_every_tick=true)src = input(title="Source", type=source, defval=close)secType = input(title="Security Type", options=["Forex", "Metal Spot", "Cryptocurrency","Custom"], defval="Forex")contracts = input(title="Custom # of Contracts", defval=1, step=1)limit = input(title="Max Lots", type=integer, defval=100)Period = input(title="Period", type=integer, defval = 20)adaptive = input(title="Adaptive Method", options=["Off", "Cos IFM", "I-Q IFM", "Average"], defval="Cos IFM")GainLimit = input(title="Gain Limit", type=integer, defval = 8)Threshold = input(title="Threshold", type = float, defval=0.05, step=0.01)fixedSL = input(title="SL Points", defval=70)fixedTP = input(title="TP Points", defval=10)risk = input(title='Risk', defval=0.01, step=0.01)range = 50 //input(title="Max Period", type=integer, defval=60, minval=8, maxval=100)PI = 3.14159265359lenIQ = 0.0lenC = 0.0//##############################################################################//I-Q IFM//##############################################################################if(adaptive=="I-Q IFM" or adaptive=="Average")imult = 0.635qmult = 0.338inphase = 0.0quadrature = 0.0re = 0.0im = 0.0deltaIQ = 0.0instIQ = 0.0V = 0.0P = src - src[7]inphase := 1.25*(P[4] - imult*P[2]) + imult*nz(inphase[3])quadrature := P[2] - qmult*P + qmult*nz(quadrature[2])re := 0.2*(inphase*inphase[1] + quadrature*quadrature[1]) + 0.8*nz(re[1])im := 0.2*(inphase*quadrature[1] - inphase[1]*quadrature) + 0.8*nz(im[1])if (re!= 0.0)deltaIQ := atan(im/re)for i=0 to rangeV := V + deltaIQ[i]if (V > 2*PI and instIQ == 0.0)instIQ := iif (instIQ == 0.0)instIQ := nz(instIQ[1])lenIQ := 0.25*instIQ + 0.75*nz(lenIQ[1])//##############################################################################//COSINE IFM//##############################################################################if(adaptive == "Cos IFM" or adaptive == "Average")s2 = 0.0s3 = 0.0deltaC = 0.0instC = 0.0v1 = 0.0v2 = 0.0v4 = 0.0v1 := src - src[7]s2 := 0.2*(v1[1] + v1)*(v1[1] + v1) + 0.8*nz(s2[1])s3 := 0.2*(v1[1] - v1)*(v1[1] - v1) + 0.8*nz(s3[1])if (s2 != 0)v2 := sqrt(s3/s2)if (s3 != 0)deltaC := 2*atan(v2)for i = 0 to rangev4 := v4 + deltaC[i]if (v4 > 2*PI and instC == 0.0)instC := i - 1if (instC == 0.0)instC := instC[1]lenC := 0.25*instC + 0.75*nz(lenC[1])if (adaptive == "Cos IFM")Period := round(lenC)if (adaptive == "I-Q IFM")Period := round(lenIQ)if (adaptive == "Average")Period := round((lenC + lenIQ)/2)//##############################################################################//ZERO LAG EXPONENTIAL MOVING AVERAGE//##############################################################################LeastError = 1000000.0EC = 0.0Gain = 0.0EMA = 0.0Error = 0.0BestGain = 0.0alpha =2/(Period + 1)EMA := alpha*src + (1-alpha)*nz(EMA[1])for i = -GainLimit to GainLimitGain := i/10EC := alpha*(EMA + Gain*(src - nz(EC[1]))) + (1 - alpha)*nz(EC[1])Error := src - ECif(abs(Error)<LeastError)LeastError := abs(Error)BestGain := GainEC := alpha*(EMA + BestGain*(src - nz(EC[1]))) + (1-alpha)*nz(EC[1])plot(EC, title="EC", color=orange, linewidth=2)plot(EMA, title="EMA", color=red, linewidth=2)//##############################################################################//Trade Logic & Risk Management//##############################################################################buy = crossover(EC,EMA) and 100*LeastError/src > Thresholdsell = crossunder(EC,EMA) and 100*LeastError/src > ThresholdsecScaler = secType == "Forex" ? 100000 : secType == "Metal Spot" ? 100 : secType == "Cryptocurrency" ? 10000 : secType == "Custom" ? contracts : 0balance = strategy.initial_capital + strategy.netprofitif (time>timestamp(2016, 1, 1 , 0, 0) and balance > 0)//LONGlots = ((risk * balance)/fixedSL)*secScalerlots := lots > limit * secScaler ? limit * secScaler : lotsstrategy.entry("BUY", strategy.long, qty=lots, oca_name="BUY", oca_type=strategy.oca.cancel, when=buy)strategy.exit("B.Exit", "BUY", qty_percent = 100, loss=fixedSL, trail_offset=15, trail_points=fixedTP)//SHORTstrategy.entry("SELL", strategy.short, qty=lots, oca_name="SELL", oca_type=strategy.oca.cancel, when=sell)strategy.exit("S.Exit", "SELL", qty_percent = 100, loss=fixedSL, trail_offset=15, trail_points=fixedTP) -
AuthorPosts
Viewing 1 post (of 1 total)