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 testTruncatedValueHolder(self): ma20 = MovingAverage(20, 'close') max5 = MovingMax(5, 'open') with self.assertRaises(TypeError): _ = TruncatedValueHolder(ma20, 1) test = TruncatedValueHolder(ma20 ^ max5, 1) test.push(dict(close=10.0, open=5.0)) test.push(dict(close=10.0, open=20.0)) self.assertAlmostEqual(test.result(), 20.0, 15) test = TruncatedValueHolder(ma20 ^ max5, 0) test.push(dict(close=10.0, open=5.0)) test.push(dict(close=15.0, open=20.0)) self.assertAlmostEqual(test.result(), 12.50, 15) test = TruncatedValueHolder(ma20 ^ max5, slice(1, 2)) test.push(dict(close=10.0, open=5.0)) test.push(dict(close=15.0, open=20.0)) self.assertAlmostEqual(test.result(), [20.0], 15) test = TruncatedValueHolder(ma20 ^ max5, slice(0, -1)) test.push(dict(close=10.0, open=5.0)) test.push(dict(close=15.0, open=20.0)) self.assertAlmostEqual(test.result(), [12.5], 15) with self.assertRaises(ValueError): _ = TruncatedValueHolder(ma20 ^ max5, slice(1, -2))
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 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