예제 #1
0
    def test_transaction_cost(self):
        strat = BuyAndHoldStrategy()
        sim = StockMarketStrategySim("test", datetime.date(2015,1,1), 1, strat)

        self.assertEqual(sim._get_transaction_cost(1), 10.0,
                         "Minimum value test failed")
        self.assertEqual(sim._get_transaction_cost(140000), 146.5,
                         "Near-max value test failed")
        self.assertEqual(sim._get_transaction_cost(150000), 150,
                         "Max value test failed")
예제 #2
0
    def test_value_update_long_position(self):
        """Tests if the values are correctly updated when holding a long
        position."""
        start_value = 1000
        sim = StockMarketStrategySim("test", datetime.date(2015,1,1),
                                     start_value, BuyAndHoldStrategy())
        sim.run()

        # The difference in points in the test csv is the following, assuming
        # buy on day 1 (at halfway point between open and close) and no sell:
        buy_val = (1005 + 1010) / 2.0
        proc_diff = (1010 - buy_val) / buy_val
        # Calculate end value. Private method in sim used here for test purposes
        # only:
        start_value_stock = start_value - sim._get_transaction_cost(start_value)
        end_value = start_value_stock * (1+proc_diff)

        # Using inequality check because of rounding differences and/or
        # inaccuracies due to using floats precision.

        assert abs(end_value - sim.get_current_value()) < .01, \
            "End value {0} expected, got {1}".format(end_value,
                                                     sim.get_current_value())