NexusFi: Find Your Edge


Home Menu

 





Request how to solve the think script below for ehler MESA moving average


Discussion in ThinkOrSwim

Updated
    1. trending_up 1,351 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 0 posts
    2. attach_file 0 attachments




 
Search this Thread

Request how to solve the think script below for ehler MESA moving average

  #1 (permalink)
Danielwalker
Singapore
 
Posts: 1 since May 2015
Thanks Given: 0
Thanks Received: 0

Dear All,

I found source code from trading view for the ehler adaptive moving average and try to convert it directly into think script and found a problem. The think script compile the code top down sequence but the code define the variables p at the bottom while the variable p is being used at the top part of the source code giving me the variable error. Does anyone able help me how to correct the code in such a way that it will work in think script?


tradingView source code:
-----------------------------------------------------------------------------------------------------------------------
src=input(hl2, title="Source")
fl=input(.5, title="Fast Limit")
sl=input(.05, title="Slow Limit")
sp = (4*src + 3*src[1] + 2*src[2] + src[3]) / 10.0
dt = (.0962*sp + .5769*nz(sp[2]) - .5769*nz(sp[4])- .0962*nz(sp[6]))*(.075*nz(p[1]) + .54)
q1 = (.0962*dt + .5769*nz(dt[2]) - .5769*nz(dt[4])- .0962*nz(dt[6]))*(.075*nz(p[1]) + .54)
i1 = nz(dt[3])
jI = (.0962*i1 + .5769*nz(i1[2]) - .5769*nz(i1[4])- .0962*nz(i1[6]))*(.075*nz(p[1]) + .54)
jq = (.0962*q1 + .5769*nz(q1[2]) - .5769*nz(q1[4])- .0962*nz(q1[6]))*(.075*nz(p[1]) + .54)
i2_ = i1 - jq
q2_ = q1 + jI
i2 = .2*i2_ + .8*nz(i2[1])
q2 = .2*q2_ + .8*nz(q2[1])
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])
p1 = iff(im!=0 and re!=0, 360/atan(im/re), nz(p[1]))
p2 = iff(p1 > 1.5*nz(p1[1]), 1.5*nz(p1[1]), iff(p1 < 0.67*nz(p1[1]), 0.67*nz(p1[1]), p1))
p3 = iff(p2<6, 6, iff (p2 > 50, 50, p2))
p = .2*p3 + .8*nz(p3[1])
spp = .33*p + .67*nz(spp[1])
phase = atan(q1 / i1)
dphase_ = nz(phase[1]) - phase
dphase = iff(dphase_< 1, 1, dphase_)
alpha_ = fl / dphase
alpha = iff(alpha_ < sl, sl, iff(alpha_ > fl, fl, alpha_))
mama = alpha*src + (1 - alpha)*nz(mama[1])
fama = .5*alpha*mama + (1 - .5*alpha)*nz(fama[1])
pa=input(false, title="Mark crossover points")
plotarrow(pa?(cross(mama, fama)?mama<fama?-1:1:na):na, title="Crossover Markers")
fr=input(false, title="Fill MAMA/FAMA Region")
duml=plot(fr?(mama>fama?mama:fama):na, style=circles, color=gray, linewidth=0, title="DummyL")
mamal=plot(mama, title="MAMA", color=red, linewidth=2)
famal=plot(fama, title="FAMA", color=green, linewidth=2)
fill(duml, mamal, red, transp=70, title="NegativeFill")
fill(duml, famal, green, transp=70, title="PositiveFill")
ebc=input(false, title="Enable Bar colors")
bc=mama>fama?lime:red
barcolor(ebc?bc:na)
------------------------------------------------------------------------------------------------------------------------------

my think script source code:

input h = high;
input
l = low;

input fl = 0.5;
input sl = 0.05;


def hl2 = (h + l) / 2;

def sp = (4 * hl2 + 3 * hl2[1] + 2 * hl2[2] + hl2[3] / 10.0);

def dt = (0.0962 * sp + 0.5769 * (if IsNaN(sp[2]) then 0 else sp[2]) - 0.5769 * (if IsNaN(sp[4]) then 0 else sp[4]) - 0.0962 * (if IsNaN(sp[6]) then 0 else sp[6])) * (0.075 * (if IsNaN(p[1]) then 0 else p[1]) + 0.54);


def q1 = (0.0962 * dt + 0.5769 * (if IsNaN(dt[2]) then 0 else dt[2]) - 0.5769 * (if IsNaN(dt[4]) then 0 else dt[4]) - 0.0962 * (if IsNaN(dt[6]) then 0 else dt[6])) * (0.075 * (if IsNaN(p[1]) then 0 else p[1]) + 0.54);


def i1 = (if IsNaN(dt[3]) then 0 else dt[3]);


def jl = (0.0962 * i1 + 0.5769 * (if IsNaN(i1[2]) then 0 else i1[2]) - 0.5769 * (if IsNaN(i1[4]) then 0 else i1[4]) - 0.0962 * (if IsNaN(i1[6]) then 0 else i1[6])) * (0.075 * (if IsNaN(p[1]) then 0 else p[1]) + 0.54);


def jq = (0.0962 * q1 + 0.5769 * (if IsNaN(q1[2]) then 0 else q1[2]) - 0.5769 * (if IsNaN(q1[4]) then 0 else q1[4]) - 0.0962 * (if IsNaN(q1[6]) then 0 else q1[6])) * (.075 * (if IsNaN(p[1]) then 0 else p[1]) + .54);


def i2_ = i1 - jq;
def q2_ = q1 + jl;
def i2 = .2 * i2_ + .8 * (if IsNaN(i2[1]) then 0 else i2[1]);
def q2 = .2 * q2_ + .8 * (if IsNaN(q2[1]) then 0 else q2[1]);


def re_ = i2 * (if IsNaN(i2[1]) then 0 else i2[1]) + q2 * (if IsNaN(q2[1]) then 0 else q2[1]);


def im_ = i2 * (if IsNaN(q2[1]) then 0 else q2[1]) - q2 * (if IsNaN(i2[1]) then 0 else i2[1]);


def re = .2 * re_ + .8 * (if IsNaN(re[1]) then 0 else re[1]);


def im = .2 * im_ + .8 * (if IsNaN(im[1]) then 0 else im[1]);


def p1 = if (im != 0 and re != 0) then 360 / Tan(im / re) else (if IsNaN(p[1]) then 0 else p[1]);


def p2 = if p1 > 1.5 * (if IsNaN(p1[1]) then 0 else p1[1]) then 1.5 * (if IsNaN(p1[1]) then 0 else p1[1]) else if p1 < 0.67 * (if IsNaN(p1[1]) then 0 else p1[1]) then 0.67 * (if IsNaN(p1[1]) then 0 else p1[1]) else p1;



def p3 = if (p2 < 6) then 6 else if (p2 > 50) then 50 else p2;


rec p = .2 * p3 + .8 * (if IsNaN(p3[1]) then 0 else p3[1]);


def spp = .33 * p + .67 * (if IsNaN(spp[1]) then 0 else spp[1]);


def phase = Tan(q1 / i1);


def dphase_ = (if IsNaN(phase[1]) then 0 else phase[1]) – phase;


def dphase = if (dphase_ < 1) then 1 else dphase_ ;

def alpha_ = fl / dphase;

def alpha = if (alpha_ < sl) then sl else if (alpha_ > fl) then fl else alpha_ ;


plot mama = alpha * hl2 + (1 - alpha) * (if IsNaN(mama[1]) then 0 else mama[1]);


plot fama = .5 * alpha * mama + (1 - .5 * alpha) * (if IsNaN(fama[1]) then 0 else fama[1]);

Reply With Quote





Last Updated on August 27, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts