Skip to content

Instantly share code, notes, and snippets.

@crapher
crapher / call_butterflies.py
Created February 15, 2020 21:21
All butterflies available with 10 contracts or less
import yfinance as yf
import math
stock = yf.Ticker("AAPL") # Get ticker
expiration = stock.options[0] # Get next expiration date
opt = stock.option_chain(expiration) # Get the option chain
options = opt.calls # Get options
for idx_body, body in options.iterrows(): # Iterate over all options
left_wings = options[options['strike'] < body['strike']] # Get left wings
@crapher
crapher / simple_bot_framework.py
Created February 16, 2020 19:31
Simple Bot Framework
from yahoo_fin import stock_info as si
import time
# Settings
ticker = "MSFT" # Ticker to follow
portfolio_cash = 10000 # Starting cash
portfolio_stocks = 0 # Starting stocks
commisions = 0.005 # Commissions
# Function implemented
@crapher
crapher / put_butterflies.py
Created February 18, 2020 01:21
All available long butterfly put spreads
import yfinance as yf
import math
stock = yf.Ticker("MSFT") # Get ticker
expiration = stock.options[0] # Get next expiration date
opt = stock.option_chain(expiration) # Get the option chain
options = opt.puts # Get options
for idx_body, body in options.iterrows(): # Iterate over all options
left_wings = options[options['strike'] < body['strike']] # Get left wings
@crapher
crapher / call_condors.py
Created February 21, 2020 12:34
Long Call Condor Spreads
import yfinance as yf
import math
stock = yf.Ticker("XOM") # Get ticker
expiration = stock.options[0] # Get next expiration date
opt = stock.option_chain(expiration) # Get the option chain
options = opt.calls # Get options
for idx_left_body, left_body in options.iterrows(): # Iterate over all options
left_wings = options[options['strike'] < left_body['strike']] # Get left wings
@crapher
crapher / date_converter.py
Created March 7, 2020 17:43
Converting string date to python datetime
import re
from datetime import date
# Months Dictionary
MONTHS = {'enero': 1, 'febrero': 2, 'marzo': 3, 'abril': 4, 'mayo': 5, 'junio': 6, 'julio': 7, 'agosto': 8, 'septiembre': 9, 'octubre': 10, 'noviembre': 11, 'diciembre': 12}
# Function that converts thex date to python datetime
def get_date_from_text(text):
# Remove spaces to simplify the RegEx
@crapher
crapher / black_scholes_volatility.py
Created March 15, 2020 16:49
Black Scholes Volatility and Price calculation
from math import *
# Cumulative standard normal distribution
def cdf(x):
return (1.0 + erf(x / sqrt(2.0))) / 2.0
# Call Price based on Black Scholes Model
# Parameters
# underlying_price: Price of underlying asset
# exercise_price: Exercise price of the option
@crapher
crapher / bjerksund_stensland.py
Created March 23, 2020 00:26
Bjerksund Stensland Volatility and Price calculation
from math import *
# Cumulative standard normal distribution
def cdf(x):
return (1.0 + erf(x / sqrt(2.0))) / 2.0
# Intermediate calculation used by both the Bjerksund Stensland 1993 and 2002 approximations
def phi(s, t, gamma, h, i, r, a, v):
lambda1 = (-r + gamma * a + 0.5 * gamma * (gamma - 1) * v**2) * t
dd = -(log(s / h) + (a + (gamma - 0.5) * v**2) * t) / (v * sqrt(t))