Options command
This commit is contained in:
parent
900f812621
commit
ddf97b03a0
2 changed files with 55 additions and 0 deletions
33
main.py
33
main.py
|
@ -216,4 +216,37 @@ async def watchlist(ctx, user: typing.Optional[discord.Member]):
|
||||||
image.close()
|
image.close()
|
||||||
|
|
||||||
|
|
||||||
|
@bot.command(aliases=["opt"])
|
||||||
|
async def options(ctx, stock, money, exp_price):
|
||||||
|
user = str(ctx.message.author)
|
||||||
|
stock = stock.upper()
|
||||||
|
try:
|
||||||
|
money = float(money)
|
||||||
|
exp_price = float(exp_price)
|
||||||
|
except ValueError:
|
||||||
|
return await ctx.send("amoutn not a number")
|
||||||
|
|
||||||
|
best_price, prices = yfi.options_chain(stock, money, exp_price)
|
||||||
|
|
||||||
|
output = inspect.cleandoc("```")
|
||||||
|
for price in prices:
|
||||||
|
if price[0] == best_price[0]:
|
||||||
|
output = (
|
||||||
|
output
|
||||||
|
+ "\n"
|
||||||
|
+ "* Buy strike ${} to make ${:.2f}".format(price[0], price[1])
|
||||||
|
)
|
||||||
|
elif price[1] < -5000:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
output = (
|
||||||
|
output
|
||||||
|
+ "\n"
|
||||||
|
+ " Buy strike ${} to make ${:.2f}".format(price[0], price[1])
|
||||||
|
)
|
||||||
|
output = output + "```"
|
||||||
|
print(len(output))
|
||||||
|
return await ctx.send(output)
|
||||||
|
|
||||||
|
|
||||||
bot.run(cfg.api_key)
|
bot.run(cfg.api_key)
|
||||||
|
|
22
yfi.py
22
yfi.py
|
@ -1,4 +1,5 @@
|
||||||
import yfinance as yf
|
import yfinance as yf
|
||||||
|
import math as maths
|
||||||
|
|
||||||
|
|
||||||
def stock_exists(ticker):
|
def stock_exists(ticker):
|
||||||
|
@ -42,3 +43,24 @@ def get_delta(ticker, return_old_price=False):
|
||||||
def get_delta_old_price(ticker):
|
def get_delta_old_price(ticker):
|
||||||
yf_obj = yf.Ticker(ticker)
|
yf_obj = yf.Ticker(ticker)
|
||||||
return yf_obj.history(interval="1d")["Close"][-2]
|
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue