def synchronize_orders(self): # If keeper balance is below `--min-eth-balance`, cancel all orders but do not terminate # the keeper, keep processing blocks as the moment the keeper gets a top-up it should # resume activity straight away, without the need to restart it. if eth_balance(self.web3, self.our_address) < self.min_eth_balance: self.logger.warning( "Keeper ETH balance below minimum. Cancelling all orders.") self.cancel_all_orders() return bands = Bands(self.bands_config) block_number = self.web3.eth.blockNumber target_price = self.price_feed.get_price() # If the is no target price feed, cancel all orders but do not terminate the keeper. # The moment the price feed comes back, the keeper will resume placing orders. if target_price is None: self.logger.warning( "Cancelling all orders as no price feed available.") self.cancel_all_orders() return self.remove_expired_orders(block_number) self.cancel_orders( itertools.chain( bands.excessive_buy_orders(self.our_buy_orders(), target_price), bands.excessive_sell_orders(self.our_sell_orders(), target_price), bands.outside_orders(self.our_buy_orders(), self.our_sell_orders(), target_price)), block_number) self.top_up_bands(bands.buy_bands, bands.sell_bands, target_price)
def synchronize_orders(self): """Update our positions in the order book to reflect keeper parameters.""" if eth_balance(self.web3, self.our_address) < self.min_eth_balance: self.lifecycle.terminate( "Keeper balance is below the minimum, terminating.") self.cancel_orders(self.our_orders()) return bands = Bands(self.bands_config) our_orders = self.our_orders() target_price = self.price_feed.get_price() if target_price is None: self.logger.warning( "Cancelling all orders as no price feed available.") self.cancel_orders(our_orders) return self.cancel_orders( itertools.chain( bands.excessive_buy_orders(self.our_buy_orders(our_orders), target_price), bands.excessive_sell_orders(self.our_sell_orders(our_orders), target_price), bands.outside_orders(self.our_buy_orders(our_orders), self.our_sell_orders(our_orders), target_price))) self.top_up_bands(our_orders, bands.buy_bands, bands.sell_bands, target_price)
def synchronize_orders(self): # If market is closed, cancel all orders but do not terminate the keeper. if self.otc.is_closed(): self.logger.warning("Marked is closed. Cancelling all orders.") self.cancel_all_orders() return # If keeper balance is below `--min-eth-balance`, cancel all orders but do not terminate # the keeper, keep processing blocks as the moment the keeper gets a top-up it should # resume activity straight away, without the need to restart it. if eth_balance(self.web3, self.our_address) < self.min_eth_balance: self.logger.warning( "Keeper ETH balance below minimum. Cancelling all orders.") self.cancel_all_orders() return bands = Bands(self.bands_config) our_orders = self.our_orders() target_price = self.price_feed.get_price() # If the is no target price feed, cancel all orders but do not terminate the keeper. # The moment the price feed comes back, the keeper will resume placing orders. if target_price is None: self.logger.warning( "No price feed available. Cancelling all orders.") self.cancel_all_orders() return # If there are any orders to be cancelled, cancel them. It is deliberate that we wait with topping-up # bands until the next block. This way we would create new orders based on the most recent price and # order book state. We could theoretically retrieve both (`target_price` and `our_orders`) again here, # but it just seems cleaner to do it in one place instead of in two. orders_to_cancel = list( itertools.chain( bands.excessive_buy_orders(self.our_buy_orders(our_orders), target_price), bands.excessive_sell_orders(self.our_sell_orders(our_orders), target_price), bands.outside_orders(self.our_buy_orders(our_orders), self.our_sell_orders(our_orders), target_price))) if len(orders_to_cancel) > 0: self.cancel_orders(orders_to_cancel) else: self.top_up_bands(our_orders, bands.buy_bands, bands.sell_bands, target_price) # We do wait some time after the orders have been created. The reason for that is sometimes # orders that have been just placed were not picked up by the next `our_orders()` call # (one can presume the block hasn't been fully imported into the node yet), which made # the keeper try to place same order(s) again. Of course the second transaction did fail, but it # resulted in wasted gas and significant delay in keeper operation. # # There is no specific reason behind choosing to wait exactly 7s. time.sleep(7)