Bid-Ask Spread Column in Options Chain


Hi Pete,

i would like to create a column in the options chain to show liquidity using the bid ask spread. The formula is the difference between the bid ask prices divided by the price of the stock/ETF (underlying). But i have difficulty creating the last close price for the underlying. How may i go abt doing it?

The code i have is as below

def askPrice = close(priceType = “ASK”);
def bidPrice = close(priceType = “BID”);
def spreadPercent; spreadPercent = ((askPrice-bidPrice)/askPrice);

AddLabel(yes, AsPercent(spreadPercent), if spreadPercent >= 0 then Color.UPTICK else Color.DOWNTICK);


So instead of dividing by the ask price, i will like to modify the code to divide by the underlying close price. Is it possible? Once again, appreciate your help. Thanks


Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on November 28, 2022 6:31 am
Private answer

Since you have requested a custom watchlist column solution, I had to move your question out of the "Chart Studies" topic and into the "Watch Lists" topic.

In Thinkscript, we have a built-in function named GetUnderlyingSymbol(). Details here:

We can use that to get the underlying symbol of an option contract. Within the option chain we do that with the following line of code:

plot data = close(GetUnderlyingSymbol());

You can add that to a custom column in your option chain to verify that it gives the correct value. From there it's pretty simple to convert that into a value you can use in your own code:

def askPrice = close(priceType = “ASK”);
def bidPrice = close(priceType = “BID”);
def stockPrice = close(GetUnderlyingSymbol());
plot spreadPercent = 100 * (askPrice - bidPrice) / stockPrice;

Notice I have cleaned up the code a bit. When you do not include spaces between variables and operators it makes it very difficult to read. And more importantly, will often cause websites to break the code as it will consider some of those characters to be treated as HTML tags. And.... I got rid of the AddLabel() statement and replaced it with a simple plot statement.

For those interested in applying this solution. The time frame must be set to an intraday time frame and this only works within the option chain columns of the Trade tab on Thinkorswim. This will not work as a chart study or within a watchlist gadget.

Oh, and all the value will display as 0.0 using your original code. So I am multiplying the value by 100 to convert it into a percent value.

Marked as spam
Posted by (Questions: 37, Answers: 4097)
Answered on November 28, 2022 9:34 am
Hi Pete, once again thanks for your help. it works, although not all the numbers are displayed. most of the data is NaN. So basiclaly i get like 30-40% of the data as actualy percentages with most of them being NaN. I have set the time to the 1 min chart. I suspect is due to the heavy memory required to calculate so many data. But anyway thanks a lot. Have a good day
( at November 28, 2022 10:22 am)
I tested it using a 5 min time frame. There is no reason to use anything less than a 5 min time frame for any custom watchlist solution. Not sure if that clears up the issue of NaN for you. But you should also make sure to use this on stocks with very heavy options activity. Ticker symbols like QQQ, SPY, AAPL. If you try to use a solution like this on inactive stocks there is simply no data for the code to work with.
( at November 28, 2022 10:27 am)