Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
def test_get_asset_positions_for_date(mocker):
    marquee_id = 'MQA1234567890'
    position_date = dt.date(2019, 2, 19)

    mock_response = {
        'results': ({
            'id':
            'mock1',
            'positionDate':
            '2019-02-19',
            'lastUpdateTime':
            '2019-02-19T12:10:32.401Z',
            'positions': [{
                'assetId': 'MQA123',
                'quantity': 0.3
            }, {
                'assetId': 'MQA456',
                'quantity': 0.7
            }],
            'type':
            'open',
            'divisor':
            100
        }, {
            'id':
            'mock2',
            'positionDate':
            '2019-02-19',
            'lastUpdateTime':
            '2019-02-20T05:04:32.981Z',
            'positions': [{
                'assetId': 'MQA123',
                'quantity': 0.4
            }, {
                'assetId': 'MQA456',
                'quantity': 0.6
            }],
            'type':
            'close',
            'divisor':
            120
        })
    }

    expected_response = (PositionSet(
        'mock1', dt.date(2019, 2, 19), dup.parse('2019-02-19T12:10:32.401Z'),
        (Position(assetId='MQA123', quantity=0.3),
         Position(assetId='MQA456', quantity=0.7)), 'open', 100),
                         PositionSet(
                             'mock2', dt.date(2019, 2, 19),
                             dup.parse('2019-02-20T05:04:32.981Z'),
                             (Position(assetId='MQA123', quantity=0.4),
                              Position(assetId='MQA456', quantity=0.6)),
                             'close', 120))

    # mock GsSession
    mocker.patch.object(GsSession.__class__,
                        'current',
                        return_value=GsSession.get(Environment.QA, 'client_id',
                                                   'secret'))
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    # run test
    response = GsAssetApi.get_asset_positions_for_date(marquee_id,
                                                       position_date)

    GsSession.current._get.assert_called_with(
        '/assets/{id}/positions/{date}'.format(id=marquee_id,
                                               date=position_date))

    testfixtures.compare(response, expected_response)

    mock_response = {
        'results': [{
            'id':
            'mock',
            'positionDate':
            '2019-02-19',
            'lastUpdateTime':
            '2019-02-20T05:04:32.981Z',
            'positions': [{
                'assetId': 'MQA123',
                'quantity': 0.4
            }, {
                'assetId': 'MQA456',
                'quantity': 0.6
            }],
            'type':
            'close',
            'divisor':
            120
        }]
    }

    expected_response = (PositionSet(
        'mock', dt.date(2019, 2, 19), dup.parse('2019-02-20T05:04:32.981Z'),
        (Position(assetId='MQA123', quantity=0.4),
         Position(assetId='MQA456', quantity=0.6)), 'close', 120), )

    # mock GsSession
    mocker.patch.object(GsSession.__class__,
                        'current',
                        return_value=GsSession.get(Environment.QA, 'client_id',
                                                   'secret'))
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    # run test

    response = GsAssetApi.get_asset_positions_for_date(marquee_id,
                                                       position_date, "close")

    testfixtures.compare(response, expected_response)

    GsSession.current._get.assert_called_with(
        '/assets/{id}/positions/{date}?type=close'.format(id=marquee_id,
                                                          date=position_date))