Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Last active May 29, 2024 08:16
Show Gist options
  • Save dharmatech/5af8071ae7501e54f849b61381810e6b to your computer and use it in GitHub Desktop.
Save dharmatech/5af8071ae7501e54f849b61381810e6b to your computer and use it in GitHub Desktop.
import io
import requests
import pandas as pd
def trade_quote(symbol, expiration, date):
url = "http://127.0.0.1:25510/v2/bulk_hist/option/trade_quote"
querystring = { "root": symbol, "exp": expiration, "start_date": date, "end_date": date, "use_csv":"true" }
headers = {"Accept": "application/json"}
response = requests.get(url, headers=headers, params=querystring)
df = pd.read_csv(io.StringIO(response.content.decode('utf-8')))
return df
# ----------------------------------------------------------------------
# Get all expirations for date
def get_all_expirations(date, symbol):
url = "http://127.0.0.1:25510/v2/list/expirations"
querystring = { "root": symbol }
headers = {"Accept": "application/json"}
response = requests.get(url, headers=headers, params=querystring)
expirations = pd.DataFrame(response.json()['response'], columns=response.json()['header']['format'])
expirations_ = expirations[expirations['date'] >= int(date)]
return expirations_
# tmp = get_all_expirations('20240517', 'TSLA')
# tmp.head(10)
# ----------------------------------------------------------------------
# Get all trades for date, symbol.
def get_all_trades(date, symbol):
print(f'Retrieving trades for {date} and {symbol}')
expirations = get_all_expirations(date, symbol)
dfs = []
for expiration in expirations['date']:
print(f' Retrieving data for expiration {expiration}')
dfs.append(trade_quote(symbol, str(expiration), date))
result = pd.concat(dfs)
return result
# ----------------------------------------------------------------------
# Get 'ATMU' trades for '20240321'
df = get_all_trades('20240321', 'ATMU')
df['premium'] = df['size'] * df['price'] * 100
columns_to_drop = ['No data for the specified timeframe and chain. Debug code 1', 'ext_condition1', 'ext_condition2', 'ext_condition3', 'ext_condition4', 'ask_condition', 'bid_condition', 'records_back', 'condition_flags', 'volume_type' ]
df = df.drop(columns=columns_to_drop)
df[df['premium'] > 10000]
>>> df[df['premium'] > 10000]
root expiration strike right ms_of_day sequence condition size exchange price price_flags ms_of_day2 bid_size bid_exchange bid ask_size ask_exchange ask date premium
5 ATMU 20240419.0 25000.0 C 36663211.0 1.218486e+09 129.0 1006.0 11.0 3.50 3.0 36646965.0 87.0 4.0 3.30 3.0 47.0 4.00 20240321.0 352100.0
157 ATMU 20240419.0 30000.0 C 43123505.0 1.713976e+09 132.0 2000.0 4.0 0.64 1.0 43122241.0 4.0 7.0 0.35 1.0 73.0 0.70 20240321.0 128000.0
405 ATMU 20240419.0 30000.0 C 43543497.0 1.743029e+09 18.0 283.0 6.0 0.70 1.0 43543497.0 11.0 9.0 0.65 1.0 60.0 0.70 20240321.0 19810.0
545 ATMU 20240419.0 30000.0 P 43123505.0 1.713976e+09 132.0 2000.0 4.0 1.64 5.0 43123505.0 229.0 4.0 1.60 4.0 47.0 1.90 20240321.0 328000.0
0 ATMU 20240719.0 20000.0 C 42082996.0 1.646707e+09 130.0 24.0 6.0 9.47 7.0 42072693.0 20.0 9.0 6.90 30.0 4.0 10.90 20240321.0 22728.0
2 ATMU 20241018.0 30000.0 C 37863841.0 1.326942e+09 133.0 10000.0 7.0 2.65 1.0 37858866.0 3.0 11.0 1.80 23.0 11.0 4.30 20240321.0 2650000.0
5 ATMU 20241018.0 35000.0 C 37863842.0 1.326942e+09 133.0 10000.0 7.0 1.00 7.0 37863135.0 1.0 1.0 0.30 30.0 9.0 1.85 20240321.0 1000000.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment