From e52e5d43dcf45c18aa5395cdebce8f45cb5c975a Mon Sep 17 00:00:00 2001 From: Laurent Andriamifidy Date: Tue, 16 Mar 2021 20:58:53 +0100 Subject: [PATCH] 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