script ConcavityDivergence {
#
# Hull Moving Average Concavity Divergence
# or
# The Second Derivative of the Hull Moving Average
#
# Author: Seth Urion (Mahsume)
# Version: 2020-02-23 V3
# Watchlist Column 2020-03-06 V2
#
# This code is licensed (as applicable) under the GPL v3
#
# ----------------------
declare lower;
input price = HL2;
input HMA_length = 55;
input lookback = 2;
plot HMA = HullMovingAvg(length = HMA_length, price = price);
def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;
def next_bar = HMA[1] + delta_per_bar;
def concavity = if HMA > next_bar then 1 else -1;
plot zero = 0;
zero.setdefaultcolor(Color.LIGHT_GRAY);
zero.setpaintingstrategy(PaintingStrategy.DASHES);
plot divergence = HMA - next_bar;
divergence.setDefaultColor(Color.LIME);
divergence.SetLineweight(2);
plot cx_up = if divergence crosses above zero then 0 else double.nan;
cx_up.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_up.SetDefaultColor(Color.LIGHT_GREEN);
cx_up.SetLineWeight(4);
plot cx_down = if divergence crosses below zero then 0 else double.nan;
cx_down.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_down.SetDefaultColor(Color.RED);
cx_down.SetLineWeight(4);
plot good_momentum = 0.32 * highest(divergence, HMA_Length * 2);
good_momentum.SetPaintingStrategy(PaintingStrategy.LINE);
good_momentum.SetStyle(Curve.SHORT_DASH);
good_momentum.SetDefaultColor(Color.GREEN);
plot bad_momentum = 0.32 * lowest(divergence, HMA_Length * 2);
bad_momentum.SetPaintingStrategy(PaintingStrategy.LINE);
bad_momentum.SetStyle(Curve.SHORT_DASH);
bad_momentum.SetDefaultColor(Color.RED);
}
plot data = ConcavityDivergence().divergence;
def HMA = ConcavityDivergence().HMA;
data.AssignValueColor(if data > data[1] then Color.BLACK else Color.WHITE);
AssignBackgroundColor(if data >= 0 then if HMA > HMA[1] then Color.GREEN else Color.DARK_GREEN else if HMA > HMA[1] then color.DARK_ORANGE else color.DARK_RED);