Bid-Ask Spread Lines Indicator for ThinkorSwim

I have updated this script to show spread in % instead of prices of BID/ASK. I have also fixed a issue that sometime the bar doesn't have BID or ASK (thus causing error a label on chart), if that's the case, it will pull data from 1 bar ago.

def lastPrice = if isnan(close(priceType = "LAST")) then lastPrice[1] else close(priceType = "LAST");
def askPrice = if isnan(close(priceType = "ASK")) then askPrice[1] else close(priceType = "ASK");
def bidPrice = if isnan(close(priceType = "BID")) then bidPrice[1] else close(priceType = "BID");
def spreadPercent = ((askPrice-bidPrice)/lastPrice);

AddLabel(yes, AsPercent(spreadPercent), if spreadPercent <= .02 then Color.CYAN else COLOR.RED);
 
Last edited:
I'm sorry if this a dumb question but what type of investment instruments would you use this type of indicator for?
 
I have updated this script to show spread in % instead of prices of BID/ASK. I have also fixed a issue that sometime the bar doesn't have BID or ASK (thus causing error a label on chart), if that's the case, it will pull data from 1 bar ago.


Does this not work with OnDemand? I am trying right now and I only see 0%.

Thanks.
 
@DojiDude Try this:

Code:
# Bid / Ask Dynamic Line
# Mobius
# Chatroom 09.20.2013

Input LineLimit = 200;
Input SizeLabels = yes;

def bid = close(priceType = "Bid");
def ask = close(priceType = "Ask");
def barNumber = barNumber();
def Mark = close(priceType = "Mark");
def Last = close(priceType = "Last");
#Dynamic_Line
script dynamic{
   Input LineLimit = 200;
   input c = close;
   def bar = if IsNaN(c)
             then bar[1]
             else BarNumber();
   def ThisBar = HighestAll(bar);
   def cLine   = if bar == ThisBar
                 then c
                 else Double.NaN;

  plot P = if ThisBar - LineLimit <= bar
           then HighestAll(cLine)
           else Double.Nan;
}

plot B = dynamic(linelimit= linelimit,c = bid);
B .SetDefaultColor(Color.green);
B.SetStyle(Curve.FIRM);
B .SetLineWeight(1);

plot A = dynamic(linelimit = linelimit,c = ask);
A .SetDefaultColor(Color.MAGENTA);
A.SetStyle(Curve.FIRM);
A .SetLineWeight(1);
def Dir = if Last == Bid then 1 else 0;
def NotDir = if (Last != Bid) or (Last != Ask) then 1 else 0;
AddCloud(if Dir then A else Double.NaN, if Dir then B else Double.NaN, color.Green, color.Current);
AddCloud(if !Dir then A else Double.NaN,if !Dir then B else Double.NaN, color.Red, color.Current);
AddCloud(if NotDir then A else Double.NaN, B, Color.Gray, Color.Current);
AddLabel(yes, Concat("BID: ", AsDollars(close(priceType = "BID"))), if Dir then Color.GREEN else Color.RED );
AddLabel(yes, Concat("ASK: ", AsDollars(close(priceType = "ASK"))), if Dir then Color.RED else Color.GREEN);
# End Code Bid Ask Lines

Thanks, just what I was looking for.
 
Last edited by a moderator:
@DojiDude Try this:

Code:
# Bid / Ask Dynamic Line
# Mobius
# Chatroom 09.20.2013

Input LineLimit = 200;
Input SizeLabels = yes;

def bid = close(priceType = "Bid");
def ask = close(priceType = "Ask");
def barNumber = barNumber();
def Mark = close(priceType = "Mark");
def Last = close(priceType = "Last");
#Dynamic_Line
script dynamic{
   Input LineLimit = 200;
   input c = close;
   def bar = if IsNaN(c)
             then bar[1]
             else BarNumber();
   def ThisBar = HighestAll(bar);
   def cLine   = if bar == ThisBar
                 then c
                 else Double.NaN;

  plot P = if ThisBar - LineLimit <= bar
           then HighestAll(cLine)
           else Double.Nan;
}

plot B = dynamic(linelimit= linelimit,c = bid);
B .SetDefaultColor(Color.green);
B.SetStyle(Curve.FIRM);
B .SetLineWeight(1);

plot A = dynamic(linelimit = linelimit,c = ask);
A .SetDefaultColor(Color.MAGENTA);
A.SetStyle(Curve.FIRM);
A .SetLineWeight(1);
def Dir = if Last == Bid then 1 else 0;
def NotDir = if (Last != Bid) or (Last != Ask) then 1 else 0;
AddCloud(if Dir then A else Double.NaN, if Dir then B else Double.NaN, color.Green, color.Current);
AddCloud(if !Dir then A else Double.NaN,if !Dir then B else Double.NaN, color.Red, color.Current);
AddCloud(if NotDir then A else Double.NaN, B, Color.Gray, Color.Current);
AddLabel(yes, Concat("BID: ", AsDollars(close(priceType = "BID"))), if Dir then Color.GREEN else Color.RED );
AddLabel(yes, Concat("ASK: ", AsDollars(close(priceType = "ASK"))), if Dir then Color.RED else Color.GREEN);
# End Code Bid Ask Lines

View attachment 6040
Any Idea on how to speed this up?
I've noticed it has a bit of a lag from my digital display of the bid and ask.
I would guess there is a setting to tinker with, but unfortunately, I'm not code savvy.
Any help would be greatly appreciated.
Thanks in advance.
 
Any Idea on how to speed this up?
I've noticed it has a bit of a lag from my digital display of the bid and ask.
I would guess there is a setting to tinker with, but unfortunately, I'm not code savvy.
Any help would be greatly appreciated.
Thanks in advance.
There is nothing in the script causing a lag.
Which sadly means nothing can be added to the script to "speed this up"
 

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

Thread starter Similar threads Forum Replies Date
C Bid Ask Spread Visualizer For ThinkOrSwim Indicators 5
BenTen Volume Spread Analysis (VSA) Indicator for ThinkorSwim Indicators 18

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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