Bid-Ask Spread Lines Indicator for ThinkorSwim

D

DojiDude

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

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

BenTen

Administrative
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 );
 
D

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



Thanks
 
BenTen

BenTen

Administrative
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
 
Last edited:
D

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

yman

Member
Does any one know how to get bid and ask volume as volume profile
 
R

RobertAndrew

New member
VIP
Dude. You Rock BenTen! Love this indicator!!!! And it looks cool too!
 
T

tado

New member
Hi BenTen. Can you plot an option debit/credit spread? In other words, how to get bid/ask prices for long leg and short leg?
 
M

mrmac

New member
VIP
Hi, I am pretty new to the forum and so far really enjoying all the learning, thank you.
I have a question about an indicator but really not sure how to articulate it though...
I am trying to find an indicator (if it even exists)that will paint an arrow in the direction of the live candle on most timeframes.
The metric behind these arrows is something I am struggling with. I imagine it might be based on volume, orders, bid vs ask?
In my mind the arrow appears once a certain "level" has been reached and if that level continues to increase the colour might change accordingly.
The arrow will only appear when there is definite movement in a certain direction.
Thank you in advance for any advice here...
 
T

thenaz

New member
hi
new here
was wondering if the spread lines indicator was working for anyone else?
I cant seem to get it to work on the /ym ?
 
T

thenaz

New member
hi all...…..benten I guess this question would be for you. great job on the indicators man..are you a wizard hahhahhahah
anyways. I cant seem to get the lines to plot on the bid/ask spread that you have come up with...is anyone else having trouble with that or is it just me...… probly . i put it on the /ym 1 min chart that should work right? thanks for the room its great.
 
V

veerasareddy

New member
is there any way to scan for the Bid/Ask spread range.
 
mckenzieatix

mckenzieatix

New member
VIP
@BenTen This is exactly what I was looking for. Thank you. Changed first value to 10 so it only extends into the future. Love the values at the top showing Bid/Ask. Do you know what value I can add to show the percentage difference between Bid/Ask and the $ difference? (% = -) ($ + -)
 
D

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
 
V

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);
 
V

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:
V

vimeo

New member
@BenTen i was wondering if there any way i can create a similar line for Average price of my current position that we see on the Active Trader.

 
T

thealphabreed

Member
hey @BenTen would this work on all time frames, such as the 1-min timeframe?
 

Similar threads

Top