开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Mid price for options spread


 

Hi Guys, does anybody have experience with midpoint()). I am showing the code below. I suspect this may not be 100% reliable. I am thinking of calculating the mid price as bid + ask/ 2, what is your opinion?
Thank you,
?
# Step 1: Define the long/short calls
symbol = 'TSLA'
long_strike = 220
short_strike = 225
expiry = '20241220'
assert long_strike < short_strike, "Not a bull call debit spread"
long_call = Option(symbol=symbol, lastTradeDateOrContractMonth=expiry, strike=long_strike,
? ? ? ? ? ? ? ? ? ?right='C', exchange='SMART', currency='USD')
short_call = Option(symbol=symbol, lastTradeDateOrContractMonth=expiry, strike=short_strike,
? ? ? ? ? ? ? ? ? ? right='C', exchange='SMART', currency='USD')
# Qualify the contracts
ib.qualifyContracts(long_call, short_call)
?
?
# Print long and short calls to check if they are correctly defined
print(long_call)
print(short_call)
?
combo_legs = [
? ? ComboLeg(conId=long_call.conId, ratio=1, action='BUY', exchange='SMART'),
? ? ComboLeg(conId=short_call.conId, ratio=1, action='SELL', exchange='SMART')
]
?
bull_call_debit_spread = Bag(symbol=symbol, comboLegs=combo_legs, exchange='SMART')?
?
# Print the bull call debit spread to verify
print(bull_call_debit_spread)
?
ticker = ib.reqMktData(bull_call_debit_spread, "", False, False)?
ib.sleep(2)
?
# Print ticker information to diagnose any issues
print(ticker)
print('Bid:', ticker.bid)
print('Ask:', ticker.ask)
print('Midpoint:', ticker.midpoint())
?
ticker.midpoint()
?
?


 

Hey Draql12,?
?
I looked up the midpoint() source code in the ticker.py file and have produced it below. Note that the function incorporates bidsize and asksize in its logic, so I would be sure to print('Bidsize:', ticker.bidSize) and print('Asksize:', ticker.askSize) as well when validating this function.
?
? ? def hasBidAsk(self) -> bool:
? ? ? ? """See if this ticker has a valid bid and ask."""
? ? ? ? return (
? ? ? ? ? ? self.bid != -1
? ? ? ? ? ? and not isNan(self.bid)
? ? ? ? ? ? and self.bidSize > 0
? ? ? ? ? ? and self.ask != -1
? ? ? ? ? ? and not isNan(self.ask)
? ? ? ? ? ? and self.askSize > 0
? ? ? ? )
? ? def midpoint(self) -> float:
? ? ? ? """
? ? ? ? Return average of bid and ask, or NaN if no valid bid and ask
? ? ? ? are available.
? ? ? ? """
? ? ? ? return (self.bid + self.ask) * 0.5 if self.hasBidAsk() else nan


 

Hey Draql12,?
?
On second thought, I think marketPrice() may be a more robust choice, as it first seeks to use the most recent trade price (comparing to only the actual ask and bid prices in that moment ignoring their sizes). If the three don't jive, then the function calls Midpoint().
?
The marketPrice() function could be especially useful for low-volume securities (such as OTM options) that don't have an active bid or ask. Although if the last trade price of the security occurred a long time ago (perhaps a week or a few days), then could adversely impact you but still, that fail would fall more on IB's data service (for providing you with an outdated and irrelevant last trade price) than the marketPrice() function itself. Naw meen.
?
I have re-printed its source code below for additional reading.
?
? ? def marketPrice(self) -> float:
? ? ? ? """
? ? ? ? Return the first available one of
? ? ? ? * last price if within current bid/ask or no bid/ask available;
? ? ? ? * average of bid and ask (midpoint).
? ? ? ? """
? ? ? ? if self.hasBidAsk():
? ? ? ? ? ? if self.bid <= self.last <= self.ask: # <== check it out ignores bid/ask sizes!
? ? ? ? ? ? ? ? price = self.last
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? price = self.midpoint() # <== what you already have
? ? ? ? else:
? ? ? ? ? ? price = self.last #<== all else failed
? ? ? ? return price
?
Regards,
Some Guy