Skip to content

Commit

Permalink
Merge pull request #342 from DidierRLopes/improve-options
Browse files Browse the repository at this point in the history
Refactor options menu
  • Loading branch information
DidierRLopes authored Apr 15, 2021
2 parents d4e63c5 + 1b16cd1 commit 5904c14
Show file tree
Hide file tree
Showing 4 changed files with 513 additions and 201 deletions.
71 changes: 47 additions & 24 deletions gamestonk_terminal/options/op_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@
from prompt_toolkit.completion import NestedCompleter
from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn
from gamestonk_terminal import feature_flags as gtff
from gamestonk_terminal.options import volume
from gamestonk_terminal.options import volume_view
from gamestonk_terminal.menu import session


class OptionsController:
"""Options Controller class."""

# Command choices
CHOICES = ["help", "q", "quit", "exp", "volume"]
CHOICES = ["help", "q", "quit", "exp", "voi", "vcalls", "vputs"]

def __init__(
self,
ticker: str,
):
def __init__(self, ticker: str, last_adj_close_price: float):
"""Construct data."""
self.ticker = ticker
self.raw_data_options = yf.Ticker(self.ticker)
self.expiry_date = self.raw_data_options.options[0]
self.yf_ticker_data = yf.Ticker(self.ticker)
self.expiry_date = self.yf_ticker_data.options[0]
self.options = self.yf_ticker_data.option_chain(self.expiry_date)
self.last_adj_close_price = last_adj_close_price
self.op_parser = argparse.ArgumentParser(add_help=False, prog="op")
self.op_parser.add_argument(
"cmd",
Expand All @@ -48,7 +47,7 @@ def expiry_dates(self, other_args: List[str]):
action="store",
type=int,
default=-1,
choices=range(len(self.raw_data_options.options)),
choices=range(len(self.yf_ticker_data.options)),
help=f"Expiry date index for {self.ticker}.",
)

Expand All @@ -64,12 +63,13 @@ def expiry_dates(self, other_args: List[str]):
# Print possible expiry dates
if ns_parser.n_date == -1:
print("\nAvailable expiry dates:")
for i, d in enumerate(self.raw_data_options.options):
for i, d in enumerate(self.yf_ticker_data.options):
print(f" {(2-len(str(i)))*' '}{i}. {d}")

# It means an expiry date was correctly selected
else:
self.expiry_date = self.raw_data_options.options[ns_parser.n_date]
self.expiry_date = self.yf_ticker_data.options[ns_parser.n_date]
self.options = self.yf_ticker_data.option_chain(self.expiry_date)
print(f"\nSelected expiry date : {self.expiry_date}")

except Exception as e:
Expand All @@ -89,7 +89,9 @@ def print_help(expiry_date):
print(f"Selected expiry date: {expiry_date}")
print("")
print(" exp see/set expiry date")
print(" volume plot options trading volume / open interest")
print(" voi volume + open interest options trading plot")
print(" vcalls calls volume + open interest plot")
print(" vputs puts volume + open interest plot")
print("")
return

Expand Down Expand Up @@ -121,26 +123,47 @@ def call_quit(self, _):
"""Process Quit command - quit the program."""
return True

def call_volume(self, _):
"""Process volume command."""
volume.volume_graph(
self.raw_data_options,
def call_exp(self, other_args: List[str]):
"""Process exp command."""
self.expiry_dates(self, other_args)

def call_voi(self, other_args: List[str]):
"""Process voi command."""
volume_view.plot_volume_open_interest(
other_args,
self.ticker,
self.expiry_date,
volume_percentile_threshold=60,
self.last_adj_close_price,
self.options.calls,
self.options.puts,
)
print("")

def call_exp(self, other_args: List[str]):
"""Process exp command."""
self.expiry_dates(self, other_args)
def call_vcalls(self, other_args: List[str]):
"""Process vcalls command."""
volume_view.plot_calls_volume_open_interest(
other_args,
self.ticker,
self.expiry_date,
self.last_adj_close_price,
self.options.calls,
)

def call_vputs(self, other_args: List[str]):
"""Process vcalls command."""
volume_view.plot_puts_volume_open_interest(
other_args,
self.ticker,
self.expiry_date,
self.last_adj_close_price,
self.options.puts,
)


def menu(ticker: str):
"""Options info Menu."""
def menu(ticker: str, last_adj_close_price: float):
""" Options Menu. """

try:
op_controller = OptionsController(ticker)
op_controller = OptionsController(ticker, last_adj_close_price)
op_controller.call_help(None)
except IndexError:
print("No options found for " + ticker)
Expand Down
176 changes: 0 additions & 176 deletions gamestonk_terminal/options/volume.py

This file was deleted.

Loading

0 comments on commit 5904c14

Please sign in to comment.