Commit Graph

107 Commits

Author SHA1 Message Date
rob ac4c085acd Improve formations drawing UX with step-by-step flow
Changed UX flow from confusing to clear:
1. Click Line/Channel -> Shows instructions + points counter
2. Click points on chart -> Updates counter (Points: 1/2)
3. After enough points -> Shows name input + Save button
4. Enter name + Save -> Formation created

formations_hud.html:
- Add instructions panel with Cancel button
- Add points status display
- Separate name input controls (shown after points placed)

formations.js:
- Add showDrawingInstructions() with type-specific text
- Add updatePointsStatus() for live counter
- Add showNameInput() after drawing complete
- Add hideAllDrawingUI() for cleanup

formation_overlay.js:
- Add onPointsChangedCallback for UI updates
- Store _pointsNeeded per drawing session
- Stop accepting clicks after enough points
- Change cursor back to default when complete

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 20:00:47 -03:00
rob fceb040ef0 Fix formations stability and socket reliability issues
formation_overlay.js:
- Fix Map mutation during iteration causing infinite loops
- Use array snapshots for _updateAllFormations and clearAllFormations
- Add guards to skip unnecessary RAF work when no formations exist
- Simplify range change detection with primitive comparison

Formations.py:
- Fix DataCache method names to match actual API
- Use insert_row_into_datacache, modify_datacache_item,
  remove_row_from_datacache

communication.js:
- Enable persistent reconnection (Infinity attempts)
- Add bounded queue (250 max) to prevent memory growth
- Coalesce candle_data messages (keep latest only)
- Prioritize control/CRUD messages in queue flush

formations.js:
- Add user feedback alert when deleting while offline

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 19:39:29 -03:00
rob 5717ac6a81 Implement chart formations MVP with Line and Channel support
Add formations feature for drawing trendlines and channels on charts:

Backend (Formations.py):
- CRUD operations with DataCache integration
- Database table with scope-based indexing
- Line value calculation with infinite extension
- Property value lookup for strategy integration

Frontend (formations.js, formation_overlay.js):
- Three-class pattern: UIManager, DataManager, Controller
- SVG overlay for drawing with RAF sync loop
- Click-to-draw interface with temp line preview
- Anchor dragging for line adjustment
- Coordinate conversion using v5 API

UI Integration:
- HUD panel with Line/Channel buttons
- Formation cards with hover details
- Drawing controls (name input, save/cancel)

Socket handlers for real-time sync:
- request_formations, new_formation
- edit_formation, delete_formation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 16:22:08 -03:00
rob ba7b6e79ff Address second round of Codex review feedback on formations plan
Fixes:
1. Class signature consistency - reference __init__ from 1.1, don't redefine
2. Dynamic chart ID - use this.data.chart1_id instead of hardcoded 'chart1'
3. Comms wiring - use this.comms.on() pattern like signals.js
4. RAF loop guards - add pause conditions, destroy() teardown hook
5. Drag null guards - validate chartCoords before mutating formation data
6. Test organization - split MVP tests from Phase C target tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 16:03:33 -03:00
rob a46d2006f3 Update formations plan for Lightweight Charts v5 compatibility
Addresses Codex review feedback:
- Use series-based coordinate conversion (series.priceToCoordinate)
- Add infinite line extension math (_getInfiniteLineEndpoints)
- Fix message pattern: use sendToApp() not emit('message')
- Remove wall clock fallback in get_current_candle_time()
- Clarify architecture: socket handlers in BrighterTrades, not app.py
- Clarify UX: shape appears at center, drag to adjust (no click-to-place)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 15:57:38 -03:00
rob 5c37fb00b6 Upgrade Lightweight Charts from v2.1.0 to v5.1.0
- Update series creation to v5 API (addSeries with type parameter)
- Migrate watermarks to createTextWatermark plugin
- Migrate markers to createSeriesMarkers primitive
- Replace getBarSpacing() with timeScale().options().barSpacing
- Update crosshair handler: seriesPrices → seriesData
- Move scaleMargins from series options to price scale
- Fix chart sync infinite loop with time-based debounce (50ms)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 15:47:18 -03:00
rob 6a7a1c2b45 Fix indicator update callbacks and card display after refactor
- Bind indicators.update callback to correct this context in general.js
- Update updateDisplay to fall back to card elements when old chart elements don't exist
- Add _formatDisplayValue helper for consistent value formatting
- Remove verbose debug console.log statements
- Add FORMATIONS_PLAN.md documenting SVG overlay approach for chart formations

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 15:01:55 -03:00
rob c8c51841cf Redesign indicator and backtest displays with icon cards
- Replace indicator table with icon card grid display
- Add edit indicator popup dialog for modifying indicator settings
- Display indicator type, value, and emoji symbols on cards
- Add visibility toggle and delete buttons on hover
- Redesign backtest items with CSS-based icons and status indicators
- Unify dialog styling across external indicator, signal type, and public strategies
- Make edit indicator dialog draggable
- Fix button text: "+ Add Public" -> "Add Public"
- Remove dark theme from signal_type_popup for consistency

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-09 23:22:15 -03:00
rob c9cc0487fe Align signals and strategies buttons with grid layout
- Use 2-column grid for signal buttons (matches indicators layout)
- Use 2-column grid for strategy buttons (matches indicators layout)
- Remove background color from External Source button

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-09 21:41:39 -03:00
rob 51601b7f93 Document ExternalSources and ExternalIndicators modules
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-09 21:35:33 -03:00
rob b4bf6c5c8e Add external data sources and indicators for backtesting
External Sources:
- New ExternalSources.py module for real-time API data fetching
- Configurable refresh intervals, JSONPath extraction, and auth headers
- External sources can be used in signals for live strategy conditions

External Indicators:
- New ExternalIndicators.py module for historical API data
- Supports backtesting by fetching and caching historical data points
- Timestamps are matched to candle times during backtest execution
- Fear & Greed index and similar APIs now work in backtests

Signal System Enhancements:
- Added signal Blockly blocks for use in strategy builder
- New signal_blocks.js for dynamic signal block generation
- Signals category added to strategy toolbox
- PythonGenerator updated to handle signal_* block types
- process_signal() method added to StrategyInstance classes

notify_user Block Enhancement:
- Now supports dynamic values (variables, indicators, expressions)
- Can display indicator values and calculations in notifications

Bug Fixes:
- Fixed Socket.IO parse error causing strategies not to load on refresh
- Enhanced sanitize_for_json to handle pandas, datetime, bytes types
- Fixed external indicator name matching (underscore/space conversion)
- Fixed PRAGMA query missing fetch_all=True in ExternalIndicators

Dependencies:
- Added jsonpath-ng for JSONPath extraction from API responses

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-09 21:25:36 -03:00
rob a4422dc556 Unify dialog styling with consistent gradient headers
- Add .dialog-header CSS class with blue gradient (#3E3AF2 → #6366f1)
- Add .dialog-close-btn for consistent close buttons in headers
- Update all 12 dialog templates to use unified header styling
- Add close buttons to all dialogs (positioned in header)
- Fix trade_details and login popups (add proper header structure)
- Update public strategy browser modal to match dialog style
- Update indicator popup styling to match app theme
- Add null checks to initializeResizablePopup for robustness
- Update CLAUDE.md with wallet module documentation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-09 14:57:14 -03:00
rob 7e77f55837 Add Bitcoin wallet system with configurable strategy fees
Features:
- Two-address custody model (Fee Address + Sweep Address)
- Credits ledger for instant, reliable strategy fee transactions
- Configurable fee percentage (1-100% of exchange commission)
- Background jobs for auto-sweep, deposit detection, withdrawal processing
- Account settings dialog accessible via username click
- $50 balance cap with auto-sweep to user's sweep address

Security improvements:
- Atomic withdrawal reservation prevents partial state
- Fee accumulation cleanup on strategy startup failure
- Deposit monitoring includes disabled wallets for recovery
- Null tx hash checks prevent silent failures
- Key export disabled by default

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-09 12:45:25 -03:00
rob cd9a69f1d4 Add exchange validation, fix indicators bug, and improve balance display
- Add exchange_validation.py module for validating exchange requirements
  before running strategies (backtest, paper, live modes)
- Fix AttributeError in Signals.py: 'Indicators' object has no attribute
  'indicators' - created IndicatorWrapper class for proper data access
- Fix testnet balance issue: explicitly pass testnet=False to all
  connect_exchange calls to prevent pickle corruption from old testnet
  Exchange objects
- Add balance exchange selector: display one exchange at a time with
  dropdown to switch between connected exchanges (defaults to chart view)
- Add unique tbl_key generation for exchange_data to prevent duplicate
  entries (format: user:exchange)
- Fix DataCache balance serialization for list types
- Update frontend error handling for exchange validation errors

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 19:41:41 -03:00
rob ee16023b6b Add public strategy subscription system
Implement a subscription system for public strategies:

Security & Auth:
- Bind WebSocket user identity at connect time (prevents spoofing)
- Add server-side ownership verification for all destructive operations
- Invalidate socket connections on logout
- Add XSS protection with HTML/JS escaping in frontend

Database:
- Add strategy_subscriptions table with proper indexes
- Fix get_all_rows_from_datacache to fall back to DB when cache empty

Backend:
- Add subscribe/unsubscribe endpoints with authorization checks
- Add get_user_strategies (owned + subscribed) and get_public_strategies_catalog
- Propagate indicator_owner_id through strategy instances for subscribed strategies
- Redact strategy internals (code, workspace) for non-owners

Frontend:
- Add "Add Public" button to browse and subscribe to public strategies
- Show subscribed strategies with creator badge and unsubscribe button
- Prevent editing of subscribed strategies (show info modal instead)
- Add public strategy browser modal

Tests:
- Update authorization tests for new subscription-required model

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 16:59:52 -03:00
rob 11c1310f49 Register all 61 candlestick patterns in frontend indicatorMap
The new patterns were only added to the backend (indicators.py) but
not registered in the frontend (indicators.js), causing "Unknown
indicator type" errors when trying to display them on charts.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 13:33:43 -03:00
rob 1e203a1159 Add SVG diagrams for all 61 candlestick patterns
Each pattern now has a visual representation showing the characteristic
candlestick formations. SVGs include:
- Single candle patterns (Doji, Hammer, Marubozu, etc.)
- Two candle patterns (Engulfing, Harami, Kicking, etc.)
- Three+ candle patterns (Morning/Evening Star, Three Soldiers, etc.)

Visual elements use green (#00C853) for bullish candles, red (#FF5252)
for bearish candles, and gray (#888) for neutral/indecision candles.
Dashed lines indicate key price levels where relevant.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 12:15:53 -03:00
rob e98861a7fb Add all 61 TA-Lib candlestick pattern indicators
Extends the initial 15 patterns to include all 61 TA-Lib candlestick
pattern functions. Adds pattern descriptions and categories for the
indicator dropdown UI.

New patterns include: Abandoned Baby, Advance Block, Belt Hold,
Breakaway, Closing Marubozu, Concealing Baby Swallow, Counterattack,
Dragonfly Doji, Gravestone Doji, Hanging Man, Hikkake, Homing Pigeon,
Identical Three Crows, In Neck, Kicking, Ladder Bottom, Long Legged
Doji, Long Line, Mat Hold, Matching Low, Rise/Fall Three Methods,
Rickshaw Man, Separating Lines, Short Line, Stalled Pattern, Stick
Sandwich, Takuri, Tasuki Gap, Thrusting, Tristar, Two Crows, Unique
Three River, Upside/Downside Gap Three Methods, and more.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 12:10:14 -03:00
rob 8f362a1379 Add categorized indicator dropdown with expandable Patterns submenu
- Group candlestick patterns under expandable "Patterns" category
- Regular indicators shown directly in dropdown
- Patterns submenu expands on hover with all CDL_* indicators
- Fixed submenu positioning outside scrollable container
- Tooltip shows pattern description and SVG when hovering items

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 11:55:43 -03:00
rob 4ab7a5023d Add candlestick pattern indicators with TA-Lib integration
- Add 15 candlestick pattern indicators (Doji, Hammer, Engulfing, etc.)
- Create dedicated Patterns chart pane for pattern visualization
- Add hover tooltips with descriptions and SVG diagrams in indicator selector
- Fix color picker compatibility by using hex format instead of RGBA
- Fix chart positioning and timestamp synchronization for pattern chart

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 05:45:51 -03:00
rob 7b796c51c8 Fix indicator toggle functionality and add loading feedback
- Fix JSON parsing bug: JavaScript FormData sends indicator list as
  JSON string inside a list, now properly parsed on backend
- Move form event listener setup to _setupIndicatorForm() called from
  addToCharts() so it's always attached regardless of indicator data
- Add immediate visual feedback: popup hides instantly on submit and
  button shows "Updating..." text

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 03:33:45 -03:00
rob 24fcb56c38 Improve backtest time handling and indicator warmup
- Add UTC time formatter to charts for consistent time display
- Show both local and UTC times in backtest trades table
- Add indicator warmup period calculation to fetch extra candles
- Append 'Z' suffix to trade/alert timestamps to indicate UTC
- Add get_available_balance to strategy execution context

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-08 00:38:22 -04:00
rob 307f251576 Add backtest debugging features: timestamps, chart markers, and alerts
- Add Open/Close Time columns to Trades Executed table
- Display trade markers (buy/sell arrows) automatically on chart
- Clear markers when closing results dialog or running new test
- Collect notify_user alerts with timestamps during backtesting
- Display Strategy Alerts section in backtest results
- Fix timestamp conversion: use unit='s' for EDM timestamps (not 'ms')
- Fix trade datetime extraction using data feed's datetime method

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-06 23:34:55 -04:00
rob 3e6463e4b3 Add Bollinger %B indicator and fix credential handling
- Add BollingerPercentB indicator class in backend (BOL%B)
  - Calculates %B = (Close - Lower) / (Upper - Lower)
  - Shows where price is relative to Bollinger Bands
  - Values: 1.0 = upper band, 0.5 = middle, 0.0 = lower
- Add BollingerPercentB JavaScript class for frontend display
  - Creates its own chart (chart4) like RSI/MACD
  - Includes chart binding for synchronized scrolling
- Add chart4 HTML container and data plumbing
- Fix credential update when reconnecting with changed API keys
- Add TESTNET_MODE config setting for live trading mode control

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-06 06:31:31 -04:00
rob 21d449d877 Fix KuCoin balance fetching and add balance refresh functionality
- Add KuCoin-specific balance fetching with type='trade' parameter
- Reinitialize ccxt client in refresh_balances() to fix pickle corruption
- Force reconnection when exchange is restored from database cache
- Add balance refresh button and socket handler in frontend
- Fix template null check for balances display
- Clean up DataCache and candles imports

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-06 06:10:31 -04:00
rob 91f51cd71f Fix set_flag blocks being dropped after trade_action
Bug: trade_order_generators.js set skipAdditionalParsing=true which
caused json_base_generator.js to return early before processing <next>
sibling blocks. This meant set_flag and other blocks connected after
trade_action were silently dropped from the JSON.

Fix: Removed the unnecessary skipAdditionalParsing flag. The code path
it was meant to protect was already in an else branch, so the flag
served no purpose and broke <next> block handling.

Also includes minor fixes to Strategies.js and backtesting.js from
earlier session work.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-06 02:28:50 -04:00
rob 3976fc8366 Fix backtest data source and chart view detection bugs
- Fix market/symbol key mismatch in PythonGenerator.py (6 locations)
  causing backtest to use wrong trading pair (BTC/USD vs BTC/USDT)
- Fix backtesting.py to always use default_source for backtest data
- Fix exchange/exchange_name key mismatch in app.py and BrighterTrades.py
  causing strategy dialog to show wrong current chart exchange
- Add favicon links to standalone HTML templates
- Add AI strategy dialog template
- Update tests and documentation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-06 00:05:31 -04:00
rob 0e481e653e Integrate EDM for candle data and improve UI dialogs
EDM Integration:
- Add edm_client module for REST/WebSocket communication with exchange-data-manager
- Refactor candles.py to fetch data exclusively from EDM
- Add EDM session management for authenticated exchange access
- Update backtesting to use EDM for historical data
- Pass edm_client through to all strategy instances
- Add /api/edm-config, /api/chart-view, /health/edm endpoints
- Remove legacy candle-fetching code from DataCache_v3 and Exchange

UI Improvements:
- Make all popup dialogs draggable and resizable (exchange config, indicator, signal, trade)
- Add default trading source (exchange, symbol, timeframe) to strategy dialog
- Convert symbol fields to dropdowns with smart prioritization (popular pairs first)
- Fix signal dialog property dropdown initialization

Bug Fixes:
- Fix PythonGenerator regex to handle symbols starting with numbers (e.g., 0G/USDT)
- Fix EDM client date format (use Unix timestamps instead of ISO strings)
- Fix strategy default_source database migration

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-04 11:42:06 -04:00
rob 33de4affb4 Fix auth UX, exchange config, and trade validation
Auth/Login improvements:
- Add dedicated login page (login_page.html)
- Redirect to /app after successful login/signup instead of welcome
- Add flash message categories to prevent cross-page message pollution
- Filter flash messages by category in templates

Exchange configuration fixes:
- Fix JS discarding API keys for public exchanges (now optional, not cleared)
- Add explicit key/secret validation in backend before saving
- Properly update configured_exchanges when keys provided

Trade dialog enhancements:
- Add exchange selector populated from connected exchanges
- Add testnet checkbox with exchange support detection
- Add production trading confirmation dialog
- Show TESTNET/LIVE badges on trade cards
- Validate exchange is configured before creating live trades

Bug fixes:
- Add try/except around get_user_data in is_logged_in()
- Fix DataFrame index access for user data retrieval

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-02 20:59:07 -04:00
rob 45395c86c5 Implement fee fetching from exchange via ccxt
Exchange.py:
- Add get_trading_fees(symbol) - fetches fees from market data
  - Tries user-specific fees for authenticated users (fetch_trading_fees)
  - Falls back to market data (public)
  - Returns maker/taker rates with source indicator
- Add get_margin_info(symbol) - returns margin trading availability

ExchangeInterface.py:
- Add get_trading_fees() - routes to appropriate exchange
- Add get_margin_info() - routes to appropriate exchange
- Both methods handle user/exchange lookup with fallback to defaults

trade.py:
- Update new_trade() to fetch actual fees from exchange
- Uses taker fee for market orders, maker fee for limit orders
- Falls back to exchange_fees defaults if fetch fails

Fees now come from actual exchange data (0.1% for Binance spot)
instead of hardcoded defaults.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-02 16:30:19 -04:00
rob b9730b3a1d Upgrade ccxt from 4.4.8 to 4.5.40
Key improvements:
- 80-190% performance gains in utility functions
- New margin/futures WebSocket support for Binance
- Better liquidation and position tracking
- coincurve library for faster ECDSA signing
- New exchange support (Aster)

All Exchange methods verified working with new version.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-02 16:19:08 -04:00
rob f603728080 Fix paper trade P/L updates and fee calculations
- Fix default fee from 0.1 (10%) to 0.001 (0.1%) in Trade class
- Add real-time trade updates via WebSocket trade_update events
- Fetch current market price for market orders instead of using cached price
- Add trade persistence to database with proper schema
- Add execution loop to update trades even without active strategies
- Add frontend handler for trade_update events in communication.js
- Add handleTradeUpdate method in trade.js for live P/L updates
- Add debug file logging for trade update debugging
- Update statistics dashboard and trading HUD templates

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-02 16:05:53 -04:00
rob 78dfd71303 Add statistics dashboard and strategy UI enhancements
Statistics HUD:
- Full statistics dashboard with running strategies list
- Strategy performance metrics (P&L, balance, trades, win rate)
- Mini equity curve chart placeholder
- Real-time stats updates via SocketIO

Strategy Improvements:
- Orphaned strategy cleanup function
- User existence validation
- Enhanced strategy card display
- Improved new strategy popup

UI/UX Enhancements:
- Updated control panel layout
- Indicator blocks improvements
- Additional CSS styling
- Welcome template
- Communication.js strategy event handling

Backend:
- Strategy instance tracking updates
- Paper strategy instance improvements
- User session handling updates
- App.py route additions

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-02 04:39:41 -04:00
rob c895a3615d Refactor Signals system and implement Alerts
Signals System Refactor:
- Migrated from config.yml to DataCache/SQLite storage
- Added user ownership (creator field) and public/private visibility
- Implemented full CRUD operations with tbl_key identifiers
- Auto-creates signals table in database on init
- Card-based UI with 100x100 icons and hover detail panels
- State visualization: green pulsing border (TRUE), red (FALSE)
- Edit support via popup form with Create/Edit buttons

Alerts System Implementation:
- Connected frontend Alerts.js to SocketIO communications
- Alerts triggered by signal state changes (s_updates)
- Support for strategy events, trades, errors, notifications
- Color-coded alerts with icons and timestamps
- Clear button and max 50 alerts limit (ephemeral, not persisted)

Backend changes:
- BrighterTrades.py: Updated signal handlers with user_id
- Added received_edit_signal() and permission checks
- Signal filtering by user ownership

Frontend changes:
- signals.js: Three-manager pattern (SigUIManager, SigDataManager, Signals)
- Alerts.js: Event handlers for updates and strategy_events
- general.js: Initialize signals and alerts with comms

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-02 04:31:39 -04:00
rob dd8467468c Fix Binance API error -2015 when fetching positions on spot exchanges
- Check if exchange supports fetchPositions before calling it
- Handle PermissionDenied (error -2015) gracefully with debug log instead of error
- Handle NotSupported exception for exchanges that don't support positions
- Use ccxt standard field names (contracts, entryPrice) with fallbacks
- Skip positions with zero size
- Add documentation noting this is a futures/derivatives feature

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-01 18:03:55 -04:00
rob 3644c219d4 Fix int64 JSON serialization error in SocketIO responses
- Add utils.py with sanitize_for_json() function to convert numpy types
  (int64, float64, bool_, ndarray) to native Python types
- Handle inf/nan values by converting to None
- Update backtesting.py to use shared sanitize_for_json utility
- Update app.py strategy_events emit to sanitize data before sending
- Remove empty root BrighterTrading.db and add *.db to gitignore

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-01 17:58:24 -04:00
rob 4e14463465 Add user feedback for exchange configuration
- Show loading state while connecting to exchange
- Display success/error messages in the form
- Prevent double-submission with disabled button
- Add 30-second timeout with error message
- Register handler for Exchange_connection_result message
- Reload page after successful connection (with delay to show message)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-01 17:34:27 -04:00
rob da9d1fc5ee Fix test_BrighterTrades.py socketio mock issue
- Added proper mocking for all BrighterTrades dependencies (DataCache,
  ExchangeInterface, Users, Strategies, Candles, Indicators, Trades)
- Pass mock socketio instance to BrighterTrades constructor
- Fixed patch paths to use correct module path (src.BrighterTrades.*)
- All 5 tests now pass

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-01 17:31:20 -04:00
rob 639043b261 Live trading infrastructure functional on testnet
Key fixes:
- ExchangeInterface: Remove stale entries before creating new exchange connections
- LiveBroker: Optimize get_total_equity() to price only top 10 assets (was hanging on 462 testnet assets)
- LiveBroker: Add fiat currency skip list to avoid failed price lookups
- PythonGenerator: Fix market symbol regex to handle 2-5 char symbols (BTC/USDT)

New features:
- LiveStrategyInstance: Full live trading strategy execution
- Circuit breaker and position limits for live trading safety
- Restart-safe order reconciliation via client order IDs

Verified working:
- Live strategy starts on Binance testnet
- Orders placed and filled successfully
- Execution loop runs with active strategies

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-01 15:23:34 -04:00
rob 6821f821e1 Implement execution loop, paper persistence, and CI pipeline
Strategy Execution Loop:
- Add tick() method to StrategyInstance for price-driven execution
- Add update() method to Strategies for iterating active instances
- Enable execution loop in received_cdata() to process candle updates
- Add PaperStrategyInstance.tick() with broker price updates

Paper Trading Persistence:
- Add Position.to_dict()/from_dict() for serialization
- Add PaperBroker state persistence (save_state/load_state)
- Add _ensure_persistence_cache() with DB schema migration
- Auto-load/save broker state in PaperStrategyInstance

Runtime Fixes (from Codex review):
- Fix get_user_info() signature mismatch in start_strategy
- Fix live/paper mode handling for stop operations
- Normalize fill event payload (filled_qty/filled_price keys)
- Remove double broker update path (delegate to tick)
- Prevent runtime events from polluting strategy list in UI

CI Pipeline:
- Add GitHub Actions workflow (.github/workflows/test.yml)
- Python 3.12 with TA-Lib dependency
- Syntax checks and 5 critical test suites

Tests: 85 passed
- test_strategy_execution.py (16 tests)
- test_execution_loop.py (17 tests)
- test_paper_persistence.py (19 tests)
- test_backtest_determinism.py (13 tests)
- test_brokers.py (18 tests)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 19:48:50 -04:00
rob b555f6e004 Fix high-severity issues identified by Codex
1. Paper equity calculation (High):
   - get_balance() now correctly includes position value (size * current_price)
   - Previously only added unrealized_pnl, missing position principal

2. Buy affordability checks (High):
   - place_order() now uses execution price including slippage for market orders
   - Uses limit price for limit orders instead of current price
   - Prevents orders that would drive cash negative

3. ExchangeInterface price stubs (High):
   - get_trade_executed_price() now handles order_price=0.0 (market orders)
   - Falls back to current price or entry price when order_price is zero

4. Determinism hash strengthening (Medium):
   - Hash now includes full trade sequence with individual trade details
   - Added test for different trade sequences with same totals

5. Live mode consistency (Medium):
   - Factory now falls back to paper trading with warning instead of raising
   - Consistent behavior between factory.py and Strategies.py

Added test: test_paper_broker_equity_calculation to verify fix #1

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 17:23:58 -04:00
rob 5dbda13924 Phase 5-6: Live trading stubs and observability
Phase 5 - Live Trading (stub implementation):
- Create LiveBroker stub with NotImplementedError for all methods
- Document required exchange API integration points
- Add testnet flag for safety

Phase 6 - Observability:
- Add structured logging with StructuredFormatter and ColoredFormatter
- Create TradingLogger for trading-specific log entries
- Implement health check system with HealthCheck class
- Add default health checks for database, exchange, memory
- Create health_endpoint() for monitoring integration

The LiveBroker is a stub that needs exchange API integration
for production use. All other trading modes (backtest, paper)
are fully functional.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 17:09:54 -04:00
rob 51ec74175d Phase 4: Paper trading implementation
- Create PaperStrategyInstance extending StrategyInstance
- Integrate PaperBroker for simulated order execution
- Add trade_order() method translating Blockly calls to broker
- Add mode selection in Strategies.create_strategy_instance()
- Update execute_strategy() to support paper/backtest/live modes
- Include comprehensive tests for paper trading functionality

Paper trading now works with:
- Market and limit orders
- Position tracking with P&L
- Balance management
- Trade history
- Reset functionality

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 17:07:34 -04:00
rob 1bb224b15d Phase 3: Backtest determinism
- Fix deprecated datetime.utcfromtimestamp() in shared_utilities.py
- Create BacktestResult schema with locked structure
- Add TradeResult and BacktestMetrics dataclasses
- Implement determinism hash for verifying reproducible results
- Add comprehensive tests for result schema and determinism

The BacktestResult schema ensures consistent output format and
provides methods to verify that same inputs produce same results.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 17:02:54 -04:00
rob f1182d4e0c Phase 2: Broker abstraction foundation
- Create brokers package with unified trading interface
- BaseBroker: Abstract base class defining broker contract
- BacktestBroker: Wraps Backtrader for backtesting mode
- PaperBroker: Simulated order execution for paper trading
- Factory function to create broker based on trading mode
- Comprehensive test suite for broker functionality

The broker abstraction enables strategies to work identically
across backtest, paper, and live trading modes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 17:00:43 -04:00
rob e245f80e18 Phase 1: Repair runtime breakpoints
- Fix set_new_candle() to update cached candle instead of raising
- Fix trades.update() type mismatch: pass {symbol: price} dict
- Fix StrategyInstance.trades collision: rename list to trade_history
- Add missing ExchangeInterface methods for trade status/qty/price
- Add get_user_balance stub for paper trading

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 16:47:13 -04:00
rob 34637df478 Phase 0: Stabilize baseline
- Security: Move API keys to environment variables in config.py
- Portability: Use cross-platform path resolution for DB_FILE
- Add config.example.py template for developers
- Fix Windows path in ExchangeInterface.get_public_exchanges()
- Add cached_last_candle attribute to Candles class
- Add pytest configuration (pytest.ini, conftest.py)
- Fix test imports for DataCache_v3
- Include identity compatibility layer (user_name/user_id resolution)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-28 16:44:11 -04:00
rob f1a184b131 Fix chart loading and stabilize local runtime 2026-02-28 15:15:56 -04:00
Rob ee51ab1d8c I am just about to move most of the strategy statistics tracking to the StrategyInstance 2024-11-22 10:28:39 -04:00
Rob dab122d15f A lot of stuff is fixed. The strategies need to reflect the stats in the ui. 2024-11-20 01:38:46 -04:00