# Beyond the Hull with Leavitt

#### solaris

##### Member
I came across this code on this site and was wondering if a scan could be written to identify when the indicator changes color from red to green or visa versa within 1 Bar. Any help would be appreciated. Thanks

# Leavitt, Jay A., PhD [2017]. “Beyond The Hull With Leavitt Projections,” Technical Analysis of StockS & commoditieS, Volume 35: February.

## orig n was 20

script LeavittProjection{

input y = close;

input n = 8;

rec x = x[1] + 1;

def a = (n * sum(x * y, n) - sum(x, n) * sum(y, n) ) / ( n *sum(Sqr(x), n) - Sqr(sum(x, n)));

def b = (sum(Sqr(x), n) * sum(y, n) - sum(x, n) * sum(x *y, n) ) / ( n * sum(Sqr(x), n) - Sqr(sum(x, n)));

plot LeavittProjection= a*x+ b;

}

script LeavittConvolution

{ input price = close;

input n = 13;

def intLength = Floor(Sqrt);

plot LeavittConvolution = LeavittProjection (LeavittProjection (price, n), intLength);

}

def price = Close;

input length = 8;
def intLength = Floor(Sqrt(length));

plot LeavittConvolution = LeavittProjection (LeavittProjection (price, length), intLength);

LeavittConvolution.AssignValueColor(if LeavittConvolution > LeavittConvolution [1] then Color.blue else if LeavittConvolution < LeavittConvolution [1] then Color.RED else color.white);

leavittConvolution.setlineweight(2);

leavittConvolution.hidebubble();

addlabel(1,"leavit " + round(leavittConvolution,2), if LeavittConvolution > LeavittConvolution [1] then Color.blue else if LeavittConvolution < LeavittConvolution [1] then Color.RED else color.white);

@solaris We don't code to look for colors, we code logic and then color the results... The code you posted uses Red, White, and Blue, not Green and Red... Regardless, you can always code to trigger scan results based on the logic that changes colors... So, in the end, the answer is, Yes, you can adapt the code for a scan...

@solaris We don't code to look for colors, we code logic and then color the results... The code you posted uses Red, White, and Blue, not Green and Red... Regardless, you can always code to trigger scan results based on the logic that changes colors... So, in the end, the answer is, Yes, you can adapt the code for a scan...
Thanks

@solaris I recently created what I call the LCMA (Leavitt Convolution Moving Average)...It is a close cousin to @mashume's most excellent
Hull Moving Average Turning Points and Concavity (2nd Derivatives)...In this case, the Leavitt Convolution code was plugged into the framework of the @mashume's Hull Moving Average:

Code:
``````# source: https://usethinkscript.com/threads/hull-moving-average-turning-points-and-concavity-2nd-derivatives.1803/#post-16566
# source: Leavitt, Jay A., PhD [2017]. “Beyond The Hull With Leavitt Projections,” Technical Analysis of Stocks & Commodities, Volume 35: February.

script LeavittProjection{
input y = close;
input n = 8; #20
rec x = x[1] + 1;
def a = (n * sum(x * y, n) - sum(x, n) * sum(y, n) ) / ( n *sum(Sqr(x), n) - Sqr(sum(x, n)));
def b = (sum(Sqr(x), n) * sum(y, n) - sum(x, n) * sum(x *y, n) ) / ( n * sum(Sqr(x), n) - Sqr(sum(x, n)));
plot LeavittProjection= a*x+ b;
}

script LeavittConvolution{
input price = close;
input n = 13;
def intLength = Floor(Sqrt(n));
plot LeavittConvolution = LeavittProjection (LeavittProjection (price, n), intLength);
}

def price = close;
input length = 55;
def intLength = Floor(Sqrt(length));
def lookback = 2;

plot LCMA = LeavittProjection (LeavittProjection (price, length), intLength);
LCMA.setStyle(Curve.FIRM); #(Curve.SHORT_DASH);
LCMA.setLineWeight(4);
LCMA.hideBubble();

def delta = LCMA[1] - LCMA[lookback + 1];

def delta_per_bar = delta / lookback;

def next_bar = LCMA[1] + delta_per_bar;

def concavity = if LCMA > next_bar then 1 else -1;

LCMA.AssignValueColor(color = if concavity[1] == -1 then
if LCMA > LCMA[1] then Color.DARK_ORANGE else Color.RED else
if LCMA < LCMA[1] then Color.DARK_GREEN else Color.GREEN);

plot LC_Max = if LCMA[-1] < LCMA and LCMA > LCMA[1] then LCMA else Double.NaN;
#LC_Max.SetDefaultColor(Color.MAGENTA);
LC_Max.SetDefaultColor(Color.WHITE);
LC_Max.SetPaintingStrategy(PaintingStrategy.SQUARES);
LC_Max.SetLineWeight(5);
#LC_Max.Hide();

plot LC_Min = if LCMA[-1] > LCMA and LCMA < LCMA[1] then LCMA else Double.NaN;
#LC_Min.SetDefaultColor(Color.MAGENTA);
LC_Min.SetDefaultColor(Color.WHITE);
LC_Min.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
LC_Min.SetLineWeight(5);
#LC_Min.Hide();

plot turning_point = if concavity[1] != concavity then LCMA else Double.NaN;
turning_point.SetDefaultColor(Color.WHITE);
turning_point.SetPaintingStrategy(paintingStrategy = PaintingStrategy.POINTS);
turning_point.SetLineWeight(5);
#turning_point.Hide();

plot sell = if turning_point and concavity == -1 then high else Double.NaN;
sell.SetDefaultColor(Color.PINK);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetLineWeight(3);
#sell.Hide();

plot buy = if turning_point and concavity == 1 then low else Double.NaN;

Try plugging the following into a Scan:

Code:
``````script LeavittProjection{

input y = close;
input n = 8; #20
rec x = x[1] + 1;
def a = (n * sum(x * y, n) - sum(x, n) * sum(y, n) ) / ( n *sum(Sqr(x), n) - Sqr(sum(x, n)));
def b = (sum(Sqr(x), n) * sum(y, n) - sum(x, n) * sum(x *y, n) ) / ( n * sum(Sqr(x), n) - Sqr(sum(x, n)));
plot LeavittProjection= a*x+ b;

}

script LeavittConvolution{

input price = close;
input n = 13;
def intLength = Floor(Sqrt(n));
plot LeavittConvolution = LeavittProjection (LeavittProjection (price, n), intLength);

}

def price = close;

input length = 55;

def intLength = Floor(Sqrt(length));
def lookback = 2;

def LCMA = LeavittProjection (LeavittProjection (price, length), intLength);

def delta = LCMA[1] - LCMA[lookback + 1];
def delta_per_bar = delta / lookback;
def next_bar = LCMA[1] + delta_per_bar;
def concavity = if LCMA > next_bar then 1 else -1;
def LC_Max = if LCMA[-1] < LCMA and LCMA > LCMA[1] then LCMA else Double.NaN;
def LC_Min = if LCMA[-1] > LCMA and LCMA < LCMA[1] then LCMA else Double.NaN;
def turning_point = if concavity[1] != concavity then LCMA else Double.NaN;
def sell = if turning_point and concavity == -1 then high else Double.NaN;
def buy = if turning_point and concavity == 1 then low else Double.NaN;

def bullish = concavity[1] == 1 and LCMA > LCMA[1];
def bearish = concavity[1] == -1 and LCMA < LCMA[1];

plot bull = if bullish then LCMA else Double.NaN;
#plot bear = if bearish then LCMA else Double.NaN;``````

Hope this helps...

@solaris I recently created what I call the LCMA (Leavitt Convolution Moving Average)...It is a close cousin to @mashume's most excellent
Hull Moving Average Turning Points and Concavity (2nd Derivatives)...In this case, the Leavitt Convolution code was plugged into the framework of the @mashume's Hull Moving Average:

Code:
``````# source: https://usethinkscript.com/threads/hull-moving-average-turning-points-and-concavity-2nd-derivatives.1803/#post-16566
# source: Leavitt, Jay A., PhD [2017]. “Beyond The Hull With Leavitt Projections,” Technical Analysis of Stocks & Commodities, Volume 35: February.

script LeavittProjection{
input y = close;
input n = 8; #20
rec x = x[1] + 1;
def a = (n * sum(x * y, n) - sum(x, n) * sum(y, n) ) / ( n *sum(Sqr(x), n) - Sqr(sum(x, n)));
def b = (sum(Sqr(x), n) * sum(y, n) - sum(x, n) * sum(x *y, n) ) / ( n * sum(Sqr(x), n) - Sqr(sum(x, n)));
plot LeavittProjection= a*x+ b;
}

script LeavittConvolution{
input price = close;
input n = 13;
def intLength = Floor(Sqrt(n));
plot LeavittConvolution = LeavittProjection (LeavittProjection (price, n), intLength);
}

def price = close;
input length = 55;
def intLength = Floor(Sqrt(length));
def lookback = 2;

plot LCMA = LeavittProjection (LeavittProjection (price, length), intLength);
LCMA.setStyle(Curve.FIRM); #(Curve.SHORT_DASH);
LCMA.setLineWeight(4);
LCMA.hideBubble();

def delta = LCMA[1] - LCMA[lookback + 1];

def delta_per_bar = delta / lookback;

def next_bar = LCMA[1] + delta_per_bar;

def concavity = if LCMA > next_bar then 1 else -1;

LCMA.AssignValueColor(color = if concavity[1] == -1 then
if LCMA > LCMA[1] then Color.DARK_ORANGE else Color.RED else
if LCMA < LCMA[1] then Color.DARK_GREEN else Color.GREEN);

plot LC_Max = if LCMA[-1] < LCMA and LCMA > LCMA[1] then LCMA else Double.NaN;
#LC_Max.SetDefaultColor(Color.MAGENTA);
LC_Max.SetDefaultColor(Color.WHITE);
LC_Max.SetPaintingStrategy(PaintingStrategy.SQUARES);
LC_Max.SetLineWeight(5);
#LC_Max.Hide();

plot LC_Min = if LCMA[-1] > LCMA and LCMA < LCMA[1] then LCMA else Double.NaN;
#LC_Min.SetDefaultColor(Color.MAGENTA);
LC_Min.SetDefaultColor(Color.WHITE);
LC_Min.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
LC_Min.SetLineWeight(5);
#LC_Min.Hide();

plot turning_point = if concavity[1] != concavity then LCMA else Double.NaN;
turning_point.SetDefaultColor(Color.WHITE);
turning_point.SetPaintingStrategy(paintingStrategy = PaintingStrategy.POINTS);
turning_point.SetLineWeight(5);
#turning_point.Hide();

plot sell = if turning_point and concavity == -1 then high else Double.NaN;
sell.SetDefaultColor(Color.PINK);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetLineWeight(3);
#sell.Hide();

plot buy = if turning_point and concavity == 1 then low else Double.NaN;

Try plugging the following into a Scan:

Code:
``````script LeavittProjection{

input y = close;
input n = 8; #20
rec x = x[1] + 1;
def a = (n * sum(x * y, n) - sum(x, n) * sum(y, n) ) / ( n *sum(Sqr(x), n) - Sqr(sum(x, n)));
def b = (sum(Sqr(x), n) * sum(y, n) - sum(x, n) * sum(x *y, n) ) / ( n * sum(Sqr(x), n) - Sqr(sum(x, n)));
plot LeavittProjection= a*x+ b;

}

script LeavittConvolution{

input price = close;
input n = 13;
def intLength = Floor(Sqrt(n));
plot LeavittConvolution = LeavittProjection (LeavittProjection (price, n), intLength);

}

def price = close;

input length = 55;

def intLength = Floor(Sqrt(length));
def lookback = 2;

def LCMA = LeavittProjection (LeavittProjection (price, length), intLength);

def delta = LCMA[1] - LCMA[lookback + 1];
def delta_per_bar = delta / lookback;
def next_bar = LCMA[1] + delta_per_bar;
def concavity = if LCMA > next_bar then 1 else -1;
def LC_Max = if LCMA[-1] < LCMA and LCMA > LCMA[1] then LCMA else Double.NaN;
def LC_Min = if LCMA[-1] > LCMA and LCMA < LCMA[1] then LCMA else Double.NaN;
def turning_point = if concavity[1] != concavity then LCMA else Double.NaN;
def sell = if turning_point and concavity == -1 then high else Double.NaN;
def buy = if turning_point and concavity == 1 then low else Double.NaN;

def bullish = concavity[1] == 1 and LCMA > LCMA[1];
def bearish = concavity[1] == -1 and LCMA < LCMA[1];

plot bull = if bullish then LCMA else Double.NaN;
#plot bear = if bearish then LCMA else Double.NaN;``````

Hope this helps...

Thanks you so much for your help. Especially the scan. I really appreciate it. Have a great trading day

87k+ Posts
325 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?