Exemplo n.º 1
0
class TestMultipleAlgorithms(unittest.TestCase):
    def setUp(self):
        self.qc = QCAlgorithm()
        Singleton.Setup(self.qc)
        self.qc.Initialize()
        self.qc.Securities = InternalSecurityManager([(FOO, 5), (BAR, 50)])
        foo = self.qc.AddEquity(FOO, Resolution.Daily).Symbol
        bar = self.qc.AddEquity(BAR, Resolution.Daily).Symbol

        self.algorithm1 = Algorithm(name="alg1", allocation=0.5)
        self.algorithm1.Portfolio.SetCash(200)
        self.algorithm1.Portfolio[foo] = Position(FOO, 10, 5)

        self.algorithm2 = Algorithm(name="alg2", allocation=0.5)
        self.algorithm2.Portfolio.SetCash(200)
        self.algorithm2.Portfolio[bar] = Position(BAR, 3, 50)

    def test_algorithms_cash(self):
        self.assertEqual(self.algorithm1.Portfolio.Cash, 200)
        self.assertEqual(self.algorithm2.Portfolio.Cash, 200)
        self.assertEqual(self.algorithm2.Portfolio.Performance, 75.0)
        self.assertEqual(self.algorithm2.Performance, 75.0)

    def test_algorithms_total_value(self):
        self.assertEqual(self.algorithm1.Portfolio.TotalPortfolioValue, 200 + (10 * 5))
        self.assertEqual(self.algorithm2.Portfolio.TotalPortfolioValue, 200 + (3 * 50))

    def test_algorithm_set_warmup(self):
        self.algorithm1.SetWarmUp(123)
        self.algorithm2.SetWarmUp(321)
        # pylint: disable=E1101
        self.assertEqual(Singleton._warm_up, 321)

    def test_qc_set_warmup_should_not_override_algorithm(self):
        self.algorithm1.SetWarmUp(123)
        self.algorithm2.SetWarmUp(321)
        Singleton.SetWarmUp(444)
        self.assertEqual(Singleton._warm_up, 321)

    def test_qc_set_warmup_from_main(self):
        Singleton.SetWarmUp(444)
        self.assertEqual(Singleton._warm_up, 444)