From 077162a275e83f29a2929ce285b1fca1d0e0319f Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Mon, 15 Mar 2021 12:10:03 +0100 Subject: [PATCH 01/20] removed rogue GME ticker in cell --- notebooks/templates/due_diligence.ipynb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/notebooks/templates/due_diligence.ipynb b/notebooks/templates/due_diligence.ipynb index 139a074132b..8d6a30e8fd1 100644 --- a/notebooks/templates/due_diligence.ipynb +++ b/notebooks/templates/due_diligence.ipynb @@ -35,9 +35,7 @@ ] }, "outputs": [], - "source": [ - "ticker=\"GME\"" - ] + "source": [] }, { "cell_type": "code", From 3edd8e3cc70dd676500187778aac290f0314fb4e Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 14:09:59 +0100 Subject: [PATCH 02/20] removed unused cell w "datetime.datetime.now()" --- notebooks/templates/due_diligence.ipynb | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/notebooks/templates/due_diligence.ipynb b/notebooks/templates/due_diligence.ipynb index 8d6a30e8fd1..bfaa7aeb4cc 100644 --- a/notebooks/templates/due_diligence.ipynb +++ b/notebooks/templates/due_diligence.ipynb @@ -15,16 +15,6 @@ " sys.path.append(module_path)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "exact-price", - "metadata": {}, - "outputs": [], - "source": [ - "datetime.datetime.now()" - ] - }, { "cell_type": "code", "execution_count": null, From c5888c6cc488591e42c87c3a4750fef264457a49 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 15:02:12 +0100 Subject: [PATCH 03/20] added options features to roadmap --- ROADMAP.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ROADMAP.md b/ROADMAP.md index 74e5d1e9828..7e37722daa4 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -19,7 +19,7 @@ * [Frontend](#Frontend) * [Developer Experience](#Developer-Experience) * [User Experience](#User-Experience) - +* [Options](#Options) --- ## Discovery @@ -203,3 +203,17 @@ ### Long term - [ ] Add a WHL installation + + +--- + +## Options + +### Short term + +- [ ] Add Volume graph +- [ ] Add Open Interest graph + +### Long term + +- [ ] Add max pain graph From b9254093987e7d9c1bad7a67770a0c097c72b293 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 15:02:44 +0100 Subject: [PATCH 04/20] assigned options @lolrenx --- ROADMAP.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 7e37722daa4..170d2fba538 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -211,8 +211,8 @@ ### Short term -- [ ] Add Volume graph -- [ ] Add Open Interest graph +- [ ] Add Volume graph (@lolrenx) +- [ ] Add Open Interest graph (@lolrenx) ### Long term From 9320436f14503d6713e04f60126ed19ecf7907d0 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 18:30:11 +0100 Subject: [PATCH 05/20] added options readme --- gamestonk_terminal/options/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 gamestonk_terminal/options/README.md diff --git a/gamestonk_terminal/options/README.md b/gamestonk_terminal/options/README.md new file mode 100644 index 00000000000..da19584da2a --- /dev/null +++ b/gamestonk_terminal/options/README.md @@ -0,0 +1,18 @@ +# OPTIONS DATA + +This menu aims to give insight on options. options can cause significant share price movement, and option pricing gives info about market sentiment for a ticker. + +* [volume](#volume) + * Show traded options for a given expiry [Yahoo Finance] + + + +## volume + +```text +usage: volume -d 'yyyy-mm-dd' +``` + +Display volume graph for a date. [Source: Yahoo Finance] + +* -d : expiration date. From e52e5d43dcf45c18aa5395cdebce8f45cb5c975a Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 20:58:53 +0100 Subject: [PATCH 06/20] generate options volume graph for ticker + expiration date --- gamestonk_terminal/options/volume.py | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 gamestonk_terminal/options/volume.py diff --git a/gamestonk_terminal/options/volume.py b/gamestonk_terminal/options/volume.py new file mode 100644 index 00000000000..5731a36964e --- /dev/null +++ b/gamestonk_terminal/options/volume.py @@ -0,0 +1,39 @@ +import yfinance as yf +import seaborn as sns +import pandas as pd +import matplotlib.pyplot as plt +import plotly.express as px + + +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 + ) + fig = px.line( + volume_data, + x="strike", + y="volume", + title=f'{ticker_name} Volume for {exp_date}', + color='type' + ) + fig.show() + + +def __volume_data(opt_data, flag): + # get option chain for specific expiration + df = opt_data.pivot_table( + index='strike', + values=['volume', 'openInterest'], + aggfunc='sum') + df.reindex() + df['strike'] = df.index + df['type'] = flag + return df From ad7d546235df6bc9284debdbed62019406c8ec48 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 20:59:53 +0100 Subject: [PATCH 07/20] created options submenu --- gamestonk_terminal/options/option_menu.py | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 gamestonk_terminal/options/option_menu.py diff --git a/gamestonk_terminal/options/option_menu.py b/gamestonk_terminal/options/option_menu.py new file mode 100644 index 00000000000..407b2b64090 --- /dev/null +++ b/gamestonk_terminal/options/option_menu.py @@ -0,0 +1,77 @@ +import argparse +import requests + +from gamestonk_terminal import config_terminal as cfg +from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn +from gamestonk_terminal.options import get_volume_graph + +def 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 l_similar + +def opt_menu(df_stock, s_ticker, s_start, s_interval): + + # Add list of arguments that the options parser accepts + opt_parser = argparse.ArgumentParser(prog="opt", add_help=False) + choices = [ + "help", + "q", + "quit", + "volume" + ] + 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) + + # Loop forever and ever + while True: + # Get input command from user + if session and gtff.USE_PROMPT_TOOLKIT: + as_input = session.prompt( + f"{get_flair()} (opt)> ", + completer=completer, + ) + 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: + (ns_known_args, l_args) = opt_parser.parse_known_args(as_input.split()) + + except SystemExit: + print("The command selected doesn't exist\n") + continue + + if ns_known_args.cmd == "help": + print_technical_analysis(s_ticker, s_start, s_interval) + + elif ns_known_args.cmd == "q": + # Just leave the FA menu + return False + + elif ns_known_args.cmd == "quit": + # Abandon the program + return True + + else: + print("Command not recognized!") From 31f7d2b269d3338875c6a125d8e8341c57b85e59 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 21:05:25 +0100 Subject: [PATCH 08/20] renamed options menu --- gamestonk_terminal/options/{option_menu.py => op_menu.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename gamestonk_terminal/options/{option_menu.py => op_menu.py} (100%) diff --git a/gamestonk_terminal/options/option_menu.py b/gamestonk_terminal/options/op_menu.py similarity index 100% rename from gamestonk_terminal/options/option_menu.py rename to gamestonk_terminal/options/op_menu.py From c657c602010a199d1379abb87cc0ec134021f200 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 21:06:15 +0100 Subject: [PATCH 09/20] renamed main function --- gamestonk_terminal/options/op_menu.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index 407b2b64090..a223852f82c 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -5,7 +5,8 @@ from gamestonk_terminal import config_terminal as cfg from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn from gamestonk_terminal.options import get_volume_graph -def volume(l_args, s_ticker): + +def print_volume(l_args, s_ticker): """ Show traded options volume. [Source: Yahoo Finance] """ parser = argparse.ArgumentParser( add_help=False, @@ -23,7 +24,8 @@ def volume(l_args, s_ticker): print(e) print("") - return l_similar + return get_volume_graph(s_ticker, expiration_date) + def opt_menu(df_stock, s_ticker, s_start, s_interval): From 6d1a7fd28b3a279369e2b277abdd7a069aa1afaa Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 23:48:24 +0100 Subject: [PATCH 10/20] 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: From 289195fbe9b7a430ee88bb7c8f343113b3db3454 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 23:55:28 +0100 Subject: [PATCH 11/20] cleanup --- gamestonk_terminal/options/op_menu.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index 60595320c63..e6358421538 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -1,13 +1,10 @@ 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 volume as vol -from gamestonk_terminal.menu import session from prompt_toolkit.completion import NestedCompleter +from gamestonk_terminal import feature_flags as gtff +from gamestonk_terminal.helper_funcs import get_flair +from gamestonk_terminal.options import volume as vol +from gamestonk_terminal.menu import session def opt_menu(df_stock, s_ticker, s_start, s_interval): From 5db13087771b2b6483f192d8efb17d5067d6f3a1 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 11:29:09 +0100 Subject: [PATCH 12/20] cleaned up data generation graph w seaborn ok graph w plotly ready (pending) --- gamestonk_terminal/options/volume.py | 88 +++++++++++++++++++++------- 1 file changed, 66 insertions(+), 22 deletions(-) diff --git a/gamestonk_terminal/options/volume.py b/gamestonk_terminal/options/volume.py index e539ff060ec..14d71ced9ab 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -30,37 +30,81 @@ def volume_graph(l_args, s_ticker): 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() + __get_volume_graph(s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) except SystemExit: print("") except Exception as e: print(e) -def __volume_data(opt_data, flag): - # get option chain for specific expiration - df = opt_data.pivot_table( + + +def __get_volume_graph(ticker_name, exp_date): + df = __get_volume_data(ticker_name, exp_date) + __generate_graph_sns(df, ticker_name, exp_date) + #__generate_graph_plotly(df, ticker_name, exp_date) + +def __pull_call_put_data(call_put, flag): + df = call_put.pivot_table( index='strike', - values=['volume', 'openInterest'], + values = ['volume','openInterest'], aggfunc='sum') + df.reindex() + df['strike'] = df.index df['type'] = flag + return df + +def __get_volume_data(ticker_name, exp_date): + + option_chain = yf.Ticker(ticker_name).option_chain(exp_date) + + calls = __pull_call_put_data( + option_chain.calls, + 'calls' + ) + + puts = __pull_call_put_data( + option_chain.puts, + 'puts' + ) + + volume_data = pd.concat( + [ + calls, + puts + ], + axis = 0 + ) + #dataframe + return volume_data + +def __generate_graph_plotly(df, ticker_name, exp_date): + #version with plotly express + fig = px.line( + df, + x="strike", + y="volume", + title=f'{ticker_name} options volume for {exp_date}', + color= 'type' + ) + fig.show() + + return + +def __generate_graph_sns(df, ticker_name, exp_date): + #version with seaborn express + plt.figure(figsize=(12,6)) + fig = sns.lineplot( + data = df, + x = 'strike', + y = 'volume', + hue = 'type', + palette= ['limegreen', 'tomato']) + + plt.title(f'{ticker_name} options volume for {exp_date}') + + plt.show() + return From 91ba7681056ec928495bb82141574bd58935b00f Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 15:11:16 +0100 Subject: [PATCH 13/20] cleanup added option help main menu --- gamestonk_terminal/main_helper.py | 3 +++ gamestonk_terminal/options/op_menu.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/gamestonk_terminal/main_helper.py b/gamestonk_terminal/main_helper.py index 065b77325fd..143895e1e2b 100644 --- a/gamestonk_terminal/main_helper.py +++ b/gamestonk_terminal/main_helper.py @@ -71,6 +71,9 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): print( " pred prediction techniques, \t e.g.: regression, arima, rnn, lstm, prophet" ) + print( + " op options info, \t e.g.: volume" + ) print("") diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index e6358421538..0522a9b9a5a 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -43,7 +43,7 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): if ns_known_args.cmd == "help": pass - #print_options(s_ticker, s_start, s_interval) + #print_options(s_ticker) elif ns_known_args.cmd == "q": # Just leave the options menu From b862b2e853c1b9d036b0771ad0415e36e16c6a19 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 19:22:39 +0100 Subject: [PATCH 14/20] open interest start open interest work graph function added menu command added --- gamestonk_terminal/options/op_menu.py | 6 +++- gamestonk_terminal/options/volume.py | 45 +++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index 0522a9b9a5a..3ecda13fd2d 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -14,7 +14,8 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): "help", "q", "quit", - "volume" + "volume", + "oi" ] opt_parser.add_argument("cmd", choices=choices) completer = NestedCompleter.from_nested_dict({c: None for c in choices}) @@ -57,5 +58,8 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): # call the volume graph vol.volume_graph(l_args, s_ticker) + elif ns_known_args.cmd == "oi": + # call the volume graph + vol.open_interest_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 14d71ced9ab..95fbef54822 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -7,7 +7,33 @@ import argparse import datetime from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn, valid_date +def open_interest_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].""", + ) + 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 + ) + l_similar = [] + try: + ns_parser = parse_known_args_and_warn(parser, l_args) + if not ns_parser: + return + __get_open_interest_graph(s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) + + except SystemExit: + print("") + except Exception as e: + print(e) def volume_graph(l_args, s_ticker): """ Show traded options volume. [Source: Yahoo Finance] """ @@ -37,6 +63,10 @@ def volume_graph(l_args, s_ticker): except Exception as e: print(e) +def __get_open_interest_graph(ticker_name, exp_date): + df = __get_volume_data(ticker_name, exp_date) + __generate_graph_sns(df, ticker_name, exp_date, True) + #__generate_graph_plotly(df, ticker_name, exp_date) def __get_volume_graph(ticker_name, exp_date): @@ -81,30 +111,33 @@ def __get_volume_data(ticker_name, exp_date): #dataframe return volume_data -def __generate_graph_plotly(df, ticker_name, exp_date): +def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest = False): #version with plotly express + op_type = 'openInterest'if for_open_interest else 'volume' + fig = px.line( df, x="strike", - y="volume", - title=f'{ticker_name} options volume for {exp_date}', + y=op_type, + title=f'{ticker_name} options {op_type} for {exp_date}', color= 'type' ) fig.show() return -def __generate_graph_sns(df, ticker_name, exp_date): +def __generate_graph_sns(df, ticker_name, exp_date, for_open_interest = False): #version with seaborn express + op_type = 'openInterest'if for_open_interest else 'volume' plt.figure(figsize=(12,6)) fig = sns.lineplot( data = df, x = 'strike', - y = 'volume', + y = 'op_type', hue = 'type', palette= ['limegreen', 'tomato']) - plt.title(f'{ticker_name} options volume for {exp_date}') + plt.title(f'{ticker_name} options {op_type} for {exp_date}') plt.show() return From b78e2cbda8e648047dad306ef74742b997ce4d04 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 19:47:15 +0100 Subject: [PATCH 15/20] Update volume.py cleanup --- gamestonk_terminal/options/volume.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gamestonk_terminal/options/volume.py b/gamestonk_terminal/options/volume.py index 95fbef54822..2456e199199 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -68,7 +68,6 @@ def __get_open_interest_graph(ticker_name, exp_date): __generate_graph_sns(df, ticker_name, exp_date, True) #__generate_graph_plotly(df, ticker_name, exp_date) - def __get_volume_graph(ticker_name, exp_date): df = __get_volume_data(ticker_name, exp_date) __generate_graph_sns(df, ticker_name, exp_date) From ccf4ce6c5d83b2a2717ec019049a218a6ff92b6f Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 19:51:29 +0100 Subject: [PATCH 16/20] housekeeping --- gamestonk_terminal/options/op_menu.py | 8 ++-- gamestonk_terminal/options/volume.py | 54 ++++++++++++++++----------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index 3ecda13fd2d..7b973c97c11 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -6,6 +6,7 @@ from gamestonk_terminal.helper_funcs import get_flair from gamestonk_terminal.options import volume as vol from gamestonk_terminal.menu import session + def opt_menu(df_stock, s_ticker, s_start, s_interval): # Add list of arguments that the options parser accepts @@ -33,7 +34,6 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): else: as_input = input(f"{get_flair()} (opt)> ") - # Parse fundamental analysis command of the list of possible commands try: (ns_known_args, l_args) = opt_parser.parse_known_args(as_input.split()) @@ -44,7 +44,7 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): if ns_known_args.cmd == "help": pass - #print_options(s_ticker) + # print_options(s_ticker) elif ns_known_args.cmd == "q": # Just leave the options menu @@ -55,11 +55,11 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): return True elif ns_known_args.cmd == "volume": - # call the volume graph + # call the volume graph vol.volume_graph(l_args, s_ticker) elif ns_known_args.cmd == "oi": - # call the volume graph + # call the volume graph vol.open_interest_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 2456e199199..d9eba64305f 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -7,6 +7,7 @@ import argparse import datetime from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn, valid_date + def open_interest_graph(l_args, s_ticker): """ Show traded options volume. [Source: Yahoo Finance] """ parser = argparse.ArgumentParser( @@ -28,13 +29,15 @@ def open_interest_graph(l_args, s_ticker): if not ns_parser: return - __get_open_interest_graph(s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) + __get_open_interest_graph( + s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) except SystemExit: - print("") + print("") except Exception as e: print(e) + def volume_graph(l_args, s_ticker): """ Show traded options volume. [Source: Yahoo Finance] """ parser = argparse.ArgumentParser( @@ -56,27 +59,31 @@ def volume_graph(l_args, s_ticker): if not ns_parser: return - __get_volume_graph(s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) + __get_volume_graph( + s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) except SystemExit: - print("") + print("") except Exception as e: print(e) + def __get_open_interest_graph(ticker_name, exp_date): df = __get_volume_data(ticker_name, exp_date) __generate_graph_sns(df, ticker_name, exp_date, True) #__generate_graph_plotly(df, ticker_name, exp_date) + def __get_volume_graph(ticker_name, exp_date): df = __get_volume_data(ticker_name, exp_date) __generate_graph_sns(df, ticker_name, exp_date) #__generate_graph_plotly(df, ticker_name, exp_date) + def __pull_call_put_data(call_put, flag): df = call_put.pivot_table( index='strike', - values = ['volume','openInterest'], + values=['volume', 'openInterest'], aggfunc='sum') df.reindex() @@ -86,6 +93,7 @@ def __pull_call_put_data(call_put, flag): return df + def __get_volume_data(ticker_name, exp_date): option_chain = yf.Ticker(ticker_name).option_chain(exp_date) @@ -93,25 +101,26 @@ def __get_volume_data(ticker_name, exp_date): calls = __pull_call_put_data( option_chain.calls, 'calls' - ) + ) puts = __pull_call_put_data( option_chain.puts, 'puts' - ) + ) volume_data = pd.concat( [ calls, puts ], - axis = 0 - ) - #dataframe + axis=0 + ) + # dataframe return volume_data -def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest = False): - #version with plotly express + +def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest=False): + # version with plotly express op_type = 'openInterest'if for_open_interest else 'volume' fig = px.line( @@ -119,22 +128,23 @@ def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest = False x="strike", y=op_type, title=f'{ticker_name} options {op_type} for {exp_date}', - color= 'type' - ) + color='type' + ) fig.show() return -def __generate_graph_sns(df, ticker_name, exp_date, for_open_interest = False): - #version with seaborn express + +def __generate_graph_sns(df, ticker_name, exp_date, for_open_interest=False): + # version with seaborn express op_type = 'openInterest'if for_open_interest else 'volume' - plt.figure(figsize=(12,6)) + plt.figure(figsize=(12, 6)) fig = sns.lineplot( - data = df, - x = 'strike', - y = 'op_type', - hue = 'type', - palette= ['limegreen', 'tomato']) + data=df, + x='strike', + y='op_type', + hue='type', + palette=['limegreen', 'tomato']) plt.title(f'{ticker_name} options {op_type} for {exp_date}') From 37588f438321d950de6659d836aa27305ac0a27c Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 20:40:14 +0100 Subject: [PATCH 17/20] blacked files --- gamestonk_terminal/options/op_menu.py | 10 +---- gamestonk_terminal/options/volume.py | 64 +++++++++++---------------- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index 7b973c97c11..6180939dfe1 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -11,17 +11,11 @@ def opt_menu(df_stock, s_ticker, s_start, s_interval): # Add list of arguments that the options parser accepts opt_parser = argparse.ArgumentParser(prog="opt", add_help=False) - choices = [ - "help", - "q", - "quit", - "volume", - "oi" - ] + choices = ["help", "q", "quit", "volume", "oi"] opt_parser.add_argument("cmd", choices=choices) completer = NestedCompleter.from_nested_dict({c: None for c in choices}) - #print_options(s_ticker, s_start, s_interval) + # print_options(s_ticker, s_start, s_interval) # Loop forever and ever while True: diff --git a/gamestonk_terminal/options/volume.py b/gamestonk_terminal/options/volume.py index d9eba64305f..c91a6f96295 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -5,7 +5,11 @@ 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 +from gamestonk_terminal.helper_funcs import ( + get_flair, + parse_known_args_and_warn, + valid_date, +) def open_interest_graph(l_args, s_ticker): @@ -21,7 +25,7 @@ def open_interest_graph(l_args, s_ticker): type=valid_date, dest="s_expiry_date", help="The expiry date (format YYYY-MM-DD) for the option chain", - required=True + required=True, ) l_similar = [] try: @@ -30,7 +34,8 @@ def open_interest_graph(l_args, s_ticker): return __get_open_interest_graph( - s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) + s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d") + ) except SystemExit: print("") @@ -51,7 +56,7 @@ def volume_graph(l_args, s_ticker): type=valid_date, dest="s_expiry_date", help="The expiry date (format YYYY-MM-DD) for the option chain", - required=True + required=True, ) l_similar = [] try: @@ -59,8 +64,7 @@ def volume_graph(l_args, s_ticker): if not ns_parser: return - __get_volume_graph( - s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) + __get_volume_graph(s_ticker, ns_parser.s_expiry_date.strftime("%Y-%m-%d")) except SystemExit: print("") @@ -71,25 +75,24 @@ def volume_graph(l_args, s_ticker): def __get_open_interest_graph(ticker_name, exp_date): df = __get_volume_data(ticker_name, exp_date) __generate_graph_sns(df, ticker_name, exp_date, True) - #__generate_graph_plotly(df, ticker_name, exp_date) + # __generate_graph_plotly(df, ticker_name, exp_date) def __get_volume_graph(ticker_name, exp_date): df = __get_volume_data(ticker_name, exp_date) __generate_graph_sns(df, ticker_name, exp_date) - #__generate_graph_plotly(df, ticker_name, exp_date) + # __generate_graph_plotly(df, ticker_name, exp_date) def __pull_call_put_data(call_put, flag): df = call_put.pivot_table( - index='strike', - values=['volume', 'openInterest'], - aggfunc='sum') + index="strike", values=["volume", "openInterest"], aggfunc="sum" + ) df.reindex() - df['strike'] = df.index - df['type'] = flag + df["strike"] = df.index + df["type"] = flag return df @@ -98,37 +101,25 @@ def __get_volume_data(ticker_name, exp_date): option_chain = yf.Ticker(ticker_name).option_chain(exp_date) - calls = __pull_call_put_data( - option_chain.calls, - 'calls' - ) + calls = __pull_call_put_data(option_chain.calls, "calls") - puts = __pull_call_put_data( - option_chain.puts, - 'puts' - ) + puts = __pull_call_put_data(option_chain.puts, "puts") - volume_data = pd.concat( - [ - calls, - puts - ], - axis=0 - ) + volume_data = pd.concat([calls, puts], axis=0) # dataframe return volume_data def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest=False): # version with plotly express - op_type = 'openInterest'if for_open_interest else 'volume' + op_type = "openInterest" if for_open_interest else "volume" fig = px.line( df, x="strike", y=op_type, - title=f'{ticker_name} options {op_type} for {exp_date}', - color='type' + title=f"{ticker_name} options {op_type} for {exp_date}", + color="type", ) fig.show() @@ -137,16 +128,13 @@ def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest=False): def __generate_graph_sns(df, ticker_name, exp_date, for_open_interest=False): # version with seaborn express - op_type = 'openInterest'if for_open_interest else 'volume' + op_type = "openInterest" if for_open_interest else "volume" plt.figure(figsize=(12, 6)) fig = sns.lineplot( - data=df, - x='strike', - y='op_type', - hue='type', - palette=['limegreen', 'tomato']) + data=df, x="strike", y="op_type", hue="type", palette=["limegreen", "tomato"] + ) - plt.title(f'{ticker_name} options {op_type} for {exp_date}') + plt.title(f"{ticker_name} options {op_type} for {exp_date}") plt.show() return From e953b8b200b2bb969cd256ba96a149aab04f1aaa Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 20:44:21 +0100 Subject: [PATCH 18/20] blacked --- gamestonk_terminal/main_helper.py | 4 +--- terminal.py | 7 +++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/gamestonk_terminal/main_helper.py b/gamestonk_terminal/main_helper.py index f971ca0ad6f..4010e85a204 100644 --- a/gamestonk_terminal/main_helper.py +++ b/gamestonk_terminal/main_helper.py @@ -76,9 +76,7 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): print( " pred prediction techniques, \t e.g.: regression, arima, rnn, lstm, prophet" ) - print( - " op options info, \t e.g.: volume" - ) + print(" op options info, \t e.g.: volume") print("") diff --git a/terminal.py b/terminal.py index 318ed535266..5b3c3f83727 100644 --- a/terminal.py +++ b/terminal.py @@ -1,4 +1,4 @@ - #!/usr/bin/env python +#!/usr/bin/env python import argparse @@ -73,8 +73,7 @@ def main(): "dd", "pred", "ca", - "op" - "fred", + "op" "fred", ] menu_parser.add_argument("opt", choices=choices) completer = NestedCompleter.from_nested_dict({c: None for c in choices}) @@ -184,7 +183,7 @@ def main(): elif ns_known_args.opt == "op": b_quit = opm.opt_menu(df_stock, s_ticker, s_start, s_interval) - + elif ns_known_args.opt == "fred": b_quit = fm.fred_menu() From add1b99f4abac48ea14047ee3d26bdbb3de22e75 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 20:51:36 +0100 Subject: [PATCH 19/20] made linter happy --- gamestonk_terminal/options/op_menu.py | 2 +- gamestonk_terminal/options/volume.py | 9 +++------ terminal.py | 5 +++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/gamestonk_terminal/options/op_menu.py b/gamestonk_terminal/options/op_menu.py index 6180939dfe1..a386a584ec2 100644 --- a/gamestonk_terminal/options/op_menu.py +++ b/gamestonk_terminal/options/op_menu.py @@ -7,7 +7,7 @@ from gamestonk_terminal.options import volume as vol from gamestonk_terminal.menu import session -def opt_menu(df_stock, s_ticker, s_start, s_interval): +def opt_menu(s_ticker): # Add list of arguments that the options parser accepts opt_parser = argparse.ArgumentParser(prog="opt", add_help=False) diff --git a/gamestonk_terminal/options/volume.py b/gamestonk_terminal/options/volume.py index c91a6f96295..8abf73bd8fa 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -1,12 +1,10 @@ +import argparse import yfinance as yf 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, ) @@ -27,7 +25,6 @@ def open_interest_graph(l_args, s_ticker): help="The expiry date (format YYYY-MM-DD) for the option chain", required=True, ) - l_similar = [] try: ns_parser = parse_known_args_and_warn(parser, l_args) if not ns_parser: @@ -58,7 +55,7 @@ def volume_graph(l_args, s_ticker): help="The expiry date (format YYYY-MM-DD) for the option chain", required=True, ) - l_similar = [] + try: ns_parser = parse_known_args_and_warn(parser, l_args) if not ns_parser: @@ -137,4 +134,4 @@ def __generate_graph_sns(df, ticker_name, exp_date, for_open_interest=False): plt.title(f"{ticker_name} options {op_type} for {exp_date}") plt.show() - return + return fig diff --git a/terminal.py b/terminal.py index 5b3c3f83727..bb8f8ee39c5 100644 --- a/terminal.py +++ b/terminal.py @@ -73,7 +73,8 @@ def main(): "dd", "pred", "ca", - "op" "fred", + "op", + "fred", ] menu_parser.add_argument("opt", choices=choices) completer = NestedCompleter.from_nested_dict({c: None for c in choices}) @@ -182,7 +183,7 @@ def main(): 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) + b_quit = opm.opt_menu(s_ticker) elif ns_known_args.opt == "fred": b_quit = fm.fred_menu() From 3d3c3a06d50456063f59cab43e0b72ddab4499a0 Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Wed, 17 Mar 2021 21:06:36 +0100 Subject: [PATCH 20/20] fixed op_type flag value --- gamestonk_terminal/options/volume.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/gamestonk_terminal/options/volume.py b/gamestonk_terminal/options/volume.py index 8abf73bd8fa..14440ef101e 100644 --- a/gamestonk_terminal/options/volume.py +++ b/gamestonk_terminal/options/volume.py @@ -109,7 +109,10 @@ def __get_volume_data(ticker_name, exp_date): def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest=False): # version with plotly express - op_type = "openInterest" if for_open_interest else "volume" + if for_open_interest: + op_type = "openInterest" + else: + op_type = "volume" fig = px.line( df, @@ -125,10 +128,14 @@ def __generate_graph_plotly(df, ticker_name, exp_date, for_open_interest=False): def __generate_graph_sns(df, ticker_name, exp_date, for_open_interest=False): # version with seaborn express - op_type = "openInterest" if for_open_interest else "volume" + if for_open_interest: + op_type = "openInterest" + else: + op_type = "volume" + plt.figure(figsize=(12, 6)) fig = sns.lineplot( - data=df, x="strike", y="op_type", hue="type", palette=["limegreen", "tomato"] + data=df, x="strike", y=op_type, hue="type", palette=["limegreen", "tomato"] ) plt.title(f"{ticker_name} options {op_type} for {exp_date}")