Skip to content

Instantly share code, notes, and snippets.

@intrinio-gists
Last active June 29, 2023 07:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save intrinio-gists/1108c38a0a2dda3ac7ed2d8b5716661f to your computer and use it in GitHub Desktop.
Save intrinio-gists/1108c38a0a2dda3ac7ed2d8b5716661f to your computer and use it in GitHub Desktop.
Complete Walkthrough Code For Options Price Forecast
import requests
import numpy as np
import pandas as pd
from datetime import datetime, date
api_key = "YOUR_INTRINIO_API_KEY_HERE"
def _latest_stock_price(identifier, source="intrinio_mx"):
res = requests.get(f"https://api-v2.intrinio.com/securities/{identifier}/prices/realtime?source={source}&api_key={api_key}").json()
latest_stock_price = res.get("last_price")
return latest_stock_price
def _option_expirations_list(identifier, after_date=date.today()):
res = requests.get(f"https://api-v2.intrinio.com/options/expirations/{identifier}?after={after_date}&api_key={api_key}").json()
expirations_list = list(res.get("expirations"))
return expirations_list[::-1]
def _option_strike_price_and_implied_volatility(identifier, option_expiration_date):
latest_stock_price = _latest_stock_price(identifier)
option_strike_price, implied_volatility = None, None
try:
res = requests.get(f"https://api-v2.intrinio.com/options/chain/{identifier}/{option_expiration_date}/realtime", params = {
"source": "delayed",
"type": "call",
"api_key": api_key,
}).json()
# Create Options DF
options_chain = res.get("chain")
options_dataset = pd.json_normalize(options_chain)
# Filter For Near The Money Option Strike Info
near_the_money_option = options_dataset.iloc[(options_dataset["option.strike"]-latest_stock_price).abs().argsort()[:2]].iloc[0]
# Grab Required Stats
implied_volatility = near_the_money_option["stats.implied_volatility"]
option_strike_price = near_the_money_option["option.strike"]
return option_strike_price, implied_volatility
except KeyError:
return option_strike_price, implied_volatility
def _stock_standard_deviation_range(option_strike_price, implied_volatility, option_expiration_date):
upper_range, lower_range = None, None
if option_strike_price or implied_volatility != None:
days_in_year = 365
option_expiration_datetime = datetime.strptime(option_expiration_date, "%Y-%m-%d").date()
days_until_expiration = (option_expiration_datetime - date.today()).days
days_expo_ratio = days_until_expiration / days_in_year
days_expo_ratio_sqrt = np.sqrt(days_expo_ratio)
std_dev_price_range_by_expo = option_strike_price * implied_volatility * days_expo_ratio_sqrt
std_dev_price_range_by_expo = round(std_dev_price_range_by_expo, 2)
upper_range = round((option_strike_price + std_dev_price_range_by_expo), 2)
lower_range = round((option_strike_price - std_dev_price_range_by_expo), 2)
return upper_range, lower_range
def _options_forecast_dataset(identifier):
options_forecast_data_list = []
expirations_list = _option_expirations_list(identifier)
for expiration_date in expirations_list:
option_strike_price, implied_volatility = _option_strike_price_and_implied_volatility(identifier, expiration_date)
upper_range, lower_range = _stock_standard_deviation_range(option_strike_price, implied_volatility, expiration_date)
if upper_range and lower_range != None:
options_forecast_data_list.append({
"expiration": expiration_date,
"upper_range": upper_range,
"lower_range": lower_range,
})
options_forecast_dataset = pd.DataFrame(options_forecast_data_list)
options_forecast_dataset = options_forecast_dataset.loc[
(options_forecast_dataset["upper_range"] >= 0 ) & (options_forecast_dataset["lower_range"] >= 0)]
return options_forecast_dataset
options_forecast_dataset = _options_forecast_dataset("AAPL")
options_forecast_dataset = _options_forecast_dataset("TSLA")
# Latest Price for AAPL on 2022-04-08 is $169.95
# 0 2022-04-14 176.29 163.71
# 1 2022-04-22 178.93 161.07
# 2 2022-04-29 182.96 157.04
# 3 2022-05-06 184.67 155.33
# 4 2022-05-13 186.01 153.99
# 5 2022-05-20 187.47 152.53
# 6 2022-05-27 188.88 151.12
# 7 2022-06-17 192.46 147.54
# 8 2022-07-15 196.44 143.56
# 9 2022-08-19 202.22 137.78
# 10 2022-09-16 205.46 134.54
# 11 2022-10-21 209.48 130.52
# 12 2022-11-18 212.83 127.17
# 13 2023-01-20 218.54 121.46
# 14 2023-03-17 224.56 115.44
# 15 2023-06-16 233.06 106.94
# 16 2023-09-15 241.05 98.95
# 17 2024-01-19 249.91 90.09
# 18 2024-06-21 261.83 78.17
# Latest Price for TSLA on 2022-04-08 is $1,032.55
# expiration upper_range lower_range
# 0 2022-04-14 1106.94 963.06
# 1 2022-04-22 1165.11 904.89
# 2 2022-04-29 1187.58 882.42
# 3 2022-05-06 1208.82 861.18
# 4 2022-05-13 1226.35 843.65
# 5 2022-05-20 1231.18 818.82
# 6 2022-05-27 1257.07 802.93
# 7 2022-06-17 1287.11 762.89
# 8 2022-07-15 1335.09 714.91
# 9 2022-08-19 1427.98 672.02
# 10 2022-09-16 1445.61 624.39
# 11 2022-10-21 1513.49 586.51
# 12 2022-11-18 1550.70 549.30
# 13 2022-12-16 1586.13 513.87
# 14 2023-01-20 1587.55 462.45
# 15 2023-03-17 1685.35 414.65
# 16 2023-06-16 1770.44 329.56
# 17 2023-09-15 1807.89 242.11
# 18 2024-01-19 1908.67 151.33
# 19 2024-06-21 2020.27 39.73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment