# How to Find the MODE of a series?

#### jphilli11

##### Member
Has anyone tried to find the MODE of a series of values in ToS? e.x. the closing price that had the highest frequency over a given period of time. I can see a lot of uses for variations of this in identifying support and resistance as well as other applications. Any help would be great!

Thanks,

Solution
I decided to stop being lazy and worked this problem a bit. It isn't perfect but here's a solution that finds the MODE with a few parameters used to dial in to a certain range and limits.

Params:
1. price:
1. the price to use to calculate SMA lower limit. close is default.
2. displace:
1. the number of bars to eliminate from MODE/support calculation. i.e. don't inlclude the most recetn xx bars in calculation (gives better true support). 30 is default.
3. period:
1. How many bars to use in factoring the MODE/support. 180 is Default
4. SMA_Lower_Limit:
1. Data will only be factored if ABOVE this SMA. 50 is default is 50
Code: (and forgive the silly disclaimer)...
I decided to stop being lazy and worked this problem a bit. It isn't perfect but here's a solution that finds the MODE with a few parameters used to dial in to a certain range and limits.

Params:
1. price:
1. the price to use to calculate SMA lower limit. close is default.
2. displace:
1. the number of bars to eliminate from MODE/support calculation. i.e. don't inlclude the most recetn xx bars in calculation (gives better true support). 30 is default.
3. period:
1. How many bars to use in factoring the MODE/support. 180 is Default
4. SMA_Lower_Limit:
1. Data will only be factored if ABOVE this SMA. 50 is default is 50
Code: (and forgive the silly disclaimer)

Code:
``````#===================================================
#                        Credits
#===================================================
#Median, SMA, MODE support calculation
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE.
#
#Requirements:
#    * ThinkorSwim version xx or above
#
#Installation: you should know

declare upper;

#===================================================
#          INPUTS AND GLOBAL VARIABLES
#===================================================
def _BN  = BarNumber();
def _nan = Double.NaN;
def _L   = low;
def _H   = high;
def _O   = open;
def _C   = close;

input price = close;
input displace = 30;
input period    = 180;
input SMA_Lower_Limit = 50;

#median
def _median = (highestall(_C) - Lowestall(_c)) / 2;
plot median = _median;
median.setpaintingStrategy(PaintingStrategy.LINE);
median.setdefaultcolor(color.light_green);

plot SMA = SimpleMovingAvg(length=SMA_Lower_Limit);
sma.setdefaultcolor(color.cyan);
sma.SetPaintingStrategy(PaintingStrategy.line);
sma.hide();

#Mode
def _M   = MedianPrice();
def _NewMode;
def _S;
_S  =
(
fold i = displace to period
with c
do c +
if
_L > SMA and
_H < _Median and
GetValue(_H, i) > _M and
GetValue(_L, i) < _M
then 1 else 0
);
_NewMode  = if
_S > _S[1]
then _M else _NewMode[1];

plot Mode = _newmode;

#===================================================
#                        HINTS
#===================================================
#hint price: the price to use to calculate SMA lower limit. close is default.
#hint displace: the number of bars to eliminate from MODE/support calculation.  i.e. don't inlclude the most recetn xx bars in calculation (gives better true support). 30 is default.
#hint period: How many bars to use in factoring the MODE/support. 180 is Default
#hint SMA_Lower_Limit: Data will only be factored if ABOVE this SMA.  50 is default is 50``````

Here's a screen shot of SSOF - seems to identify strong support areas for 'worst case' pullbacks. White line is the MODE support.

I decided to stop being lazy and worked this problem a bit. It isn't perfect but here's a solution that finds the MODE with a few parameters used to dial in to a certain range and limits.

Params:
1. price:
1. the price to use to calculate SMA lower limit. close is default.
2. displace:
1. the number of bars to eliminate from MODE/support calculation. i.e. don't inlclude the most recetn xx bars in calculation (gives better true support). 30 is default.
3. period:
1. How many bars to use in factoring the MODE/support. 180 is Default
4. SMA_Lower_Limit:
1. Data will only be factored if ABOVE this SMA. 50 is default is 50
Code: (and forgive the silly disclaimer)

Code:
``````#===================================================
#                        Credits
#===================================================
#Median, SMA, MODE support calculation
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE.
#
#Requirements:
#    * ThinkorSwim version xx or above
#
#Installation: you should know

declare upper;

#===================================================
#          INPUTS AND GLOBAL VARIABLES
#===================================================
def _BN  = BarNumber();
def _nan = Double.NaN;
def _L   = low;
def _H   = high;
def _O   = open;
def _C   = close;

input price = close;
input displace = 30;
input period    = 180;
input SMA_Lower_Limit = 50;

#median
def _median = (highestall(_C) - Lowestall(_c)) / 2;
plot median = _median;
median.setpaintingStrategy(PaintingStrategy.LINE);
median.setdefaultcolor(color.light_green);

plot SMA = SimpleMovingAvg(length=SMA_Lower_Limit);
sma.setdefaultcolor(color.cyan);
sma.SetPaintingStrategy(PaintingStrategy.line);
sma.hide();

#Mode
def _M   = MedianPrice();
def _NewMode;
def _S;
_S  =
(
fold i = displace to period
with c
do c +
if
_L > SMA and
_H < _Median and
GetValue(_H, i) > _M and
GetValue(_L, i) < _M
then 1 else 0
);
_NewMode  = if
_S > _S[1]
then _M else _NewMode[1];

plot Mode = _newmode;

#===================================================
#                        HINTS
#===================================================
#hint price: the price to use to calculate SMA lower limit. close is default.
#hint displace: the number of bars to eliminate from MODE/support calculation.  i.e. don't inlclude the most recetn xx bars in calculation (gives better true support). 30 is default.
#hint period: How many bars to use in factoring the MODE/support. 180 is Default
#hint SMA_Lower_Limit: Data will only be factored if ABOVE this SMA.  50 is default is 50``````

what you are doing isn't scanning all prices, to find which one occurs the most.
if you want to find the true MODE price of a range, take a look at this and see if it helps.

find the close price that occurs with the greatest frequency.

87k+ Posts
412 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?