Beispiel #1
0
    def get_quotes(cls, isin, time_range):
        today = date_to_key(datetime.now())
        new_range = time_range
        if time_range.end == today:
            pre = time_range.end_time - timedelta(days=1)
            new_range = TimeRange(time_range.start_time, pre)

        data = cls.get(isin=isin, time=new_range, sort='time',
                       fields={'time': 1, 'price': 1})
        result = OrderedDict()

        last_date = None
        for record in data:
            result[date_to_key(record['time'])] = record['price']
            last_date = record['time']
            last_date += timedelta(days=1)

        if not last_date:
            data = OrdersManager.get(isin=isin, sort=('time', -1))
            if data:
                last_date = data[0]['date']
        if not last_date:
            last_date = time_range.start_time

        last_date = last_date.replace(hour=0, minute=0, second=0)
        if date_to_key(last_date) < new_range.end:
            new_range = TimeRange(last_date, new_range.end_time)
            data = QuotesLoader.history(isin, new_range)
            if data:
                cls.insert(data)
                for record in data:
                    if date_to_key(record['time']) > new_range.end:
                        break
                    result[date_to_key(record['time'])] = record['price']

        if time_range.end == today:
            price = QuotesLoader.current(isin)
            result[today] = price

        return result
Beispiel #2
0
 def get_data(cls, portfolio_id=None, time_range=None, broker_id=None):
     filters = {'sort': 'date'}
     if broker_id:
         filters['broker'] = broker_id
     if portfolio_id:
         filters['portfolio'] = portfolio_id
     if time_range:
         filters['date'] = time_range
     data = cls.get(**filters)
     data = [cls.model(date=date_to_key(row['date']), cur=row['cur'],
                       sum=row['sum'], portfolio=row['portfolio'],
                       broker=row['broker'], comment=row['comment'])
             for row in data]
     return data
Beispiel #3
0
 def get_data(cls, portfolio_id=None, time_range=None, broker_id=None,
              sort=1):
     filters = {'sort': ('date', sort)}
     if broker_id:
         filters['broker'] = broker_id
     if portfolio_id:
         filters['portfolio'] = portfolio_id
     if time_range:
         filters['date'] = time_range
     data = cls.get(**filters)
     data = [cls.model(date=date_to_key(row['date']), isin=row['isin'],
                       quantity=row['quantity'], sum=row['sum'],
                       cur=row['cur'], portfolio=row['portfolio'],
                       broker=row['broker'], price=row['price'],
                       market=row['market']) for row in data]
     return data
Beispiel #4
0
 def get_dates(self, time_range):
     date = time_range.start_time
     while date < time_range.end_time:
         yield date_to_key(date)
         date += timedelta(days=1)