Bid-Ask Spread Lines Indicator for ThinkorSwim

I have been using this Bid/Ask spread lines code for day trading code for the past year from Mobius and all of a sudden this week it is not working where price will move but the lines are not keeping up with the Bid/Ask in real time. If someone can please help or have another current Bid/Ask with lines on chart code that works would be great. Thank you

Here is the 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(no, Concat("ASK: ", AsDollars(close(priceType = "ASK"))), if Dir then Color.GREEN else Color.ORANGE );
AddLabel(no, Concat("BID: ", AsDollars(close(priceType = "BID"))), if Dir then Color.ORANGE else Color.GREEN);
def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
def spread_l1 = 0.05;
def spread_l2 = 0.15;
AddLabel(1, "Spread: " + spread, if spread <= spread_l1 then Color.GREEN else if spread_l1 < spread <= spread_l2 then Color.YELLOW else COLOR.RED);
# End Code Bid Ask Lines

Thank you

J
This is caused by the HighestAll() function used in HighestAll(bar) and HighestAll(cLine) in the dynamic function, but I for the life of me haven't found a work around to change the plot to use the latest candle values and paint the line across the whole chart.
 
This is caused by the HighestAll() function used in HighestAll(bar) and HighestAll(cLine) in the dynamic function, but I for the life of me haven't found a work around to change the plot to use the latest candle values and paint the line across the whole chart.
I called TOS today and they said the {highestbar} was the issue as they changed this last weekend on the backend and they really didnt know how to fix it so it tracks the bid / ask tick for tick... If your ever able to figure it out and update the code that would be great... Thanks
 
Hey all, here is an updated Bid/Ask Line with Label as I want to give @Mobius credit from the TOS Thinkscript Room as he re-wrote the code for me this morning and wanted to share with everyone so they have a working code. Thanks again @Mobius

Code:
# Bid | Ask Line at current prices
# Mobius
# Alternative to using HighestAll()
# Chat Room Request 04.30.2021

script line
    {
     input barsBack = 1000;
     input data = close;
     def c = if !IsNaN(data) and IsNaN(data[-1])
             then data
             else c[1];
     plot line = if isNaN(data[-barsBack])
                 then c[-barsBack]
                 else Double.NaN;
    }
input length = 1000;

def b = close(priceType = "BID");
def a = close(PriceType = "ASK");
plot bid_ = line(length, b);
plot ask_ = line(length, a);

input limit = 0.02;
def priceB = close(priceType = PriceType.BID);
def priceA = close(priceType = PriceType.ASK);
AddLabel(yes, Concat("Bid: ", priceB), Color.Red);
AddLabel(yes, Concat("Ask: ", priceA), Color.Green);
AddLabel(yes, Concat("Spread: ", priceA - priceB), if priceA - priceB > limit then Color.RED else Color.WHITE);
# End Code
 
anyone knows why the bid/ask lines are not plotted on tick charts ?

Do you get a circled "!" in the upper left corner of the chart...??? If so, click on it and read what it says the problem is... My guess is a compatibility issue...
 
Last edited by a moderator:
Do you get a circled "!" in the upper left corner of the chart...??? If so, click on it and read what it says the problem is... My guess is a compatibility issue... Tick-based charts have their limits which is why I don't use them as much these days... I got tired of the limitations...

Tick charts have many limitations indeed... very frustrating, but I still find them to be better for trading futures. I did see the "!" now that you mentioned it, and its saying: "Secondary periods less than 1 day are not allowed on tick charts"

I do have a priceline script that is plotting the last price on the Tick charts, but Bid/Ask would be more helpful.

Thank you !
 
Thanks @rad14733 I've been banging my head against the wall doing that for a few days. Here's an example - I would like to add a condition to my scan: "bid/ask > 0.85" so it picks up only options that satisfy this condition. I tried adding a filter to my scan and entered the following in thinkscript editor, which promptly put red all over it.

(pricetype.aSK / pricetype.bid) > 0.85;

I also tried the following, to no avail:

def spread = (pricetype.aSK / pricetype.bid) > 0.85;
plot spread;
 
@cooldyood Unfortunately,there is no way to incorporate Bid and Ask into calculations for Options... I
 
Last edited by a moderator:
I hacked the code to my liking using your older code with the newest so it just shows the spread colored based on spread amount with an optional alert when the spread is wide (see options).

3vTvgqm.png


Code:
# Spread on Chart by Wiiinii
# https://usethinkscript.com/threads/bid-ask-spread-lines-indicator-for-thinkorswim.1140/page-2#post-76541

script line
    {
     input barsBack = 1000;
     input data = close;
     def c = if !IsNaN(data) and IsNaN(data[-1])
             then data
             else c[1];
     plot line = if isNaN(data[-barsBack])
                 then c[-barsBack]
                 else Double.NaN;
    }
input length = 1000;

def BidPrice = close(priceType = PriceType.BID);
def AskPrice = close(priceType = PriceType.ASK);
def Spread = AskPrice - BidPrice;
AddLabel(yes, Concat("Spread: ", Spread), if Spread <= .03 then Color.GREEN else if Spread <= .05 then Color.YELLOW else COLOR.RED);
Alert(Spread > .1, "SPREAD WIDE!", Alert.Bar, Sound.Chimes);
# End Code

Thanks for making this!
 
Last edited:
@DojiDude Glad you're getting your hands dirty. There is already an existing bid/ask spreads for stocks. Take a look and see if this is what you're looking for.

Code:
declare hide_on_daily;
def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
AddLabel(1, "Spread: " + spread );
Hi, I am definitely getting the syntax wrong. I just want to add study to filter in the scan screen, basically if the current (anytime including pre and post market) ask is greater than the bid, by let's say 5 cents, then it wouldn't show in the scan.

I tried copying and pasting your code, and then just the following to no avail. I even changed the variable name to ABCD just to be sure. It kept saying at least one plot should be defined but I am not trying to plot anything on a chart?
def ABCD = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);

Do you mind or have time to help me out? Just starting with the script and trying to figure out the syntax.
 
Hi, I am definitely getting the syntax wrong. I just want to add study to filter in the scan screen, basically if the current (anytime including pre and post market) ask is greater than the bid, by let's say 5 cents, then it wouldn't show in the scan.

I tried copying and pasting your code, and then just the following to no avail. I even changed the variable name to ABCD just to be sure. It kept saying at least one plot should be defined but I am not trying to plot anything on a chart?
def ABCD = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);

Do you mind or have time to help me out? Just starting with the script and trying to figure out the syntax.
bid / ask are not available in scans
 
@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

jimGBz1.png
Is there a way to modify this to have it display a short line at the right side axis on the daily chart? Thanks
 
Thanks @rad14733 I've been banging my head against the wall doing that for a few days. Here's an example - I would like to add a condition to my scan: "bid/ask > 0.85" so it picks up only options that satisfy this condition. I tried adding a filter to my scan and entered the following in thinkscript editor, which promptly put red all over it.

(pricetype.aSK / pricetype.bid) > 0.85;

I also tried the following, to no avail:

def spread = (pricetype.aSK / pricetype.bid) > 0.85;
plot spread;
To my knowledge, custom scans don't work for options in TOS.
 
Is there a way to plot the bid and the ask on the chart? Maybe the lowest bid and the highest ask per minute with a cloud in between to simulate price?
 
I hacked the code to my liking using your older code with the newest so it just shows the spread colored based on price.

Code:
# Bid | Ask Line at current prices
# Mobius
# Alternative to using HighestAll()
# Chat Room Request 04.30.2021

script line
    {
     input barsBack = 1000;
     input data = close;
     def c = if !IsNaN(data) and IsNaN(data[-1])
             then data
             else c[1];
     plot line = if isNaN(data[-barsBack])
                 then c[-barsBack]
                 else Double.NaN;
    }
input length = 1000;

def BidPrice = close(priceType = PriceType.BID);
def AskPrice = close(priceType = PriceType.ASK);
def Spread = AskPrice - BidPrice;
AddLabel(yes, Concat("Spread: ", Spread), if Spread <= .03 then Color.GREEN else if Spread <= .05 then Color.YELLOW else COLOR.RED);
# End Code

Thanks for making this!

This is great, but there is small thing needed to be fixed. Sometime the code is having trouble reading ASK price from the source, BID usually works. anyway, it's causing this.

Capture.png
 
Last edited:
@DojiDude Glad you're getting your hands dirty. There is already an existing bid/ask spreads for stocks. Take a look and see if this is what you're looking for.

Code:
declare hide_on_daily;
def spread = close(priceType = PriceType.ASK) - close(priceType = PriceType.BID);
AddLabel(1, "Spread: " + spread );
Hi. I'm new to this site and TOS. I'm in search of a label that just displays the Bid/Ask Volumes. e.g. "154/299". I thought this would be a no-brainer. but can not find it. thanks
 
This is great, but there is small thing needed to be fixed. Sometime the code is having trouble reading ASK price from the source, BID usually works. anyway, it's causing this.

Capture.png
You replied to me, but that's not my code in your image as mine only shows the spread. I added an image to my original post and updated the code with an optional alert if the spread is wide.
 
Last edited:

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
416 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