From 19ea7bd7aa18c2b0ad8849e2d5f2bfcbd0a82af1 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 13 Nov 2018 12:58:05 -0500 Subject: [PATCH] Add option-chain cmd --- piker/cli.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/piker/cli.py b/piker/cli.py index 74bd1d69..0f8b5264 100644 --- a/piker/cli.py +++ b/piker/cli.py @@ -116,6 +116,36 @@ def quote(loglevel, broker, tickers, df_output): click.echo(colorize_json(quotes)) +@cli.command() +@click.option('--broker', '-b', default=DEFAULT_BROKER, + help='Broker backend to use') +@click.option('--loglevel', '-l', default='warning', help='Logging level') +@click.option('--df-output', '-df', flag_value=True, + help='Output in `pandas.DataFrame` format') +@click.argument('symbol', required=True) +def option_chain(loglevel, broker, symbol, df_output): + """Retreive symbol quotes on the console in either json or dataframe + format. + """ + brokermod = get_brokermod(broker) + get_console_log(loglevel) + quotes = trio.run(partial(core.option_chain, brokermod, symbol))[symbol] + if not quotes: + log.error(f"No quotes could be found for {tickers}?") + return + + if df_output: + cols = next(filter(bool, quotes.values())).copy() + df = pd.DataFrame( + (quote.values() for contract, quote in quotes.items()), + index=quotes.keys(), + columns=cols.keys(), + ) + click.echo(df) + else: + click.echo(colorize_json(quotes)) + + @cli.command() @click.option('--broker', '-b', default=DEFAULT_BROKER, help='Broker backend to use')