Example #1
0
 def get_data_series(self, frequency=None, bar_filter=None):
     self.set_bar_filter(bar_filter)
     if self.__bar_ds == None:
         self.__bar_ds = dataseries.BarDataSeries()
         for bar_ in self.get_bars():
             self.__bar_ds.appendValue(bar_)
     return self.__bar_ds
Example #2
0
    def testNonEmpty(self):
        ds = dataseries.BarDataSeries()
        for i in range(10):
            ds.append_value(
                bar.Bar(
                    datetime.datetime.now() + datetime.timedelta(seconds=i), 0,
                    0, 0, 0, 0, 0))

        for i in range(0, 10):
            self.assertTrue(ds.get_value(i) != None)
Example #3
0
    def __loadSarTestBarDs(self):
        seconds = 0

        ret = dataseries.BarDataSeries()
        for i in xrange(len(SAR_HIGH)):
            date_time = datetime.datetime.now() + datetime.timedelta(
                seconds=seconds)
            ret.append_value(
                bar.Bar(date_time, SAR_LOW[i], SAR_HIGH[i], SAR_LOW[i],
                        SAR_HIGH[i], 0, SAR_LOW[i]))
            seconds += 1
        return ret
Example #4
0
 def testAppendInvalidDatetime(self):
     ds = dataseries.BarDataSeries()
     for i in range(10):
         now = datetime.datetime.now() + datetime.timedelta(seconds=i)
         ds.append_value(bar.Bar(now, 0, 0, 0, 0, 0, 0))
         # Adding the same datetime twice should fail
         self.assertRaises(Exception, ds.append_value,
                           bar.Bar(now, 0, 0, 0, 0, 0, 0))
         # Adding a previous datetime should fail
         self.assertRaises(
             Exception, ds.append_value,
             bar.Bar(now - datetime.timedelta(seconds=i), 0, 0, 0, 0, 0, 0))
Example #5
0
    def __loadBarDS(self):
        seconds = 0

        ret = dataseries.BarDataSeries()
        for i in xrange(len(OPEN_VALUES)):
            date_time = datetime.datetime.now() + datetime.timedelta(
                seconds=seconds)
            ret.append_value(
                bar.Bar(date_time, OPEN_VALUES[i], HIGH_VALUES[i],
                        LOW_VALUES[i], CLOSE_VALUES[i], VOLUME_VALUES[i],
                        CLOSE_VALUES[i]))
            seconds += 1
        return ret
Example #6
0
 def calculateValue(self, first_idx, last_idx):
     ''' If the value wasn't in the cache, we need to calculate new value(s).
     Retrieve the needed bars, call the function, and update the cache.
     Finally, return the requested value (ie last_idx).
     '''
     tmp_ds = dataseries.BarDataSeries()
     ds = self.getDataSeries()
     for i in xrange(first_idx, last_idx + 1):
         tmp_ds.append(ds.getValueAbsolute(i))
     self.__func_handle.set_data_series(tmp_ds)
     data = self.__func_handle()
     self.__update_cache(data)
     return self.__cache[last_idx]
Example #7
0
    def testSeqLikeOps(self):
        ds = dataseries.BarDataSeries()
        for i in range(10):
            ds.append_value(
                bar.Bar(
                    datetime.datetime.now() + datetime.timedelta(seconds=i), 2,
                    4, 1, 3, 10, 3))

        self.assertEqual(ds[-1], ds.get_value())
        self.assertEqual(ds[-2], ds.get_value(1))
        self.assertEqual(ds[0], ds[0])
        self.assertEqual(ds[1], ds[1])
        self.assertEqual(ds[-2:][-1], ds.get_value())
Example #8
0
    def add_bars_from_sequence(self, symbol, bars):
        if self.__started:
            raise Exception(
                "Can't add more bars once you started consuming bars")
        self.__bars.setdefault(symbol, [])
        self.__next_bar_idx.setdefault(symbol, 0)

        # Add and sort the bars
        self.__bars[symbol].extend(bars)
        barCmp = lambda x, y: cmp(x.get_date_time(), y.get_date_time())
        self.__bars[symbol].sort(barCmp)
        if symbol not in self.__ds:
            self.__ds[symbol] = dataseries.BarDataSeries()
Example #9
0
    def testEmpty(self):
        ds = dataseries.BarDataSeries()
        self.assertTrue(ds.get_value(-2) == None)
        self.assertTrue(ds.get_value(-1) == None)
        self.assertTrue(ds.get_value() == None)
        self.assertTrue(ds.get_value(1) == None)
        self.assertTrue(ds.get_value(2) == None)

        with self.assertRaises(IndexError):
            ds[-1]
        with self.assertRaises(IndexError):
            ds[0]
        with self.assertRaises(IndexError):
            ds[1000]
Example #10
0
    def testNestedDataSeries(self):
        ds = dataseries.BarDataSeries()
        for i in range(10):
            ds.append_value(
                bar.Bar(
                    datetime.datetime.now() + datetime.timedelta(seconds=i), 2,
                    4, 1, 3, 10, 3))

        self.__testGetValue(ds.get_open_data_series(), 10, 2)
        self.__testGetValue(ds.get_close_data_series(), 10, 3)
        self.__testGetValue(ds.get_high_data_series(), 10, 4)
        self.__testGetValue(ds.get_low_data_series(), 10, 1)
        self.__testGetValue(ds.get_volume_data_series(), 10, 10)
        self.__testGetValue(ds.get_adj_close_data_series(), 10, 3)