2020-11-06 17:23:14 +00:00
|
|
|
# piker: trading gear for hackers
|
|
|
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-03-20 17:13:07 +00:00
|
|
|
"""
|
2020-07-15 12:28:13 +00:00
|
|
|
Broker high level cross-process API layer.
|
|
|
|
|
|
|
|
This API should be kept "remote service compatible" meaning inputs to
|
|
|
|
routines should be primitive data types where possible.
|
2018-03-20 17:13:07 +00:00
|
|
|
"""
|
|
|
|
import inspect
|
|
|
|
from types import ModuleType
|
2018-11-22 20:53:00 +00:00
|
|
|
from typing import List, Dict, Any, Optional
|
2018-03-20 17:13:07 +00:00
|
|
|
|
2021-05-28 16:29:58 +00:00
|
|
|
import trio
|
|
|
|
|
2018-11-15 03:58:12 +00:00
|
|
|
from ..log import get_logger
|
2019-02-26 03:29:48 +00:00
|
|
|
from . import get_brokermod
|
2021-05-28 16:29:58 +00:00
|
|
|
from .._daemon import maybe_spawn_brokerd
|
2021-08-09 15:43:45 +00:00
|
|
|
from .._cacheables import open_cached_client
|
2018-06-26 21:55:52 +00:00
|
|
|
|
|
|
|
|
2020-07-15 12:28:13 +00:00
|
|
|
log = get_logger(__name__)
|
2018-03-20 17:13:07 +00:00
|
|
|
|
|
|
|
|
2019-02-26 03:29:48 +00:00
|
|
|
async def api(brokername: str, methname: str, **kwargs) -> dict:
|
2018-11-12 02:44:46 +00:00
|
|
|
"""Make (proxy through) a broker API call by name and return its result.
|
2018-03-20 17:13:07 +00:00
|
|
|
"""
|
2019-02-26 03:29:48 +00:00
|
|
|
brokermod = get_brokermod(brokername)
|
2018-03-20 17:13:07 +00:00
|
|
|
async with brokermod.get_client() as client:
|
2020-07-15 12:28:13 +00:00
|
|
|
meth = getattr(client, methname, None)
|
2018-11-12 02:44:46 +00:00
|
|
|
if meth is None:
|
2020-06-16 15:55:37 +00:00
|
|
|
log.debug(
|
2018-11-30 13:14:36 +00:00
|
|
|
f"Couldn't find API method {methname} looking up on client")
|
2020-07-15 12:28:13 +00:00
|
|
|
meth = getattr(client.api, methname, None)
|
2018-11-12 02:44:46 +00:00
|
|
|
|
2018-03-20 17:13:07 +00:00
|
|
|
if meth is None:
|
|
|
|
log.error(f"No api method `{methname}` could be found?")
|
|
|
|
return
|
2018-11-12 02:44:46 +00:00
|
|
|
|
|
|
|
if not kwargs:
|
2018-03-20 17:13:07 +00:00
|
|
|
# verify kwargs requirements are met
|
|
|
|
sig = inspect.signature(meth)
|
|
|
|
if sig.parameters:
|
|
|
|
log.error(
|
|
|
|
f"Argument(s) are required by the `{methname}` method: "
|
|
|
|
f"{tuple(sig.parameters.keys())}")
|
|
|
|
return
|
|
|
|
|
|
|
|
return await meth(**kwargs)
|
|
|
|
|
|
|
|
|
2018-11-12 02:44:46 +00:00
|
|
|
async def stocks_quote(
|
|
|
|
brokermod: ModuleType,
|
|
|
|
tickers: List[str]
|
|
|
|
) -> Dict[str, Dict[str, Any]]:
|
2018-03-20 17:13:07 +00:00
|
|
|
"""Return quotes dict for ``tickers``.
|
|
|
|
"""
|
|
|
|
async with brokermod.get_client() as client:
|
2018-12-01 21:11:38 +00:00
|
|
|
return await client.quote(tickers)
|
2018-03-20 17:13:07 +00:00
|
|
|
|
|
|
|
|
2018-11-30 13:14:36 +00:00
|
|
|
# TODO: these need tests
|
2018-11-13 17:57:21 +00:00
|
|
|
async def option_chain(
|
|
|
|
brokermod: ModuleType,
|
|
|
|
symbol: str,
|
2018-11-22 20:53:00 +00:00
|
|
|
date: Optional[str] = None,
|
2018-11-13 17:57:21 +00:00
|
|
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
2018-11-22 20:53:00 +00:00
|
|
|
"""Return option chain for ``symbol`` for ``date``.
|
|
|
|
|
|
|
|
By default all expiries are returned. If ``date`` is provided
|
|
|
|
then contract quotes for that single expiry are returned.
|
|
|
|
"""
|
|
|
|
async with brokermod.get_client() as client:
|
|
|
|
if date:
|
|
|
|
id = int((await client.tickers2ids([symbol]))[symbol])
|
|
|
|
# build contracts dict for single expiry
|
2018-11-30 13:14:36 +00:00
|
|
|
return await client.option_chains(
|
|
|
|
{(symbol, id, date): {}})
|
2018-11-22 20:53:00 +00:00
|
|
|
else:
|
|
|
|
# get all contract expiries
|
|
|
|
# (takes a long-ass time on QT fwiw)
|
|
|
|
contracts = await client.get_all_contracts([symbol])
|
|
|
|
# return chains for all dates
|
|
|
|
return await client.option_chains(contracts)
|
|
|
|
|
|
|
|
|
|
|
|
async def contracts(
|
|
|
|
brokermod: ModuleType,
|
|
|
|
symbol: str,
|
|
|
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
|
|
|
"""Return option contracts (all expiries) for ``symbol``.
|
2018-11-13 17:57:21 +00:00
|
|
|
"""
|
|
|
|
async with brokermod.get_client() as client:
|
2018-11-30 13:14:36 +00:00
|
|
|
# return await client.get_all_contracts([symbol])
|
2018-11-22 20:53:00 +00:00
|
|
|
return await client.get_all_contracts([symbol])
|
2020-05-23 19:39:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def bars(
|
|
|
|
brokermod: ModuleType,
|
|
|
|
symbol: str,
|
|
|
|
**kwargs,
|
|
|
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
|
|
|
"""Return option contracts (all expiries) for ``symbol``.
|
|
|
|
"""
|
|
|
|
async with brokermod.get_client() as client:
|
|
|
|
return await client.bars(symbol, **kwargs)
|
2020-07-15 12:28:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def symbol_info(
|
|
|
|
brokermod: ModuleType,
|
|
|
|
symbol: str,
|
|
|
|
**kwargs,
|
|
|
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
|
|
|
"""Return symbol info from broker.
|
|
|
|
"""
|
|
|
|
async with brokermod.get_client() as client:
|
|
|
|
return await client.symbol_info(symbol, **kwargs)
|
2020-06-24 18:13:00 +00:00
|
|
|
|
|
|
|
|
2021-05-28 16:29:58 +00:00
|
|
|
async def search_w_brokerd(name: str, pattern: str) -> dict:
|
|
|
|
|
|
|
|
async with open_cached_client(name) as client:
|
|
|
|
|
|
|
|
# TODO: support multiple asset type concurrent searches.
|
|
|
|
return await client.search_symbols(pattern=pattern)
|
|
|
|
|
|
|
|
|
2020-06-24 18:13:00 +00:00
|
|
|
async def symbol_search(
|
2021-05-28 16:29:58 +00:00
|
|
|
brokermods: list[ModuleType],
|
2020-07-15 12:28:13 +00:00
|
|
|
pattern: str,
|
2020-06-24 18:13:00 +00:00
|
|
|
**kwargs,
|
2022-03-17 13:03:44 +00:00
|
|
|
|
2020-06-24 18:13:00 +00:00
|
|
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
2022-03-17 13:03:44 +00:00
|
|
|
'''
|
|
|
|
Return symbol info from broker.
|
|
|
|
|
|
|
|
'''
|
2021-05-28 16:29:58 +00:00
|
|
|
results = []
|
|
|
|
|
2022-03-17 13:03:44 +00:00
|
|
|
async def search_backend(
|
|
|
|
brokermod: ModuleType
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
brokername: str = mod.name
|
2021-05-28 16:29:58 +00:00
|
|
|
|
|
|
|
async with maybe_spawn_brokerd(
|
2022-03-17 13:03:44 +00:00
|
|
|
mod.name,
|
|
|
|
infect_asyncio=getattr(mod, '_infect_asyncio', False),
|
2021-05-28 16:29:58 +00:00
|
|
|
) as portal:
|
|
|
|
|
|
|
|
results.append((
|
|
|
|
brokername,
|
|
|
|
await portal.run(
|
|
|
|
search_w_brokerd,
|
|
|
|
name=brokername,
|
|
|
|
pattern=pattern,
|
|
|
|
),
|
|
|
|
))
|
|
|
|
|
|
|
|
async with trio.open_nursery() as n:
|
|
|
|
|
|
|
|
for mod in brokermods:
|
|
|
|
n.start_soon(search_backend, mod.name)
|
|
|
|
|
|
|
|
return results
|