I manage to code it for lookback period of 10 but the AFL does not look efficient. If experts here can give some pointers to a more compact code and that would be much appreciated.
lookback = 10;
m = LinRegSlope( Close, lookback );
// Search for high bound
h0 = Ref(H,-9);
h1 = Ref(H,-8);
h2 = Ref(H,-7);
h3 = Ref(H,-6);
h4 = Ref(H,-5);
h5 = Ref(H,-4);
h6 = Ref(H,-3);
h7 = Ref(H,-2);
h8 = Ref(H,-1);
h9 = H;
ch0 = h0 - m * 0;
ch1 = h1 - m * 1;
ch2 = h2 - m * 2;
ch3 = h3 - m * 3;
ch4 = h4 - m * 4;
ch5 = h5 - m * 5;
ch6 = h6 - m * 6;
ch7 = h7 - m * 7;
ch8 = h8 - m * 8;
ch9 = h9 - m * 9;
upper_bound = IIf(ch0 > ch1, ch0, ch1);
upper_bound = IIf(upper_bound > ch2, upper_bound, ch2);
upper_bound = IIf(upper_bound > ch3, upper_bound, ch3);
upper_bound = IIf(upper_bound > ch4, upper_bound, ch4);
upper_bound = IIf(upper_bound > ch5, upper_bound, ch5);
upper_bound = IIf(upper_bound > ch6, upper_bound, ch6);
upper_bound = IIf(upper_bound > ch7, upper_bound, ch7);
upper_bound = IIf(upper_bound > ch8, upper_bound, ch8);
upper_bound = IIf(upper_bound > ch9, upper_bound, ch9);
// Search for low bound
l0 = Ref(L,-9);
l1 = Ref(L,-8);
l2 = Ref(L,-7);
l3 = Ref(L,-6);
l4 = Ref(L,-5);
l5 = Ref(L,-4);
l6 = Ref(L,-3);
l7 = Ref(L,-2);
l8 = Ref(L,-1);
l9 = L;
cl0 = l0 - m * 0;
cl1 = l1 - m * 1;
cl2 = l2 - m * 2;
cl3 = l3 - m * 3;
cl4 = l4 - m * 4;
cl5 = l5 - m * 5;
cl6 = l6 - m * 6;
cl7 = l7 - m * 7;
cl8 = l8 - m * 8;
cl9 = l9 - m * 9;
lower_bound = IIf(cl0 < cl1, cl0, cl1);
lower_bound = IIf(lower_bound < cl2, lower_bound, cl2);
lower_bound = IIf(lower_bound < cl3, lower_bound, cl3);
lower_bound = IIf(lower_bound < cl4, lower_bound, cl4);
lower_bound = IIf(lower_bound < cl5, lower_bound, cl5);
lower_bound = IIf(lower_bound < cl6, lower_bound, cl6);
lower_bound = IIf(lower_bound < cl7, lower_bound, cl7);
lower_bound = IIf(lower_bound < cl8, lower_bound, cl8);
lower_bound = IIf(lower_bound < cl9, lower_bound, cl9);
Filter = 1;
SetOption("NoDefaultColumns",True);
AddTextColumn(Name(),"Ticker");
AddColumn(DateTime(),"Date",formatDateTime);
AddColumn(m,"slope",1.2);
AddColumn(lower_bound,"Lower bound",1.2);
AddColumn(upper_bound,"Upper bound",1.2);
AddColumn((upper_bound-lower_bound)/Close*100,"Range",1.2);