Beispiel #1
0
    def test_get_quotes_success(self, mock_get, mock_now):
        mock_get.return_value = _make_response(
            200, """{
          "SPY": {
            "chart": [
              {"date":"2020-03-05","close":319.69,"volume":242964067,"change":0,"changePercent":0,"changeOverTime":0},
              {"date":"2020-03-06","close":308.48,"volume":300862938,"change":-9.12,"changePercent":-2.9921,"changeOverTime":-0.029457},
              {"date":"2020-03-09","close":301.01,"volume":123123131,"change":-7.47,"changePercent":-2.4216,"changeOverTime":-0.01311}
            ]
          }
        }""")
        mock_now.return_value = time.FromCivil(time.CivilTime(2020, 3, 11))

        results = self.importer.get_data('SPY', time.CivilTime(2020, 3, 5),
                                         time.CivilTime(2020, 3, 7))

        _, kwargs = mock_get.call_args
        self.assertEqual(kwargs['params']['symbols'], 'SPY')
        self.assertEqual(kwargs['params']['range'], '1m')

        assert_that(
            results[data_type_pb2.DataType.CLOSE_PRICE],
            contains(
                equals_proto(f"""
                    symbol: "SPY"
                    data_space: STOCK_DATA
                    data_type: CLOSE_PRICE
                    value: 319.69
                    timestamp {{ seconds: {time.as_seconds(2020, 3, 5)} }}
                    updated_at {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                """),
                equals_proto(f"""
                    symbol: "SPY"
                    data_space: STOCK_DATA
                    data_type: CLOSE_PRICE
                    value: 308.48
                    timestamp {{ seconds: {time.as_seconds(2020, 3, 6)} }}
                    updated_at {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                """)))

        assert_that(
            results[data_type_pb2.DataType.VOLUME],
            contains(
                equals_proto(f"""
                    symbol: "SPY"
                    data_space: STOCK_DATA
                    data_type: VOLUME
                    value: 242964067.0
                    timestamp {{ seconds: {time.as_seconds(2020, 3, 5)} }}
                    updated_at {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                """),
                equals_proto(f"""
                    symbol: "SPY"
                    data_space: STOCK_DATA
                    data_type: VOLUME
                    value: 300862938.0
                    timestamp {{ seconds: {time.as_seconds(2020, 3, 6)} }}
                    updated_at {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                """)))
Beispiel #2
0
 def test_making_series_input_shape(self):
     got = input_util.series_source_inputs_shape(
         source_calc_type=_DataType.Enum.CLOSE_PRICE,
         t=time.FromUnixSeconds(1578009600),
         time_spec=calc.CalcTimeSpecs(num_periods=2,
                                      period_length=time.Hours(24)))
     expected = get_input_shapes()
     assert_that(
         got, contains(equals_proto(expected[0]), equals_proto(expected[1])))
Beispiel #3
0
    def test_ema_20_initial_calculation(self):
        # INTC 2019-12-03 to 2019-12-31
        price_data = [
            56.07, 56.02, 56.08, 56.81, 56.53, 56.59, 57.07, 57.55, 57.79, 57.70,
            57.23, 57.17, 57.96, 58.95, 59.23, 59.41, 59.82, 60.08, 59.62, 59.85,
        ]  # yapf: disable

        date = time.CivilTime(2019, 12, 31)
        close_prices = [
            _make_close_price(value, time.FromCivil(d))
            for value, d in zip(price_data, trading_days.get_last_n(date, 20))
        ]

        # Note: this is different than the actual EMA20 for Intel on 2019/12/31,
        # which is 58.46, because EMA accounts for *all* past data
        assert_that(
            ema.make_ema_20d_producer().calculate(close_prices),
            equals_proto(f"""
                symbol: "TEST"
                data_space: STOCK_DATA
                data_type: EMA_20D
                value: 58.255796513052346
                timestamp {{
                    seconds: {time.as_seconds(2019, 12, 31)}
                }}"""))
Beispiel #4
0
        def test_add_then_read(self):
            self._store.add(get_data_entry_one())

            lookup_key = key.make_lookup_key(get_data_entry_one())
            assert_that(
                self._store.read(lookup_key),
                ignoring_field_paths({('id', )},
                                     equals_proto(get_data_entry_one())))
Beispiel #5
0
 def test_making_series_input_shape_account_for_trading_days(self):
     got = input_util.series_source_inputs_shape(
         source_calc_type=_DataType.Enum.CLOSE_PRICE,
         t=time.FromCivil(time.CivilTime(2017, 12, 26)),
         time_spec=calc.CalcTimeSpecs(num_periods=2,
                                      period_length=time.Hours(24)))
     assert_that(
         got,
         contains(
             equals_proto(f"""
                 data_type: CLOSE_PRICE
                 timestamp {{
                     seconds: {time.as_seconds(2017, 12, 22)}
                 }}"""),
             equals_proto(f"""
                 data_type: CLOSE_PRICE
                 timestamp {{
                     seconds: {time.as_seconds(2017, 12, 26)}
                 }}""")))
Beispiel #6
0
 def test_making_recursive_input_shape(self):
     got = input_util.recursive_inputs_shape(
         base_calc_type=_DataType.Enum.EMA_20D,
         incr_calc_type=_DataType.Enum.CLOSE_PRICE,
         t=time.FromUnixSeconds(1578009600),
         time_spec=calc.CalcTimeSpecs(num_periods=20,
                                      period_length=time.Hours(24)))
     assert_that(
         got,
         contains(
             equals_proto(f"""
                 data_type: EMA_20D
                 timestamp {{
                     seconds: {time.as_seconds(2020, 1, 2)}
                 }}"""),
             equals_proto(f"""
                 data_type: CLOSE_PRICE
                 timestamp {{
                     seconds: {time.as_seconds(2020, 1, 3)}
                 }}""")))
Beispiel #7
0
    def test_get_quotes_default_single_day(self, mock_get, mock_now):
        mock_get.return_value = _make_response(
            200, """{
          "ABC": {
            "chart": [
              {"date":"2020-03-11","close":300.00,"volume":231231312,"change":-0,"changePercent": 0.0,"changeOverTime":-0.04321}
            ]
          }
        }""")
        mock_now.return_value = time.FromCivil(time.CivilTime(2020, 3, 11))

        results = self.importer.get_data('ABC')

        _, kwargs = mock_get.call_args
        self.assertEqual(kwargs['params']['symbols'], 'ABC')
        self.assertEqual(kwargs['params']['range'], '5d')

        assert_that(
            results[data_type_pb2.DataType.CLOSE_PRICE],
            contains(
                equals_proto(f"""
                    symbol: "ABC"
                    data_space: STOCK_DATA
                    data_type: CLOSE_PRICE
                    value: 300.00
                    timestamp {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                    updated_at {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                """)))

        assert_that(
            results[data_type_pb2.DataType.VOLUME],
            contains(
                equals_proto(f"""
                    symbol: "ABC"
                    data_space: STOCK_DATA
                    data_type: VOLUME
                    value: 231231312.0
                    timestamp {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                    updated_at {{ seconds: {time.as_seconds(2020, 3, 11)} }}
                """)))
Beispiel #8
0
    def test_ema_20_for_constant_list(self):
        date = time.CivilTime(2017, 10, 10)
        close_prices = [
            _make_close_price(200.0, time.FromCivil(d))
            for d in trading_days.get_last_n(date, 20)
        ]

        assert_that(
            ema.make_ema_20d_producer().calculate(close_prices),
            equals_proto(f"""
                symbol: "TEST"
                data_space: STOCK_DATA
                data_type: EMA_20D
                value: 200.0
                timestamp {{
                    seconds: {time.as_seconds(2017, 10, 10)}
                }}"""))
Beispiel #9
0
        def test_lookup(self, data_space: _DataEntry.DataSpace, symbol: str,
                        data_type: data_type_pb2.DataType.Enum,
                        data_entries: Tuple[_DataEntry]):
            store_impl = impl_factory()
            store_impl.add(get_data_entry_one())
            store_impl.add(get_data_entry_two())
            store_impl.add(get_data_entry_three())

            lookup_key = store.DataStore.LookupKey(data_space=data_space,
                                                   symbol=symbol or "",
                                                   data_type=data_type)
            actual = store_impl.lookup(lookup_key)
            expected = data_entry_pb2.DataEntries()
            expected.entries.extend(data_entries)

            assert_that(
                actual,
                ignoring_field_paths({('entries', 'id')},
                                     ignoring_repeated_field_ordering(
                                         equals_proto(expected))))
Beispiel #10
0
    def test_ema20_recursive_calculation(self):
        date = time.FromCivil(time.CivilTime(2020, 1, 1))
        close_now = _make_close_price(60.84, date)

        ema_last = _DataEntry(
            symbol='TEST',
            data_space=_DataEntry.STOCK_DATA,
            data_type=data_type_pb2.DataType.EMA_20D,
            value=58.46,
            timestamp=time_util.from_time(date - time.Hours(24)),
        )

        assert_that(
            ema.make_ema_20d_producer().calculate((ema_last, close_now)),
            equals_proto(f"""
                symbol: "TEST"
                data_space: STOCK_DATA
                data_type: EMA_20D
                value: 58.68666666666667
                timestamp {{
                    seconds: {time.as_seconds(2020, 1, 1)}
                }}"""))
Beispiel #11
0
 def test_more_data_than_needed_to_sort_to_inputs_shape(self):
     inputs = [*reversed(get_valid_inputs()), *get_valid_inputs()]
     got = input_util.sort_to_inputs_shape(inputs, get_input_shapes())
     expected = get_valid_inputs()
     assert_that(
         got, contains(equals_proto(expected[0]), equals_proto(expected[1])))
Beispiel #12
0
 def test_sort_to_inputs_shape(self):
     inputs = reversed(get_valid_inputs())
     got = input_util.sort_to_inputs_shape(inputs, get_input_shapes())
     expected = get_valid_inputs()
     assert_that(
         got, contains(equals_proto(expected[0]), equals_proto(expected[1])))