Port cli to new options api
parent
cabc616b85
commit
c2ec4800d6
46
piker/cli.py
46
piker/cli.py
|
@ -4,6 +4,7 @@ Console interface to broker client/daemons.
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from operator import attrgetter
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
@ -32,11 +33,6 @@ def pikerd(loglevel, host, tl):
|
||||||
get_console_log(loglevel)
|
get_console_log(loglevel)
|
||||||
tractor.run_daemon(
|
tractor.run_daemon(
|
||||||
rpc_module_paths=['piker.brokers.data'],
|
rpc_module_paths=['piker.brokers.data'],
|
||||||
statespace={
|
|
||||||
'broker2tickersubs': {},
|
|
||||||
'clients': {},
|
|
||||||
'dtasks': set(),
|
|
||||||
},
|
|
||||||
name='brokerd',
|
name='brokerd',
|
||||||
loglevel=loglevel if tl else None,
|
loglevel=loglevel if tl else None,
|
||||||
)
|
)
|
||||||
|
@ -130,11 +126,6 @@ async def maybe_spawn_brokerd_as_subactor(sleep=0.5, tries=10, loglevel=None):
|
||||||
"No broker daemon could be found, spawning brokerd..")
|
"No broker daemon could be found, spawning brokerd..")
|
||||||
portal = await nursery.start_actor(
|
portal = await nursery.start_actor(
|
||||||
'brokerd',
|
'brokerd',
|
||||||
statespace={
|
|
||||||
'broker2tickersubs': {},
|
|
||||||
'clients': {},
|
|
||||||
'dtasks': set(),
|
|
||||||
},
|
|
||||||
rpc_module_paths=['piker.brokers.data'],
|
rpc_module_paths=['piker.brokers.data'],
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
)
|
)
|
||||||
|
@ -168,23 +159,10 @@ def monitor(loglevel, broker, rate, name, dhost, test, tl):
|
||||||
async with maybe_spawn_brokerd_as_subactor(
|
async with maybe_spawn_brokerd_as_subactor(
|
||||||
tries=tries, loglevel=loglevel
|
tries=tries, loglevel=loglevel
|
||||||
) as portal:
|
) as portal:
|
||||||
if test:
|
|
||||||
# stream from a local test file
|
|
||||||
agen = await portal.run(
|
|
||||||
"piker.brokers.data", 'stream_from_file',
|
|
||||||
filename=test
|
|
||||||
)
|
|
||||||
# agen = data.stream_from_file(test)
|
|
||||||
else:
|
|
||||||
# start live streaming from broker daemon
|
|
||||||
agen = await portal.run(
|
|
||||||
"piker.brokers.data", 'start_quote_stream',
|
|
||||||
broker=brokermod.name, tickers=tickers)
|
|
||||||
|
|
||||||
# run app "main"
|
# run app "main"
|
||||||
await _async_main(
|
await _async_main(
|
||||||
name, portal, tickers,
|
name, portal, tickers,
|
||||||
brokermod, rate, agen,
|
brokermod, rate, test=test,
|
||||||
)
|
)
|
||||||
|
|
||||||
tractor.run(
|
tractor.run(
|
||||||
|
@ -330,14 +308,16 @@ def dump(ctx, name):
|
||||||
def contracts(loglevel, broker, symbol, ids):
|
def contracts(loglevel, broker, symbol, ids):
|
||||||
brokermod = get_brokermod(broker)
|
brokermod = get_brokermod(broker)
|
||||||
get_console_log(loglevel)
|
get_console_log(loglevel)
|
||||||
quotes = trio.run(partial(core.contracts, brokermod, symbol))
|
contracts = trio.run(partial(core.contracts, brokermod, symbol))
|
||||||
if not ids:
|
if not ids:
|
||||||
# just print out expiry dates which can be used with
|
# just print out expiry dates which can be used with
|
||||||
# the option_chain_quote cmd
|
# the option_chain_quote cmd
|
||||||
id, contracts = next(iter(quotes.items()))
|
output = tuple(map(attrgetter('expiry'), contracts))
|
||||||
quotes = list(contracts)
|
else:
|
||||||
|
output = tuple(contracts.items())
|
||||||
|
|
||||||
click.echo(colorize_json(quotes))
|
# TODO: need a cli test to verify
|
||||||
|
click.echo(colorize_json(output))
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
@ -358,17 +338,15 @@ def optsquote(loglevel, broker, symbol, df_output, date):
|
||||||
partial(
|
partial(
|
||||||
core.option_chain, brokermod, symbol, date
|
core.option_chain, brokermod, symbol, date
|
||||||
)
|
)
|
||||||
)[symbol]
|
)
|
||||||
if not quotes:
|
if not quotes:
|
||||||
log.error(f"No quotes could be found for {symbol}?")
|
log.error(f"No option quotes could be found for {symbol}?")
|
||||||
return
|
return
|
||||||
|
|
||||||
if df_output:
|
if df_output:
|
||||||
cols = next(filter(bool, quotes.values())).copy()
|
|
||||||
df = pd.DataFrame(
|
df = pd.DataFrame(
|
||||||
(quote.values() for contract, quote in quotes.items()),
|
(quote.values() for quote in quotes),
|
||||||
index=quotes.keys(),
|
columns=quotes[0].keys(),
|
||||||
columns=cols.keys(),
|
|
||||||
)
|
)
|
||||||
click.echo(df)
|
click.echo(df)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue