Add a `quote` subcommand

Add `piker quote <tickerA> <tickerB> <tickerC>` command for easily
dumping quote data to the console. With `-df` will dump as a pandas data
frame. Add key filtering to `piker api` calls.
kivy_mainline_and_py3.8
Tyler Goodlet 2018-02-08 02:26:55 -05:00
parent adecc082ac
commit 6781a23850
1 changed files with 42 additions and 3 deletions

View File

@ -6,6 +6,7 @@ from importlib import import_module
import click
import trio
import pandas as pd
from ..log import get_console_log, colorize_json
@ -30,9 +31,11 @@ def cli():
@cli.command()
@click.option('--broker', default='questrade', help='Broker backend to use')
@click.option('--loglevel', '-l', default='warning', help='Logging level')
@click.option('--keys', '-k', multiple=True,
help='Return results only for these keys')
@click.argument('meth', nargs=1)
@click.argument('kwargs', nargs=-1)
def api(meth, kwargs, loglevel, broker):
def api(meth, kwargs, loglevel, broker, keys):
"""client for testing broker API methods with pretty printing of output.
"""
log = get_console_log(loglevel)
@ -47,7 +50,43 @@ def api(meth, kwargs, loglevel, broker):
_kwargs[key] = value
data = run(partial(brokermod.api, meth, **_kwargs), loglevel=loglevel)
if data:
if keys:
# filter to requested keys
filtered = []
if meth in data: # often a list of dicts
for item in data[meth]:
filtered.append({key: item[key] for key in keys})
else: # likely just a dict
filtered.append({key: data[key] for key in keys})
data = filtered
click.echo(colorize_json(data))
@cli.command()
@click.option('--broker', default='questrade', help='Broker backend to use')
@click.option('--loglevel', '-l', default='warning', help='Logging level')
@click.option('--df-output', '-df', flag_value=True,
help='Ouput in `pandas.DataFrame` format')
@click.argument('tickers', nargs=-1)
def quote(loglevel, broker, tickers, df_output):
"""client for testing broker API methods with pretty printing of output.
"""
brokermod = import_module('.' + broker, 'piker.brokers')
data = run(partial(brokermod.quote, tickers), loglevel=loglevel)
quotes = data['quotes']
cols = quotes[0].copy()
cols.pop('symbol')
if df_output:
df = pd.DataFrame(
data['quotes'],
index=[item['symbol'] for item in quotes],
columns=cols,
)
click.echo(df)
else:
click.echo(colorize_json(data))
@ -55,7 +94,7 @@ def api(meth, kwargs, loglevel, broker):
@click.option('--broker', default='questrade', help='Broker backend to use')
@click.option('--loglevel', '-l', default='info', help='Logging level')
@click.argument('tickers', nargs=-1)
def stream(broker, loglevel, tickers):
def stream(broker, loglevel, tickers, keys):
# import broker module daemon entry point
bm = import_module('.' + broker, 'piker.brokers')
run(