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.

Code:
#Plots Bid/Ask Line with spreads shadow that shows the current value on given Ticker

input pricea = priceType.ASK;
input priceb = priceType.BID;
input offset = 0;
input length = 300;

Plot askp = close(Pricea);
Plot BidP = close(Priceb);

Askp.SetLineWeight(1);
Askp.SetDefaultColor(color.blue);
Askp.HideBubble();
AskP.setstyle(curve.short_dash);
BidP.SetLineWeight(1);
Bidp.SetDefaultColor(color.red);
Bidp.HideBubble();
BidP.setstyle(curve.short_dash);

AddCloud(AskP, BidP, color.blue, color.red);

P.S I have Implemented you ORB Script and love it! Cant wait to try it out. Thanks
 
Last edited by a moderator:

BenTen

Administrative
Staff member
Staff
VIP
@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 );
 

DojiDude

New member
@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
 

BenTen

Administrative
Staff member
Staff
VIP
@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:

DojiDude

New member
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.
 

Brain Monkey

New member
Ok so this is a real simple watch list indicator to help (maybe) give me some idea which strikes get pinned on Options Exp.
  • I start by making a custom watch list with just SPX.
  • Then I run an options scan on my SPX watch list and I filter "Days to Exp" to capture just those options that pertain to the up coming Options Exp.
  • I then add columns for price, volume, open interest and this custom column. (I know this makes A LOT of assumptions about whether the volume is net short or long plus a bazillion other things)
  • Then I click on the columns to get a sense of what is being most heavily traded, what has the most open interest and which delta's the market makers have to hedge in order to remain market neutral.
Code:
def Pain = ((bid + ask)/2) *  volume;
plot R = Pain/1000;

My question is: I want to make an "if then" statement that takes all my NaN results and makes them zero, so that when I click on my Custom Pain column, all the highest results go to the top and all the NaN's go to the bottom. I hope this makes sense. Also if anyone else has a better approach, I would love to see. Thank you.
 
Last edited:

DavidP

New member
@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
 

vimeo

New member
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);
 

vimeo

New member
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:

evanevans

Active member
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.
 

Welkin

Active member
VIP
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.
 

diazlaz

Well-known member
2019 Donor
VIP
@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.
 

pennyworld

New member
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.
 

BenTen

Administrative
Staff member
Staff
VIP
@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);
 

rad14733

Well-known member
VIP
@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...
 

mbarcala

New member
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);
 

Similar threads

Top