def get_tick(self, pair, reverse=False): # book = { # lastUpdateId: str, # bids: [[price: float, volume: float]], # asks: [[price: float, volume: float]] # } book = self.client.get_order_book(symbol=pair) def get_price(orders, unit): if len(orders) == 0: return None acc = 0 for [price, volume] in orders: acc += float(volume) if acc >= unit: break return float(price) bid = get_price(book['bids'], self.tickUnit) ask = get_price(book['asks'], self.tickUnit) if bid is None or ask is None: return None if not reverse: return OneTick(ask, bid) else: return OneTick(1. / bid, 1. / ask)
def get_tick(self): self.logger.debug("Fetching Quoine tick.") try: board = self.call_json_api('GET', API_PATH_BOARD) except QuoineAPIError as e: self.logger.warn( 'Failed to fetch Quoine tick, API error={e}'.format(e=e)) return None ask_levels = board[BOARD_SIDE_ASK] bid_levels = board[BOARD_SIDE_BID] price_ask = None price_bid = None sum = 0 for price, amount in ask_levels: sum = sum + float(amount) if sum >= PRICE_TICK_SIZE: price_ask = price break if price_ask is None: price_ask, amount = ask_levels[-1] sum = 0 for price, amount in bid_levels: sum = sum + float(amount) if sum >= PRICE_TICK_SIZE: price_bid = price break if price_bid is None: price_bid, amount = bid_levels[-1] self.logger.debug( 'Completed fetching Quoine tick, ask={ask}, bid={bid}'.format( ask=price_ask, bid=price_bid)) return OneTick(price_ask, price_bid)
def all(self, exchangers=None, start=None, end=None, limit=10, order=-1): """ (self: Ticks, exchangers: [str]?, start: float?, end: float?, limit: int?) -> {(exchanger: str): [Tick]} """ if exchangers is None: exchangers = Tick.exchangers() order = 1 if order > 0 else -1 collections = [self.collections[e] for e in exchangers] conditions = [] if start is not None: if self.saveDateInString: dateStart = datetime.datetime.fromtimestamp(start) dateStart = datetimeToStr(dateStart) conditions.append({'datetime': {'$gt': dateStart}}) else: conditions.append({'datetime': {'$gt': start}}) if end is not None: if self.saveDateInString: dateEnd = datetime.datetime.fromtimestamp(end) dateEnd = datetimeToStr(dateEnd) conditions.append({'datetime': {'$lt': dateEnd}}) else: conditions.append({'datetime': {'$lt': end}}) if len(conditions) > 0: curs = [c.find({'$and': conditions}) for c in collections] else: curs = [c.find() for c in collections] curs = [c.sort('datetime', order).limit(limit) for c in curs] result = {} for e, cur in zip(exchangers, curs): result[e] = [OneTick.fromDict(t) for t in cur] return result
def sync(dashb, ticksModel, dateStart=None): finish = datetime.datetime.now().timestamp() for exchanger in SYNC_EXCHANGERS: if dateStart is None: latest = ticksModel.one(exchanger) if latest is not None: dateStart = latest.date else: dateStart = SYNC_DATE_START start = dateStart.timestamp() count = 0 logger.debug('Start from {s} to {e}.'.format(s=start, e=finish)) while start < finish: logger.info('Syncing {ex} after {s}.'.format( ex=exchanger, s=datetime.datetime.fromtimestamp(start).isoformat())) res = dashb.requestTicks(exchanger, start) ticks = res['ticks'][exchanger] if len(ticks) > 0: toSave = [OneTick.fromDict(t) for t in ticks] logger.debug('Writing {count} items.'.format(count=len(ticks))) ticksModel.saveAll(exchanger, toSave) start = ticks[-1]['datetime'] count += len(ticks) else: start = start + SYNC_STEP_SECONDS time.sleep(SYNC_INTERVAL_SECONDS) if count == 0: logger.error('No ticks synchronized. Ticker may not be working!!')
def one(self, exchanger, order=-1): collection = self.collections[exchanger] items = collection.find().sort('datetime', order).limit(1) item = next(items, None) if item is not None: item = OneTick.fromDict(item) print(item) return item
def all(self, exchanger, dateStart=None, dateEnd=None, order=1): collection = self.collections[exchanger] conditions = [] if dateStart is not None: conditions.append({'datetime': {'$gte': dateStart.timestamp()}}) if dateEnd is not None: conditions.append({'datetime': {'$lte': dateEnd.timestamp()}}) if len(conditions) > 0: items = collection.find({'$and': conditions}) else: items = collection.find({}) items = items.sort('datetime', order) return (OneTick.fromDict(t) for t in items)
def one(self, exchangers=None): """ (self: Ticks, exchangers: [str]?) -> Tick """ if exchangers is None: exchangers = Tick.exchangers() collections = [self.collections[e] for e in exchangers] curs = [c.find().sort('datetime', -1).limit(1) for c in collections] result = {} for e, cur in zip(exchangers, curs): t = next(cur, None) if t is None: result[e] = None else: result[e] = OneTick.fromDict(t) return Tick(result)
def get_tick(self, code='FX_BTC_JPY'): self.logger.debug('Fetching BitFlyer tick.') try: path = API_PATH_BOARD.format(product_code=code) board = self.call_json_api('GET', path) except BitFlyerAPIError as e: self.logger.warning( 'Failed to fetch BitFlyer tick, API error={e}'.format(e=e)) return None ask_levels = board[BOARD_SIDE_ASK] bid_levels = board[BOARD_SIDE_BID] self.logger.debug('#asks={asks}, #bids={bids}'.format( asks=len(ask_levels), bids=len(bid_levels))) price_ask = None price_bid = None sum = 0 for v in ask_levels: sum = sum + float(v[BOARD_SIZE]) if sum >= PRICE_TICK_SIZE: price_ask = v[BOARD_PRICE] break if price_ask is None: price_ask = ask_levels[-1][BOARD_PRICE] sum = 0 for v in bid_levels: sum = sum + float(v[BOARD_SIZE]) if sum >= PRICE_TICK_SIZE: price_bid = v[BOARD_PRICE] break if price_bid is None: price_bid = ask_levels[-1][BORAD_PRICE] self.logger.debug( 'Completed fetching BitFlyer tick, ask={ask}, bid={bid}.'.format( ask=price_ask, bid=price_bid)) return OneTick(price_ask, price_bid)