def close(self): self.stop() async def _close(): await self.xs.stop() self.xs.close() self.closed.set() call_via_loop_afut(_close, loop=self.loop)
async def _activate(self, timeout=None): # Close any previous connection if by chance left open (though it shouldn't happen) await self._exit_conn() # Renew the queue to ensure that we won't be receiving anything from previous sockets # TODO: set max size to this queue? self._socket_recv_queue = asyncio.Queue(loop=self.loop) self.conn = None fut = asyncio.ensure_future(self._connect()) wait_started = time.time() is_exceeded = lambda: timeout is not None and time.time( ) - wait_started > timeout while not self._stopped and not is_exceeded() and not fut.done(): await asyncio.wait([fut], timeout=0.1) to_occurred = is_exceeded() if fut.done(): await fut # raise exception if occurred else: fut.cancel() await self._exit_conn() if to_occurred: raise asyncio.TimeoutError( '{} - connect timeout occurred'.format(self.name)) if self._stopped: return self.station.broadcast('active') self.broadcast_event('socket', 1) if self.on_activate is not None: on_activate = [self.on_activate] if not hasattr(self.on_activate, '__iter__') \ else self.on_activate for i, f in enumerate(on_activate): args = [self] if get_arg_count(f) else [] key = 'on_activate{}'.format('_{}'.format(i) if i else '') self.futures[key] = call_via_loop_afut(f, args)
def start(self): if self._closed: raise TerminatedException("'{}' is closed".format(self.name)) if asyncio.get_event_loop() is self.loop: return asyncio.ensure_future(self.tp.start()) else: return call_via_loop_afut(self.tp.start, loop=self.loop)
async def _init_orderbooks(self, cnx): """ Force create orderbooks (that haven't already been automatically created on 1st .send_update/.send_ob) X seconds after cnx activation """ wait = self.cfg['force_create'] if wait is None: return await asyncio.sleep(wait) for s in self.xs.sh.subscriptions: symbol = s.params.get('symbol') if s.channel != self.channel or s.cnx != cnx: continue elif not s.state and self._is_time(symbol, 'reload'): self.xs.log('force creating {} {}'.format(self.name, symbol)) call_via_loop_afut(self._fetch_and_create, (symbol, ), loop=self.xs.loop)
def stop(self): if asyncio.get_event_loop() is self.loop: return asyncio.ensure_future(self.tp.stop()) else: return call_via_loop_afut(self.tp.stop, loop=self.loop)
def send(self, message, dump=True, log=True): return call_via_loop_afut(self._send, (message, dump, log), loop=self.loop)
def manage(self, symbol, changes=None, force_cancel=False): if self.symbols is not None and symbol not in self.symbols: # previously this was due to technical limitations, but now keep it this way # to not place unintended stop-loss orders return if symbol not in self.gui.symbol_rows: self.xs.subscribe_to_own_market(symbol) self.gui.add_symbol(symbol) if not self.xs.positions.get(symbol, {}).get('amount'): self.gui.empty_query(symbol) if changes is not None and \ not any(x in changes for x in ['amount','price','liq_price']): return is_account_active = self.xs.is_subscribed_to({'_': 'account'}, True) is_own_market_active = self.xs.is_subscribed_to( { '_': 'own_market', 'symbol': symbol }, True) if not is_account_active or not is_own_market_active: return enabled = self.gui.enabled_vars[self.gui.symbol_rows[symbol]].get() side, amount, stop_price, is_edit_worthy = self.calc_order_params( symbol) o = self.get_stop_order(symbol) q = self.queues[symbol] wait_on_cancel = None if o is not None: cancel = not amount or side != o['side'] if (cancel or force_cancel) and not q.contains('cancel'): f = call_via_loop_afut(self.cancel, (symbol, ), loop=self.loop, cb_loop=self.loop) q.put(f, 'cancel') wait_on_cancel = q.get('cancel') elif enabled and amount and not q.contains( 'edit') and not q.contains('create') and is_edit_worthy: f2 = call_via_loop_afut( self.place, (symbol, side, amount, stop_price, 'edit', wait_on_cancel), loop=self.loop, cb_loop=self.loop) q.put(f2, 'edit') if enabled and amount and not q.contains('create') and not q.contains('edit') and \ (o is None and not q.contains('cancel') or wait_on_cancel is not None): f3 = call_via_loop_afut( self.place, (symbol, side, amount, stop_price, 'create', wait_on_cancel), loop=self.loop, cb_loop=self.loop) q.put(f3, 'create')
def _put(self, yes_or_no): call_via_loop_afut(self.yes_no_queue.put, (yes_or_no, ), loop=self.sl.loop)
def start(self): return call_via_loop_afut(self.run, loop=self.xs.loop)