# Tasks: Pacifica Multi-Account BTC Wash Trading System **Input**: Design documents from `/specs/001-ts-pacifica-perp/` **Prerequisites**: plan.md (required), research.md, data-model.md, contracts/ ## Execution Flow (main) ``` 1. Load plan.md from feature directory → Extract: TypeScript 5.0+, Node.js 18+, Pacifica API integration 2. Load design documents: → data-model.md: 8 entities (Account, WashTradingSession, OrderPair, Order, TradingStrategy, RiskMetrics, RiskAlert, AuditEntry) → contracts/: 5 contract files (wash-trading-api.md, account-contracts.md, hedging-api.md, hedging-contracts.md, trading-contracts.md) → research.md: Pacifica API integration, multi-account coordination, risk management 3. Generate tasks by category: → Setup: TypeScript project, dependencies, configuration → Tests: Contract tests, integration tests, unit tests → Core: Models, services, strategies, CLI commands → Integration: Pacifica API, WebSocket, risk management → Polish: Performance tests, documentation, deployment 4. Apply task rules: → Different files = mark [P] for parallel → Same file = sequential (no [P]) → Tests before implementation (TDD) 5. Number tasks sequentially (T001-T095) 6. Generate dependency graph 7. Create parallel execution examples 8. Validate task completeness 9. Return: SUCCESS (tasks ready for execution) ``` ## Format: `[ID] [P?] Description` - **[P]**: Can run in parallel (different files, no dependencies) - Include exact file paths in descriptions ## Path Conventions - **Single project**: `src/`, `tests/` at repository root - Paths based on plan.md structure: models/, services/, strategies/, cli/, utils/ ## Phase 3.1: Setup - [x] T001 Create project structure per implementation plan - [x] T002 Initialize TypeScript project with Node.js 18+ dependencies - [x] T003 [P] Configure ESLint and Prettier for TypeScript - [x] T004 [P] Set up Jest testing framework with TypeScript support - [x] T005 [P] Create environment configuration system - [x] T006 [P] Set up logging system with structured logging - [x] T007 [P] Create encryption utilities for private key management - [x] T008 [P] Set up validation utilities with Joi - [x] T009 [P] Create configuration files (accounts.json, strategies.json, risk-limits.json) - [x] T010 [P] Set up package.json with all required dependencies ## Phase 3.2: Tests First (TDD) ⚠️ MUST COMPLETE BEFORE 3.3 **CRITICAL: These tests MUST be written and MUST FAIL before ANY implementation** - [x] T011 [P] Contract test wash-trading-api endpoints in tests/contract/test-wash-trading-api.ts - [x] T012 [P] Contract test account management endpoints in tests/contract/test-account-contracts.ts - [x] T013 [P] Contract test hedging API endpoints in tests/contract/test-hedging-api.ts - [x] T014 [P] Contract test trading contracts in tests/contract/test-trading-contracts.ts - [x] T015 [P] Integration test multi-account wash trading session in tests/integration/test-wash-trading-session.ts - [x] T016 [P] Integration test account coordination in tests/integration/test-account-coordination.ts - [x] T017 [P] Integration test risk management in tests/integration/test-risk-management.ts - [x] T018 [P] Integration test Pacifica API integration in tests/integration/test-pacifica-integration.ts - [x] T019 [P] Integration test WebSocket real-time updates in tests/integration/test-websocket-updates.ts - [x] T020 [P] Integration test CLI commands in tests/integration/test-cli-commands.ts ## Phase 3.3: Core Implementation (ONLY after tests are failing) ### Data Models - [x] T021 [P] Account model in src/models/Account.ts - [x] T022 [P] WashTradingSession model in src/models/WashTradingSession.ts - [x] T023 [P] OrderPair model in src/models/OrderPair.ts - [x] T024 [P] Order model in src/models/Order.ts - [x] T025 [P] TradingStrategy model in src/models/TradingStrategy.ts - [x] T026 [P] RiskMetrics model in src/models/RiskMetrics.ts - [x] T027 [P] RiskAlert model in src/models/RiskAlert.ts - [x] T028 [P] AuditEntry model in src/models/AuditEntry.ts ### Core Services - [x] T029 [P] PacificaAPIClient service in src/services/PacificaAPIClient.ts - [x] T030 [P] WebSocketManager service in src/services/WebSocketManager.ts - [x] T031 [P] AccountManager service in src/services/AccountManager.ts - [x] T032 [P] OrderCoordinator service in src/services/OrderCoordinator.ts - [x] T033 [P] RiskManager service in src/services/RiskManager.ts - [x] T034 [P] AuditLogger service in src/services/AuditLogger.ts ### Trading Strategies - [x] T035 [P] BaseStrategy abstract class in src/strategies/BaseStrategy.ts - [x] T036 [P] EqualVolumeStrategy implementation in src/strategies/EqualVolumeStrategy.ts - [x] T037 [P] TimeBasedStrategy implementation in src/strategies/TimeBasedStrategy.ts - [x] T038 [P] MarketMakingStrategy implementation in src/strategies/MarketMakingStrategy.ts - [x] T039 [P] Strategy configuration system in src/strategies/StrategyConfig.ts ### CLI Commands - [x] T040 [P] Create session command in src/cli/commands/create-session.ts - [x] T041 [P] Start session command in src/cli/commands/start-session.ts - [x] T042 [P] Stop session command in src/cli/commands/stop-session.ts - [x] T043 [P] Pause/Resume session commands in src/cli/commands/pause-resume-session.ts - [x] T044 [P] Status monitoring command in src/cli/commands/status.ts - [x] T045 [P] Account management commands in src/cli/commands/account-management.ts - [ ] T046 [P] Risk monitoring commands in src/cli/commands/risk-monitoring.ts - [ ] T047 [P] Configuration commands in src/cli/commands/configure.ts - [ ] T048 [P] CLI dashboard interface in src/cli/dashboard.ts ## Phase 3.4: API Implementation - [x] T049 [P] REST API server setup in src/api/server.ts - [x] T050 [P] Session management API endpoints in src/api/routes/sessions.ts - [x] T051 [P] Account management API endpoints in src/api/routes/accounts.ts - [ ] T052 [P] Strategy management API endpoints in src/api/routes/strategies.ts - [ ] T053 [P] Risk monitoring API endpoints in src/api/routes/risk.ts - [ ] T054 [P] System health API endpoint in src/api/health.ts - [ ] T055 [P] WebSocket event handling in src/api/websocket.ts - [ ] T056 [P] Authentication middleware in src/middleware/auth.ts - [ ] T057 [P] Rate limiting middleware in src/middleware/rate-limit.ts - [ ] T058 [P] Error handling middleware in src/middleware/error-handler.ts - [ ] T059 [P] Request validation middleware in src/middleware/validation.ts - [ ] T060 [P] API router configuration in src/api/router.ts ## Phase 3.5: Integration - [ ] T061 [P] Pacifica API integration in src/integrations/PacificaIntegration.ts - [ ] T062 [P] WebSocket connection management in src/integrations/WebSocketIntegration.ts - [ ] T063 [P] Multi-account coordination system in src/integrations/AccountCoordination.ts - [ ] T064 [P] Real-time risk monitoring in src/integrations/RiskMonitoring.ts - [ ] T065 [P] Order execution coordination in src/integrations/OrderExecution.ts - [ ] T066 [P] Event system for inter-service communication in src/integrations/EventSystem.ts - [ ] T067 [P] Configuration management system in src/integrations/ConfigManager.ts - [ ] T068 [P] Database connection (if using persistent storage) in src/integrations/Database.ts - [ ] T069 [P] Main application entry point in src/index.ts - [ ] T070 [P] Application startup and initialization in src/app.ts ## Phase 3.6: Testing Implementation - [ ] T071 [P] Unit tests for Account model in tests/unit/models/test-Account.ts - [ ] T072 [P] Unit tests for WashTradingSession model in tests/unit/models/test-WashTradingSession.ts - [ ] T073 [P] Unit tests for OrderPair model in tests/unit/models/test-OrderPair.ts - [ ] T074 [P] Unit tests for Order model in tests/unit/models/test-Order.ts - [ ] T075 [P] Unit tests for TradingStrategy model in tests/unit/models/test-TradingStrategy.ts - [ ] T076 [P] Unit tests for RiskMetrics model in tests/unit/models/test-RiskMetrics.ts - [ ] T077 [P] Unit tests for RiskAlert model in tests/unit/models/test-RiskAlert.ts - [ ] T078 [P] Unit tests for AuditEntry model in tests/unit/models/test-AuditEntry.ts - [ ] T079 [P] Unit tests for PacificaAPIClient in tests/unit/services/test-PacificaAPIClient.ts - [ ] T080 [P] Unit tests for WebSocketManager in tests/unit/services/test-WebSocketManager.ts - [ ] T081 [P] Unit tests for AccountManager in tests/unit/services/test-AccountManager.ts - [ ] T082 [P] Unit tests for OrderCoordinator in tests/unit/services/test-OrderCoordinator.ts - [ ] T083 [P] Unit tests for RiskManager in tests/unit/services/test-RiskManager.ts - [ ] T084 [P] Unit tests for AuditLogger in tests/unit/services/test-AuditLogger.ts - [ ] T085 [P] Unit tests for trading strategies in tests/unit/strategies/test-strategies.ts ## Phase 3.7: Polish - [ ] T086 [P] Performance benchmarks for order execution latency in tests/performance/test-order-execution.ts - [ ] T087 [P] Performance benchmarks for multi-account coordination in tests/performance/test-account-coordination.ts - [ ] T088 [P] Performance benchmarks for WebSocket throughput in tests/performance/test-websocket-throughput.ts - [ ] T089 [P] Load testing for concurrent sessions in tests/performance/test-concurrent-sessions.ts - [ ] T090 [P] Memory usage optimization and testing in tests/performance/test-memory-usage.ts - [ ] T091 [P] Update API documentation in docs/api.md - [ ] T092 [P] Update quickstart guide with real examples in docs/quickstart.md - [ ] T093 [P] Create deployment guide in docs/deployment.md - [ ] T094 [P] Remove code duplication and optimize imports - [ ] T095 [P] Run manual testing scenarios from quickstart.md ## Dependencies - Setup (T001-T010) before all other phases - Tests (T011-T020) before implementation (T021-T070) - Models (T021-T028) before services (T029-T034) - Services (T029-T034) before strategies (T035-T039) - Strategies (T035-T039) before CLI commands (T040-T048) - CLI commands (T040-T048) before API implementation (T049-T060) - API implementation (T049-T060) before integration (T061-T070) - Integration (T061-T070) before testing implementation (T071-T085) - Testing implementation (T071-T085) before polish (T086-T095) ## Parallel Execution Examples ### Phase 3.1 Setup (T003-T010 can run in parallel): ``` Task: "Configure ESLint and Prettier for TypeScript" Task: "Set up Jest testing framework with TypeScript support" Task: "Create environment configuration system" Task: "Set up logging system with structured logging" Task: "Create encryption utilities for private key management" Task: "Set up validation utilities with Joi" Task: "Create configuration files (accounts.json, strategies.json, risk-limits.json)" Task: "Set up package.json with all required dependencies" ``` ### Phase 3.2 Contract Tests (T011-T020 can run in parallel): ``` Task: "Contract test wash-trading-api endpoints in tests/contract/test-wash-trading-api.ts" Task: "Contract test account management endpoints in tests/contract/test-account-contracts.ts" Task: "Contract test hedging API endpoints in tests/contract/test-hedging-api.ts" Task: "Contract test trading contracts in tests/contract/test-trading-contracts.ts" Task: "Integration test multi-account wash trading session in tests/integration/test-wash-trading-session.ts" Task: "Integration test account coordination in tests/integration/test-account-coordination.ts" Task: "Integration test risk management in tests/integration/test-risk-management.ts" Task: "Integration test Pacifica API integration in tests/integration/test-pacifica-integration.ts" Task: "Integration test WebSocket real-time updates in tests/integration/test-websocket-updates.ts" Task: "Integration test CLI commands in tests/integration/test-cli-commands.ts" ``` ### Phase 3.3 Data Models (T021-T028 can run in parallel): ``` Task: "Account model in src/models/Account.ts" Task: "WashTradingSession model in src/models/WashTradingSession.ts" Task: "OrderPair model in src/models/OrderPair.ts" Task: "Order model in src/models/Order.ts" Task: "TradingStrategy model in src/models/TradingStrategy.ts" Task: "RiskMetrics model in src/models/RiskMetrics.ts" Task: "RiskAlert model in src/models/RiskAlert.ts" Task: "AuditEntry model in src/models/AuditEntry.ts" ``` ### Phase 3.3 Core Services (T029-T034 can run in parallel): ``` Task: "PacificaAPIClient service in src/services/PacificaAPIClient.ts" Task: "WebSocketManager service in src/services/WebSocketManager.ts" Task: "AccountManager service in src/services/AccountManager.ts" Task: "OrderCoordinator service in src/services/OrderCoordinator.ts" Task: "RiskManager service in src/services/RiskManager.ts" Task: "AuditLogger service in src/services/AuditLogger.ts" ``` ### Phase 3.3 Trading Strategies (T035-T039 can run in parallel): ``` Task: "BaseStrategy abstract class in src/strategies/BaseStrategy.ts" Task: "EqualVolumeStrategy implementation in src/strategies/EqualVolumeStrategy.ts" Task: "TimeBasedStrategy implementation in src/strategies/TimeBasedStrategy.ts" Task: "MarketMakingStrategy implementation in src/strategies/MarketMakingStrategy.ts" Task: "Strategy configuration system in src/strategies/StrategyConfig.ts" ``` ### Phase 3.3 CLI Commands (T040-T048 can run in parallel): ``` Task: "Create session command in src/cli/commands/create-session.ts" Task: "Start session command in src/cli/commands/start-session.ts" Task: "Stop session command in src/cli/commands/stop-session.ts" Task: "Pause/Resume session commands in src/cli/commands/pause-resume-session.ts" Task: "Status monitoring command in src/cli/commands/status.ts" Task: "Account management commands in src/cli/commands/account-management.ts" Task: "Risk monitoring commands in src/cli/commands/risk-monitoring.ts" Task: "Configuration commands in src/cli/commands/configure.ts" Task: "CLI dashboard interface in src/cli/dashboard.ts" ``` ### Phase 3.4 API Implementation (T049-T060 can run in parallel): ``` Task: "Session management API endpoints in src/api/sessions.ts" Task: "Account management API endpoints in src/api/accounts.ts" Task: "Strategy management API endpoints in src/api/strategies.ts" Task: "Order management API endpoints in src/api/orders.ts" Task: "Risk management API endpoints in src/api/risk.ts" Task: "System health API endpoint in src/api/health.ts" Task: "WebSocket event handling in src/api/websocket.ts" Task: "Authentication middleware in src/middleware/auth.ts" Task: "Rate limiting middleware in src/middleware/rate-limit.ts" Task: "Error handling middleware in src/middleware/error-handler.ts" Task: "Request validation middleware in src/middleware/validation.ts" Task: "API router configuration in src/api/router.ts" ``` ### Phase 3.5 Integration (T061-T070 can run in parallel): ``` Task: "Pacifica API integration in src/integrations/PacificaIntegration.ts" Task: "WebSocket connection management in src/integrations/WebSocketIntegration.ts" Task: "Multi-account coordination system in src/integrations/AccountCoordination.ts" Task: "Real-time risk monitoring in src/integrations/RiskMonitoring.ts" Task: "Order execution coordination in src/integrations/OrderExecution.ts" Task: "Event system for inter-service communication in src/integrations/EventSystem.ts" Task: "Configuration management system in src/integrations/ConfigManager.ts" Task: "Database connection (if using persistent storage) in src/integrations/Database.ts" Task: "Main application entry point in src/index.ts" Task: "Application startup and initialization in src/app.ts" ``` ### Phase 3.6 Testing Implementation (T071-T085 can run in parallel): ``` Task: "Unit tests for Account model in tests/unit/models/test-Account.ts" Task: "Unit tests for WashTradingSession model in tests/unit/models/test-WashTradingSession.ts" Task: "Unit tests for OrderPair model in tests/unit/models/test-OrderPair.ts" Task: "Unit tests for Order model in tests/unit/models/test-Order.ts" Task: "Unit tests for TradingStrategy model in tests/unit/models/test-TradingStrategy.ts" Task: "Unit tests for RiskMetrics model in tests/unit/models/test-RiskMetrics.ts" Task: "Unit tests for RiskAlert model in tests/unit/models/test-RiskAlert.ts" Task: "Unit tests for AuditEntry model in tests/unit/models/test-AuditEntry.ts" Task: "Unit tests for PacificaAPIClient in tests/unit/services/test-PacificaAPIClient.ts" Task: "Unit tests for WebSocketManager in tests/unit/services/test-WebSocketManager.ts" Task: "Unit tests for AccountManager in tests/unit/services/test-AccountManager.ts" Task: "Unit tests for OrderCoordinator in tests/unit/services/test-OrderCoordinator.ts" Task: "Unit tests for RiskManager in tests/unit/services/test-RiskManager.ts" Task: "Unit tests for AuditLogger in tests/unit/services/test-AuditLogger.ts" Task: "Unit tests for trading strategies in tests/unit/strategies/test-strategies.ts" ``` ### Phase 3.7 Polish (T086-T095 can run in parallel): ``` Task: "Performance benchmarks for order execution latency in tests/performance/test-order-execution.ts" Task: "Performance benchmarks for multi-account coordination in tests/performance/test-account-coordination.ts" Task: "Performance benchmarks for WebSocket throughput in tests/performance/test-websocket-throughput.ts" Task: "Load testing for concurrent sessions in tests/performance/test-concurrent-sessions.ts" Task: "Memory usage optimization and testing in tests/performance/test-memory-usage.ts" Task: "Update API documentation in docs/api.md" Task: "Update quickstart guide with real examples in docs/quickstart.md" Task: "Create deployment guide in docs/deployment.md" Task: "Remove code duplication and optimize imports" Task: "Run manual testing scenarios from quickstart.md" ``` ## Notes - [P] tasks = different files, no dependencies - Verify tests fail before implementing - Commit after each task - Avoid: vague tasks, same file conflicts - Focus on BTC trading pairs (BTC/USD, BTC/ETH) as primary targets - Ensure position neutrality across all accounts - Maintain <100ms order execution latency - Support up to 20 concurrent accounts - Handle 1000+ orders/hour throughput ## Task Generation Rules *Applied during main() execution* 1. **From Contracts**: - 5 contract files → 5 contract test tasks [P] - 20+ endpoints → 20+ implementation tasks 2. **From Data Model**: - 8 entities → 8 model creation tasks [P] - Relationships → service layer tasks 3. **From User Stories**: - 3 primary stories → 3 integration tests [P] - Quickstart scenarios → validation tasks 4. **Ordering**: - Setup → Tests → Models → Services → Strategies → CLI → API → Integration → Testing → Polish - Dependencies block parallel execution ## Validation Checklist *GATE: Checked by main() before returning* - [x] All contracts have corresponding tests (5 contract files → 5 test tasks) - [x] All entities have model tasks (8 entities → 8 model tasks) - [x] All tests come before implementation (T011-T020 before T021-T070) - [x] Parallel tasks truly independent (different files, no shared dependencies) - [x] Each task specifies exact file path - [x] No task modifies same file as another [P] task - [x] BTC trading pair focus maintained throughout - [x] Multi-account coordination requirements addressed - [x] Risk management and audit logging included - [x] Performance targets specified (<100ms latency, 1000+ orders/hour) ## Config & Exposure Streamlining Plan (2025-10-05) - Use shared base equity = min(account balances) to derive unified capacity caps. - Introduce utilization regimes (<60% build, 60–80% balance, ≥80% reduce) and expose to strategy/monitoring modules. - Let ExposureRiskMonitor own rebalance signals; SimpleStrategyEngine only produces neutral volume orders respecting regime. - Refactor configs: execution.json (done), monitoring/phases next, plus schema validation. - Add logging & docs for key thresholds; ensure SignalExecutor locks prevent concurrent rebalance.