Skip to content

Implement Phase 4: TWAP and ICEBERG algorithm execution with goroutine scheduler#19

Merged
JetSquirrel merged 2 commits into
mainfrom
claude/longbridge-fs-implement-next-3-items
Apr 1, 2026
Merged

Implement Phase 4: TWAP and ICEBERG algorithm execution with goroutine scheduler#19
JetSquirrel merged 2 commits into
mainfrom
claude/longbridge-fs-implement-next-3-items

Conversation

@Claude

@Claude Claude AI commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Implements algorithmic order execution for splitting large orders to reduce market impact. Adds TWAP (time-weighted) and ICEBERG (visibility-based) algorithms with full audit trail support.

Implementation

Core Components

  • AlgoScheduler: Manages concurrent algorithm tasks via goroutines with context-based cancellation
  • AlgoTask: Tracks execution state per order (slices, timing, completion)
  • TWAP: Splits order into equal slices distributed evenly over specified duration
  • ICEBERG: Submits slices sequentially, revealing partial order size at a time

Data Model Extensions

type ParsedOrder struct {
    // ... existing fields ...
    Algo         string  // TWAP, ICEBERG
    AlgoDuration string  // "30m", "1h" (TWAP only)
    AlgoSlices   int     // number of slices
}

Beancount Order Format

2026-03-31 * "ORDER" "BUY AAPL.US 500 via TWAP"
  ; intent_id: 20260330-010
  ; algo: TWAP
  ; algo_duration: 30m
  ; algo_slices: 5

Slice Execution Output

2026-03-31 * "EXECUTION" "TWAP slice 1/5 BUY AAPL.US"
  ; intent_id: 20260330-010
  ; order_id: LOCAL-1774977622015148771
  ; qty: 100
  ; executed_at: 2026-03-31T17:20:22Z
  ; slice: 1/5
  ; algo: TWAP

Integration

  • ProcessLedgerWithScheduler(): Routes algo orders to scheduler instead of direct execution
  • Scheduler lifecycle managed in controller loop (init, cleanup, graceful shutdown)
  • Algo orders pass through same risk gate validation as regular orders
  • AppendExecutionWithMeta(): Writes slice metadata to beancount ledger
  • Audit logging tracks active algo task count

Files Changed

New

  • internal/broker/algo.go - Scheduler and task management (210 lines)
  • internal/broker/algo_twap.go - TWAP implementation (65 lines)
  • internal/broker/algo_iceberg.go - ICEBERG implementation (70 lines)
  • internal/broker/algo_test.go - Test coverage (163 lines)

Modified

  • internal/model/types.go - Added algo fields to ParsedOrder
  • internal/ledger/parser.go - Parse algo metadata from beancount
  • internal/broker/broker.go - Added scheduler integration
  • internal/audit/audit.go - Track algo_tasks_active metric
  • cmd/longbridge-fs/main.go - Initialize scheduler in controller
  • task.md - Mark Phase 4 complete

Testing

Tests verify correct slice count, timing, and ledger writes for both algorithms:

  • TestTWAPExecution: 5 slices with time-based distribution
  • TestICEBERGExecution: 3 slices with sequential submission

@Claude Claude AI linked an issue Mar 31, 2026 that may be closed by this pull request
@Claude Claude AI changed the title [WIP] Implement next 3 items for automated development task Implement Phase 4: TWAP and ICEBERG algorithm execution with goroutine scheduler Mar 31, 2026
@Claude Claude AI requested a review from JetSquirrel March 31, 2026 17:25
@JetSquirrel JetSquirrel marked this pull request as ready for review April 1, 2026 04:56
@JetSquirrel JetSquirrel merged commit 5e769eb into main Apr 1, 2026
2 checks passed
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.

[Auto-Dev] Iteration 2: implement next 3 items

2 participants