Esempio n. 1
0
def test_get_portfolio_positions(mocker):
    id_1 = 'MP1'
    start_date = dt.date(2019, 2, 18)
    end_date = dt.date(2019, 2, 19)

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

    expected_response = (
        PositionSet('mock1', start_date, dup.parse('2019-02-19T12:10:32.401Z'), (
            Position(assetId='MQA123', quantity=0.3),
            Position(assetId='MQA456', quantity=0.7)
        )),
        PositionSet('mock2', end_date, dup.parse('2019-02-20T05:04:32.981Z'), (
            Position(assetId='MQA123', quantity=0.4),
            Position(assetId='MQA456', quantity=0.6)
        ))
    )

    # 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 = GsPortfolioApi.get_positions(id_1, start_date, end_date)

    GsSession.current._get.assert_called_with(
        '/portfolios/{id}/positions?type=close&startDate={sd}&endDate={ed}'.format(id=id_1, sd=start_date, ed=end_date))

    assert response == expected_response
Esempio n. 2
0
 def get_position_sets(self,
                       start: dt.date = DateLimit.LOW_LIMIT.value,
                       end: dt.date = dt.date.today(),
                       position_type: PositionType = PositionType.CLOSE) -> List[PositionSet]:
     if self.positioned_entity_type == EntityType.ASSET:
         response = GsAssetApi.get_asset_positions_for_dates(self.id, start, end, position_type)
         return [PositionSet.from_target(position_set) for position_set in response]
     if self.positioned_entity_type == EntityType.PORTFOLIO:
         response = GsPortfolioApi.get_positions(portfolio_id=self.id,
                                                 start_date=start,
                                                 end_date=end)
         return [PositionSet.from_target(position_set) for position_set in response]
     raise NotImplementedError