Position Management and Closing

This document covers the position monitoring, early closure detection, and P&L-based decision making components of the Signal trading system. The CloseOutline service continuously monitors open positions and makes intelligent decisions about whether to hold or close positions based on real-time P&L dynamics, technical analysis, and historical patterns.

This page focuses specifically on the post-entry phase of trading. For information about position entry and signal generation, see Signal Generation and Validation. For broader risk assessment across market conditions, see Risk Assessment.

The CloseOutline system operates as the second stage in the three-stage AI decision pipeline, specializing in position management after entry. It continuously monitors open positions and evaluates whether to maintain or close them based on sophisticated P&L analysis.

Mermaid Diagram

Service Responsibility File Location
CloseOutline AI decision making for position closure src/logic/outline/close.outline.ts
CloseAutoService Automated position monitoring scheduler Referenced in architecture
CloseValidationService Risk-adjusted close decision validation src/lib/signal/services/validation/CloseValidationService.ts
ClosePromptService Prompt management for AI interactions DI container registration
BacktestDbService Position state and P&L history tracking Referenced in consolidation system

The position monitoring system tracks multiple dimensions of position performance to make informed closure decisions.

Mermaid Diagram

The system performs comprehensive breakeven analysis considering trading commissions and position performance:

Mermaid Diagram

The CloseOutline AI uses sophisticated prompt engineering to analyze position dynamics and make closure decisions based on professional trading intuition.

The system applies two primary decision paths based on P&L behavior:

  • P&L demonstrates stable or ascending growth - positive dynamics or minimal drawdowns up to 0.2%
  • Momentum shows stability or growth - P&L grows confidently, even with temporary drawdowns up to 0.15%
  • Position approaches Take Profit or maintains growth potential based on original opening rationale
  • Normal volatility - fluctuations up to 0.2% considered acceptable
  • Technical indicators show trend continuation - signal agreement across most timeframes
  • Momentum consistently falling for 15+ minutes - P&L declining without recovery signs (drawdowns over 0.3%)
  • Excessively high volatility - P&L experiencing uncontrolled fluctuations (over 0.3% from peak)
  • Position losing progress toward Take Profit or steadily moving toward Stop Loss for 15+ minutes
  • Sustained loss over 0.3% without recovery signs
  • Technical indicators show clear trend reversal - sustained signal disagreement for 15+ minutes

Mermaid Diagram

The CloseOutline employs an adaptive Time-To-Live (TTL) system that adjusts monitoring frequency based on current P&L dynamics, optimizing between responsiveness and resource usage.

Mermaid Diagram

P&L State TTL Duration Rationale
PROFIT_RISING_TTL 2.5 minutes Moderate frequency when profit growing and price rising
PROFIT_FALLING_TTL 1.0 minutes High frequency when profit declining and price falling
LOSS_RISING_TTL 5.0 minutes Low frequency when loss shrinking and price rising
LOSS_FALLING_TTL 5.0 minutes Low frequency when loss growing and price falling

The CloseValidationService provides an additional layer of risk management and validation before executing closure decisions.

Mermaid Diagram

The validation system integrates with the feature flag architecture to provide granular control over closure behavior:

Feature Flag Purpose
isFeatureCloseBreakevenEnable Controls microtrading mode with exclusive early closure
isFeatureCloseWithProfitEnable Enables early closure of profitable positions
isFeatureCloseWithLossEnable Enables early closure of losing positions
isFeatureCloseRiskFilterEnable Applies risk filter for market condition consideration

The position management system integrates seamlessly with the broader trading execution pipeline.

Mermaid Diagram

The system maintains comprehensive audit trails and historical data for analysis:

Mermaid Diagram

The close.json file demonstrates a real CloseOutline analysis for an XRPUSDT position:

Position Details:

  • Symbol: XRPUSDT
  • Entry Price: 2.8922 USD
  • Current Price: 2.8918 USD
  • Take Profit: 2.916 USD (+0.8%)
  • Stop Loss: 2.881 USD (-0.3%)
  • Current P&L: -0.01% (-0.01 USD)
  • Position Age: 4 minutes

P&L History Analysis:

Price P&L % P&L USD Breakeven Time
2.8922 0.00% +0.00 NO 05:56:29
2.8943 0.07% +0.07 NO 05:57:31
2.8952 0.10% +0.10 NO 05:59:06
2.8937 0.05% +0.05 NO 06:00:28

AI Decision: CLOSE

  • Reasoning: "P&L demonstrates weak momentum — reached peak +0.10% but retreated to +0.05%, indicating loss of initial progress. Historical pattern analysis shows 9 out of 11 similar conditions led to early closure with minimal profit or loss."

The system analyzes historical trading outcomes to inform current decisions:

  • Partial Profit Trades: 2 trades with early profitable exits
  • Commission Loss Trades: 9 trades closed with minimal losses
  • Stop Loss Trades: 0 trades reaching maximum loss
  • Full Profit Trades: 0 trades reaching take profit targets

This pattern indicates the system's effectiveness in preventing large losses while capturing available profits through intelligent early closure detection.