#Formulate_new_candlesticks
input hide_price_plot = yes;
HidePricePlot(hide_price_plot);
def op = (0.5 * (open[1] + open[2]));
def cl = (0.5 * (close[1] + close[2]));
def hi = (0.5 * (high[1] + high[2]));
def lo = (0.5 * (low[1] + low[2]));
def HAclose = (op + hi + lo + cl) / 4;
def HAopen = (CompoundValue(1, (HAopen[1] + HAclose[1]) / 2, (op[1] + cl[1]) / 2));
def haopen_ = (HAopen + 0) ;
def HAhigh = (Max(Max(hi, haopen_), HAclose));
def HAlow = (Min(Min(lo, haopen_), HAclose));
input charttype = ChartType.CANDLE;
def o1 = if op < cl
then op
else Double.NaN;
def c1 = if op < cl
then cl
else Double.NaN;
def h1 = if op < cl
then hi
else Double.NaN;
def l1 = if op < cl
then lo
else Double.NaN;
AddChart(growColor = Color.GREEN, fallColor = Color.RED, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);
def o2 = if op > cl
then op
else Double.NaN;
def h2 = if op > cl
then hi
else Double.NaN;
def l2 = if op > cl
then lo
else Double.NaN;
def c2 = if op > cl
then cl
else Double.NaN;
AddChart(growColor = Color.RED, fallColor = Color.GREEN, neutralColor = Color.GRAY, high = h2, low = l2, open = o2, close = c2, type = charttype);
def o3 = if op == cl and HAhigh < HAhigh[1]
then HAhigh
else Double.NaN;
def h3 = if op == cl and HAhigh < HAhigh[1]
then HAhigh
else Double.NaN;
def l3 = if op == cl and HAhigh < HAhigh[1]
then HAlow
else Double.NaN;
def c3 = if op == cl and HAhigh < HAhigh[1]
then HAlow
else Double.NaN;
AddChart(growColor = Color.RED, fallColor = Color.GREEN, neutralColor = Color.GRAY, high = h3, low = l3, open = o3, close = c3, type = charttype);
def o4 = if op == cl and HAhigh >= HAhigh[1]
then HAhigh
else Double.NaN;
def h4 = if op == cl and HAhigh >= HAhigh[1]
then HAhigh
else Double.NaN;
def l4 = if op == cl and HAhigh >= HAhigh[1]
then HAlow
else Double.NaN;
def c4 = if op == cl and HAhigh >= HAhigh[1]
then HAlow
else Double.NaN;
AddChart(growColor = Color.GREEN, fallColor = Color.GREEN, neutralColor = Color.GRAY, high = h4, low = l4, open = o4, close = c4, type = charttype);