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
#Copyright (c) 2021 Jonathan Phillips, TrendTradez - http://www.trendtradez.com
#
#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.

kikV8FGs3TrFkN7?width=778&height=542&cropmode=none.png
 
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
#Copyright (c) 2021 Jonathan Phillips, TrendTradez - http://www.trendtradez.com
#
#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.
https://usethinkscript.com/threads/use-a-profile-of-close-to-find-the-mode-of-a-period.9324/
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
428 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