#avgs_hilo_boxes
#https://usethinkscript.com/threads/looking-for-help-building-lindas-nyse-tick-indicator-on-tos.21503/
#Looking for help building Linda's NYSE $TICK indicator on tos
#SixVix 9/15
#1
#I saw this post on twitter from Linda Raschke and the indicator she built shows the Moving Average of the NYSE $TICK highs on the top and the lows on the bottom of her $TICK chart.
#----------------------
def bn = barnumber();
def na = double.nan;
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
input top_avg_length = 5;
input top_type = AverageType.exponential;
def top_avg = MovingAverage(top_type, high, top_avg_length);
input bottom_avg_length = 5;
input bottom_type = AverageType.exponential;
def bottom_avg = MovingAverage(bottom_type, low, bottom_avg_length);
# find: hi,lo of stk. lo of top avg. hi of bot avg
# calc spacing of avgs away from stock
def stkhi;
def stklo;
def top_lo;
def bot_hi;
if bn == 1 then {
stkhi = highestall(high);
stklo = lowestall(low);
top_lo = lowestall(top_avg);
bot_hi = highestall(bottom_avg);
} else {
stkhi = stkhi[1];
stklo = stklo[1];
top_lo = top_lo[1];
bot_hi = bot_hi[1];
}
# find diff from stk highest to avg
def topgap = stkhi - top_lo;
def botgap = stklo - bot_hi;
# calc spacing, avg lines to stock bars
input factor1 = 0.0;
#hint factor1: Enter a negative number to move average lines closer to main candles (ex. -0.8)
plot ztopavg = top_avg + (topgap * (1 + factor1));
plot zbotavg = bottom_avg + (botgap * (1 + factor1));
ztopavg.SetDefaultColor(Color.cyan);
ztopavg.setlineweight(1);
ztopavg.hidebubble();
zbotavg.SetDefaultColor(Color.yellow);
zbotavg.setlineweight(1);
zbotavg.hidebubble();
#------------------------
# find gaps, from top avg to bottom avg, biggest(green) and smallest(red) ,
# use peak/valley code
def avg_gap = ztopavg - zbotavg;
def highx = avg_gap;
def lowx = avg_gap;
input min_max_length = 7;
def offset = Min(min_max_length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], min_max_length - 1) and highx == GetValue(Highest(highx, min_max_length), -offset);
def valley = lowx < Lowest(lowx[1], min_max_length - 1) and lowx == GetValue(Lowest(lowx, min_max_length), -offset);
plot zhi = if 0 and peak then high*1.001 else na;
plot zlo = if 0 and valley then low*0.999 else na;
zlo.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo.SetDefaultColor(Color.red);
zlo.setlineweight(1);
zlo.hidebubble();
zhi.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi.SetDefaultColor(Color.green);
zhi.setlineweight(1);
zhi.hidebubble();
# draw clouds
# define 3 bars, 1 on each side of peak/valley, for cloud
def grn = (peak[1] or peak[0] or peak[-1]);
def red = (valley[1] or valley[0] or valley[-1]);
def grntop = if grn then ztopavg else na;
def redtop = if red then ztopavg else na;
addcloud(grntop, zbotavg, color.green);
addcloud(redtop, zbotavg, color.red);
# ------------------
# test stuff
# addverticalline(grn, "-", color.green);
#-------------------
# refs
# peak valley code
# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# post10 - Robert Payne
#