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())
class PnlAnalyzer(Analyzer): Pnl = "Pnl" def __init__(self, portfolio, state): self.portfolio = portfolio self.state = state self.series = DataSeries(time_series=self.state.pnl.series) def update(self, timestamp: int, total_equity: float): performance_series = self.portfolio.performance.series if self.series.size() >= 2: self.state.pnl.last_pnl = performance_series.get_by_idx(-1, PerformanceAnalyzer.TotalEquity) - \ performance_series.get_by_idx(-2, PerformanceAnalyzer.TotalEquity) self.series.add(timestamp=timestamp, data={self.Pnl: self.state.pnl.last_pnl}) else: self.state.pnl.last_pnl = 0 self.series.add(timestamp=timestamp, data={self.Pnl: self.state.pnl.last_pnl}) def get_result(self): return {self.Pnl: self.state.pnl.last_pnl} def get_series(self, keys=None): keys = keys if keys else self.Pnl return {self.Pnl: self.series.get_series(self.Pnl)} def last_pnl(self) -> float: return self.state.pnl.last_pnl
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"))
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"])