def __init__(self, feed, instrument, nper, lowerThreshold, upperThreshold): super(ZScoreStrategy, self).__init__(feed) self.position = None self.instrument = instrument self.sma = ma.SMA(feed[instrument].getPriceDataSeries(), nper) self.roc = roc.RateOfChange(feed[instrument].getPriceDataSeries(), 1) self.zscore = stats.ZScore(self.roc, nper) self.lowerThreshold = lowerThreshold self.upperThreshold = upperThreshold
def testZScore(self): values = [1.10, 2.20, 4.00, 5.10, 6.00, 7.10, 8.20, 9.00, 10.10, 3.00, 4.10, 5.20, 7.00, 8.10, 9.20, 16.00, 17.10, 18.20, 19.30, 20.40] expected = [None, None, None, None, 1.283041407, 1.317884611, 1.440611043, 1.355748299, 1.4123457, -1.831763202, -0.990484842, -0.388358578, 0.449889908, 1.408195169, 1.332948099, 1.867732104, 1.334258333, 1.063608066, 0.939656572, 1.414213562] seqDS = dataseries.SequenceDataSeries() zscore = stats.ZScore(seqDS, 5) i = 0 for value in values: seqDS.append(value) if i >= 4: self.assertEqual(round(zscore[-1], 4), round(expected[i], 4)) i += 1