def testCompoundedOperator(self): ma5 = MovingAverage(5, 'x') maxer = Max('close') max5ma = Max('close') >> MovingAverage(5) max5ma2 = MovingAverage(5, Max('close')) average = Average('close') sumM = Sum('close') mvTest = Correlation(dependency=('x', 'y')) mvCorr = (Average('close') ^ Sum('close')) >> Correlation(dependency=('x', 'y')) for i, close in enumerate(self.sampleClose): data = {'close': close, 'open': open} maxer.push(data) data2 = {'x': maxer.result()} ma5.push(data2) max5ma.push(data) max5ma2.push(data) average.push(data) sumM.push(data) data3 = {'x': average.result(), 'y': sumM.result()} mvTest.push(data3) mvCorr.push(data) expected = ma5.result() calculated = max5ma.result() self.assertAlmostEqual( calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated)) calculated = max5ma2.result() self.assertAlmostEqual( calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated)) if i >= 1: expected = mvTest.result() calculated = mvCorr.result() self.assertAlmostEqual( calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated)) with self.assertRaises(ValueError): _ = Max('close') >> math.sqrt with self.assertRaises(ValueError): _ = (Max('close') ^ Minimum('close')) >> MovingCorrelation( 20, dependency=('x', 'y', 'z')) (Max('close') ^ Minimum('close')) >> MovingCorrelation( 20, dependency=('x', 'y'))
def testMinimum(self): mm = Minimum(dependency='close') for i, value in enumerate(self.samplesClose): mm.push(dict(close=value)) expected = min(self.samplesClose[:i + 1]) calculated = mm.result() self.assertAlmostEqual(expected, calculated, 10, "at index {0:d}\n" "expected min: {1:f}\n" "calculated min: {2:f}".format(i, expected, calculated))
def testMinimum(self): mm = Minimum(dependency='close') for i, value in enumerate(self.samplesClose): mm.push(dict(close=value)) expected = min(self.samplesClose[:i + 1]) calculated = mm.result() self.assertAlmostEqual( expected, calculated, 10, "at index {0:d}\n" "expected min: {1:f}\n" "calculated min: {2:f}".format(i, expected, calculated))
def testMinimumWithAccumulator(self): mm = Minimum(x=Latest('open'), y=Latest('close')) for i, value in enumerate(zip(self.samplesOpen, self.samplesClose)): mm.push(dict(open=value[0], close=value[1])) expected = min(self.samplesOpen[i], self.samplesClose[i]) calculated = mm.result() self.assertAlmostEqual( expected, calculated, 10, "at index {0:d}\n" "expected max: {1:f}\n" "calculated max: {2:f}".format(i, expected, calculated))
def testGetItemOperator(self): listHolder = MovingAverage(20, 'close') ^ Max('open') ^ Minimum('close') listHolder1 = listHolder[1] listHolder2 = listHolder[1:3] maxer = Max('open') for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)): data = {'close': close, 'open': open} listHolder1.push(data) listHolder2.push(data) maxer.push(data) expected = maxer.result() calculated = listHolder1.result() self.assertAlmostEqual( expected, calculated, 12, "at index {0:d}\n" "expected beta: {1:f}\n" "calculated beta: {2:f}".format(i, expected, calculated)) calculated = listHolder2.result()[0] self.assertAlmostEqual( expected, calculated, 12, "at index {0:d}\n" "expected beta: {1:f}\n" "calculated beta: {2:f}".format(i, expected, calculated))
def testDivOperator(self): mc5 = MovingCorrelation(5, ['open', 'close']) minum = Minimum('open') divRes = Minimum('open') / MovingCorrelation(5, ['open', 'close']) for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)): data = {'close': close, 'open': open} mc5.push(data) minum.push(data) divRes.push(data) if i >= 1: expected = minum.result() / mc5.result() calculated = divRes.result() self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated))
def testGreaterOperator(self): m1 = Max('x') m2 = Minimum('x') cmp = m1 > m2 cmp.push(dict(x=1.0)) self.assertEqual(False, cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual(True, cmp.result())
def testLessOrEqualOperators(self): m1 = Max('x') m2 = Minimum('x') cmp = m1 <= m2 cmp.push(dict(x=1.0)) self.assertEqual(True, cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual(False, cmp.result())
def testDivOperator(self): mc5 = MovingCorrelation(5, ['open', 'close']) minum = Minimum('open') divRes = Minimum('open') / MovingCorrelation(5, ['open', 'close']) concated = (Minimum('open') ^ MovingCorrelation(5, ['open', 'close'])) / MovingCorrelation(5, ['open', 'close']) concated2 = MovingCorrelation(5, ['open', 'close']) / ( Minimum('open') ^ MovingCorrelation(5, ['open', 'close'])) for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)): data = {'close': close, 'open': open} mc5.push(data) minum.push(data) divRes.push(data) concated.push(data) concated2.push(data) if i >= 1: expected = minum.result() / mc5.result() calculated = divRes.result() self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated)) expected = (minum.result() / mc5.result(), mc5.result() / mc5.result()) calculated = concated.result() self.assertAlmostEqual(calculated[0], expected[0], 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected[0], calculated[0])) self.assertAlmostEqual(calculated[1], expected[1], 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected[1], calculated[1])) expected = (mc5.result() / minum.result(), mc5.result() / mc5.result()) calculated = concated2.result() self.assertAlmostEqual(calculated[0], expected[0], 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected[0], calculated[0])) self.assertAlmostEqual(calculated[1], expected[1], 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected[1], calculated[1]))
def testListedOperator(self): ma20 = MovingAverage(20, 'close') maxer = Max('open') minimumer = Minimum('close') listHolder = MovingAverage(20, 'close') ^ Max('open') ^ Minimum('close') listHolder2 = 2.0 ^ MovingAverage(20, 'close') listHolder3 = MovingAverage(20, 'close') ^ 2.0 for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)): data = {'close': close, 'open': open} ma20.push(data) maxer.push(data) minimumer.push(data) listHolder.push(data) listHolder2.push(data) listHolder3.push(data) expected = (ma20.result(), maxer.result(), minimumer.result()) calculated = listHolder.result() for ev, cv in zip(expected, calculated): self.assertAlmostEqual(ev, cv, 12, "at index {0:d}\n" "expected: {1}\n" "calculated: {2}".format(i, expected, calculated)) expected = (2.0, ma20.result()) calculated = listHolder2.result() for ev, cv in zip(expected, calculated): self.assertAlmostEqual(ev, cv, 12, "at index {0:d}\n" "expected: {1}\n" "calculated: {2}".format(i, expected, calculated)) expected = (ma20.result(), 2.0) calculated = listHolder3.result() for ev, cv in zip(expected, calculated): self.assertAlmostEqual(ev, cv, 12, "at index {0:d}\n" "expected: {1}\n" "calculated: {2}".format(i, expected, calculated))
def testGreaterOrEqualOperator(self): m1 = Minimum('x') m2 = Max('x') cmp = m1 >= m2 cmp.push(dict(x=1.0)) self.assertEqual(True, cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual(False, cmp.result()) cmp = (m1 ^ m2) >= m2 cmp.push(dict(x=1.0)) self.assertEqual((True, True), cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual((False, True), cmp.result()) cmp = m1 >= (m1 ^ m2) cmp.push(dict(x=1.0)) self.assertEqual((True, True), cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual((True, False), cmp.result())
def testLessOperator(self): m1 = Minimum('x') m2 = Max('x') cmp = m1 < m2 cmp.push(dict(x=1.0)) self.assertEqual(False, cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual(True, cmp.result()) cmp = (m1 ^ m2) < m2 cmp.push(dict(x=1.0)) self.assertEqual((False, False), cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual((True, False), cmp.result()) cmp = m1 < (m1 ^ m2) cmp.push(dict(x=1.0)) self.assertEqual((False, False), cmp.result()) cmp.push(dict(x=2.0)) self.assertEqual((False, True), cmp.result())
def testListedOperator(self): ma20 = MovingAverage(20, 'close') maxer = Max('open') minimumer = Minimum('close') listHolder = MovingAverage(20, 'close') ^ Max('open') ^ Minimum('close') listHolder2 = 2.0 ^ MovingAverage(20, 'close') listHolder3 = MovingAverage(20, 'close') ^ 2.0 for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)): data = {'close': close, 'open': open} ma20.push(data) maxer.push(data) minimumer.push(data) listHolder.push(data) listHolder2.push(data) listHolder3.push(data) expected = (ma20.result(), maxer.result(), minimumer.result()) calculated = listHolder.result() for ev, cv in zip(expected, calculated): self.assertAlmostEqual( ev, cv, 12, "at index {0:d}\n" "expected: {1}\n" "calculated: {2}".format(i, expected, calculated)) expected = (2.0, ma20.result()) calculated = listHolder2.result() for ev, cv in zip(expected, calculated): self.assertAlmostEqual( ev, cv, 12, "at index {0:d}\n" "expected: {1}\n" "calculated: {2}".format(i, expected, calculated)) expected = (ma20.result(), 2.0) calculated = listHolder3.result() for ev, cv in zip(expected, calculated): self.assertAlmostEqual( ev, cv, 12, "at index {0:d}\n" "expected: {1}\n" "calculated: {2}".format(i, expected, calculated))