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-01-23 02:50:26 +00:00
|
|
|
"""
|
2018-01-23 06:03:51 +00:00
|
|
|
Broker clients, daemons and general back end machinery.
|
2018-01-23 02:50:26 +00:00
|
|
|
"""
|
2023-06-13 19:22:51 +00:00
|
|
|
from contextlib import (
|
|
|
|
asynccontextmanager as acm,
|
|
|
|
)
|
2018-03-27 20:03:01 +00:00
|
|
|
from importlib import import_module
|
|
|
|
from types import ModuleType
|
|
|
|
|
2023-06-13 19:22:51 +00:00
|
|
|
from tractor.trionics import maybe_open_context
|
|
|
|
|
|
|
|
from ._util import (
|
|
|
|
log,
|
|
|
|
BrokerError,
|
|
|
|
SymbolNotFound,
|
|
|
|
NoData,
|
|
|
|
DataUnavailable,
|
|
|
|
DataThrottle,
|
|
|
|
resproc,
|
2023-06-19 21:59:40 +00:00
|
|
|
get_logger,
|
2023-06-13 19:22:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
__all__: list[str] = [
|
|
|
|
'BrokerError',
|
|
|
|
'SymbolNotFound',
|
|
|
|
'NoData',
|
|
|
|
'DataUnavailable',
|
|
|
|
'DataThrottle',
|
|
|
|
'resproc',
|
2023-06-19 21:59:40 +00:00
|
|
|
'get_logger',
|
2023-06-13 19:22:51 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
__brokers__: list[str] = [
|
2021-05-25 02:01:43 +00:00
|
|
|
'binance',
|
2020-07-03 23:09:57 +00:00
|
|
|
'ib',
|
2020-07-15 12:20:29 +00:00
|
|
|
'kraken',
|
2024-05-20 16:55:45 +00:00
|
|
|
'kucoin',
|
2023-05-25 20:01:21 +00:00
|
|
|
|
2022-11-17 18:34:50 +00:00
|
|
|
# broken but used to work
|
|
|
|
# 'questrade',
|
|
|
|
# 'robinhood',
|
|
|
|
|
|
|
|
# TODO: we should get on these stat!
|
|
|
|
# alpaca
|
|
|
|
# wstrade
|
|
|
|
# iex
|
|
|
|
|
|
|
|
# deribit
|
|
|
|
# bitso
|
2018-03-27 20:03:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_brokermod(brokername: str) -> ModuleType:
|
2023-02-21 15:05:28 +00:00
|
|
|
'''
|
|
|
|
Return the imported broker module by name.
|
|
|
|
|
|
|
|
'''
|
2024-05-20 16:55:45 +00:00
|
|
|
module: ModuleType = import_module('.' + brokername, 'piker.brokers')
|
2019-03-22 02:01:50 +00:00
|
|
|
# we only allow monkeying because it's for internal keying
|
2023-02-21 15:05:28 +00:00
|
|
|
module.name = module.__name__.split('.')[-1]
|
2018-04-17 21:17:08 +00:00
|
|
|
return module
|
2018-03-27 20:03:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def iter_brokermods():
|
2023-02-21 15:05:28 +00:00
|
|
|
'''
|
|
|
|
Iterate all built-in broker modules.
|
|
|
|
|
|
|
|
'''
|
2018-03-27 20:03:01 +00:00
|
|
|
for name in __brokers__:
|
|
|
|
yield get_brokermod(name)
|
2023-06-13 19:22:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@acm
|
|
|
|
async def open_cached_client(
|
|
|
|
brokername: str,
|
|
|
|
**kwargs,
|
|
|
|
|
|
|
|
) -> 'Client': # noqa
|
|
|
|
'''
|
|
|
|
Get a cached broker client from the current actor's local vars.
|
|
|
|
|
|
|
|
If one has not been setup do it and cache it.
|
|
|
|
|
|
|
|
'''
|
|
|
|
brokermod = get_brokermod(brokername)
|
|
|
|
async with maybe_open_context(
|
|
|
|
acm_func=brokermod.get_client,
|
|
|
|
kwargs=kwargs,
|
|
|
|
|
|
|
|
) as (cache_hit, client):
|
|
|
|
|
|
|
|
if cache_hit:
|
2023-07-10 13:21:12 +00:00
|
|
|
log.runtime(f'Reusing existing {client}')
|
2023-06-13 19:22:51 +00:00
|
|
|
|
|
|
|
yield client
|