def test_fetch_historical_prices_by_epic_and_numpoints(self, ig_service: IGService):
     response = ig_service.fetch_historical_prices_by_epic_and_num_points(
         "CS.D.EURUSD.MINI.IP", "H", 4
     )
     assert isinstance(response["allowance"], dict)
     assert isinstance(response["prices"], pd.DataFrame)
     assert len(response["prices"]) == 4
Пример #2
0
    def test_historical_prices_by_epic_and_num_points_happy(self):

        # fetch_historical_prices_by_epic_and_num_points - daily resolution

        with open('tests/data/historic_prices_v2.json', 'r') as file:
            response_body = json.loads(file.read())

        responses.add(
            responses.GET,
            'https://demo-api.ig.com/gateway/deal/prices/MT.D.GC.Month2.IP/DAY/10',
            match_querystring=False,
            headers={
                'CST': 'abc123',
                'X-SECURITY-TOKEN': 'xyz987'
            },
            json=response_body,
            status=200)

        ig_service = IGService('username', 'password', 'api_key', 'DEMO')
        result = ig_service.fetch_historical_prices_by_epic_and_num_points(
            epic='MT.D.GC.Month2.IP', resolution='D', numpoints=10)

        prices = result['prices']
        assert isinstance(result, dict)
        assert isinstance(prices, pd.DataFrame)

        # assert DataFrame shape
        assert prices.shape[0] == 10
        assert prices.shape[1] == 13

        # assert time series rows are 1 day apart
        prices['tvalue'] = prices.index
        prices['delta'] = (prices['tvalue'] - prices['tvalue'].shift())
        assert any(prices["delta"].dropna() == datetime.timedelta(days=1))
Пример #3
0
    def test_historical_prices_by_epic_and_num_points_bad_resolution(self):

        # fetch_historical_prices_by_epic_and_num_points, invalid resolution

        responses.add(
            responses.GET,
            'https://demo-api.ig.com/gateway/deal/prices/MT.D.GC.Month2.IP',
            match_querystring=False,
            headers={
                'CST': 'abc123',
                'X-SECURITY-TOKEN': 'xyz987'
            },
            json={},
            status=200)

        with pytest.raises(ValueError) as excinfo:
            ig_service = IGService('username', 'password', 'api_key', 'DEMO')
            ig_service.fetch_historical_prices_by_epic_and_num_points(
                epic='MT.D.GC.Month2.IP', resolution='X', numpoints=10)
            assert "Invalid frequency" in str(excinfo.value)
    def test_historical_prices_by_epic_and_num_points_bad_numpoints(self):

        # fetch_historical_prices_by_epic_and_num_points, invalid numpoints

        responses.add(
            responses.GET,
            'https://demo-api.ig.com/gateway/deal/prices/MT.D.GC.Month2.IP',
            match_querystring=False,
            headers={'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'},
            json={'errorCode': 'Unable to convert value=3.14159 to type= Integer int'},  # noqa
            status=400)

        with pytest.raises(Exception):
            ig_service = IGService('username', 'password', 'api_key', 'DEMO')
            ig_service.crud_session.HEADERS["LOGGED_IN"] = {}
            result = ig_service.fetch_historical_prices_by_epic_and_num_points(
                epic='MT.D.GC.Month2.IP',
                resolution='D',
                numpoints=3.14159)
            assert result['errorCode'].startswith('Unable to convert value')