def updater(self): while self.running_updater: try: now = time() for key in self.updating_intervals: if type(self.time_keepers[key]) == dict: for symbol in self.time_keepers[key]: if now - self.time_keepers[key][ symbol] > self.updating_intervals[key]: if 0 < self.time_keepers[key][symbol] < 10: self.time_keepers[key][symbol] += 1 else: self.time_keepers[key][symbol] = 1 threaded(getattr(self, key))(symbol) sleep(0.01) else: if now - self.time_keepers[ key] > self.updating_intervals[key]: if 0 < self.time_keepers[key] < 10: self.time_keepers[key] += 1 else: self.time_keepers[key] = 1 threaded(getattr(self, key))() sleep(0.01) sleep(1) except (Exception) as e: print('ERROR with updater\n\n', e) raise Exception(e)
def start_call_limiter(self, interval: float = 0.1): def f(): while self.running_order_ts_flusher: self.flush_order_timestamps() sleep(interval) if not self.running_order_ts_flusher: self.running_order_ts_flusher = True threaded(f)()
def execute(self, symbol: str, fns: [Callable], args_list: [tuple] = [()], kwargs_list: [dict] = [{}], force_execution: bool = False): if not fns: return if force_execution or self.cm.can_execute(len(fns)): if self.locks[symbol].locked(): if time() - self.time_keepers['locks'][ symbol] > self.force_lock_release_timeout: self.locks[symbol].release() else: threaded(self.lock_execute_release)(symbol, fns, args_list, kwargs_list)
def init(self, symbols: [str]): print() calls = [] for i, symbol in enumerate(symbols): sys.stdout.write( f'\r{i + 1} / {len(symbols)} calculating ema for {symbol} ... ' ) sys.stdout.flush() calls.append(threaded(self.init_ema)(symbol)) sleep(0.1) [call.result() for call in calls] print() tickers = self.cc.fetch_tickers() self.symbol_split = {s: s.split('/') for s in symbols} self.conversion_costs = init_conversion_costs( {s: tickers[s] for s in symbols}, self.fee) self.order_book = { s: { 'bids': [{ 'price': tickers[s]['bid'], 'amount': tickers[s]['bidVolume'] }], 'asks': [{ 'price': tickers[s]['ask'], 'amount': tickers[s]['askVolume'] }] } for s in symbols } self.previous_updates = self.order_book.copy()
def init(self): self.update_balance() calls = [] for i, symbol in enumerate(self.symbols): coin, quot = self.symbol_split[symbol] calls.append(threaded(self.update_my_trades)(symbol)) sleep(0.1) [call.result() for call in calls] self.start_updater()
def lock_execute_release(self, symbol: str, fns: [Callable], args_list: [tuple], kwargs_list: [dict]): try: self.locks[symbol].acquire() self.time_keepers['locks'][symbol] = time() self.cm.add_ts_to_lists(len(fns)) future_results = [] for i in range(len(fns)): args = args_list[i] if len(args_list) > i else () kwargs = kwargs_list[i] if len(kwargs_list) > i else {} future_result = threaded(fns[i])(*args, **kwargs) future_results.append(future_result) for future_result in future_results: if future_result.exception(): if 'Unknown order sent' in future_result.exception( ).args[0]: print_([ 'ERROR', 'margin', symbol, 'trying to cancel non-existing order; schedule open orders update' ]) self.time_keepers['update_open_orders'][symbol] = 0 elif 'Account has insufficient balance' in future_result.exception( ).args[0]: print_([ 'ERROR', 'margin', symbol, fns[i].__name__, args, 'insufficient funds; schedule balance update' ]) self.time_keepers['update_balance'] = 0 else: print_([ 'ERROR', 'margin', symbol, future_result.exception(), fns, args_list ]) else: self.try_wrapper(self.update_after_execution, (future_result.result(), )) self.locks[symbol].release() except (Exception) as e: print('\n\nERROR with lock_execute_release', e)
def start_updater(self): if not self.running_updater: self.running_updater = True threaded(self.updater)()