class WeightedMovingAverageTests(unittest.TestCase): # Fixture Setup def setUp(self): # Arrange self.w = [round(i * 0.1, 2) for i in range(1, 11)] self.wma = WeightedMovingAverage(10, self.w) self.wma_noweights = WeightedMovingAverage(10) self.wma_factory = MovingAverageFactory.create( 10, MovingAverageType.WEIGHTED, weights=self.w) def test_name_returns_expected_name(self): # Act # Assert self.assertEqual("WeightedMovingAverage", self.wma.name) def test_str_returns_expected_string(self): # Act # Assert self.assertEqual( "WeightedMovingAverage(10, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])", str(self.wma)) def test_repr_returns_expected_string(self): # Act # Assert self.assertTrue( repr(self.wma).startswith( "<WeightedMovingAverage(10, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]) object at" )) self.assertTrue(repr(self.wma).endswith(">")) def test_weights_returns_expected_weights(self): # Act # Assert self.assertEqual(self.w, self.wma.weights) def test_wma_factory_kwargs(self): for i in range(1, 12): self.wma_factory.update_raw(float(i)) self.assertEqual(8.0, self.wma_factory.value) self.assertEqual(self.w, self.wma_factory.weights) def test_value_with_one_input_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) # Act # Assert self.assertEqual(1.0, self.wma.value) def test_value_with_two_input_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(10.00000) # 10 * 1.0, 1 * 0.9 # Act # Assert self.assertEqual((10 * 1.0 + 1 * 0.9) / 1.9, self.wma.value) def test_value_with_no_weights(self): # Arrange self.wma_noweights.update_raw(1.00000) self.wma_noweights.update_raw(2.00000) # Act # Assert self.assertEqual(1.5, self.wma_noweights.value) def test_value_with_ten_inputs_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) self.wma.update_raw(4.00000) self.wma.update_raw(5.00000) self.wma.update_raw(6.00000) self.wma.update_raw(7.00000) self.wma.update_raw(8.00000) self.wma.update_raw(9.00000) self.wma.update_raw(10.00000) # Act # Assert self.assertAlmostEqual(7.00, self.wma.value, 2) def test_value_at_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) self.wma.update_raw(4.00000) self.wma.update_raw(5.00000) self.wma.update_raw(6.00000) self.wma.update_raw(7.00000) self.wma.update_raw(8.00000) self.wma.update_raw(9.00000) self.wma.update_raw(10.00000) self.wma.update_raw(11.00000) # Act # Assert self.assertEqual(8.0, self.wma.value) def test_with_battery_signal(self): # Arrange battery_signal = BatterySeries.create() output = [] # Act for point in battery_signal: self.wma.update_raw(point) output.append(self.wma.value) # Assert self.assertEqual(len(battery_signal), len(output))
class WeightedMovingAverageTests(unittest.TestCase): def setUp(self): # Fixture Setup self.w = [round(i * 0.1, 2) for i in range(1, 11)] self.wma = WeightedMovingAverage(10, self.w) self.wma_noweights = WeightedMovingAverage(10) self.wma_factory = MovingAverageFactory.create( 10, MovingAverageType.WEIGHTED, weights=self.w) def test_name_returns_expected_string(self): # Arrange # Act # Assert self.assertEqual("WeightedMovingAverage", self.wma.name) def test_str_repr_returns_expected_string(self): # Arrange # Act # Assert weights_repr = repr(self.wma.weights) self.assertEqual( f"WeightedMovingAverage(10, {weights_repr})", str(self.wma), ) self.assertEqual( f"WeightedMovingAverage(10, {weights_repr})", repr(self.wma), ) def test_weights_returns_expected_weights(self): # Arrange # Act # Assert self.assertEqual(self.w, list(self.wma.weights)) def test_wma_factory_update_raw(self): # Arrange # Act for i in range(1, 12): self.wma_factory.update_raw(float(i)) # Assert self.assertEqual(8.0, self.wma_factory.value) self.assertEqual(list(self.w), list(self.wma_factory.weights)) def test_handle_quote_tick_updates_indicator(self): # Arrange indicator = WeightedMovingAverage(10, self.w, 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 = WeightedMovingAverage(10, self.w) 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 = WeightedMovingAverage(10, self.w) 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_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) # Act # Assert self.assertEqual(1.0, self.wma.value) def test_value_with_two_input_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(10.00000) # 10 * 1.0, 1 * 0.9 # Act # Assert self.assertEqual((10 * 1.0 + 1 * 0.9) / 1.9, self.wma.value) def test_value_with_no_weights(self): # Arrange self.wma_noweights.update_raw(1.00000) self.wma_noweights.update_raw(2.00000) # Act # Assert self.assertEqual(1.5, self.wma_noweights.value) def test_value_with_ten_inputs_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) self.wma.update_raw(4.00000) self.wma.update_raw(5.00000) self.wma.update_raw(6.00000) self.wma.update_raw(7.00000) self.wma.update_raw(8.00000) self.wma.update_raw(9.00000) self.wma.update_raw(10.00000) # Act # Assert self.assertAlmostEqual(7.00, self.wma.value, 2) def test_value_at_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) self.wma.update_raw(4.00000) self.wma.update_raw(5.00000) self.wma.update_raw(6.00000) self.wma.update_raw(7.00000) self.wma.update_raw(8.00000) self.wma.update_raw(9.00000) self.wma.update_raw(10.00000) self.wma.update_raw(11.00000) # Act # Assert self.assertEqual(8.0, self.wma.value) def test_reset(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) # Act self.wma.reset() # Assert self.assertFalse(self.wma.initialized) self.assertEqual(0, self.wma.value)
class TestWeightedMovingAverage: def setup(self): # Fixture Setup self.w = [round(i * 0.1, 2) for i in range(1, 11)] self.wma = WeightedMovingAverage(10, self.w) self.wma_noweights = WeightedMovingAverage(10) self.wma_factory = MovingAverageFactory.create( 10, MovingAverageType.WEIGHTED, weights=self.w ) def test_name_returns_expected_string(self): # Arrange, Act, Assert assert self.wma.name == "WeightedMovingAverage" def test_str_repr_returns_expected_string(self): # Arrange, Act, Assert weights_repr = repr(self.wma.weights) assert str(self.wma) == f"WeightedMovingAverage(10, {weights_repr})" assert repr(self.wma) == f"WeightedMovingAverage(10, {weights_repr})" def test_weights_returns_expected_weights(self): # Arrange, Act, Assert assert list(self.wma.weights) == self.w def test_wma_factory_update_raw(self): # Arrange, Act for i in range(1, 12): self.wma_factory.update_raw(float(i)) # Assert assert self.wma_factory.value == 8.0 assert list(self.wma_factory.weights) == list(self.w) def test_handle_quote_tick_updates_indicator(self): # Arrange indicator = WeightedMovingAverage(10, self.w, 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 = WeightedMovingAverage(10, self.w) 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 = WeightedMovingAverage(10, self.w) 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_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) # Act, Assert assert self.wma.value == 1.0 def test_value_with_two_input_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(10.00000) # 10 * 1.0, 1 * 0.9 # Act, Assert assert self.wma.value == (10 * 1.0 + 1 * 0.9) / 1.9 def test_value_with_no_weights(self): # Arrange self.wma_noweights.update_raw(1.00000) self.wma_noweights.update_raw(2.00000) # Act, Assert assert self.wma_noweights.value == 1.5 def test_value_with_ten_inputs_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) self.wma.update_raw(4.00000) self.wma.update_raw(5.00000) self.wma.update_raw(6.00000) self.wma.update_raw(7.00000) self.wma.update_raw(8.00000) self.wma.update_raw(9.00000) self.wma.update_raw(10.00000) # Act, Assert assert self.wma.value == pytest.approx(7.00, 2) def test_value_at_returns_expected_value(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) self.wma.update_raw(4.00000) self.wma.update_raw(5.00000) self.wma.update_raw(6.00000) self.wma.update_raw(7.00000) self.wma.update_raw(8.00000) self.wma.update_raw(9.00000) self.wma.update_raw(10.00000) self.wma.update_raw(11.00000) # Act, Assert assert self.wma.value == 8.0 def test_reset(self): # Arrange self.wma.update_raw(1.00000) self.wma.update_raw(2.00000) self.wma.update_raw(3.00000) # Act self.wma.reset() # Assert assert not self.wma.initialized assert self.wma.value == 0