From 6d1a7fd28b3a279369e2b277abdd7a069aa1afaa Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 23:48:24 +0100 Subject: [PATCH] first running graph w plotly --- gamestonk_terminal/options/op_menu.py | 39 ++++++----------- gamestonk_terminal/options/volume.py | 61 +++++++++++++++++++-------- terminal.py | 7 ++- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index a223852f82c..60595320c63 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -2,30 +2,12 @@ import argparse import requests from gamestonk_terminal import config_terminal as cfg +from gamestonk_terminal import feature_flags as gtff from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn -from gamestonk_terminal.options import get_volume_graph - - -def print_volume(l_args, s_ticker): - """ Show traded options volume. [Source: Yahoo Finance] """ - parser = argparse.ArgumentParser( - add_help=False, - prog="volume", - description="""Display volume graph for a date. [Source: Yahoo Finance].""", - ) - - l_similar = [] - try: - ns_parser = parse_known_args_and_warn(parser, l_args) - if not ns_parser: - return - - except Exception as e: - print(e) - - print("") - return get_volume_graph(s_ticker, expiration_date) +from gamestonk_terminal.options import volume as vol +from gamestonk_terminal.menu import session +from prompt_toolkit.completion import NestedCompleter def opt_menu(df_stock, s_ticker, s_start, s_interval): @@ -40,7 +22,7 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): opt_parser.add_argument("cmd", choices=choices) completer = NestedCompleter.from_nested_dict({c: None for c in choices}) - print_technical_analysis(s_ticker, s_start, s_interval) + #print_options(s_ticker, s_start, s_interval) # Loop forever and ever while True: @@ -53,8 +35,6 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): else: as_input = input(f"{get_flair()} (opt)> ") - # Images are non blocking - allows to close them if we type other command - plt.close() # Parse fundamental analysis command of the list of possible commands try: @@ -65,15 +45,20 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): continue if ns_known_args.cmd == "help": - print_technical_analysis(s_ticker, s_start, s_interval) + pass + #print_options(s_ticker, s_start, s_interval) elif ns_known_args.cmd == "q": - # Just leave the FA menu + # Just leave the options menu return False elif ns_known_args.cmd == "quit": # Abandon the program return True + elif ns_known_args.cmd == "volume": + # call the volume graph + vol.volume_graph(l_args, s_ticker) + else: print("Command not recognized!") diff --git a/gamestonk_terminal/options/volume.py b/gamestonk_terminal/options/volume.py index 5731a36964e..e539ff060ec 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -3,29 +3,56 @@ import seaborn as sns import pandas as pd import matplotlib.pyplot as plt import plotly.express as px +import argparse +import datetime +from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn, valid_date -def get_volume_graph(ticker_name, exp_date): - opt = yf.Ticker(ticker_name).option_chain(exp_date) - # data available via: opt.calls, opt.puts - - volume_data = pd.concat( - [ - __volume_data(opt.calls, 'calls'), - __volume_data(opt.puts, 'puts') - ], - axis=0 +def volume_graph(l_args, s_ticker): + """ Show traded options volume. [Source: Yahoo Finance] """ + parser = argparse.ArgumentParser( + add_help=False, + prog="volume", + description="""Display volume graph for a date. [Source: Yahoo Finance].""", ) - fig = px.line( - volume_data, - x="strike", - y="volume", - title=f'{ticker_name} Volume for {exp_date}', - color='type' + parser.add_argument( + "-e", + "--expiry", + type=valid_date, + dest="s_expiry_date", + help="The expiry date (format YYYY-MM-DD) for the option chain", + required=True ) - fig.show() + l_similar = [] + try: + ns_parser = parse_known_args_and_warn(parser, l_args) + if not ns_parser: + return + opt = yf.Ticker(s_ticker).option_chain(ns_parser.s_expiry_date.strftime("%Y-%m-%d")) + # data available via: opt.calls, opt.puts + + volume_data = pd.concat( + [ + __volume_data(opt.calls, 'calls'), + __volume_data(opt.puts, 'puts') + ], + axis=0 + ) + fig = px.line( + volume_data, + x="strike", + y="volume", + title=f'{s_ticker} Volume for {ns_parser.s_expiry_date.strftime("%Y-%m-%d")}', + color='type' + ) + fig.show() + + except SystemExit: + print("") + except Exception as e: + print(e) def __volume_data(opt_data, flag): # get option chain for specific expiration diff --git a/terminal.py b/terminal.py index e30805c294c..1f34f27ba63 100644 --- a/terminal.py +++ b/terminal.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python + #!/usr/bin/env python import argparse @@ -20,6 +20,7 @@ from gamestonk_terminal.papermill import papermill_menu as mill from gamestonk_terminal.sentiment import sen_menu as sm from gamestonk_terminal.technical_analysis import ta_menu as tam from gamestonk_terminal.comparison_analysis import ca_menu as cam +from gamestonk_terminal.options import op_menu as opm # import warnings # warnings.simplefilter("always") @@ -65,6 +66,7 @@ def main(): "dd", "pred", "ca", + "op" ] menu_parser.add_argument("opt", choices=choices) completer = NestedCompleter.from_nested_dict({c: None for c in choices}) @@ -155,6 +157,9 @@ def main(): elif ns_known_args.opt == "dd": b_quit = ddm.dd_menu(df_stock, s_ticker, s_start, s_interval) + elif ns_known_args.opt == "op": + b_quit = opm.opt_menu(df_stock, s_ticker, s_start, s_interval) + elif ns_known_args.opt == "pred": if not gtff.ENABLE_PREDICT: