Skip to content

Commit

Permalink
[ExchangeManager] Improve WS initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Herklos committed Feb 17, 2020
1 parent 921ff08 commit e0f57d7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
2 changes: 0 additions & 2 deletions octobot_trading/exchanges/exchange_manager.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ cdef class ExchangeManager(Initializable):
cpdef void reset_exchange_symbols_data(self)
cpdef void reset_exchange_personal_data(self)
cpdef bint check_config(self, str exchange_name)
cpdef force_disable_web_socket(self, str exchange_name)
cpdef check_web_socket_config(self, str exchange_name)
cpdef bint symbol_exists(self, str symbol)
cpdef bint time_frame_exists(self, object time_frame)
cpdef int get_rate_limit(self)
Expand Down
68 changes: 34 additions & 34 deletions octobot_trading/exchanges/exchange_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from octobot_trading.exchanges.exchange_simulator import ExchangeSimulator
from octobot_trading.exchanges.exchanges import Exchanges
from octobot_trading.exchanges.rest_exchange import RestExchange
from octobot_trading.exchanges.websockets import check_web_socket_config, search_websocket_class
from octobot_trading.exchanges.websockets.abstract_websocket import AbstractWebsocket
from octobot_trading.producers import UNAUTHENTICATED_UPDATER_PRODUCERS, AUTHENTICATED_UPDATER_PRODUCERS
from octobot_trading.producers.simulator import AUTHENTICATED_UPDATER_SIMULATOR_PRODUCERS
Expand Down Expand Up @@ -165,8 +166,8 @@ async def _create_real_exchange(self):
# create Websocket exchange if possible
if not self.rest_only:
# search for websocket
if self.check_web_socket_config(self.exchange.name):
await self._search_and_create_websocket(AbstractWebsocket)
if check_web_socket_config(self.config, self.exchange.name):
await self._search_and_create_websocket()

async def _create_simulated_exchange(self):
self.exchange = ExchangeSimulator(self.config, self.exchange_type, self, self.backtesting_files)
Expand Down Expand Up @@ -205,48 +206,47 @@ async def _create_exchange_producers(self):
for updater in AUTHENTICATED_UPDATER_SIMULATOR_PRODUCERS:
await updater(get_chan(updater.CHANNEL_NAME, self.id)).run()

"""
Websocket
"""
def _is_managed_by_websocket(self, channel): # TODO improve checker
return not self.rest_only and self.has_websocket and \
any([self.exchange_web_socket.is_feed_available(feed)
for feed in WEBSOCKET_FEEDS_TO_TRADING_CHANNELS[channel]])

async def _search_and_create_websocket(self, websocket_class):
for socket_manager in websocket_class.__subclasses__():
# add websocket exchange if available
if socket_manager.has_name(self.exchange.name):
self.exchange_web_socket = socket_manager.get_websocket_client(self.config, self)

# init websocket
try:
await self.exchange_web_socket.init_web_sockets(self.exchange_config.traded_time_frames,
self.exchange_config.traded_symbol_pairs)

# start the websocket
self.exchange_web_socket.start_sockets()

self.has_websocket = self.exchange_web_socket.is_websocket_running
self._logger.info(f"{socket_manager.get_name()} connected to {self.exchange.name}")
except Exception as e:
self._logger.error(f"Fail to init websocket for {websocket_class} : {e}")
self.exchange_web_socket = None
self.has_websocket = False
raise e

# Exchange configuration functions
for feed in WEBSOCKET_FEEDS_TO_TRADING_CHANNELS[channel]])

async def _search_and_create_websocket(self):
socket_manager = search_websocket_class(AbstractWebsocket, self.exchange_name)
if socket_manager is not None:
await self._create_websocket(AbstractWebsocket.__name__, socket_manager)

async def _create_websocket(self, websocket_class_name, socket_manager):
try:
self.exchange_web_socket = socket_manager.get_websocket_client(self.config, self)
self._logger.info(f"{socket_manager.get_name()} connected to {self.exchange.name}")
except Exception as e:
self._logger.error(f"Fail to init websocket for {websocket_class_name} : {e}")
self.exchange_web_socket = None
self.has_websocket = False
raise e

async def _init_websocket(self):
await self.exchange_web_socket.init_web_sockets(self.exchange_config.traded_time_frames,
self.exchange_config.traded_symbol_pairs)

self.exchange_web_socket.start_sockets()

self.has_websocket = self.exchange_web_socket.is_websocket_running

"""
Exchange Configuration
"""
def check_config(self, exchange_name):
if CONFIG_EXCHANGE_KEY not in self.config[CONFIG_EXCHANGES][exchange_name] \
or CONFIG_EXCHANGE_SECRET not in self.config[CONFIG_EXCHANGES][exchange_name]:
return False
else:
return True

def force_disable_web_socket(self, exchange_name):
return CONFIG_EXCHANGE_WEB_SOCKET in self.config[CONFIG_EXCHANGES][exchange_name] \
and not self.config[CONFIG_EXCHANGES][exchange_name][CONFIG_EXCHANGE_WEB_SOCKET]

def check_web_socket_config(self, exchange_name):
return not self.force_disable_web_socket(exchange_name)

def enabled(self):
# if we can get candlestick data
if self.is_simulated or self.exchange.name in self.config[CONFIG_EXCHANGES]:
Expand Down
19 changes: 19 additions & 0 deletions octobot_trading/exchanges/websockets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,24 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
from octobot_websockets.constants import CONFIG_EXCHANGE_WEB_SOCKET
from octobot_trading.constants import CONFIG_EXCHANGES

from .octobot_websocket import *


def force_disable_web_socket(config, exchange_name) -> bool:
return CONFIG_EXCHANGE_WEB_SOCKET in config[CONFIG_EXCHANGES][exchange_name] \
and not config[CONFIG_EXCHANGES][exchange_name][CONFIG_EXCHANGE_WEB_SOCKET]


def check_web_socket_config(config, exchange_name) -> bool:
return not force_disable_web_socket(config, exchange_name)


def search_websocket_class(websocket_class, exchange_name):
for socket_manager in websocket_class.__subclasses__():
# return websocket exchange if available
if socket_manager.has_name(exchange_name):
return socket_manager
return None

0 comments on commit e0f57d7

Please sign in to comment.