Skip to content

Add base classes for position analysis#223

Closed
maread99 wants to merge 2 commits intomainfrom
claude/refactor-position-base-classes-yWhWi
Closed

Add base classes for position analysis#223
maread99 wants to merge 2 commits intomainfrom
claude/refactor-position-base-classes-yWhWi

Conversation

@maread99
Copy link
Copy Markdown
Owner

@maread99 maread99 commented Mar 5, 2026

Summary

This PR introduces a new positions module that provides base classes for representing, analyzing, and visualizing financial positions. The module enables tracking of individual positions taken in financial instruments and aggregating results across multiple instruments.

Key Changes

  • PositionBase: A frozen dataclass representing a single position with:

    • OHLC price data tracking the position's lifecycle
    • Properties for calculating returns (gross and net of spread), profitability, duration, and price extrema
    • Support for chart analysis through inheritance from CaseBase and CaseSupportsChartAnaly
  • PositionsBase: A container for all positions in a single instrument over an analysis period with:

    • Aggregated metrics across all positions (total returns, profitability percentage)
    • Convenience properties for accessing position attributes in bulk (open/close dates, prices, returns)
    • HTML generation for position tooltips with color-coding based on profitability
    • Integration with chart analysis framework
  • ConsolidatedBase: A summary class for consolidating position results across multiple instruments with:

    • Per-instrument return tracking and sorting
    • Aggregate statistics (total returns, percentage of profitable instruments)
    • Chart generation for distributed returns visualization
  • ChartPositionsBase: An OHLC chart with position overlays showing:

    • Colored lines tracking each position (green for profitable, red for losses)
    • Scatter marks for position opens (circles) and closes (crosses)
    • Interactive tooltips with detailed position information
  • PositionsGuiBase: An interactive GUI for displaying and analyzing positions with:

    • Integration with the existing GUI framework
    • Support for narrow and wide view modes
    • Customizable chart parameters

Implementation Details

  • Positions track both the mid-price and spread costs, allowing calculation of net returns
  • The line property provides a special series representation with duplicate indices for session opens to capture both entry and session-end prices
  • Profitability is determined on a net basis (accounting for spread costs)
  • The module integrates with existing market_analy infrastructure (cases, charts, formatters, GUIs)
  • All classes use type hints and frozen dataclasses where appropriate for immutability

https://claude.ai/code/session_01TnPAQhPW1RpEYuKuQFwqmY

claude added 2 commits March 5, 2026 12:16
Extract general position functionality into new base classes:
- PositionBase: base frozen dataclass for a single position
- PositionsBase: base frozen dataclass for a collection of positions
- ConsolidatedBase: base class for consolidated multi-instrument results
- ChartPositionsBase: base OHLC chart with position overlay
- PositionsGuiBase: base GUI for interactive position analysis
- distributed_rtrns: chart function for return distribution

These base classes parallel the existing trends subpackage pattern and
enable downstream packages (e.g. inv) to extend with domain-specific
position logic.

https://claude.ai/code/session_01TnPAQhPW1RpEYuKuQFwqmY
Rename properties on PositionBase and PositionsBase to be timeframe
agnostic:
- sessions -> bars
- open_date -> open_bar
- close_date -> close_bar
- max_date -> max_bar
- open_dates -> open_bars
- close_dates -> close_bars
- max_dates -> max_bars

Update all docstrings to use "bar" terminology instead of
"session"/"daily".

https://claude.ai/code/session_01TnPAQhPW1RpEYuKuQFwqmY
@maread99
Copy link
Copy Markdown
Owner Author

maread99 commented Mar 5, 2026

Not implemented.

@maread99 maread99 closed this Mar 5, 2026
@maread99 maread99 deleted the claude/refactor-position-base-classes-yWhWi branch March 9, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants