def testShiftedSecurityValueHolder(self):
        mm = SecurityMovingAverage(2, 'close')
        shifted1 = mm.shift(1)

        data1 = {'aapl': {'close': 1.0},
                 'ibm': {'close': 2.0},
                 'goog': {'close': 3.0}}
        shifted1.push(data1)
        calculated = shifted1.value
        for name in calculated.index():
            self.assertTrue(np.isnan(calculated[name]))

        data2 = {'aapl': {'close': 2.0},
                 'ibm': {'close': 3.0},
                 'goog': {'close': 4.0}}

        shifted1.push(data2)
        expected = SeriesValues({'aapl': 1.0,
                                 'ibm': 2.0,
                                 'goog': 3.0})
        calculated = shifted1.value
        for name in expected.index():
            self.assertAlmostEqual(expected[name], calculated[name])

        data3 = ({'aapl': {'close': 3.0},
                  'ibm': {'close': 4.0},
                  'goog': {'close': 5.0}})

        shifted1.push(data3)
        expected = SeriesValues({'aapl': 1.5,
                                 'ibm': 2.5,
                                 'goog': 3.5})
        calculated = shifted1.value
        for name in expected.index():
            self.assertAlmostEqual(expected[name], calculated[name])
    def testShiftedSecurityValueHolder(self):
        mm = SecurityMovingAverage(2, 'close')
        shifted1 = mm.shift(1)

        data1 = {'aapl': {'close': 1.0},
                 'ibm': {'close': 2.0},
                 'goog': {'close': 3.0}}
        shifted1.push(data1)
        calculated = shifted1.value
        for name in calculated.index:
            self.assertTrue(np.isnan(calculated[name]))

        data2 = {'aapl': {'close': 2.0},
                 'ibm': {'close': 3.0},
                 'goog': {'close': 4.0}}

        shifted1.push(data2)
        expected = SecuritiesValues({'aapl': 1.0,
                   'ibm': 2.0,
                   'goog': 3.0})
        calculated = shifted1.value
        for name in expected.index:
            self.assertAlmostEqual(expected[name], calculated[name])

        data3 = SecuritiesValues({'aapl': {'close': 3.0},
                 'ibm': {'close': 4.0},
                 'goog': {'close': 5.0}})

        shifted1.push(data3)
        expected = SecuritiesValues({'aapl': 1.5,
                    'ibm': 2.5,
                    'goog': 3.5})
        calculated = shifted1.value
        for name in expected.index:
            self.assertAlmostEqual(expected[name], calculated[name])
    def testShiftedSecurityValueHolder(self):
        mm = SecurityMovingAverage(2, 'close', ['aapl', 'ibm', 'goog'])
        shifted1 = mm.shift(1)

        data1 = {'aapl': {'close': 1.0},
                 'ibm': {'close': 2.0},
                 'goog': {'close': 3.0}}
        shifted1.push(data1)
        expected = {'aapl': np.nan,
                    'ibm': np.nan,
                    'goog': np.nan}
        calculated = shifted1.value
        for name in expected:
            self.assertIs(expected[name], calculated[name])

        data2 = {'aapl': {'close': 2.0},
                 'ibm': {'close': 3.0},
                 'goog': {'close': 4.0}}
        shifted1.push(data2)
        expected = {'aapl': 1.0,
                   'ibm': 2.0,
                   'goog': 3.0}
        calculated = shifted1.value
        for name in expected:
            self.assertAlmostEqual(expected[name], calculated[name])

        data3 = {'aapl': {'close': 3.0},
                 'ibm': {'close': 4.0},
                 'goog': {'close': 5.0}}
        shifted1.push(data3)
        expected = {'aapl': 1.5,
                    'ibm': 2.5,
                    'goog': 3.5}
        calculated = shifted1.value
        for name in expected:
            self.assertAlmostEqual(expected[name], calculated[name])
 def testShiftedSecurityValueHolderWithLengthZero(self):
     mm = SecurityMovingAverage(2, 'close')
     with self.assertRaises(ValueError):
         _ = mm.shift(0)
 def testShiftedSecurityValueHolderWithLengthZero(self):
     mm = SecurityMovingAverage(2, 'close', ['aapl', 'ibm', 'goog'])
     with self.assertRaises(ValueError):
         _ = mm.shift(0)