def multipleDatetimeAligned(self, feed): # in case of multiple dataseries, add multiple datatime aligned function ds = dict() ds[self.__instrumentList[0]] = feed[self.__instrumentList[0]].getAdjCloseDataSeries() for i in range(1,len(self.__instrumentList)) : ds[self.__instrumentList[0]], ds[self.__instrumentList[i]] = \ aligned.datetime_aligned(ds[self.__instrumentList[0]], feed[self.__instrumentList[i]].getAdjCloseDataSeries()) for i in range(1,len(self.__instrumentList)) : ds[self.__instrumentList[0]], ds[self.__instrumentList[i]] = \ aligned.datetime_aligned(ds[self.__instrumentList[0]], ds[self.__instrumentList[i]]) return ds
def testIncremental_Recursive(self): size = 20 ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) # Align the aligned dataseries again with respect to each other. ads1, ads2 = aligned.datetime_aligned(ads1, ads2) now = datetime.datetime.now() for i in range(size): ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(len(ads1), len(ads2)) self.assertEqual(ads1[:], ads2[:]) self.assertEqual(ads1.getDateTimes()[:], ads2.getDateTimes()[:])
def __init__(self, ds1, ds2, windowSize): # We're going to use datetime aligned versions of the dataseries. self.__ds1, self.__ds2 = aligned.datetime_aligned(ds1, ds2) self.__windowSize = windowSize self.__hedgeRatio = None self.__spread = None self.__spreadMean = None self.__spreadStd = None self.__zScore = None
def testFullyAligned_Recursive(self): size = 20 ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) # Align the aligned dataseries again with respect to each other. ads1, ads2 = aligned.datetime_aligned(ads1, ads2) now = datetime.datetime.now() for i in xrange(size): ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(len(ds1), len(ds2)) for ads in [ads1, ads2]: self.assertEqual(len(ads), size) for i in xrange(size): self.assertEqual(ads.getValueAbsolute(i), i) self.assertEqual(ads.getDateTimes()[i], now + datetime.timedelta(seconds=i))
def testFullyAligned_Recursive(self): size = 20 ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) # Align the aligned dataseries again with respect to each other. ads1, ads2 = aligned.datetime_aligned(ads1, ads2) now = datetime.datetime.now() for i in range(size): ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(len(ds1), len(ds2)) for ads in [ads1, ads2]: self.assertEqual(len(ads), size) for i in range(size): self.assertEqual(ads.getValueAbsolute(i), i) self.assertEqual(ads.getDateTimes()[i], now + datetime.timedelta(seconds=i))
def testIncremental(self): size = 20 ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) now = datetime.datetime.now() for i in range(size): ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(ads1.getLength(), ads2.getLength()) self.assertEqual(ads1[:], ads2[:]) self.assertEqual(ads1.getDateTimes()[:], ads2.getDateTimes()[:])
def testNotAligned_Recursive(self): size = 20 ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) # Align the aligned dataseries again with respect to each other. ads1, ads2 = aligned.datetime_aligned(ads1, ads2) now = datetime.datetime.now() for i in range(size): if i % 2 == 0: ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) else: ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(len(ds1), len(ds2)) for ads in [ads1, ads2]: self.assertEqual(ads.getLength(), 0) self.assertEqual(ads.getFirstValidPos(), 0) self.assertEqual(ads.getValueAbsolute(0), None) self.assertEqual(ads.getDateTimes(), [])
def testBoundedSources(self): ds1 = dataseries.SequenceDataSeries(1) ds2 = dataseries.SequenceDataSeries(1) ads1, ads2 = aligned.datetime_aligned(ds1, ds2) now = datetime.datetime.now() ds1.appendWithDateTime(now + datetime.timedelta(seconds=1), 1) ds1.appendWithDateTime(now + datetime.timedelta(seconds=2), 2) ds1.appendWithDateTime(now + datetime.timedelta(seconds=3), 3) ds2.appendWithDateTime(now + datetime.timedelta(seconds=2), 2) ds2.appendWithDateTime(now + datetime.timedelta(seconds=3), 3) ds2.appendWithDateTime(now + datetime.timedelta(seconds=4), 4) self.assertEqual(ads1[:], [2, 3]) self.assertEqual(ads2[:], [2, 3])
def testBounded(self): ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2, 1) now = datetime.datetime.now() ds1.appendWithDateTime(now + datetime.timedelta(seconds=1), 1) ds1.appendWithDateTime(now + datetime.timedelta(seconds=2), 2) ds1.appendWithDateTime(now + datetime.timedelta(seconds=3), 3) ds2.appendWithDateTime(now + datetime.timedelta(seconds=2), 2) ds2.appendWithDateTime(now + datetime.timedelta(seconds=3), 3) ds2.appendWithDateTime(now + datetime.timedelta(seconds=4), 4) self.assertEqual(ads1.getValues(), [3]) self.assertEqual(ads2.getValues(), [3])
def testPartiallyAligned_Recursive(self): size = 20 commonDateTimes = [] ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) # Align the aligned dataseries again with respect to each other. ads1, ads2 = aligned.datetime_aligned(ads1, ads2) now = datetime.datetime.now() for i in range(size): if i % 3 == 0: commonDateTimes.append(now + datetime.timedelta(seconds=i)) ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) elif i % 2 == 0: ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) else: ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(len(ads1), len(ads2)) self.assertEqual(ads1[:], ads2[:]) self.assertEqual(ads1.getDateTimes(), commonDateTimes) self.assertEqual(ads2.getDateTimes(), commonDateTimes)
def testFullyAligned(self): size = 20 ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) now = datetime.datetime.now() for i in range(size): ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(len(ds1), len(ds2)) for ads in [ads1, ads2]: self.assertEqual(ads.getLength(), size) self.assertEqual(ads.getFirstValidPos(), 0) for i in range(size): self.assertEqual(ads.getValueAbsolute(i), i) self.assertEqual(ads.getDateTimes()[i], now + datetime.timedelta(seconds=i))
def testNotAligned(self): size = 20 ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) now = datetime.datetime.now() for i in range(size): if i % 2 == 0: ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) else: ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(len(ds1), len(ds2)) for ads in [ads1, ads2]: self.assertEqual(len(ads), 0) self.assertEqual(ads.getValueAbsolute(0), None) self.assertEqual(ads.getDateTimes(), [])
def testPartiallyAligned(self): size = 20 commonDateTimes = [] ds1 = dataseries.SequenceDataSeries() ds2 = dataseries.SequenceDataSeries() ads1, ads2 = aligned.datetime_aligned(ds1, ds2) now = datetime.datetime.now() for i in range(size): if i % 3 == 0: commonDateTimes.append(now + datetime.timedelta(seconds=i)) ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) elif i % 2 == 0: ds1.appendWithDateTime(now + datetime.timedelta(seconds=i), i) else: ds2.appendWithDateTime(now + datetime.timedelta(seconds=i), i) self.assertEqual(ads1.getLength(), ads2.getLength()) self.assertEqual(ads1[:], ads2[:]) self.assertEqual(ads1.getDateTimes(), commonDateTimes) self.assertEqual(ads2.getDateTimes(), commonDateTimes)