Beispiel #1
0
def load_test_csv(path):
    inputSeq = []
    expectedSeq = []
    csvFile = open(path, "r")
    reader = csv.DictReader(csvFile)
    for row in reader:
        inputSeq.append(float(row["Input"]))
        expected = row["Expected"]
        if not expected:
            expected = None
        else:
            expected = float(expected)
        expectedSeq.append(expected)

    return (dataseries.SequenceDataSeries(inputSeq),
            dataseries.SequenceDataSeries(expectedSeq))
    def testSeqLikeOps(self):
        seq = range(10)
        ds = dataseries.SequenceDataSeries(seq)

        # Test length and every item.
        self.assertEqual(len(ds), len(seq))
        for i in xrange(len(seq)):
            self.assertEqual(ds[i], seq[i])

        # Test negative indices
        self.assertEqual(ds[-1], seq[-1])
        self.assertEqual(ds[-2], seq[-2])
        self.assertEqual(ds[-9], seq[-9])

        # Test slices
        sl = slice(0, 1, 2)
        self.assertEqual(ds[sl], seq[sl])
        sl = slice(0, 9, 2)
        self.assertEqual(ds[sl], seq[sl])
        sl = slice(0, -1, 1)
        self.assertEqual(ds[sl], seq[sl])

        for i in xrange(-100, 100):
            self.assertEqual(ds[i:], seq[i:])

        for step in xrange(1, 10):
            for i in xrange(-100, 100):
                self.assertEqual(ds[i::step], seq[i::step])
 def testEmpty(self):
     ds = dataseries.SequenceDataSeries([])
     self.assertTrue(ds.get_first_valid_index() == 0)
     self.assertTrue(ds.get_length() == 0)
     with self.assertRaises(IndexError):
         ds[-1]
     with self.assertRaises(IndexError):
         ds[-2]
     with self.assertRaises(IndexError):
         ds[0]
     with self.assertRaises(IndexError):
         ds[1]
    def testNonEmpty(self):
        ds = dataseries.SequenceDataSeries(range(10))
        self.assertTrue(ds.get_first_valid_index() == 0)
        self.assertTrue(ds.get_length() == 10)
        self.assertTrue(ds[-1] == 9)
        self.assertTrue(ds[-2] == 8)
        self.assertTrue(ds[0] == 0)
        self.assertTrue(ds[1] == 1)

        self.assertTrue(ds.get_values(1) == [9])
        self.assertTrue(ds.get_values(2) == [8, 9])
        self.assertTrue(ds.get_values(1, 1) == [8])
        self.assertTrue(ds.get_values(2, 1) == [7, 8])

        self.assertTrue(ds.get_values_absolute(1, 3) == [1, 2, 3])
        self.assertTrue(ds.get_values_absolute(9, 9) == [9])
        self.assertTrue(ds.get_values_absolute(9, 10) == None)
        self.assertTrue(ds.get_values_absolute(9, 10, True) == [9, None])
Beispiel #5
0
    def testInvalidPosNotCached(self):
        values = []
        ds = dataseries.SequenceDataSeries(values)
        for i in range(10):
            values.append(i)
            values.append(None)  # Interleave Nones.

        testFilter = DataSeriesFilterTest.TestFilter(ds)
        self.assertTrue(testFilter[-1] == None)
        self.assertTrue(testFilter[-2] == 9)
        self.assertTrue(
            testFilter[-4] == 8
        )  # We go 3 instead of 2 because we need to skip the interleaved None values.

        self.assertTrue(testFilter[18] == 9)
        self.assertTrue(testFilter[19] == None)
        # Absolut pos 20 should have the next value once we insert it, but right now it should be invalid.
        with self.assertRaises(IndexError):
            testFilter[20]
        values.append(10)
        self.assertTrue(testFilter[20] == 10)
Beispiel #6
0
 def __init__(self):
     self.__net_returns = dataseries.SequenceDataSeries()
     self.__cumulative_returns = dataseries.SequenceDataSeries()
Beispiel #7
0

class Accumulator(technical.DataSeriesFilter):
    def __init__(self, dataSeries, windowSize):
        technical.DataSeriesFilter.__init__(self, dataSeries, windowSize)

    def calculateValue(self, first_idx, last_idx):
        accum = 0
        for value in self.get_data_series()[first_idx:last_idx + 1]:
            # If any value from the wrapped DataSeries is None then we abort calculation and return None.
            if value is None:
                return None
            accum += value
        return accum


# Build a sequence based DataSeries.
ds = dataseries.SequenceDataSeries(range(0, 50))

# Wrap it with a 3 element Accumulator filter.
ds = Accumulator(ds, 3)

# Get some values.
print ds[0]  # Not enough values yet.
print ds[1]  # Not enough values yet.
print ds[2]  # Ok, now we should have at least 3 values.
print ds[3]

# Get the last value, which should equals 49 + 48 + 47.
print ds[-1]
Beispiel #8
0
 def __loadMedPriceDS(self):
     ret = dataseries.SequenceDataSeries()
     for i in xrange(len(OPEN_VALUES)):
         ret.append_value(LOW_VALUES[i] +
                          (HIGH_VALUES[i] - LOW_VALUES[i]) / 2.0)
     return ret