Skip to content

Instantly share code, notes, and snippets.

@pbamotra
Created April 12, 2021 01:03
Show Gist options
  • Save pbamotra/583612be24388b434ddc7c999548b7fb to your computer and use it in GitHub Desktop.
Save pbamotra/583612be24388b434ddc7c999548b7fb to your computer and use it in GitHub Desktop.
Filter good and affordable calls
# Execute this in a Jupyter notebook
import os
import json
import base64
import pandas as pd
from pprint import pprint
# Import USD 10B+, 1MM vol+, 25+ P/E, Buy/Strong Buy rated
buy_rated_tradingview = pd.read_csv('~/Downloads/america_2021-04-11.csv')
buy_rated_tradingview.head()
# Rate limit is 120 queries per min
tickers = buy_rated_tradingview.Ticker.values
len(tickers)
# Get API key at - https://developer(dot)tdameritrade(dot)com
with open(os.path.expanduser('~/Documents/myapp.key'), "rt") as f:
pwd64 = f.readline().strip()
API_KEY=base64.b64decode(pwd64).decode('utf-8').strip()
for TICKER in tickers:
calls = !curl -sS -X GET --header "Authorization: " "https://api.tdameritrade.com/v1/marketdata/chains?apikey=$API_KEY&symbol=$TICKER&contractType=CALL&strategy=ANALYTICAL"
calls = calls[0]
calls = json.loads(calls)
for chain, options in calls['callExpDateMap'].items():
date_of_expiry, days_left = chain.split(':')
days_left = int(days_left)
to_print = {}
MAX_SPEND_PER_OPTION = 6 # USD, Since contracts are in 100x, you'll pay this amountx100 max
OPEN_INTEREST_THRESH = 5000
MIN_DAYS_TO_EXP = 50
DELTA_THRESH = 0 # 0.4
for price, data in options.items():
data = data[0]
price = float(price)
# → Refer: https://www.optionsplaybook.com/options-introduction/option-greeks/
# That’s the amount an option value will change in theory based on a
# one percentage-point change in interest rates.
rho = float(data['rho'])
# sensitivity of the price of an option to changes in volatility
vega = float(data['vega'])
# the dollar amount an option will lose each day due to the passage of time
theta = float(data['theta'])
# [-1, 1] indicates how much the value of an option should change
# when the price of the underlying stock rises by one dollar.
delta = float(data['delta'])
# Gamma measures the rate of change in the delta
gamma = float(data['gamma'])
bid = float(data['bid'])
ask = float(data['ask'])
tval = float(data['theoreticalOptionValue'])
tvol = float(data['theoreticalVolatility'])
act = (bid+ask)/2.
time = float(data['timeValue'])
volat = float(data['volatility'])
vol = int(data['totalVolume'])
is_itm = 'I' if bool(data['inTheMoney']) else 'O'
openint = int(data['openInterest'])
# (r){rho:.3f}
greeks = f'(v){vega:.3f}\t(t){theta:.3f}\t(g){gamma:.3f}\t(d){delta:.3f}'
if (openint > OPEN_INTEREST_THRESH) \
and (tval > act) \
and (act < MAX_SPEND_PER_OPTION) \
and (days_left > MIN_DAYS_TO_EXP) \
and (delta > DELTA_THRESH):
to_print[price] = f"{price:.2f}({tval:.2f}, {act:.2f}, {is_itm})\t{greeks}\t{volat}\t{openint}({vol})"
if to_print:
print(TICKER)
print(date_of_expiry, days_left)
print('-'*len(chain))
for k, v in to_print.items():
print(v)
print()
print('-x-\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment