def testSecurityWhereValueHolder(self): benchmark = SecurityLatestValueHolder(dependency='close') testValueHolder = SecurityIIFValueHolder(benchmark > 0, benchmark, -benchmark) 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] } } benchmark.push(data) testValueHolder.push(data) calculated = testValueHolder.value self.assertTrue(np.all((calculated > 0.).values)) for name in calculated.index(): rawValue = data[name][Factors.CLOSE] if rawValue > 0.: self.assertAlmostEqual(rawValue, calculated[name]) else: self.assertAlmostEqual(-rawValue, calculated[name])
def testSecurityWhereValueHolderWithMissingSymbol(self): benchmark = SecurityLatestValueHolder(x='open') benchmark2 = SecurityLatestValueHolder(x='close') testValueHolder = SecurityIIFValueHolder(benchmark < 0, 'open', -benchmark2) data = {'aapl': {'close': 2.0, 'open': 1.5}, 'ibm': {'open': 1.7}} benchmark.push(data) benchmark2.push(data) testValueHolder.push(data) calculated = testValueHolder.value self.assertAlmostEqual(calculated['aapl'], -data['aapl']['close']) self.assertTrue(np.isnan(calculated['ibm']))
def testSecurityWhereValueHolderWithSymbolName(self): benchmark = SecurityLatestValueHolder(x='close') testValueHolder = SecurityIIFValueHolder(benchmark > 0, 'open', -benchmark) 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] } } benchmark.push(data) testValueHolder.push(data) calculated = testValueHolder.value for name in calculated.index(): rawValue = data[name][Factors.CLOSE] if rawValue > 0.: self.assertAlmostEqual(data[name][Factors.OPEN], calculated[name]) else: self.assertAlmostEqual(-rawValue, calculated[name]) testValueHolder = SecurityIIFValueHolder(benchmark > 0, 'open', 'close') 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] } } benchmark.push(data) testValueHolder.push(data) calculated = testValueHolder.value for name in calculated.index(): rawValue = data[name][Factors.CLOSE] if rawValue > 0.: self.assertAlmostEqual(data[name][Factors.OPEN], calculated[name]) else: self.assertAlmostEqual(rawValue, calculated[name])
def testSecurityWhereValueHolderWithScalarValue(self): benchmark = SecurityLatestValueHolder(dependency='open') testValueHolder = SecurityIIFValueHolder(benchmark > 0, 1., -1.) 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] } } benchmark.push(data) testValueHolder.push(data) calculated = testValueHolder.value for name in calculated.index(): rawValue = data[name][Factors.OPEN] if rawValue > 0.: self.assertAlmostEqual(1., calculated[name]) self.assertAlmostEqual(1., testValueHolder.value_by_name(name)) else: self.assertAlmostEqual(-1., calculated[name]) self.assertAlmostEqual(-1., testValueHolder.value_by_name(name))
def IIF(flag, left, right): return SecurityIIFValueHolder(flag, left, right)