def reset_book(self): self._asks = RBTree() self._bids = RBTree() self.tref = datetime.datetime.now() # res = self._client.get_product_order_book(product_id=self.product_id, level=3) ################## params = {'level': 3} r = requests.get('https://api.gdax.com/products/{}/book'.format( self.product_id), params=params, timeout=30) try: res = r.json() except: res['bids'] = {} res['asks'] = {} res['sequence'] = 0 # r.raise_for_status() ################## for bid in res['bids']: self.add1({ 'id': bid[2], 'side': 'buy', 'price': Decimal(bid[0]), 'size': Decimal(bid[1]) }) for ask in res['asks']: self.add1({ 'id': ask[2], 'side': 'sell', 'price': Decimal(ask[0]), 'size': Decimal(ask[1]) }) self._sequence = res['sequence']
def __init__(self, endpoint="https://www.bitmex.com/api/v1", symbol='XBTUSD'): '''Connect to the websocket and initialize data stores.''' self.logger = logging.getLogger(__name__) self.logger.debug("Initializing WebSocket.") self.endpoint = endpoint self.symbol = symbol self.data = {} self.keys = {} self.exited = False self._asks = RBTree() self._bids = RBTree() # We can subscribe right in the connection querystring, so let's build that. # Subscribe to all pertinent endpoints wsURL = self.__get_url() self.logger.debug("Connecting to %s" % wsURL) self.__connect(wsURL, symbol) self.logger.info('Connected to WS, waiting for partials.') # Connected. Wait for partials self.__wait_for_symbol(symbol) self.logger.info('Got all market data. Starting.')
def process_snapshot(self, message: Dict): """ Process a snapshot message :param message: json """ # If a snapshot is sent reset trees self._asks = RBTree() self._bids = RBTree() # Parse all asks and add them to tree for ask in message['asks']: price, size = ask price = Decimal(price) size = Decimal(size) self._asks.insert(price, size) # Parse all bids and add them to tree for bid in message['bids']: price, size = bid price = Decimal(price) size = Decimal(size) self._bids.insert(price, size)
def __init__(self, ticker, threshold): GDAX.OrderBook.__init__(self, product_id = ticker) self._threshold = threshold self._buyWhales = RBTree() self._sellWhales = RBTree() self._topBuyWhale = None self._topSellWhale = None
def __init__(self, product_id='BTC-USD', feed=None, log_to=None): self._product_id = product_id self._asks = RBTree() self._bids = RBTree() self._sequence = -1 self._current_ticker = None self._feed = feed
def __init__(self, product_id='BTC-USD'): print("Initializing order Book websocket for " + product_id) self.product = product_id super(GDaxBook, self).__init__(products=[self.product]) super(GDaxBook, self).start() self._asks = RBTree() self._bids = RBTree() self._client = PublicClient() self._sequence = -1 self._current_ticker = None
def __init__(self, product_id='BTC-USD', log_to=None): super(OrderBook, self).__init__(products=product_id) self._asks = RBTree() self._bids = RBTree() self._client = PublicClient() self._sequence = -1 self._log_to = log_to if self._log_to: assert hasattr(self._log_to, 'write') self._current_ticker = None
def __init__(self, product_id, log_to=None): self._asks = RBTree() self._bids = RBTree() self._client = PublicClient() self._sequence = -1 self.sync = -1 self.product_id = product_id self._log_to = log_to if self._log_to: assert hasattr(self._log_to, 'write') self._current_ticker = None
def __init__(self, product_id='BTC-USD'): print("Initializing order Book websocket for " + product_id) self.product = product_id super(GDaxBook, self).__init__(url="wss://ws-feed.pro.coinbase.com", products=[self.product], channels=["ticker"]) super(GDaxBook, self).start() self._asks = RBTree() self._bids = RBTree() self._client = PublicClient() self._sequence = -1 self._current_ticker = None
def on_message(self, message): if self._log_to: pickle.dump(message, self._log_to) sequence = message['sequence'] # initialize the order book from REST api if self._sequence == -1: self._asks = RBTree() self._bids = RBTree() res = self._client.get_product_order_book( product_id=self.product_id, level=3) for bid in res['bids']: self.add({ 'id': bid[2], 'side': 'buy', 'price': bid[0], 'size': bid[1] }) for ask in res['asks']: self.add({ 'id': ask[2], 'side': 'sell', 'price': ask[0], 'size': ask[1] }) self._sequence = res['sequence'] if sequence <= self._sequence: # ignore older messages (e.g. before order book initialization from getProductOrderBook) return elif sequence > self._sequence + 1: print( 'Error: messages missing ({} - {}). Re-initializing websocket.' .format(sequence, self._sequence)) self.close() self.start() return msg_type = message['type'] if msg_type == 'open': self.add(message) # market orders will not have a remaining_size or price field as they are never on the open order book so we # ignore them. elif msg_type == 'done' and 'price' in message: self.remove(message) elif msg_type == 'match': self.match(message) self._current_ticker = message elif msg_type == 'change': self.change(message) self._sequence = sequence
def __init__(self, product_id='BTC-USD', log_to=None, public_client=None): self._product_id = product_id self._asks = RBTree() self._bids = RBTree() if public_client is None: public_client = PublicGDAXClient() self._client = public_client self._sequence = -1 self._log_to = log_to if self._log_to: assert hasattr(self._log_to, 'write') self._current_ticker = None
def start(self, signal_q): authclient = authenticated_client.AuthenticatedClient( key=API_KEY, b64secret=API_SECRET, passphrase=API_PASS, api_url=API_URL) logging.basicConfig( filename='websock2.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s %(message)s') lock = threading.Lock() gv.lock = threading.Lock() gv._asks = RBTree() gv._bids = RBTree() gv.logger = logging.getLogger(__name__) gv.transactions_onbook = [] gv.market_price = None gv.sql_to_pandas = None gv.USD = None gv.crypto_balance = None signal_q = signal_q # bid_ask_q = Queue() M_get_depth = Queue() auth_user_msg_q = Queue() manager = Manager() M_bidask = manager.dict(({})) M_get_depth = manager.Value(ctypes.c_char_p, 'None') # print(M_bidask['bid']) logging.info('Start of Program') order_book = orderbook.OrderBook(API_KEY=API_KEY, API_SECRET=API_SECRET, API_PASS=API_PASS, API_URL=API_URL, WSS_URL=WSS_URL, products=products, M_bidask=M_bidask, authclient=authclient, auth_user_msg_q=auth_user_msg_q, M_get_depth=M_get_depth) order_book.start() print('started orderbook') orderwatch2 = orderwatch.orderwatch(M_bidask=M_bidask, signal_q=signal_q, auth_user_msg_q=auth_user_msg_q, M_get_depth=M_get_depth, products=products, authclient=authclient) orderwatch2.start() print('started orderwatch')
def on_message(self, message): if self._log_to: pickle.dump(message, self._log_to) sequence = message['sequence'] if self._sequence == -1: self._asks = RBTree() self._bids = RBTree() res = self._client.get_product_order_book(level=3) for bid in res['bids']: self.add({ 'id': bid[2], 'side': 'buy', 'price': Decimal(bid[0]), 'size': Decimal(bid[1]) }) for ask in res['asks']: self.add({ 'id': ask[2], 'side': 'sell', 'price': Decimal(ask[0]), 'size': Decimal(ask[1]) }) self._sequence = res['sequence'] if sequence <= self._sequence: # ignore older messages (e.g. before order book initialization from getProductOrderBook) return elif sequence > self._sequence + 1: print( 'Error: messages missing ({} - {}). Re-initializing websocket.' .format(sequence, self._sequence)) self.close() self.start() return msg_type = message['type'] if msg_type == 'open': self.add(message) elif msg_type == 'done' and 'price' in message: self.remove(message) elif msg_type == 'match': self.match(message) self._current_ticker = message elif msg_type == 'change': self.change(message) self._sequence = sequence
def onMessage(self, message): #Logging.logger.info(message) sequence = message['sequence'] if self._sequence == -1: self._asks = RBTree() self._bids = RBTree() res = self._client.getProductOrderBook(level=3) for bid in res['bids']: self.add({ 'id': bid[2], 'side': 'buy', 'price': Decimal(bid[0]), 'size': Decimal(bid[1]) }) for ask in res['asks']: self.add({ 'id': ask[2], 'side': 'sell', 'price': Decimal(ask[0]), 'size': Decimal(ask[1]) }) self._sequence = res['sequence'] if sequence <= self._sequence: # ignore older messages (e.g. before order book initialization from getProductOrderBook) return elif sequence > self._sequence + 1: Logging.logger.error( 'Error: messages missing ({} - {}). Re-initializing websocket.' .format(sequence, self._sequence)) self.close() self.start() return msg_type = message['type'] if msg_type == 'open': self.add(message) elif msg_type == 'done' and 'price' in message: self.remove(message) elif msg_type == 'match': self.match(message) elif msg_type == 'change': self.change(message) self._sequence = sequence if not self.bookChanged is None: self.bookChanged()
def __init__(self): self.tree = RBTree() self.prices = dict() # { price : Queue } objects self.orders = dict() # { order_id : Order } objects self.volume = 0 # Contains total quantity from all Orders in tree self.num_orders = 0 # Contains count of Orders in tree self.depth = 0 # Number of different prices in tree (http://en.wikipedia.org/wiki/Order_book_(trading)#Book_depth)
def __init__(self): WordCollection.word_tree = RBTree() with open('dictionary.txt', 'r', encoding='utf8') as file: WordCollection.word_list = file.readlines() for line in WordCollection.word_list: WordCollection.word_tree.insert(line.rstrip(), True)
def main(): ''' main ''' files = [] try: input_xml = parse_and_remove('/home/den/small_table_xml', 'route-information/route-table/rt') files.append(input_xml) except FileNotFoundError: pass try: ipv6_input_xml = parse_and_remove('/home/den/ipv6_xml', 'route-information/route-table/rt') files.append(ipv6_input_xml) except FileNotFoundError: pass tree = RBTree() prep = 'http://xml.juniper.net/junos/12.3R3/junos-routing' ns = XMLNamespaces(prep=prep) for file in files: create_tree(file, tree, prep, ns) send_update(tree)
def __init__(self): self.price_tree = RBTree() self.price_map = {} # Dictionary containing price : OrderList object self.order_map = {} # Dictionary containing order_id : Order object self.volume = 0 # Contains total quantity from all Orders in tree self.num_orders = 0 # Contains count of Orders in tree self.depth = 0 # Number of different prices in tree
def build_minimal_height_bst_bintrees(nums): # here SelfBalancingBST would be your implementaion of BST with self balancing property. # tree = SelfBalancingBST() tree = RBTree() for i in nums: tree.insert(i, i) return tree
def count_duplicates(lines): h, m = RBTree(), 0 for l in lines: if (l in h): h[l] += 1 else: h[l] = 1 if (m < h[l]): m = h[l] return h, m
def __init__(self, saveMessage=0, product_id='BTC-USD'): self.url = "wss://ws-feed.gdax.com/" self.ws = create_connection(self.url) self.product_id = product_id self.sequence = -1 self.stop = False self._sequence = -1 self._client = PublicClient() self._asks = RBTree() self._bids = RBTree() self._current_ticker = None self.saveMessage = saveMessage self.t1 = None self.t2 = None self.t3 = None
def __init__(self, delta=0.01, K=25, CX=1.1): self.delta = delta self.K = K self.CX = CX self.centroids = RBTree() self.nreset = 0 self.reset()
def __init__(self): self.price_tree = RBTree() self.price_map = {} # Dictionary containing price : std::list<std::shared_ptr<Order>> object self.order_map = {} # Dictionary containing order_id : Order object self.volume = 0 # Contains total quantity from all Orders in tree self.num_orders = 0 # Contains count of Orders in tree self.depth = 0 # Number of different prices in tree (http://en.wikipedia.org/wiki/Order_book_(trading)#Book_depth)
def __init__(self): self.priceTree = RBTree() self.priceMap = {} # Map from price -> orderList object self.orderMap = {} # Order ID to Order object self.volume = 0 # How much volume on this side? self.nOrders = 0 # How many orders? self.lobDepth = 0 # How many different prices on lob?
def example_centroids(): return RBTree([ (-1.1, Centroid(-1.1, 1)), (-0.5, Centroid(-0.5, 1)), (0.1, Centroid(0.1, 1)), (1.5, Centroid(1.5, 1)), ])
def calc(arr, max_size): import heapq; print max_size nxt = arr[:]; pos = {}; for i in range(len(arr)): pos[arr[i]] = len(arr); for i in reversed(range(len(arr))): nxt[i] = pos[arr[i]]; pos[arr[i]] = i; print nxt; prio_queue = RBTree(); in_queue = {}; cache_miss = 0; cur_in_queue_cnt = 0; print arr; for i in range(len(arr)): out_str = str(i)+" "; print prio_queue; #cache missed if in_queue.has_key(arr[i]) == False: #while( space need): out_str += "Missed: "; cache_miss +=1; #add into cache if (cur_in_queue_cnt+1>max_size): del in_queue[prio_queue.min_item()[1]]; cur_in_queue_cnt-=1; out_str+= str(prio_queue.min_item()[1])+" is kicked. And "; prio_queue.discard(prio_queue.min_key()); out_str+= str(arr[i])+" is added into Q."; prio_queue.insert((-nxt[i],i),arr[i]); cur_in_queue_cnt+=1; in_queue[arr[i]] = 1; else : prio_queue.discard(prio_queue.ceiling_key((-i,0))); prio_queue.insert((-nxt[i],i),arr[i]); out_str += str(arr[i])+" hit."; print out_str; print cur_in_queue_cnt; #print prio_queue.q; print "Cache hit: "+str(len(arr)-cache_miss); print "Cache miss: "+str(cache_miss); print "Ratio: "+ str((len(arr)-cache_miss)*1.0/len(arr));
def test_add_centroid_if_key_already_present(self, empty_tdigest, example_positive_centroids): empty_tdigest.C = example_positive_centroids new_centroid = Centroid(1.1, 5) empty_tdigest._add_centroid(new_centroid) assert (empty_tdigest.C - RBTree([ (0.5, Centroid(0.5, 1)), (1.1, Centroid(1.1, 1 + 5)), (1.5, Centroid(1.5, 1)), ])).is_empty()
def reset_book(self, snapshot): self._asks = RBTree() self._bids = RBTree() for bid in snapshot['bids']: self._add({ 'id': bid[2], 'side': 'buy', 'price': Decimal(bid[0]), 'size': Decimal(bid[1]) }) for ask in snapshot['asks']: self._add({ 'id': ask[2], 'side': 'sell', 'price': Decimal(ask[0]), 'size': Decimal(ask[1]) }) self._sequence = snapshot['sequence']
def reset_book(self): self._asks = RBTree() self._bids = RBTree() res = self._client.get_product_order_book(product_id=self.product_id, level=3) for bid in res['bids']: self.add({ 'id': bid[2], 'side': 'buy', 'price': Decimal(bid[0]), 'size': Decimal(bid[1]) }) for ask in res['asks']: self.add({ 'id': ask[2], 'side': 'sell', 'price': Decimal(ask[0]), 'size': Decimal(ask[1]) }) self._sequence = res['sequence']
def calculate_medians_with_rbt(numbers): """Calculate the sum of all 10,000 medians, modulo 10000""" smaller = RBTree() # For storing the smaller half of numbers larger = RBTree() # For storing the larger half of numbers medians = [] for number in numbers: if not len(smaller) or smaller.max_item()[0] > number: smaller.insert(number, None) if len(smaller) > len(larger) + 1: larger.insert(smaller.max_item()[0], None) else: larger.insert(number, None) if len(larger) > len(smaller) + 1: smaller.insert(larger.min_item()[0], None) if len(smaller) >= len(larger): medians.append(smaller.max_item()[0]) else: medians.append(larger.min_item()[0]) return medians