# ascending_01a
# find the average slope , of len lines, that go to the last bar
# identify a range of bars that are within len bars from the last bar
# diff = last bar close - current midbody
# slope = diff / (qty of bars current bar is away from last bar) ($ chg bar to bar)
# add up the slopes and divide by len, to get an avg slope
# https://usethinkscript.com/threads/help-for-internal-function-isascending-isdescending.11609/#post-100660
#Help for internal function IsAscending/isDescending
# IsAscending
# Tests if the trend is ascending by calculating the average slope coefficient of trendlines whose start points are MidBodyVal for a specified number of bars and end points are value for current bar. If the slope is positive then the trend is considered ascending.
# calc avg slope , for all the bars that are within len bars from last bar
def bn = barnumber();
def na = double.nan;
input len = 5;
input price = close;
def mid = midbodyval();
def lastbn = highestall(if !isnan(close) then bn else 0);
def lastcls = highestall(if lastbn == bn then close else 0);
def first = ( !isnan(close[-len]) and isnan(close[-(len+1)]));
def firstbn = if first then bn else firstbn[1];
def last_off = lastbn - bn;
# range of bars, len + last bar
def rng = (!isnan(close[0]) and isnan(close[-(len+1)]));
# range of 'len' bars before last bar
def rng_lines = ( !isnan(close[-1]) and isnan(close[-(len+1)]));
#--------------------------
# built in study
input show_builtin_function = yes;
def isas = isascending(price, len);
addlabel(show_builtin_function, " ", color.black);
#addlabel(show_builtin_function, "isascending , length: " + len, (if isas then color.green else color.red));
addlabel(show_builtin_function, "isascending function " + isas, (if isas then color.green else color.red));
#------------------
# find avg slope of 'len' lines
def slope_total;
if bn == 1 then {
slope_total = 0;
} else if bn == lastbn then {
slope_total = fold i = 1 to (len+1)
with p
do p + ((lastcls - getvalue(mid, i))/i);
} else {
slope_total = slope_total[1];
}
def slope_avg = slope_total/len;
def isslopeup = if slope_avg > 0 then 1 else 0;
input show_calc_function = yes;
addlabel(1, " ", color.black);
addlabel(1, "calc isascending " + isslopeup, ( if isslopeup then color.green else color.red));
input show_compare_label = yes;
addlabel(show_compare_label, " ", color.black);
# compare
addlabel(show_compare_label, (if (isas == isslopeup) then "same" else "different"), (if (isas == isslopeup) then color.cyan else color.yellow));
#----------------------------
# plot up to 5 lines
def b01 = if len >= 1 and (!isnan(close[-1]) and isnan(close[-(1+1)])) then 1 else 0;
def b02 = if len >= 2 and (!isnan(close[-2]) and isnan(close[-(2+1)])) then 1 else 0;
def b03 = if len >= 3 and (!isnan(close[-3]) and isnan(close[-(3+1)])) then 1 else 0;
def b04 = if len >= 4 and (!isnan(close[-4]) and isnan(close[-(4+1)])) then 1 else 0;
def b05 = if len >= 5 and (!isnan(close[-5]) and isnan(close[-(5+1)])) then 1 else 0;
def mid01 = if (bn == 1 or len < 1) then na else if b01 then mid else mid01[1];
def mid02 = if (bn == 1 or len < 2) then na else if b02 then mid else mid02[1];
def mid03 = if (bn == 1 or len < 3) then na else if b03 then mid else mid03[1];
def mid04 = if (bn == 1 or len < 4) then na else if b04 then mid else mid04[1];
def mid05 = if (bn == 1 or len < 5) then na else if b05 then mid else mid05[1];
def slope01 = (lastcls - mid01)/1;
def slope02 = (lastcls - mid02)/2;
def slope03 = (lastcls - mid03)/3;
def slope04 = (lastcls - mid04)/4;
def slope05 = (lastcls - mid05)/5;
def line01 = if b01 then mid01
else if rng then line01[1] + slope01
else na;
def line02 = if b02 then mid02
else if rng then line02[1] + slope02
else na;
def line03 = if b03 then mid03
else if rng then line03[1] + slope03
else na;
def line04 = if b04 then mid04
else if rng then line04[1] + slope04
else na;
def line05 = if b05 then mid05
else if rng then line05[1] + slope05
else na;
plot z1 = line01;
plot z2 = line02;
plot z3 = line03;
plot z4 = line04;
plot z5 = line05;
z1.setdefaultcolor(color.cyan);
z2.setdefaultcolor(color.cyan);
z3.setdefaultcolor(color.cyan);
z4.setdefaultcolor(color.cyan);
z5.setdefaultcolor(color.cyan);
# identify bars within the range
plot q = if rng_lines then high*1.01 else na;
q.SetPaintingStrategy(PaintingStrategy.SQUARES);
q.SetDefaultColor(Color.cyan);
q.setlineweight(3);
q.hidebubble();
# bar offset numbers, from last bar
plot n = if rng_lines then last_off else na;
n.SetPaintingStrategy(PaintingStrategy.VALUES_below);
n.SetDefaultColor(Color.cyan);
n.setlineweight(3);
n.hidebubble();
#