Ejemplo n.º 1
0
    def test_get_by_idx(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test", keys=["timestamp", "v1", "v2"])

        series = DataSeries(time_series=ts)

        series.add(data={"timestamp": 0, "v1": 2})
        series.add(data={"timestamp": 1, "v1": 2.4})
        series.add(data={"timestamp": 1, "v2": 3.0})

        # index and key
        self.assertEqual(2, series.get_by_idx(idx=0, keys="v1"))
        self.assertEqual(0.0, series.get_by_idx(idx=0, keys="v2"))
        self.assertEqual(2.4, series.get_by_idx(idx=1, keys="v1"))
        self.assertEqual(3.0, series.get_by_idx(idx=1, keys="v2"))

        # index only
        self.assertEqual({"timestamp": 0, "v1": 2, "v2": 0.0}, series.get_by_idx(idx=0))
        self.assertEqual({"timestamp": 1, "v1": 2.4, "v2": 3.0}, series.get_by_idx(idx=1))

        # test index slice
        series2 = self.create_series_by_list(range(100))
        sliced = series2.get_by_idx(keys='v1', idx=slice(-10, None, None))
        self.assertEqual(len(sliced), 10)

        endPoint = series2.get_by_idx(keys='v1', idx=slice(-1, None, None))
        self.assertEqual(endPoint[0], 99)
Ejemplo n.º 2
0
    def test_compare_against_oneoff_calculation(self):
        rw = np.cumsum(np.random.normal(0, 2, 1000)) + 100
        close = DataSeries(time_series=ModelFactory.build_time_series(
            series_id="close"))
        close.start(self.app_context)

        t = 1
        sma = SMA(inputs=close, input_keys='close', length=50)
        sma.start(self.app_context)

        result = []

        for x in rw:
            close.add(timestamp=t, data={"close": x})
            result.append(sma.now('value'))
            t = t + 3

        result = np.array(result)

        # either apply or direct call is equivalent
        target = close.apply('close',
                             start=None,
                             end=None,
                             func=talib.SMA,
                             timeperiod=50)
        # target = talib.SMA(np.array(close.get_series('close')), timeperiod=50)

        result[np.isnan(result)] = 0
        target[np.isnan(target)] = 0

        try:
            np.testing.assert_almost_equal(target, result, 5)
        except AssertionError as e:
            self.fail(e.message)
Ejemplo n.º 3
0
 def setUp(self):
     self.time = 9000000000
     self.simluation_clock = SimulationClock()
     self.simluation_clock.reset()
     self.simluation_clock.update_time(self.time)
     self.input = DataSeries(time_series=TimeSeries())
     self.event_bus = BarAggregatorTest.DummyEventBus()
Ejemplo n.º 4
0
    def test_ago(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test")

        series = DataSeries(time_series=ts)
        self.assertEqual(0.0, series.now())

        series.add(data={"timestamp": 0, "v1": 1, "v2": 2})
        self.assertEqual({"timestamp": 0, "v1": 1, "v2": 2}, series.ago(0))

        series.add(data={"timestamp": 1, "v1": 1.2, "v2": 2.2})
        self.assertEqual({"timestamp": 1, "v1": 1.2, "v2": 2.2}, series.ago(0))
        self.assertEqual({"timestamp": 0, "v1": 1, "v2": 2}, series.ago(1))

        series.add(data={"timestamp": 2, "v1": 1.3, "v2": 2.3})
        self.assertEqual({"timestamp": 2, "v1": 1.3, "v2": 2.3}, series.ago(0))
        self.assertEqual({"timestamp": 1, "v1": 1.2, "v2": 2.2}, series.ago(1))
        self.assertEqual({"timestamp": 0, "v1": 1, "v2": 2}, series.ago(2))

        series.add(data={"timestamp": 3, "v1": 1.4, "v2": 2.4})
        self.assertEqual({"timestamp": 3, "v1": 1.4, "v2": 2.4}, series.ago(0))
        self.assertEqual({"timestamp": 2, "v1": 1.3, "v2": 2.3}, series.ago(1))
        self.assertEqual({"timestamp": 1, "v1": 1.2, "v2": 2.2}, series.ago(2))
        self.assertEqual({"timestamp": 0, "v1": 1, "v2": 2}, series.ago(3))

        self.assertEqual({"v1": 1.4, "v2": 2.4}, series.ago(0, ["v1", "v2"]))
        self.assertEqual(1.4, series.ago(0, "v1"))
        self.assertEqual(1.4, series.ago(0, ["v1"]))
Ejemplo n.º 5
0
    def __create_series(self):
        close = DataSeries(time_series=TimeSeries())
        t = 0
        for idx, value in enumerate(self.values):
            close.add(data={"timestamp": t, "v1": value, "v2": value})
            t = t + 3

        return close
Ejemplo n.º 6
0
    def create_series_by_list(valuelist):
        close = DataSeries(time_series=TimeSeries())

        t = 0

        for value in valuelist:
            close.add(data={"timestamp": t, "v1": value})
            t = t + 3
        return close
Ejemplo n.º 7
0
    def test_get_data(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test")

        series = DataSeries(time_series=ts)

        series.add(data={"timestamp": 1, "v1": 1, "v2": 1})
        series.add(data={"timestamp": 2, "v1": 2, "v2": 2})

        self.assertEqual([{"timestamp": 1, "v1": 1, "v2": 1},
                          {"timestamp": 2, "v1": 2, "v2": 2}], series.get_data())
Ejemplo n.º 8
0
    def create_series_by_list(valuelist):
        close = DataSeries(time_series=ModelFactory.build_time_series(
            series_id="close"))

        t = 1

        for value in valuelist:
            close.add(timestamp=t, data={"v1": value})
            t = t + 3
        return close
Ejemplo n.º 9
0
    def test_size(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test")

        series = DataSeries(time_series=ts)
        self.assertEqual(0, series.size())

        series.add(data={"timestamp": 0, "v1": 1})
        self.assertEqual(1, series.size())

        series.add(data={"timestamp": 1, "v1": 1})
        self.assertEqual(2, series.size())
Ejemplo n.º 10
0
    def __create_plot(self):
        series = DataSeries(time_series=TimeSeries())
        t = 20170101
        for idx, value in enumerate(self.values):
            ts = datestr_to_unixtimemillis(t)
            series.add(timestamp=ts, data={"value": value})
            t = t + 1
        values = series.get_series(["value"])
        plot = TimeSeriesPlot(values)

        return plot
Ejemplo n.º 11
0
    def create_random_walk_series():
        close = DataSeries(time_series=TimeSeries())

        t1 = 1
        t = t1
        w = np.random.normal(0, 1, 1000)
        xs = 100 + np.cumsum(w)

        for value in xs:
            close.add(data={"timestamp": t, "v1": value, "v2": value})
            t = t + 3
        return close
Ejemplo n.º 12
0
    def test_init_w_keys(self):

        ts = DataSeriesTest.factory.build_time_series(series_id="test",
                                                      keys=["v1"])

        series = DataSeries(time_series=ts)

        series.add(data={"timestamp": 0, "v1": 1, "v2": 1})

        result = series.get_data()

        self.assertEqual(1, len(result))
        self.assertTrue("v1" in result[0])
        self.assertFalse("v2" in result[0])
Ejemplo n.º 13
0
    def test_init_w_data(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test",
                                                      keys=["v1"])
        DataSeriesTest.factory.add_time_series_item(ts, 0, {"v1": 1})
        DataSeriesTest.factory.add_time_series_item(ts, 1, {"v1": 2})
        series = DataSeries(time_series=ts)

        self.assertEqual(2, series.size())

        result = series.get_data()

        self.assertEqual(2, len(result))
        self.assertEqual(1, result[0]["v1"])
        self.assertEqual(2, result[1]["v1"])
Ejemplo n.º 14
0
    def test_get_data_dict(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test")

        series = DataSeries(time_series=ts)

        series.add(data={"timestamp": 1, "v1": 1, "v2": 1})
        series.add(data={"timestamp": 2, "v1": 2, "v2": 2})

        self.assertEqual({"timestamp": {1: 1, 2: 2},
                          "v1": {1: 1, 2: 2},
                          "v2": {1: 1, 2: 2}}, series.get_data_dict())

        self.assertEqual({"v1": {1: 1, 2: 2}, "v2": {1: 1, 2: 2}},
                         series.get_data_dict(['v1', 'v2']))
        self.assertEqual({1: 1, 2: 2}, series.get_data_dict('v1'))
Ejemplo n.º 15
0
    def test_get_by_time(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test", keys=["timestamp", "v1", "v2"])

        series = DataSeries(time_series=ts)

        # time and key
        series.add(data={"timestamp": 0, "v1": 2})
        series.add(data={"timestamp": 1, "v1": 2.4})
        series.add(data={"timestamp": 1, "v2": 3.0})

        self.assertEqual(2, series.get_by_time(time=0, keys="v1"))
        self.assertEqual(0.0, series.get_by_time(time=0, keys="v2"))
        self.assertEqual(2.4, series.get_by_time(time=1, keys="v1"))
        self.assertEqual(3.0, series.get_by_time(time=1, keys="v2"))
        # time only
        self.assertEqual({"timestamp": 0, "v1": 2, "v2": 0.0}, series.get_by_time(time=0))
        self.assertEqual({"timestamp": 1, "v1": 2.4, "v2": 3.0}, series.get_by_time(time=1))
Ejemplo n.º 16
0
    def test_override_w_same_time(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test", keys=["timestamp", "v1", "v2", "v3"])

        series = DataSeries(time_series=ts)

        series.add(data={"timestamp": 1, "v1": 2, "v2": 3})
        self.assertEqual(1, series.size())
        self.assertEqual(2, series.get_by_idx(0, "v1"))
        self.assertEqual(2, series.get_by_time(1, "v1"))
        self.assertEqual(3, series.get_by_idx(0, "v2"))
        self.assertEqual(3, series.get_by_time(1, "v2"))
        self.assertEqual(0.0, series.get_by_idx(0, "v3"))
        self.assertEqual(0.0, series.get_by_time(1, "v3"))

        series.add(data={"timestamp": 1, "v1": 2.4, "v2": 3.4, "v3": 1.1})
        self.assertEqual(1, series.size())
        self.assertEqual(2.4, series.get_by_idx(0, "v1"))
        self.assertEqual(2.4, series.get_by_time(1, "v1"))
        self.assertEqual(3.4, series.get_by_idx(0, "v2"))
        self.assertEqual(3.4, series.get_by_time(1, "v2"))
        self.assertEqual(1.1, series.get_by_idx(0, "v3"))
        self.assertEqual(1.1, series.get_by_time(1, "v3"))

        series.add(data={"timestamp": 2, "v1": 2.6, "v2": 3.6})
        self.assertEqual(2, series.size())
        self.assertEqual(2.4, series.get_by_idx(0, "v1"))
        self.assertEqual(2.4, series.get_by_time(1, "v1"))
        self.assertEqual(3.4, series.get_by_idx(0, "v2"))
        self.assertEqual(3.4, series.get_by_time(1, "v2"))
        self.assertEqual(1.1, series.get_by_idx(0, "v3"))
        self.assertEqual(1.1, series.get_by_time(1, "v3"))

        self.assertEqual(2.6, series.get_by_idx(1, "v1"))
        self.assertEqual(2.6, series.get_by_time(2, "v1"))
        self.assertEqual(3.6, series.get_by_idx(1, "v2"))
        self.assertEqual(3.6, series.get_by_time(2, "v2"))
        self.assertEqual(0.0, series.get_by_idx(1, "v3"))
        self.assertEqual(0.0, series.get_by_time(2, "v3"))
Ejemplo n.º 17
0
    def test_add(self):
        ts = DataSeriesTest.factory.build_time_series(series_id="test")

        series = DataSeries(time_series=ts)

        self.assertTrue(len(series.get_data()) == 0)

        series.add(data={"timestamp": 0, "v1": 1, "v2": 1})
        series.add(data={"timestamp": 1, "v1": 2, "v2": 2})

        self.assertEqual([{"timestamp": 0, "v1": 1, "v2": 1},
                          {"timestamp": 1, "v1": 2, "v2": 2}], series.get_data())

        series.add(data={"timestamp": 1, "v1": 3, "v2": 3})

        self.assertEqual([{"timestamp": 0, "v1": 1, "v2": 1},
                          {"timestamp": 1, "v1": 3, "v2": 3}], series.get_data())

        series.add(data={"timestamp": 2, "v1": 4, "v2": 4})

        self.assertEqual([{"timestamp": 0, "v1": 1, "v2": 1},
                          {"timestamp": 1, "v1": 3, "v2": 3},
                          {"timestamp": 2, "v1": 4, "v2": 4}], series.get_data())
Ejemplo n.º 18
0
    def load_all(self):
        if self.store:
            self.store.start(self.app_context)
            series_states = self.store.load_all('time_series')
            for series_state in series_states:
                if hasattr(series_state,
                           'series_cls') and series_state.series_cls:
                    cls = get_cls(series_state.series_cls)
                    series = cls(time_series=series_state)
                else:
                    series = DataSeries(time_series=series_state)
                self.__series_dict[series.id()] = series

            bars = self.store.load_all('bars')
            for bar in bars:
                self.__bar_dict[get_series_id(bar)] = bar

            trades = self.store.load_all('trades')
            for trade in trades:
                self.__trade_dict[get_series_id(trade)] = trade

            quotes = self.store.load_all('quotes')
            for quote in quotes:
                self.__quote_dict[get_series_id(quote)] = quote
Ejemplo n.º 19
0
 def __init__(self, portfolio, state):
     self.portfolio = portfolio
     self.state = state
     self.series = DataSeries(time_series=self.state.performance.series)
Ejemplo n.º 20
0
 def __init__(self, portfolio, state):
     self.portfolio = portfolio
     self.state = state
     self.series = DataSeries(time_series=self.state.drawdown.series)