def testDividedSecurityValueHolders(self):
        window1 = 10
        window2 = 5
        dependency1 = Factors.CLOSE
        dependency2 = Factors.OPEN
        ma = SecurityMovingAverage(window1, dependency1)
        mm = SecurityMovingSum(window2, dependency2)
        combined = ma / mm

        for i in range(len(self.datas['aapl']['close'])):
            data = {
                'aapl': {
                    Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['aapl'][Factors.OPEN][i]
                },
                'ibm': {
                    Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['ibm'][Factors.OPEN][i]
                }
            }
            ma.push(data)
            mm.push(data)
            combined.push(data)

            expected = ma.value / mm.value
            calculated = combined.value
            for name in expected.index:
                self.assertAlmostEqual(expected[name], calculated[name], 12)
    def testSecurityMovingSum(self):
        window = 10
        ma1 = SecurityMovingSum(window, ['close'])

        for i in range(len(self.aapl['close'])):
            data = {
                'aapl': {
                    'close': self.aapl['close'][i],
                    'open': self.aapl['open'][i]
                }
            }
            data['ibm'] = {
                'close': self.ibm['close'][i],
                'open': self.ibm['open'][i]
            }
            ma1.push(data)
            if i < 10:
                start = 0
            else:
                start = i + 1 - window

            value = ma1.value
            for name in value.index:
                expected = np.sum(self.dataSet[name]['close'][start:(i + 1)])
                calculated = value[name]
                self.assertAlmostEqual(
                    expected, calculated, 12, 'at index {0}\n'
                    'expected:   {1:.12f}\n'
                    'calculated: {2:.12f}'.format(i, expected, calculated))
Beispiel #3
0
    def testAddedSecurityValueHolderWithName(self):
        window = 10
        dependency = ['close']
        mm = SecurityMovingSum(window, dependency)
        combined = 'open' + mm

        for i in range(len(self.datas['aapl']['close'])):
            data = {
                'aapl': {
                    Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['aapl'][Factors.OPEN][i]
                },
                'ibm': {
                    Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['ibm'][Factors.OPEN][i]
                }
            }
            mm.push(data)
            combined.push(data)

            base = mm.value
            calculated = combined.value
            for name in calculated.index():
                self.assertAlmostEqual(base[name] + data[name]['open'],
                                       calculated[name], 12)
def MSUM(window, x='x', closed="right"):
    if isinstance(window, int):
        return SecurityMovingSum(window, x)
    elif isinstance(window, str):
        return SecurityTimeMovingSum(window, x, closed)
    else:
        raise ValueError("wrong window format <{0}>".format(window))
    def testDividedSecurityValueHoldersWithScalar(self):
        window = 10
        dependency = 'close'
        mm = SecurityMovingSum(window, dependency)
        combined = 2.0 / mm
        for i in range(len(self.datas['aapl']['close'])):
            data = {'aapl': {Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                             Factors.OPEN: self.datas['aapl'][Factors.OPEN][i]},
                    'ibm': {Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                            Factors.OPEN: self.datas['ibm'][Factors.OPEN][i]}}
            mm.push(data)
            combined.push(data)

            expected = 2.0 / mm.value
            calculated = combined.value
            for name in expected.index():
                self.assertAlmostEqual(expected[name], calculated[name], 12)
    def testDividedSecurityValueHoldersWithScalar(self):
        window = 10
        dependency = ['close']
        mm = SecurityMovingSum(window, dependency, ['aapl', 'ibm'])
        combined = 2.0 / mm
        for i in range(len(self.datas['aapl']['close'])):
            data = {'aapl': {Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                             Factors.OPEN: self.datas['aapl'][Factors.OPEN][i]},
                    'ibm': {Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                            Factors.OPEN: self.datas['ibm'][Factors.OPEN][i]}}
            mm.push(data)
            combined.push(data)

            expected = 2.0 / mm.value
            calculated = combined.value
            for name in expected:
                self.assertAlmostEqual(expected[name], calculated[name], 12)
    def testSecurityMovingSum(self):
        window = 10
        ma1 = SecurityMovingSum(window, ['close'])

        for i in range(len(self.aapl['close'])):
            data = {'aapl': {'close': self.aapl['close'][i], 'open': self.aapl['open'][i]}}
            data['ibm'] = {'close': self.ibm['close'][i], 'open': self.ibm['open'][i]}
            ma1.push(data)
            if i < 10:
                start = 0
            else:
                start = i + 1 - window

            value = ma1.value
            for name in value.index:
                expected = np.sum(self.dataSet[name]['close'][start:(i + 1)])
                calculated = value[name]
                self.assertAlmostEqual(expected, calculated, 12, 'at index {0}\n'
                                                                 'expected:   {1:.12f}\n'
                                                                 'calculated: {2:.12f}'.format(i, expected, calculated))
    def testDividedSecurityValueHolders(self):
        window1 = 10
        window2 = 5
        dependency1 = Factors.CLOSE
        dependency2 = Factors.OPEN
        ma = SecurityMovingAverage(window1, dependency1, ['aapl', 'ibm'])
        mm = SecurityMovingSum(window2, dependency2, ['aapl', 'ibm'])
        combined = ma / mm

        for i in range(len(self.datas['aapl']['close'])):
            data = {'aapl': {Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                             Factors.OPEN: self.datas['aapl'][Factors.OPEN][i]},
                    'ibm': {Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                            Factors.OPEN: self.datas['ibm'][Factors.OPEN][i]}}
            ma.push(data)
            mm.push(data)
            combined.push(data)

            expected = ma.value / mm.value
            calculated = combined.value
            for name in expected:
                self.assertAlmostEqual(expected[name], calculated[name], 12)
Beispiel #9
0
def MSUM(window, x='x'):
    return SecurityMovingSum(window, x)
Beispiel #10
0
def MSUM(window, dependency='x'):
    return SecurityMovingSum(window, dependency)
Beispiel #11
0
def SUM(window, dependency='x', symbolList=None):
    return SecurityMovingSum(window, dependency, symbolList)