Skip to content

Instantly share code, notes, and snippets.

@hn4002
Created November 5, 2025 23:51
Show Gist options
  • Select an option

  • Save hn4002/775696a0b74e2994240d4dc7e1cc463d to your computer and use it in GitHub Desktop.

Select an option

Save hn4002/775696a0b74e2994240d4dc7e1cc463d to your computer and use it in GitHub Desktop.
Workaround fix for the bugs in the Schwab Streaming API because of the lot vs share size changes
l2_quote["service"] = event["service"]
if l2_quote["service"].upper() == "NYSE_BOOK":
# Apply the volume fix - divide volume by 100 for all asks and bids for all exchanges
bidLevelContainer = l2_quote["BIDS"]
for bidLevel in bidLevelContainer:
bidLevelVolume = 0
for bidRow in bidLevel["BIDS"]:
bidRow["BID_VOLUME"] = bidRow["BID_VOLUME"] / 100.0
bidLevelVolume += bidRow["BID_VOLUME"]
bidLevel["TOTAL_VOLUME_ORIG"] = bidLevel["TOTAL_VOLUME"]
bidLevel["TOTAL_VOLUME"] = bidLevelVolume
# Sort the bidLevel.BIDS by volume descending
#bidLevel["BIDS"].sort(key=lambda x: x["BID_VOLUME"], reverse=True)
askLevelContainer = l2_quote["ASKS"]
for askLevel in askLevelContainer:
askLevelVolume = 0
for askRow in askLevel["ASKS"]:
askRow["ASK_VOLUME"] = askRow["ASK_VOLUME"] / 100.0
askLevelVolume += askRow["ASK_VOLUME"]
askLevel["TOTAL_VOLUME_ORIG"] = askLevel["TOTAL_VOLUME"]
askLevel["TOTAL_VOLUME"] = askLevelVolume
# Sort the askLevel.ASKS by volume descending
#askLevel["ASKS"].sort(key=lambda x: x["ASK_VOLUME"], reverse=True)
elif l2_quote["service"].upper() == "NASDAQ_BOOK":
# For all bids and asks, remove row for the exchange = NSDQ if exchange = Q is present in the same bidLevel/askLevel
bidLevelContainer = l2_quote["BIDS"]
for bidLevel in bidLevelContainer:
exchanges_in_level = set()
for bidRow in bidLevel["BIDS"]:
exchanges_in_level.add(bidRow["EXCHANGE"].upper())
if "Q" in exchanges_in_level:
# Remove NSDQ rows
bidLevel["BIDS"] = [bidRow for bidRow in bidLevel["BIDS"] if bidRow["EXCHANGE"].upper() != "NSDQ"]
# Recompute the TOTAL_VOLUME
bidLevelVolume = 0
for bidRow in bidLevel["BIDS"]:
bidLevelVolume += bidRow["BID_VOLUME"]
bidLevel["TOTAL_VOLUME_ORIG"] = bidLevel["TOTAL_VOLUME"]
bidLevel["TOTAL_VOLUME"] = bidLevelVolume
askLevelContainer = l2_quote["ASKS"]
for askLevel in askLevelContainer:
exchanges_in_level = set()
for askRow in askLevel["ASKS"]:
exchanges_in_level.add(askRow["EXCHANGE"].upper())
if "Q" in exchanges_in_level:
# Remove NSDQ rows
askLevel["ASKS"] = [askRow for askRow in askLevel["ASKS"] if askRow["EXCHANGE"].upper() != "NSDQ"]
# Recompute the TOTAL_VOLUME
askLevelVolume = 0
for askRow in askLevel["ASKS"]:
askLevelVolume += askRow["ASK_VOLUME"]
askLevel["TOTAL_VOLUME_ORIG"] = askLevel["TOTAL_VOLUME"]
askLevel["TOTAL_VOLUME"] = askLevelVolume
@vamirineni87
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment