Пример #1
0
class TestAdaptiveMovingAverage:
    def setup(self):
        # Fixture Setup
        self.ama = AdaptiveMovingAverage(10, 2, 30)

    def test_name_returns_expected_string(self):
        # Arrange, Act, Assert
        assert self.ama.name == "AdaptiveMovingAverage"

    def test_str_repr_returns_expected_string(self):
        # Arrange, Act, Assert
        assert str(self.ama) == "AdaptiveMovingAverage(10, 2, 30)"
        assert repr(self.ama) == "AdaptiveMovingAverage(10, 2, 30)"

    def test_period(self):
        # Arrange, Act, Assert
        assert self.ama.period == 10

    def test_initialized_without_inputs_returns_false(self):
        # Arrange, Act, Assert
        assert self.ama.initialized is False

    def test_initialized_with_required_inputs_returns_true(self):
        # Arrange
        # Arrange, Act
        for _i in range(10):
            self.ama.update_raw(1.00000)

        # Assert
        assert self.ama.initialized is True

    def test_handle_quote_tick_updates_indicator(self):
        # Arrange
        indicator = AdaptiveMovingAverage(10, 2, 30, PriceType.MID)

        tick = TestDataStubs.quote_tick_5decimal(AUDUSD_SIM.id)

        # Act
        indicator.handle_quote_tick(tick)

        # Assert
        assert indicator.has_inputs
        assert indicator.value == 1.00002

    def test_handle_trade_tick_updates_indicator(self):
        # Arrange
        indicator = AdaptiveMovingAverage(10, 2, 30)

        tick = TestDataStubs.trade_tick_5decimal(AUDUSD_SIM.id)

        # Act
        indicator.handle_trade_tick(tick)

        # Assert
        assert indicator.has_inputs
        assert indicator.value == 1.00001

    def test_handle_bar_updates_indicator(self):
        # Arrange
        indicator = AdaptiveMovingAverage(10, 2, 30)

        bar = TestDataStubs.bar_5decimal()

        # Act
        indicator.handle_bar(bar)

        # Assert
        assert indicator.has_inputs
        assert indicator.value == 1.00003

    def test_value_with_one_input(self):
        # Arrange
        self.ama.update_raw(1.00000)

        # Act, Assert
        assert self.ama.value == 1.0

    def test_value_with_three_inputs(self):
        # Arrange
        self.ama.update_raw(1.00000)
        self.ama.update_raw(2.00000)
        self.ama.update_raw(3.00000)

        # Act, Assert
        assert self.ama.value == 2.135802469135802

    def test_reset_successfully_returns_indicator_to_fresh_state(self):
        # Arrange
        for _i in range(1000):
            self.ama.update_raw(1.00000)

        # Act
        self.ama.reset()

        # Assert
        assert not self.ama.initialized
        assert self.ama.value == 0
Пример #2
0
class AdaptiveMovingAverageTests(unittest.TestCase):
    def setUp(self):
        # Fixture Setup
        self.ama = AdaptiveMovingAverage(10, 2, 30)

    def test_name_returns_expected_string(self):
        # Arrange
        # Act
        # Assert
        self.assertEqual("AdaptiveMovingAverage", self.ama.name)

    def test_str_repr_returns_expected_string(self):
        # Arrange
        # Act
        # Assert
        self.assertEqual("AdaptiveMovingAverage(10, 2, 30)", str(self.ama))
        self.assertEqual("AdaptiveMovingAverage(10, 2, 30)", repr(self.ama))

    def test_period(self):
        # Arrange
        # Act
        # Assert
        self.assertEqual(10, self.ama.period)

    def test_initialized_without_inputs_returns_false(self):
        # Arrange
        # Act
        # Assert
        self.assertEqual(False, self.ama.initialized)

    def test_initialized_with_required_inputs_returns_true(self):
        # Arrange
        # Arrange
        # Act
        for _i in range(10):
            self.ama.update_raw(1.00000)

        # Assert
        self.assertEqual(True, self.ama.initialized)

    def test_handle_quote_tick_updates_indicator(self):
        # Arrange
        indicator = AdaptiveMovingAverage(10, 2, 30, PriceType.MID)

        tick = TestStubs.quote_tick_5decimal(AUDUSD_SIM.id)

        # Act
        indicator.handle_quote_tick(tick)

        # Assert
        self.assertTrue(indicator.has_inputs)
        self.assertEqual(1.00002, indicator.value)

    def test_handle_trade_tick_updates_indicator(self):
        # Arrange
        indicator = AdaptiveMovingAverage(10, 2, 30)

        tick = TestStubs.trade_tick_5decimal(AUDUSD_SIM.id)

        # Act
        indicator.handle_trade_tick(tick)

        # Assert
        self.assertTrue(indicator.has_inputs)
        self.assertEqual(1.00001, indicator.value)

    def test_handle_bar_updates_indicator(self):
        # Arrange
        indicator = AdaptiveMovingAverage(10, 2, 30)

        bar = TestStubs.bar_5decimal()

        # Act
        indicator.handle_bar(bar)

        # Assert
        self.assertTrue(indicator.has_inputs)
        self.assertEqual(1.00003, indicator.value)

    def test_value_with_one_input(self):
        # Arrange
        self.ama.update_raw(1.00000)

        # Act
        # Assert
        self.assertEqual(1.0, self.ama.value)

    def test_value_with_three_inputs(self):
        # Arrange
        self.ama.update_raw(1.00000)
        self.ama.update_raw(2.00000)
        self.ama.update_raw(3.00000)

        # Act
        # Assert
        self.assertEqual(2.135802469135802, self.ama.value, 10)

    def test_reset_successfully_returns_indicator_to_fresh_state(self):
        # Arrange
        for _i in range(1000):
            self.ama.update_raw(1.00000)

        # Act
        self.ama.reset()

        # Assert
        self.assertFalse(self.ama.initialized)
        self.assertEqual(0, self.ama.value)
Пример #3
0
class AdaptiveMovingAverageTests(unittest.TestCase):

    # Fixture Setup
    def setUp(self):
        # Arrange
        self.ama = AdaptiveMovingAverage(10, 2, 30)

    def test_name(self):
        # Act
        # Assert
        self.assertEqual("AdaptiveMovingAverage", self.ama.name)

    def test_str(self):
        # Act
        # Assert
        self.assertEqual("AdaptiveMovingAverage(10, 2, 30)", str(self.ama))

    def test_repr(self):
        # Act
        # Assert
        self.assertTrue(
            repr(self.ama).startswith(
                "<AdaptiveMovingAverage(10, 2, 30) object at"))
        self.assertTrue(repr(self.ama).endswith(">"))

    def test_period(self):
        # Act
        # Assert
        self.assertEqual(10, self.ama.period)

    def test_initialized_without_inputs_returns_false(self):
        # Act
        # Assert
        self.assertEqual(False, self.ama.initialized)

    def test_initialized_with_required_inputs_returns_true(self):
        # Arrange
        # Act
        for _i in range(10):
            self.ama.update_raw(1.00000)

        # Assert
        self.assertEqual(True, self.ama.initialized)

    def test_value_with_one_input(self):
        # Arrange
        self.ama.update_raw(1.00000)

        # Act
        # Assert
        self.assertEqual(1.0, self.ama.value)

    def test_value_with_three_inputs(self):
        # Arrange
        self.ama.update_raw(1.00000)
        self.ama.update_raw(2.00000)
        self.ama.update_raw(3.00000)

        # Act
        # Assert
        self.assertEqual(2.135802469135802, self.ama.value, 10)

    def test_reset_successfully_returns_indicator_to_fresh_state(self):
        # Arrange
        for _i in range(1000):
            self.ama.update_raw(1.00000)

        # Act
        self.ama.reset()

        # Assert
        self.assertEqual(0.0, self.ama.value)  # No assertion errors.

    def test_with_battery_signal(self):
        # Arrange
        battery_signal = BatterySeries.create()
        output = []

        # Act
        for point in battery_signal:
            self.ama.update_raw(point)
            output.append(self.ama.value)

        # Assert
        self.assertEqual(len(battery_signal), len(output))