Options Alternative to Close() when underlying doesn't trade in aggregation period

bnnnboy

New member
I am writing a script to average the mark of an option strategy over the last 10 minutes of trading and have encountered a problem where the script only works if both options trade in the aggregation period. Is there a function similar to Close() that will pull the Mark (although Bid and Ask are good enough) regardless of whether the options trade?

Here's my script I'm working with

Code:
def dura = 10;
input PutStrike = 165;
input CallStrike = 176;
input underlyingEXP = ".GLD220318";

plot avg = Average(close(concat(underlyingEXP, concat("C",CallStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK)+close(concat(underlyingEXP, concat("P",PutStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK),dura);

Thanks!
 
Solution
That's a nifty way to fill those gaps on a plot, thanks! However, it still requires both options to trade in an aggregation period before it starts to show anything. The problem I'm encountering is if I submit the conditional order an hour or two before the close and they don't trade together after the submission, the order won't work.

Surely there must be some way to pull the current bid or ask of an option, regardless of whether it is trading, similar to how an alert gets the data.


i was wrong on earlier post.
...trying to use an offset on a plot variable doesn't work.
...trying to read an offset of the average variable instead of the call or put price.

if a formula includes an offset of the answer variable, then it needs...
If I increase the AggregationPeriod I stand a better chance of both trading within the time period, but I lose fidelity and I still end up with gaps where the script returns "N/A" as shown here:
GW13f5u.png


Any ideas on how to just pull the bid, ask, or mark of these options? Is there a way to use the 'bid' and 'ask' commands on an option spread?
 

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

..only works if both options trade in the aggregation period. Is there a function to pull the Mark regardless of whether the options trade?

no. If there are no trades there is no price data to read.

what if, when there is a price error , you read the previous price, Instead of the current price?


def avg = Average(close(concat(underlyingEXP, concat("C",CallStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK)+close(concat(underlyingEXP, concat("P",PutStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK),dura);

plot p = if isnan(avg) then p[1] else avg;
 
no. If there are no trades there is no price data to read.

what if, when there is a price error , you read the previous price, Instead of the current price?


def avg = Average(close(concat(underlyingEXP, concat("C",CallStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK)+close(concat(underlyingEXP, concat("P",PutStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK),dura);

plot p = if isnan(avg) then p[1] else avg;
That's a nifty way to fill those gaps on a plot, thanks! However, it still requires both options to trade in an aggregation period before it starts to show anything. The problem I'm encountering is if I submit the conditional order an hour or two before the close and they don't trade together after the submission, the order won't work.

Surely there must be some way to pull the current bid or ask of an option, regardless of whether it is trading, similar to how an alert gets the data.
 
That's a nifty way to fill those gaps on a plot, thanks! However, it still requires both options to trade in an aggregation period before it starts to show anything. The problem I'm encountering is if I submit the conditional order an hour or two before the close and they don't trade together after the submission, the order won't work.

Surely there must be some way to pull the current bid or ask of an option, regardless of whether it is trading, similar to how an alert gets the data.


i was wrong on earlier post.
...trying to use an offset on a plot variable doesn't work.
...trying to read an offset of the average variable instead of the call or put price.

if a formula includes an offset of the answer variable, then it needs to use def.
def a = b * a[1];

i didn't try to trap bar 1, maybe that would have fixed it?
. if barnumber() == 1 then do stuff else other stuff.....


--------------------------------------------

we don't need different price types. we need to check individual option prices for errors and replace, not the average number.

sometimes, it helps to split formulas out into separate parts.
then verify each part is working.

your avg formula is long and complicated. it should work, but to verify the parts are working, you would need to copy out different sections of the formula. so why not have a call price formula and a put price formula, then combine them into an average formula.

i separated the call and put sections.
this allowed me to check each if the prices are valid or an error. if an error read a previous price.
put the adjusted prices in the average.

i added a choice for strike type,
. offset - input 1 number, that is added and subtracted to a calculated atm strike, to create low and high strikes
. separate_numbers - input 2 numbers

this is a lower study. it plots the call and put and average prices.
descriptive labels, colored to match the lines.


Ruby:
# test_option_pricenone_01

declare lower;

def na = double.nan;

def dura = 10;
input PutStrike = 165;
input CallStrike = 176;
input underlyingEXP = ".GLD220318";
input agg = AggregationPeriod.MIN;

input priceType1 = PriceType.BID;
input priceType2 = PriceType.ASK;
input priceType3 = PriceType.MARK;
# few prices with last
#input priceType4 = PriceType.last;


input strike_offsets = 5;
def cls = close;
def atm_strike = round(cls, 0);

input strike_type = { default offset , separate_numbers };
def hi_strike;
def lo_strike;
# call strike is higher /  put strike is lower 
switch (strike_type) {
case offset:
  hi_strike = atm_strike + strike_offsets;
  lo_strike = atm_strike - strike_offsets;
case separate_numbers:
  hi_strike = CallStrike;
  lo_strike = putStrike;
}

# call strike is higher  ,  put strike is lower 
addlabel(1, "strikes", color.yellow);
addlabel(1, hi_strike, color.yellow);
#addlabel(1, "atm " + atm_strike, color.yellow);
addlabel(1, lo_strike, color.yellow);
addlabel(1, "--", color.black);
# -------------------------------------
def call_mark1 = close(Concat(underlyingEXP, Concat("C", hi_strike)), period = agg, priceType = priceType3);
def call_mark2 = if isnan(call_mark1) then call_mark2[1] else call_mark1;

input show_call_plots = yes;
plot c_mark = if show_call_plots then call_mark2 else na;
c_mark.setdefaultcolor(color.cyan);

addlabel(1, Concat(underlyingEXP, Concat("C", hi_strike)), color.cyan);
addlabel(1, "--", color.black);
# -------------------------------------
def put_mark1 = close(concat(underlyingEXP, concat("P", lo_strike)), period = agg, priceType = priceType3);
def put_mark2 = if isnan(put_mark1) then put_mark2[1] else put_mark1;

input show_put_plots = yes;
plot p_mark = if show_put_plots then put_mark2 else na;
p_mark.setdefaultcolor(color.yellow);

addlabel(1, Concat(underlyingEXP, Concat("P", lo_strike)), color.yellow);
addlabel(1, "--", color.black);
# -------------------------------------

def avg = Average((call_mark2 + put_mark2), dura);
plot zavg = avg;
zavg.setdefaultcolor(color.magenta);

addlabel(1, "average", color.magenta);

# -------------------------------------
#plot z = 0;
# -------------------------------------
# original
#plot avg = Average(close(concat(underlyingEXP, concat("C",CallStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK)+close(concat(underlyingEXP, concat("P",PutStrike)), period = AggregationPeriod.MIN, priceType = PriceType.MARK),dura);
#


TL5qiuq.jpg



https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/PriceType
. ask, bid , last, mark
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/PriceType/PriceType-BID
 
Solution
Thanks! Separating out the put and call calculations with the offset before the average does the trick. It still doesn't get the current bid/ask/mark, only what's traded, but I'll generally be looking at liquid options so that shouldn't be too big of a problem. And if it's far OTM and not trading much, the price really shouldn't be moving much. I appreciate your help!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
439 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