Expected Liquidity Range for ThinkOrSwim


Well-known member

Creator Message:
Simple but effective script that displays Liquidity Premium/ Discount areas in an adaptive way based on key Fibonacci levels.
You can increase or decrease the 'Period' value in the Settings to adjust the gap between the lines as you see fit.
By default the value is '46' which should suit most markets.


#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © RickSimpson
#indicator('Expected Liquidity', 'Expected Liquidity Range', true, max_lines_count=500, max_labels_count=500)
# Converted and mod by [email protected] - 01/2023
input period = 46;       # 'Period' minval=15, maxval=60
input extendLine =  yes; # 'Lines Extension'
input fibLevel1 = 0.236;
input fibLevel2 = 0.382;
input fibLevel3 = 0.618;
input fibLevel4 = 0.764;

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !IsNaN(data) then data else repl;
    plot return = ret_val;

def i_period = if period<15 then 15 else if period>60 then 60 else period;
def Extend = if extendLine then 0 else isNaN(close);
#//Constants Variables Declaration
def highbar;
def highfractal;
def lowfractal;
def closefractal;

#//Lines Style String Function
#//Pivots Definition

def hb   = Highest(high, i_period);
def lb   = Lowest(low,   i_period);
def dist = hb - lb;

#//Premium Fibonacci Levels Calculation

def hf  = hb - dist * fibLevel1;
def chf = hb - dist * fibLevel2;

#//Discount Fibonacci Levels Calculation

def clf = hb - dist * fibLevel3;
def lf  = hb - dist * fibLevel4;

#//High/Low Fractals Scalling

def pfractal            = high  >  hb[1];
def dfractal            = low   <  lb[1];
def premiumexpectstart  = hb[3] == hb[2] and hb[2] == hb[1] and pfractal;
def discountexpectstart = lb[3] == lb[2] and lb[2] == lb[1] and dfractal;

#//Liquidity Range Calculation

def highexpect = premiumexpectstart  or high[1] == hb;
def lowexpect = discountexpectstart or low[1]  == lb;
highbar       = if highexpect then yes else if lowexpect then no else nz(highbar[1], yes);
def barexp  = if (close > hf) then barexp[1] + 1 else 0;
def barexpin = barexp == 1;
def barexpot = if (close < hf) then barexpot[1] + 1 else 0;
def barexpout = barexpot == 1;
highfractal   = if barexpin then yes else if barexpout then no else nz(highfractal[1], yes);
def fractexp = if (close < lf) then fractexp[1] + 1 else 0;
def fractexpin = fractexp == 1;
def fractexpot = if (close > lf) then fractexpot[1] + 1 else 0;
def fractexpout = fractexpot == 1;
lowfractal    = if fractexpin then yes else if fractexpout then no else nz(lowfractal[1], yes);
closefractal  = if !highfractal and !lowfractal then yes else no;

#//Drawing Calculation
def isintraday = GetAggregationPeriod() < AggregationPeriod.DAY;
def bar = IsNaN(BarNumber());
def isrealtime =  IsNaN(close[-1]) and !IsNaN(close);
def stateislast = if isrealtime and isintraday then !IsNaN(close[1]) else isrealtime;
def hbdtrue     = stateislast and highfractal;
def lbdtrue     = stateislast and lowfractal;
def chfdtrue    = stateislast and closefractal and highbar;
def clfdtrue    = stateislast and closefractal and !highbar;

#/Expected Liquidity Range Conditions

def hbexpect;#  = hb
hbexpect = if hbdtrue then hb else if lbdtrue then lf else if chfdtrue then hf  else if clfdtrue then chf else na;
def lbexpect;#  = lb
lbexpect = if hbdtrue then hf else if lbdtrue then lb else if chfdtrue then clf else if clfdtrue then lf else na;

def R_extendout = if !IsNaN(hbexpect) then hbexpect else nz(R_extendout[1],hb);
def R_extendin  = if !IsNaN(lbexpect) then lbexpect else nz(R_extendin[1],lb);

plot premium  = if R_extendout == 0 or Extend then na else R_extendout;
plot discount =  if R_extendin == 0 or Extend then na else R_extendin;

def mid = (premium + discount) / 2;

plot midLine = mid;

AddCloud(premium,  discount,  Color.DARK_GRAY);

#---- END CODE ---
Last edited by a moderator:

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
329 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.