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)