def get_latest_positions(asset_id: str, position_type: PositionType = None ) -> PositionSet: url = '/assets/{id}/positions/last'.format(id=asset_id) if position_type is not None and position_type is not PositionType.ANY: url += '?type={ptype}'.format(ptype=position_type if isinstance( position_type, str) else position_type.value) results = GsSession.current._get(url)['results'] return PositionSet.from_dict(results)
def get_asset_positions_for_date( asset_id: str, position_date: dt.date, position_type: PositionType = None, ) -> Tuple[PositionSet, ...]: position_date_str = position_date.isoformat() url = '/assets/{id}/positions/{date}'.format(id=asset_id, date=position_date_str) if position_type is not None: url += '?type=' + position_type.value results = GsSession.current._get(url)['results'] return tuple(PositionSet.from_dict(r) for r in results)
def get_asset_positions_for_date( asset_id: str, position_date: dt.date, position_type: PositionType = None, ) -> Tuple[PositionSet, ...]: position_date_str = position_date.isoformat() url = f'/assets/{asset_id}/positions/{position_date_str}' if position_type is not None: url += f'?type={position_type}' if isinstance( position_type, str) else f'?type={position_type.value}' results = GsSession.current._get(url)['results'] return tuple(PositionSet.from_dict(r) for r in results)
def get_asset_positions_for_dates( asset_id: str, start_date: dt.date, end_date: dt.date, position_type: PositionType = PositionType.CLOSE, ) -> Tuple[PositionSet, ...]: position_type = position_type if isinstance( position_type, str) else position_type.value position_sets = [] periods = (end_date - start_date).days // 30 start_date_str = start_date.isoformat() if periods > 0: end_dates = pd.date_range(start=start_date, end=end_date, periods=periods, closed='right') for date in end_dates: end_date_str = date.date().isoformat() url = f'/assets/{asset_id}/positions?startDate={start_date_str}&endDate={end_date_str}&type={position_type}' try: position_sets += GsSession.current._get( url)['positionSets'] start_date_str = (date.date() + dt.timedelta(days=1)).isoformat() except HTTPError as err: raise ValueError( f'Unable to fetch position data at {url} with {err}') else: end_date_str = end_date.isoformat() url = f'/assets/{asset_id}/positions?startDate={start_date_str}&endDate={end_date_str}&type={position_type}' try: position_sets += GsSession.current._get(url)['positionSets'] except HTTPError as err: raise ValueError( f'Unable to fetch position data at {url} with {err}') return tuple(PositionSet.from_dict(r) for r in position_sets)