Exclude open paper trades from discovery
Deploy / lint (push) Successful in 7s
Deploy / test (push) Successful in 1m25s
Deploy / deploy (push) Successful in 43s

This commit is contained in:
2026-07-04 13:01:58 +02:00
parent edc1a9757b
commit 23d1db1f30
4 changed files with 113 additions and 1 deletions
+11
View File
@@ -19,6 +19,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.exceptions import NotFoundError
from app.models.fundamental import FundamentalData
from app.models.ohlcv import OHLCVRecord
from app.models.paper_trade import PaperTrade
from app.models.score import CompositeScore, DimensionScore
from app.models.sentiment import SentimentScore
from app.models.signal_context_snapshot import SignalContextSnapshot
@@ -599,6 +600,7 @@ async def get_trade_setups(
recommended_action: str | None = None,
symbol: str | None = None,
live_recommendation: bool = False,
exclude_open_trade_tickers: bool = False,
) -> list[dict]:
"""Get latest stored trade setups, optionally filtered."""
stmt = (
@@ -615,6 +617,15 @@ async def get_trade_setups(
stmt = stmt.where(TradeSetup.confidence_score >= min_confidence)
if recommended_action is not None and not live_recommendation:
stmt = stmt.where(TradeSetup.recommended_action == recommended_action)
if exclude_open_trade_tickers:
open_trade_result = await db.execute(
select(PaperTrade.ticker_id)
.where(PaperTrade.status == "open")
.distinct()
)
open_ticker_ids = {ticker_id for ticker_id, in open_trade_result.all()}
if open_ticker_ids:
stmt = stmt.where(~TradeSetup.ticker_id.in_(open_ticker_ids))
stmt = stmt.order_by(TradeSetup.detected_at.desc(), TradeSetup.id.desc())