ignore extreme values when calculating an average for ThinkOrSwim

halcyonguy

Moderator - Expert
VIP
Lifetime
this is an upper study, that will find the mth highest and lowest price levels in a range of n numbers.

pick how many bars, n, to look for an average
pick a quantity of price levels from the extreme, m, to ignore
(if m = 3, then find the 3rd highest high price level, and ignore the values above it when calculating an average of highs)
(opposite for lows)

cyan lines are the mth price levels for highs and lows

dashed lines,
adjusted average of highs, that are less than the mth high price level
adjusted average of lows, that are higher than the mth low price level


i made this to be modified to be used for this request,
https://usethinkscript.com/threads/...-filter-a-set-of-numbers-for-averaging.14335/
to find an average, that exludes some high values.


Code:
# find_mth_highest_lowest_numbers_00f

#----------------
# halcyonguy
# 23-02-05
# find mth highest/lowest price levels in a group of n bars
#----------------


def na = double.nan;
def bn = barnumber();

#-----------------------------------------
# get chart agg
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#addlabel(yes, chartmin + " min", color.yellow);
def daybars = 390 / chartmin;
#-----------------------------------------

#declare lower;

# n = qty of past bars to read (includes current bar)
#input n = 30;
input quantity_of_bars = 15;

# m = find the mth highest and lowest prices from the past n bars. 
# 1 = highest/lowest , 2 = 2nd highest/lowest , 4 = 4th highest/lowest price levels
input price_level_ranking = 2;

def nbad = (quantity_of_bars < 1);

def mbad1 = (price_level_ranking < 1);
def mbad2 = (price_level_ranking > quantity_of_bars);

#addlabel(  quantity_of_bars  def m = if price_level_ranking


def n = if nbad then 1 else quantity_of_bars;
def m = if price_level_ranking < 1 then 1 else if price_level_ranking > n then n else price_level_ranking;


def lastbar = (!isnan(close) and isnan(close[-1]));
def nbars_rng = (!isnan(close) and isnan(close[-n]));

def nbars_first = (!isnan(close[-(n-1)]) and isnan(close[-n]));
addverticalline(nbars_first, "-", color.cyan);


# x bars after last bar , bubbles
def bars_after = 1;
def x = (!isnan(close[(bars_after)]) and isnan(close[bars_after-1]));


input show_labels = yes;
addlabel(show_labels, GetSymbol(), color.cyan);
addlabel(show_labels, chartmin + " min", color.cyan);

addlabel(show_labels , n + " bars", color.yellow);

addlabel(show_labels, " ", color.black);


# % chg , bar to bar
#def ret = absValue(close/close[1]-1);
def ret = 100*absValue(close/close[1]-1);
#plot z = ret;
#z.setdefaultcolor(color.cyan);


#def data = ret;
def data1 = high;
def data2 = low;
def small = 0;
def big = 99999;


#--------------------------------


# highest
# find mth highest high price level
# with each i loop , find next smaller , highest high
def hi2 = fold i = 0 to m
 with p = big
 do (fold k = 0 to n
     with q = small
# counts m different price levels, not m bars
#  if 2+ bars have same high, then that is 1 price level
    do if getvalue(data1, k) >= q and getvalue(data1, k) < p then getvalue(data1, k) else q);

#  if hi = 0 , then set it to the lowest high.  if there are many price levels at same price, hi2 may = 0
def hi = if hi2 == 0 then lowest(high, n) else hi2;

def hilvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(hi, -(n-1))
 else hilvl[1];

# draw a line at the m price level, over the last n bars on chart
plot z3 = if hilvl == 0 then na else hilvl;
z3.setdefaultcolor(color.cyan);

addlabel(show_labels , m + " th highest price", color.yellow);
addlabel(1, hi, color.cyan);


#--------------------------------


# lowest
# find mth lowest low price level
# with each i loop , find next higher , lowest low
def lo2 = fold i2 = 0 to m
 with p2 = small
 do (fold k2 = 0 to n
     with q2 = big
# counts m different price levels, not m bars
#  if 2+ bars have same low, then that is 1 price level
    do if getvalue(data2, k2) <= q2 and getvalue(data2, k2) > p2 then getvalue(data2, k2) else q2);

#  if hi = 0 , then set it to the lowest high.  if there are many price levels at same price, hi2 may = 0
def lo = if lo2 == 0 then highest(low, n) else lo2;

def lolvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(lo, -(n-1))
 else lolvl[1];

# draw a line at the m price level, over the last n bars on chart
plot z4 = if lolvl == 0 then na else lolvl;
z4.setdefaultcolor(color.cyan);

addlabel(show_labels , m + " th lowest price", color.yellow);
addlabel(1, lo, color.cyan);

#--------------------------------

#------------------------

def vert = 0.0005;
#------------------------

input oooooooooooo = 0;

# high test bubbles
input test1_hi = yes;
addchartbubble(test1_hi and !isnan(hilvl) and (data1 == hilvl), high*(1+vert),
data1 + "\n" 
,(if data1 == hilvl then color.yellow else color.gray), yes);

input test2_hi_other_bubbles = no;
addchartbubble(test2_hi_other_bubbles and !isnan(hilvl) and (data1 != hilvl), high*(1+vert),
data1 + "\n" 
,(if data1 == hilvl then color.yellow else color.gray), yes);

addchartbubble(test1_hi and x, hi[bars_after],
m + "th highest\n" +
hi[bars_after]
,color.cyan, yes);

input test3_hi = no;
addchartbubble(test3_hi, high*(1+vert),
hi + "\n" +
hilvl
, color.yellow, yes);

#------------------

input oooooooooo = 0;

# low test bubbles
input test4_lo = yes;
addchartbubble(test4_lo and !isnan(lolvl) and (data2 == lolvl), low*(1-vert),
data2 + "\n" 
,(if data2 == lolvl then color.yellow else color.gray), no);

input test5_lo_other_bubbles = no;
addchartbubble(test5_lo_other_bubbles and !isnan(lolvl) and (data2 != lolvl), low*(1-vert),
data2 + "\n" 
,(if data2 == lolvl then color.yellow else color.gray), no);

addchartbubble(test4_lo and x, lo[bars_after],
m + "th lowest\n" +
lo[bars_after]
,color.cyan, no);

input test6_lo = no;
addchartbubble(test6_lo, low*(1-vert),
lo + "\n" +
lolvl
, color.yellow, no);
#

input ooooooooooo = 0;

#--------------------------------

# averages

# avg - hi
def hitotal = fold h3 = 0 to n
  with p3
  do p3 + (if getvalue(data1, h3) <= hi then getvalue(data1, h3) else 0);

def hiqty = fold h4 = 0 to n
  with p4
  do p4 + (if getvalue(data1, h4) <= hi then 1 else 0);

def hiavg = round(hitotal / hiqty, 2);


# avg - lo
def lototal = fold l3 = 0 to n
  with q3
  do q3 + (if getvalue(data2, l3) >= lo then getvalue(data2, l3) else 0);

def loqty = fold l4 = 0 to n
  with q4
  do q4 + (if getvalue(data2, l4) >= lo then 1 else 0);

def loavg = round(lototal / loqty, 2);

#------------------------

addlabel(1, " ", color.black);

addlabel(1, hiavg + " adjusted high avg, data below " + hilvl, color.green);
addlabel(1, loavg + " adjusted low avg, data above " + lolvl, color.red);

input test7_labels = yes;
addlabel(test7_labels, " ", color.black);
addlabel(test7_labels, "hi q " + hiqty, color.yellow);
addlabel(test7_labels, "lo q " + loqty, color.yellow);

input test8_labels = no;
addlabel(test8_labels, "hi t " + hitotal, color.yellow);
addlabel(test8_labels, "lo t " + lototal, color.yellow);

#------------------------

# average lines

def hiavglvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(hiavg, -(n-1))
 else hiavglvl[1];

plot zhiavg = hiavglvl;
zhiavg.setdefaultcolor(color.green);
zhiavg.SetStyle(Curve.MEDIUM_DASH);


def loavglvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(loavg, -(n-1))
 else loavglvl[1];

plot zloavg = loavglvl;
zloavg.setdefaultcolor(color.red);
zloavg.SetStyle(Curve.MEDIUM_DASH);
#

cyan lines are the mth price limits for highs and lows
green dash line is adjusted average of highs, that are less than the mth high price level
red dash line is adjusted average of lows, that are higher than the mth low price level
z4RV8Pj.jpg
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

this is an upper study, that will find the mth highest and lowest price levels in a range of n numbers.

pick how many bars, n, to look for an average
pick a quantity of price levels from the extreme, m, to ignore
(if m = 3, then find the 3rd highest high price level, and ignore the values above it when calculating an average of highs)
(opposite for lows)

cyan lines are the mth price levels for highs and lows

dashed lines,
adjusted average of highs, that are less than the mth high price level
adjusted average of lows, that are higher than the mth low price level


i made this to be modified to be used for this request,
https://usethinkscript.com/threads/...-filter-a-set-of-numbers-for-averaging.14335/
to find an average, that exludes some high values.


Code:
# find_mth_highest_lowest_numbers_00f

#----------------
# halcyonguy
# 23-02-05
# find mth highest/lowest price levels in a group of n bars
#----------------


def na = double.nan;
def bn = barnumber();

#-----------------------------------------
# get chart agg
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#addlabel(yes, chartmin + " min", color.yellow);
def daybars = 390 / chartmin;
#-----------------------------------------

#declare lower;

# n = qty of past bars to read (includes current bar)
#input n = 30;
input quantity_of_bars = 15;

# m = find the mth highest and lowest prices from the past n bars.
# 1 = highest/lowest , 2 = 2nd highest/lowest , 4 = 4th highest/lowest price levels
input price_level_ranking = 2;

def nbad = (quantity_of_bars < 1);

def mbad1 = (price_level_ranking < 1);
def mbad2 = (price_level_ranking > quantity_of_bars);

#addlabel(  quantity_of_bars  def m = if price_level_ranking


def n = if nbad then 1 else quantity_of_bars;
def m = if price_level_ranking < 1 then 1 else if price_level_ranking > n then n else price_level_ranking;


def lastbar = (!isnan(close) and isnan(close[-1]));
def nbars_rng = (!isnan(close) and isnan(close[-n]));

def nbars_first = (!isnan(close[-(n-1)]) and isnan(close[-n]));
addverticalline(nbars_first, "-", color.cyan);


# x bars after last bar , bubbles
def bars_after = 1;
def x = (!isnan(close[(bars_after)]) and isnan(close[bars_after-1]));


input show_labels = yes;
addlabel(show_labels, GetSymbol(), color.cyan);
addlabel(show_labels, chartmin + " min", color.cyan);

addlabel(show_labels , n + " bars", color.yellow);

addlabel(show_labels, " ", color.black);


# % chg , bar to bar
#def ret = absValue(close/close[1]-1);
def ret = 100*absValue(close/close[1]-1);
#plot z = ret;
#z.setdefaultcolor(color.cyan);


#def data = ret;
def data1 = high;
def data2 = low;
def small = 0;
def big = 99999;


#--------------------------------


# highest
# find mth highest high price level
# with each i loop , find next smaller , highest high
def hi2 = fold i = 0 to m
 with p = big
 do (fold k = 0 to n
     with q = small
# counts m different price levels, not m bars
#  if 2+ bars have same high, then that is 1 price level
    do if getvalue(data1, k) >= q and getvalue(data1, k) < p then getvalue(data1, k) else q);

#  if hi = 0 , then set it to the lowest high.  if there are many price levels at same price, hi2 may = 0
def hi = if hi2 == 0 then lowest(high, n) else hi2;

def hilvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(hi, -(n-1))
 else hilvl[1];

# draw a line at the m price level, over the last n bars on chart
plot z3 = if hilvl == 0 then na else hilvl;
z3.setdefaultcolor(color.cyan);

addlabel(show_labels , m + " th highest price", color.yellow);
addlabel(1, hi, color.cyan);


#--------------------------------


# lowest
# find mth lowest low price level
# with each i loop , find next higher , lowest low
def lo2 = fold i2 = 0 to m
 with p2 = small
 do (fold k2 = 0 to n
     with q2 = big
# counts m different price levels, not m bars
#  if 2+ bars have same low, then that is 1 price level
    do if getvalue(data2, k2) <= q2 and getvalue(data2, k2) > p2 then getvalue(data2, k2) else q2);

#  if hi = 0 , then set it to the lowest high.  if there are many price levels at same price, hi2 may = 0
def lo = if lo2 == 0 then highest(low, n) else lo2;

def lolvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(lo, -(n-1))
 else lolvl[1];

# draw a line at the m price level, over the last n bars on chart
plot z4 = if lolvl == 0 then na else lolvl;
z4.setdefaultcolor(color.cyan);

addlabel(show_labels , m + " th lowest price", color.yellow);
addlabel(1, lo, color.cyan);

#--------------------------------

#------------------------

def vert = 0.0005;
#------------------------

input oooooooooooo = 0;

# high test bubbles
input test1_hi = yes;
addchartbubble(test1_hi and !isnan(hilvl) and (data1 == hilvl), high*(1+vert),
data1 + "\n"
,(if data1 == hilvl then color.yellow else color.gray), yes);

input test2_hi_other_bubbles = no;
addchartbubble(test2_hi_other_bubbles and !isnan(hilvl) and (data1 != hilvl), high*(1+vert),
data1 + "\n"
,(if data1 == hilvl then color.yellow else color.gray), yes);

addchartbubble(test1_hi and x, hi[bars_after],
m + "th highest\n" +
hi[bars_after]
,color.cyan, yes);

input test3_hi = no;
addchartbubble(test3_hi, high*(1+vert),
hi + "\n" +
hilvl
, color.yellow, yes);

#------------------

input oooooooooo = 0;

# low test bubbles
input test4_lo = yes;
addchartbubble(test4_lo and !isnan(lolvl) and (data2 == lolvl), low*(1-vert),
data2 + "\n"
,(if data2 == lolvl then color.yellow else color.gray), no);

input test5_lo_other_bubbles = no;
addchartbubble(test5_lo_other_bubbles and !isnan(lolvl) and (data2 != lolvl), low*(1-vert),
data2 + "\n"
,(if data2 == lolvl then color.yellow else color.gray), no);

addchartbubble(test4_lo and x, lo[bars_after],
m + "th lowest\n" +
lo[bars_after]
,color.cyan, no);

input test6_lo = no;
addchartbubble(test6_lo, low*(1-vert),
lo + "\n" +
lolvl
, color.yellow, no);
#

input ooooooooooo = 0;

#--------------------------------

# averages

# avg - hi
def hitotal = fold h3 = 0 to n
  with p3
  do p3 + (if getvalue(data1, h3) <= hi then getvalue(data1, h3) else 0);

def hiqty = fold h4 = 0 to n
  with p4
  do p4 + (if getvalue(data1, h4) <= hi then 1 else 0);

def hiavg = round(hitotal / hiqty, 2);


# avg - lo
def lototal = fold l3 = 0 to n
  with q3
  do q3 + (if getvalue(data2, l3) >= lo then getvalue(data2, l3) else 0);

def loqty = fold l4 = 0 to n
  with q4
  do q4 + (if getvalue(data2, l4) >= lo then 1 else 0);

def loavg = round(lototal / loqty, 2);

#------------------------

addlabel(1, " ", color.black);

addlabel(1, hiavg + " adjusted high avg, data below " + hilvl, color.green);
addlabel(1, loavg + " adjusted low avg, data above " + lolvl, color.red);

input test7_labels = yes;
addlabel(test7_labels, " ", color.black);
addlabel(test7_labels, "hi q " + hiqty, color.yellow);
addlabel(test7_labels, "lo q " + loqty, color.yellow);

input test8_labels = no;
addlabel(test8_labels, "hi t " + hitotal, color.yellow);
addlabel(test8_labels, "lo t " + lototal, color.yellow);

#------------------------

# average lines

def hiavglvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(hiavg, -(n-1))
 else hiavglvl[1];

plot zhiavg = hiavglvl;
zhiavg.setdefaultcolor(color.green);
zhiavg.SetStyle(Curve.MEDIUM_DASH);


def loavglvl = if bn == 1 then na
 else if isnan(close) then na
 else if nbars_first then getvalue(loavg, -(n-1))
 else loavglvl[1];

plot zloavg = loavglvl;
zloavg.setdefaultcolor(color.red);
zloavg.SetStyle(Curve.MEDIUM_DASH);
#

cyan lines are the mth price limits for highs and lows
green dash line is adjusted average of highs, that are less than the mth high price level
red dash line is adjusted average of lows, that are higher than the mth low price level
z4RV8Pj.jpg
Hi Halcyonguy, can you please help to scan for lolvl (low point at Z4) and hilvl (high point at Z3)? Thanks,
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
346 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top