def testMultipleOperators(self): ma20 = MovingAverage(20, 'close') ma120 = MovingAverage(120, 'close') mmax = MovingMax(50, 'open') res = (MovingAverage(20, 'close') - MovingAverage(120, 'close')) / MovingMax(50, 'open') for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)): data = {'close': close, 'open': open} ma20.push(data) ma120.push(data) mmax.push(data) res.push(data) expected = (ma20.result() - ma120.result()) / mmax.result() calculated = res.result() self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated))
class MovingDrawDown(StatefulValueHolder): def __init__(self, window, dependency='ret'): super(MovingDrawDown, self).__init__(window, dependency) self._returnSize = 3 self._maxer = MovingMax(window + 1, dependency='x') self._maxer.push(dict(x=0.0)) self._runningCum = 0.0 self._highIndex = 0 self._runningIndex = 0 def push(self, data): value = super(MovingDrawDown, self).push(data) if value is None: return self._runningIndex += 1 self._runningCum += value self._maxer.push(dict(x=self._runningCum)) self._currentMax = self._maxer.result() if self._runningCum >= self._currentMax: self._highIndex = self._runningIndex def result(self): return self._runningCum - self._currentMax, self._runningIndex - self._highIndex, self._highIndex
class MovingDrawDown(StatefulValueHolder): def __init__(self, window, dependency='ret'): super(MovingDrawDown, self).__init__(window, dependency) self._returnSize = 3 self._maxer = MovingMax(window + 1, dependency='x') self._maxer.push(dict(x=0.0)) self._runningCum = 0.0 self._highIndex = 0 self._runningIndex = -1 def push(self, data): value = super(MovingDrawDown, self).push(data) if value is None: return self._runningIndex += 1 self._runningCum += value self._maxer.push(dict(x=self._runningCum)) self._currentMax = self._maxer.result() if self._runningCum >= self._currentMax: self._highIndex = self._runningIndex def result(self): return self._runningCum - self._currentMax, self._runningIndex - self._highIndex, self._highIndex