Bid-Ask Spread Lines Indicator for ThinkorSwim

DojiDude

New member
This thread has been updated. You can find the code for ThinkorSwim's Bid-Ask Spread Lines down below (post #4)

I tried the above code hoping that it would work on standard stocks in TOS but unfortunately this does not work. I then decided to try my hand at creating my own script (shown below) but can seem to get it to work please can you take a look and advise what Im doing wrong (go easy on my I have just got TOS this month and have been scripting for all of 5 hours :geek:)

What Im trying to do is have the Bid Price (Red) & Ask Price (Blue) on the chart with the area in between grey if possible. Similar to what you see on MT4 when trading Forex.
 
Last edited by a moderator:

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

@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 );
 
@BenTen Trying to:cry:. Thanks for the reply. The script only seems to put the Spread in the top left I wanted to add line to the chart like below

bid-ask-mt4.png


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

jimGBz1.png
 
Last edited:
Yes that work perfectly! Thanks

Where can I go to learn more about ThinkScript rather than blind guess work? Trying to find the structure rule etc.
 
@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 );
Thanks for this BenTen. Very surprised TOS doesn't have a out-of-the-box Spread indicator. Your code puts a block in the upper left corner of a chart and is very usable, but I notice it can not be added as a column in a watchlist which is where I'd really like it. Would be nice to know if a spread is outrageous before I even click on the chart.
How hard would it be to add to the code to make it available as a column in a watchlist. Or maybe be able to change the color of the box that currently displays. It's a bright red as of now. Be nice to tone it down a bit.
Thanks in advance for any help you may be able to provide.
Dave
 
Great work, I have added below code to your script to show spread value at the left corner and its color changes based on spread value.
If Spread is <=.05 then GREEN
If Spread is between .06 and .15 then YELLOW
ELSE RED.

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);
 
Extension of BenTen's work.

I have added below code to your script to show spread value at the left corner and its color changes based on spread value.
If Spread is <=.05 then GREEN
If Spread is between .06 and .15 then YELLOW
ELSE RED.

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.ORANGE );
AddLabel(yes, Concat("ASK: ", AsDollars(close(priceType = "ASK"))), 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
 
Last edited by a moderator:
Hi @Welkin,

I've been racking my brain on this one, trying not to bother you :) But, I wonder if you might understand how to do the following to this code of Mobius's:

1) Make the Bid / Ask lines change color depending on if they just went up from where they were at (green) or down from where they were just at (red)

Here's the code I'm using:
Code:
# Bid / Ask Dynamic Line
# Mobius
# Chatroom 09.20.2013

input LineLimit = 30;
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 = 30;
    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.CYAN);
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.CYAN, Color.CURRENT);
AddCloud(if !Dir then A else Double.NaN, if !Dir then B else Double.NaN, Color.MAGENTA, 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.ORANGE );
#AddLabel(yes, Concat("ASK: ", AsDollars(close(priceType = "ASK"))), 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

Looking forward to hearing what you think. Hope you're doing well.
 
honestly not sure on this one, played around for a little bit, simply can't use AssignValueColor() function for A > A[1] to color green or red because it will be color striped... defining a new variable and using a custom script to get an output for the comparison between current and prev bid[1] and ask[1] also won't work, even if it outputs a 1 or 0 for true/false the color will not dynamically change the entire line. I'll probably tinker with it again at a later time if I can think of something else.
 
@YungTraderFromMontana

Code:
declare lower;
def ask = close(priceType = PriceType.ASK);
def bid = close(priceType = PriceType.BID);
def c = close;
plot data = (ask - bid)/c;
Please be aware that depending on the instrument, this sometimes will only be value during real trading hours, not reliable on backtest or historical data.
 
I haven't been able to find a function in TOS that does a simple calculation. Spread = High - Low. I then want to create a Scanner condition to retrieve stocks that have a spread of at least $1 or more (as an example) in ALL of the prior 20 daily bars. Can someone provide the thinkscript code for that? Then I can copy/paste and tweak it for my need. That would be very, very helpful!! Thanks.
 
@pennyworld High and low of the previous candle?

Here is the spread analyzer script. Not sure if that will help.

Code:
# Spread Analyzer
# Thanks AlphaInvestor (Bid Ask Script) and WalkingBallista (DMI Range Script)
# Assembled by BenTen at useThinkScript.com

declare lower;

def bid = close(priceType = PriceType.BID);
def ask = close(priceType = PriceType.ASK);

plot range = AbsValue(AbsValue(ask)-AbsValue(bid));

addLabel(yes, "Spread: " + range, Color.WHITE);
 
@pennyworld Did you try:

Ruby:
plot BarRange1 = AbsValue("value" = AbsValue(high) - AbsValue(low)) is greater than 3 and !(AbsValue("value" = AbsValue(high) - AbsValue(low)) is less than 3 within 10 bars);

Notice the placement of the "!" character... Try with and without parenthesis...
 
Hello,

I would like to know how can I add this spread percentage code into a label and instead of changing the text color to just change the label color and not the text?

Code:
Spread Color
Plot Spread =((ask-bid)/bid)*100;
spread.assignValueColor(if spread >=10 then color.Downtick else color.green);
 
@mbarcala Is this what you were looking for?

Code:
def bid = close(priceType = "Bid");
def ask = close(priceType = "Ask");

def spread = ((ask - bid) / bid) * 100;
AddLabel(yes, spread + "%", if spread >= 10 then color.DOWNTICK else color.GREEN);
 
Last edited:
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:
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
 
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
338 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