async def get_last_nbrb_global_with_rates(): async with connection() as cur: return await anext( cur.execute( nbrb.select((nbrb.c.kind == NbrbKind.GLOBAL.value) & (nbrb.c.byn != None)).order_by( sa.desc(nbrb.c.date)).limit(1)), None)
async def get_last_rolling_average_date() -> Optional[datetime.date]: async with connection() as cur: row = await anext( cur.execute( rolling_average.select( rolling_average.c.date < LAST_ROLLING_AVERAGE_MAGIC_DATE). order_by(sa.desc(rolling_average.c.date)).limit(1)), None) return row and row.date
async def get_accumulated_error(date: datetime.date) -> Optional[Decimal]: async with connection() as cur: row = await anext( cur.execute( sa.select([trade_date.c.accumulated_error ]).select_from(trade_date).where( (trade_date.c.accumulated_error != None) & (trade_date.c.date <= date)).order_by( sa.desc(trade_date.c.date)).limit(1)), None) return row and row.accumulated_error
async def get_last_external_currency_datetime( currency: str) -> datetime.datetime: async with connection() as cur: row = await anext( cur.execute( external_rate.select( external_rate.c.currency == currency).order_by( sa.desc(external_rate.c.timestamp)).limit(1)), None) return datetime.datetime.fromtimestamp( row.timestamp if row is not None else 0)
async def get_the_last_external_rates( currencies: Iterable[str], end_dt: datetime.datetime) -> Dict[str, dict]: currency_to_data = {} end_dt = end_dt.timestamp() async with connection() as cur: for currency in currencies: row = await anext( cur.execute( external_rate.select( (external_rate.c.timestamp <= end_dt) & (external_rate.c.currency == currency)).order_by( sa.desc(external_rate.c.timestamp)).limit(1)), None) if row is not None: currency_to_data[currency] = _parse_external_rate_row(row) return currency_to_data
async def get_last_predicted_trade_date(): async with connection() as cur: return await anext( cur.execute( trade_date.select(trade_date.c.predicted != None).order_by( sa.desc(trade_date.c.date)).limit(1)), None)
async def get_last_nbrb_record(kind: NbrbKind): async with connection() as cur: return await anext( cur.execute( nbrb.select(nbrb.c.kind == kind.value).order_by( sa.desc(nbrb.c.date)).limit(1)), None)