# math_floor_zero_lag_00
#https://usethinkscript.com/threads/ok-im-stumped-zero-lag-ema-on-tick-charts.14929/
#OK. I'm stumped. Zero Lag EMA on Tick Charts
#mashume 3/22
#1
#OK. I've been banging my head against this for too long. This code works just fine on time based charts, but not at all on tick based charts. Sometimes I can look at code and tell why... but not this time. Anyone have an idea what in this code keeps it from running on tick based charts?
def bn = barnumber();
def na = double.nan;
#It's from Mobius Zero Lag MACD for reference.
input length1 = 10;
input price1 = close;
def K1 = Floor(2 / (length1 + 1));
def lag1 = Floor((length1 - 1) / 2);
def ZLEMA1 = K1 * (2 * price1 - price1[lag1]) + (1 - K1) * ZLEMA1[1];
plot data1 = ZLEMA1;
#I even tried this in place of the original definition given above to no avail.
#Code:
#def ZLEMA = K * (2 * price - getValue(price, lag)) + (1 - K) * ZLEMA[1];
#I'm sure that, as soon as I post this, it will all be clear. But for now, I'm stumped.
#-mashume
#Mashume
#Scientific Programmer, Trader, Dad, RPN fanatic in the PacNW
#SleepyZ
#2
#Using floor in def K results in zero. Therefore, I could not get it to plot as is on time also.
#MerryDay
#3
#@mashume
#@SleepyZ is correct:
#0t6zTkr.png
#Ruby:
# 2 - orig with floor()
input length2 = 10;
input price2 = close;
def K2 = Floor(2 / (length2 + 1));
def lag2 = Floor((length2 - 1) / 2);
def ZLEMA2 = K2 * (2 * price2 - price2[lag2]) + (1 - K2) * ZLEMA2[1];
plot data2 = ZLEMA2;
#AddChartBubble(close > open, high, "k= " + K2 + "\n" + ZLEMA2, Color.VIOLET);
#mashume
#4
#OK. For those who may run into this some day in the future, it works if you explicitly make everything a float rather than an int. No idea if this is new, or always been there.
#This code works:
# 3 - floats, w/o floor()
input length3 = 40.0;
input price3 = close;
def K3 = (2.0 / (length3 + 1.0));
def lag3 = ((length3 - 1.0) / 2.0);
def ZLEMA3 = K3 * (2.0 * price3 - GetValue(price3, lag3)) + (1.0 - K3) * ZLEMA3[1];
plot data3 = ZLEMA3;
AddLabel(yes, ZLEMA3);
# 4 - floats, with floor()
input length4 = 40.0;
input price4 = close;
def K4 = floor(2.0 / (length4 + 1.0));
def lag4 = ((length4 - 1.0) / 2.0);
def ZLEMA4 = K4 * (2.0 * price4 - GetValue(price4, lag4)) + (1.0 - K4) * ZLEMA4[1];
plot data4 = ZLEMA4;
addLabel(yes, zlema4);
# 2 - orig with floor()
# 3 - floats, w/o floor()
# 4 - floats, with floor()
# show bubble every s bars
def s = 10;
def x = bn % s == 0;
addchartbubble(x, low,
"K2 " + k2 + " - orig with floor() | data2 " + data2 + "\n" +
"K3 " + k3 + " - floats, w/o floor() | data3 " + data3 + "\n" +
"K4 " + k4 + " - floats, with floor() | data4 " + data4 + "\n"
, color.violet, no);
#