From 61ce6848b9f89c81131164056681a01b33ea0714 Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Mon, 8 Feb 2021 09:02:12 -0600 Subject: [PATCH] Sort positions by DTE before printing. --- thetagang/portfolio_manager.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/thetagang/portfolio_manager.py b/thetagang/portfolio_manager.py index d94cafb7f..faa0eeff9 100644 --- a/thetagang/portfolio_manager.py +++ b/thetagang/portfolio_manager.py @@ -218,7 +218,13 @@ def summarize_account(self): click.echo() for symbol in portfolio_positions.keys(): click.secho(f" {symbol}:", fg="cyan") - for p in portfolio_positions[symbol]: + sorted_positions = sorted( + portfolio_positions[symbol], + key=lambda p: option_dte(p.contract.lastTradeDateOrContractMonth) + if isinstance(p.contract, Option) + else 0, + ) + for p in sorted_positions: if isinstance(p.contract, Stock): pnl = round(position_pnl(p) * 100, 2) click.secho(