def of_metric(portfolio, metric: PerformanceMetric, time_frame: TimeFrame): # Create SnapshotAssetPriceCollection object for iteration snapshot_asset_price_collection = SnapshotAssetPriceCollection( portfolio=portfolio, time_frame=time_frame, ) if PerformanceMetric.OVERALL_PERFORMANCE.equals(metric): return PerformanceService().generate_overall_performance( snapshot_asset_price_collection) elif PerformanceMetric.DELTA.equals(metric): return PerformanceService().generate_delta( snapshot_asset_price_collection) raise NotImplementedError("Performance metric not supported")
def test_asset_prices_collection_one_minute(self): portfolio = SQLLitePortfolio.query.first() self.create_limit_order( portfolio, self.TARGET_SYMBOL_A, amount=1, price=self.get_price(self.TARGET_SYMBOL_A).price, creation_datetime=datetime.utcnow(), side=OrderSide.BUY.value, execution_datetime=None, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_B, amount=1, price=self.get_price(self.TARGET_SYMBOL_B).price, creation_datetime=datetime.utcnow(), side=OrderSide.BUY.value, execution_datetime=None, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_C, amount=1, price=self.get_price(self.TARGET_SYMBOL_C).price, creation_datetime=datetime.utcnow(), side=OrderSide.BUY.value, execution_datetime=None, executed=True, ) asset_price_collection = SnapshotAssetPriceCollection( portfolio, TimeFrame.ONE_HOUR ) self.assertEqual(3, len(asset_price_collection.asset_prices)) for asset_prices_entry in asset_price_collection.asset_prices: self.assertEqual( TimeInterval.MINUTES_ONE.amount_of_data_points(), len(asset_prices_entry) )
def test(self): portfolio = SQLLitePortfolio.query.first() snapshots_asset_price_collection = SnapshotAssetPriceCollection( portfolio, TimeFrame.ONE_HOUR ) snapshots = snapshots_asset_price_collection.snapshots self.assertTrue(len(snapshots) > 0) snapshot_queue = SnapShotQueue(snapshots) self.assertTrue(snapshot_queue.size > 0) self.assertFalse(snapshot_queue.empty()) while not snapshot_queue.empty(): current_snapshot = snapshot_queue.pop() peek_snapshot = snapshot_queue.peek() if peek_snapshot is not None: self.assertTrue( current_snapshot.created_at < peek_snapshot.created_at )
def test_iteration_operation_with_inner_snapshots(self): amount_of_snapshots_visited = 0 portfolio = SQLLitePortfolio.query.first() self.assertEqual(1, SQLLitePortfolioSnapshot.query.count()) first_snapshot = SQLLitePortfolioSnapshot.query.filter_by( portfolio_id=portfolio.id ).first() # Snapshot before range first_snapshot.created_at = datetime.utcnow() - timedelta(minutes=70) db.session.commit() self.create_limit_order( portfolio, self.TARGET_SYMBOL_A, amount=1, price=self.get_price(self.TARGET_SYMBOL_A).price, creation_datetime=datetime.utcnow() - timedelta(minutes=50), side=OrderSide.BUY.value, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_B, amount=1, price=self.get_price(self.TARGET_SYMBOL_B).price, creation_datetime=datetime.utcnow() - timedelta(minutes=49), side=OrderSide.BUY.value, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_C, amount=1, price=self.get_price(self.TARGET_SYMBOL_C).price, creation_datetime=datetime.utcnow() - timedelta(minutes=48), side=OrderSide.BUY.value, executed=True, ) asset_price_collection = SnapshotAssetPriceCollection( portfolio, TimeFrame.ONE_DAY ) self.assertEqual(7, len(asset_price_collection.snapshots)) index = 0 old_snapshot = None for interval_date, snapshot, asset_prices in asset_price_collection: if old_snapshot != snapshot: amount_of_snapshots_visited += 1 index += 1 # 3 prices per entry self.assertEqual(3, len(asset_prices)) self.assertIsNotNone(interval_date) peek_date, _ = asset_price_collection.asset_prices_queue.peek() peek_snapshot = asset_price_collection.snapshot_queue.peek() if peek_date is not None: self.assertTrue(interval_date < peek_date) if snapshot is not None and peek_snapshot is not None: self.assertTrue(snapshot.created_at < peek_snapshot.created_at) old_snapshot = snapshot
def test_iterate_operation_one_year(self): portfolio = SQLLitePortfolio.query.first() self.assertEqual( 1, SQLLitePortfolioSnapshot.query.filter_by( portfolio_id=portfolio.id).count() ) first_snapshot = SQLLitePortfolioSnapshot.query.filter_by( portfolio_id=portfolio.id ).first() first_snapshot.created_at = datetime.utcnow() - timedelta(weeks=40) db.session.commit() self.create_limit_order( portfolio, self.TARGET_SYMBOL_A, amount=1, price=self.get_price(self.TARGET_SYMBOL_A).price, creation_datetime=datetime.utcnow() - timedelta(weeks=20), side=OrderSide.BUY.value, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_B, amount=1, price=self.get_price(self.TARGET_SYMBOL_B).price, creation_datetime=datetime.utcnow() - timedelta(weeks=18), side=OrderSide.BUY.value, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_C, amount=1, price=self.get_price(self.TARGET_SYMBOL_C).price, creation_datetime=datetime.utcnow() - timedelta(weeks=15), side=OrderSide.BUY.value, executed=True, ) asset_price_collection = SnapshotAssetPriceCollection( portfolio, TimeFrame.ONE_YEAR ) self.assertEqual(7, len(asset_price_collection.snapshots)) total_amount_of_snapshots_visited = 0 first_snapshot_passed = False current_snapshot = None for interval_date, snapshot, asset_prices in asset_price_collection: # 3 prices per entry self.assertEqual(3, len(asset_prices)) self.assertIsNotNone(interval_date) peek_date, _ = asset_price_collection.asset_prices_queue.peek() peek_snapshot = asset_price_collection.snapshot_queue.peek() if peek_date is not None: self.assertTrue(interval_date < peek_date) if snapshot is not None: if not first_snapshot_passed: total_amount_of_snapshots_visited += 1 self.assertEqual( snapshot.created_at, first_snapshot.created_at ) current_snapshot = snapshot first_snapshot_passed = True if current_snapshot is not None: if current_snapshot.created_at != snapshot.created_at: total_amount_of_snapshots_visited += 1 current_snapshot = snapshot if peek_snapshot is not None: self.assertTrue( snapshot.created_at < peek_snapshot.created_at ) # 7 snapshots should be visited, including the snapshot created at # the start self.assertEqual(7, total_amount_of_snapshots_visited)
def test_unique_symbols(self): portfolio = SQLLitePortfolio.query.first() self.create_limit_order( portfolio, self.TARGET_SYMBOL_A, amount=1, price=self.get_price(self.TARGET_SYMBOL_A).price, creation_datetime=datetime.utcnow(), side=OrderSide.BUY.value, execution_datetime=None, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_A, amount=1, price=self.get_price(self.TARGET_SYMBOL_A).price, creation_datetime=datetime.utcnow(), side=OrderSide.BUY.value, execution_datetime=None, executed=True, ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_A, amount=1, price=self.get_price(self.TARGET_SYMBOL_A).price, creation_datetime=datetime.utcnow(), side=OrderSide.BUY.value, execution_datetime=None, executed=True, ) asset_price_collection = SnapshotAssetPriceCollection( portfolio, TimeFrame.ONE_HOUR ) self.assertEqual( 1, len(asset_price_collection._retrieve_unique_symbols()) ) self.create_limit_order( portfolio, self.TARGET_SYMBOL_B, amount=1, price=self.get_price(self.TARGET_SYMBOL_B).price, creation_datetime=datetime.utcnow(), side=OrderSide.BUY.value, execution_datetime=None, executed=True, ) asset_price_collection = SnapshotAssetPriceCollection( portfolio, TimeFrame.ONE_HOUR ) self.assertEqual( 2, len(asset_price_collection._retrieve_unique_symbols()) )