66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
import yfinance as yf
|
|
import math as maths
|
|
|
|
|
|
def stock_exists(ticker):
|
|
try:
|
|
return yf.Ticker(ticker).info
|
|
except KeyError:
|
|
return False
|
|
|
|
|
|
def stock_info(ticker):
|
|
if not (stock := stock_exists(ticker)):
|
|
return False
|
|
|
|
return stock
|
|
|
|
|
|
def stock_history(ticker, timespan="6mo", interval="1d"):
|
|
if not stock_exists(ticker):
|
|
return False
|
|
|
|
return yf.Ticker(ticker).history("6mo", interval="1d", prepost=True)
|
|
|
|
|
|
def get_current_price(ticker):
|
|
yf_obj = yf.Ticker(ticker)
|
|
todays_data = yf_obj.history(period="1d")
|
|
return round(todays_data["Close"][0], 2)
|
|
|
|
|
|
def get_delta(ticker, return_old_price=False):
|
|
yf_obj = yf.Ticker(ticker)
|
|
old_price = yf_obj.history(interval="1d")["Close"][-2]
|
|
current_price = get_current_price(ticker)
|
|
delta = 100 * ((current_price / old_price) - 1)
|
|
if return_old_price:
|
|
return delta, old_price
|
|
else:
|
|
return delta
|
|
|
|
|
|
def get_delta_old_price(ticker):
|
|
yf_obj = yf.Ticker(ticker)
|
|
return yf_obj.history(interval="1d")["Close"][-2]
|
|
|
|
|
|
def options_chain(stock, money, exp_price):
|
|
stock_data = yf.Ticker(stock)
|
|
strike_date = stock_data.options[0]
|
|
calls_chain_raw = stock_data.option_chain(date=strike_date)
|
|
|
|
calls_chain = []
|
|
for i in range(50):
|
|
calls_chain.append((calls_chain_raw.calls.strike[i], calls_chain_raw.calls.ask[i]))
|
|
|
|
prices = []
|
|
best_price = (0, 0)
|
|
for strike in calls_chain:
|
|
options_count = maths.floor( ( money / (( strike[1] * 100) or 1 ) ) )
|
|
profits_per_contract = ((exp_price - strike[0]) * 100) - (strike[1] * 100)
|
|
profits = profits_per_contract * options_count
|
|
prices.append( ( strike[0], profits ) )
|
|
if profits > best_price[1]:
|
|
best_price = (strike[0], profits)
|
|
return best_price, prices
|