Files
OpenBB/discordbot/stocks/options/hist.py
teh_coderer 445ec3256c Discordbot/expiry reactions (#1114)
* added load extension for options menu

* options menu: oi, iv, unusual options

* Update opt_menu.py

* Update unu.py

if no num requested set default to 10

* Update opt_menu.py

+ !oi ticker expiry request added without inputting date format. accepts 0-9 up to 10 weeks
+ added option volume plot (works same as oi)

* Create vol.py

* Create reaction_helper.py

* Cleaned up, added historical options

Cleaned up
+ added historical options data
+ added expiry reaction helper function

* Create hist.py

* cleanup

* added min/max strike price

* cleanup

* cleanup, added min/max strike price

* Update opt_menu.py

* added yfinance if no tradier token

* Update vol.py

* Update opt_menu.py

* Create oi_tradier.py

* Update oi.py

* Update opt_menu.py

* Update oi.py

* Update opt_menu.py

* Update oi.py

* Update iv.py

* Update unu.py

* Update oi_tradier.py

* Update hist.py

* Update opt_menu.py

* Update reaction_helper.py

* Create options_menu.py

* Update run_discordbot.py

* Update vol.py

* Rename discordbot/options_menu.py to discordbot/stocks/options/options_menu.py

* Delete opt_menu.py

* Update oi.py

+ min strike/max strike

* Delete oi_tradier.py

* Update options_menu.py

* Update hist.py

* Update vol.py

* Update oi.py

* Update options_menu.py

* Update options_menu.py

* Update options_menu.py

* Linting/Pre-Commit Fixes

* linting

* Linting

* linting

* pylint fixes

* Update iv.py

* Update options_menu.py

* Update reaction_helper.py

* Improved Code

- removed 148 lines same function

* Update run_discordbot.py

* Update reaction_helper.py

* Updated Options Menu

+ reaction expiry lookup
+ added unu, iv, oi, vol ,hist

* mypy fix

* msg delete delay

* Update options_menu.py

* fix

seems bot doesn't like when last emoji is empty.
added an X when strike c/p empty

* Update opt_cmd.py

* Update options_menu.py

* better handling

* Update options_menu.py

* Update opt_cmd.py

* fix stocks.opt indention

* hist.py token/error handling

* Update hist.py

* Update options_menu.py

* Update hist.py
2022-01-01 17:26:42 +00:00

130 lines
3.8 KiB
Python

import os
import asyncio
import discord
import mplfinance as mpf
from gamestonk_terminal.config_terminal import TRADIER_TOKEN
from gamestonk_terminal.stocks.options import tradier_model
import discordbot.config_discordbot as cfg
async def hist_command(
ctx, ticker: str = None, expiry: str = "", strike: float = None, put: bool = False
):
"""Plot historical option prices
Parameters
----------
ticker: str
Stock ticker
expiry: str
accepts 0-9
0 being weeklies
1+ for weeks out
prompts reaction helper if empty
strike: float
Option strike price
put: bool
c for call
p for put
"""
async with ctx.typing():
await asyncio.sleep(0.2)
try:
# Debug
if cfg.DEBUG:
print(f"!stocks.opt.hist {ticker} {strike} {put} {expiry}")
error = ""
if TRADIER_TOKEN == "REPLACE_ME":
raise Exception("Tradier Token is required")
# Check for argument
if ticker is None:
raise Exception("Stock ticker is required")
if strike is None or put == "":
raise Exception(
'A strike and c/p is required\n```bash\n"!stocks.opt.hist {ticker} {strike} {c/p}"```'
)
chain_id = None
df_hist = tradier_model.get_historical_options(
ticker, expiry, strike, put, chain_id
)
op_type = ["call", "put"][put]
mc = mpf.make_marketcolors(
up="green",
down="red",
edge="black",
wick="black",
volume="in",
ohlc="i",
)
s = mpf.make_mpf_style(
base_mpl_style="seaborn",
marketcolors=mc,
gridstyle=":",
y_on_right=True,
)
mpf.plot(
df_hist,
type="candle",
volume=True,
title=f"\n{ticker.upper()} {strike} {op_type} expiring {expiry} Historical",
tight_layout=True,
style=s,
figratio=(10, 7),
figscale=1.10,
figsize=(12, 5),
update_width_config=dict(
candle_linewidth=1.0, candle_width=0.8, volume_linewidth=1.0
),
savefig="opt_hist.png",
)
imagefile = "opt_hist.png"
image = discord.File(imagefile)
if cfg.DEBUG:
print(f"Image: {imagefile}")
title = f"{ticker.upper()} {strike} {op_type} expiring {expiry} Historical"
embed = discord.Embed(title=title, colour=cfg.COLOR)
embed.set_image(url="attachment://opt_hist.png")
embed.set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
os.remove("opt_hist.png")
await ctx.send(embed=embed, file=image)
except TypeError:
error = (
'Invalid format.\n\nUse:\n```bash\n"!stocks.opt.hist {ticker} {strike} {c/p}"```'
'```bash\n"!stocks.opt {ticker} {expiration} {strike} {c/p}"```'
)
except Exception as e:
error = str(e)
finally:
if error != "":
embed = discord.Embed(
title="ERROR Options: History",
colour=cfg.COLOR,
description=error,
)
embed.set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
await ctx.send(embed=embed, delete_after=30.0)