Skip to content

Add connection monitor to automatically release held trades on reconnect#786

Merged
dantelrharrell-debug merged 2 commits intomainfrom
copilot/fix-held-trades-execution
Feb 21, 2026
Merged

Add connection monitor to automatically release held trades on reconnect#786
dantelrharrell-debug merged 2 commits intomainfrom
copilot/fix-held-trades-execution

Conversation

Copy link
Contributor

Copilot AI commented Feb 21, 2026

Trading threads were only started once at boot. If the platform account wasn't connected, user API keys were invalid, or accounts were unfunded at startup, those accounts never traded — with no retry path.

Changes

bot/independent_broker_trader.py

  • start_connection_monitor() / stop_connection_monitor() — starts/stops a daemon thread (NijaConnectionMonitor) automatically called at the end of start_independent_trading() and the start of stop_all_trading()
  • _connection_monitor_loop() — runs every 5 min (configurable via NIJA_CONNECTION_RETRY_INTERVAL); invokes both retry helpers
  • _retry_platform_connections() — for each platform broker without a running thread: calls broker.connect(), checks funding, then starts the trading thread
  • _retry_user_connections() — calls connect_users_from_config() (clears failure cache for fixed credentials), then starts threads for newly-connected + funded user accounts
  • _start_platform_thread() / _start_user_thread() — extracted thread-creation logic shared by initial startup and the monitor; guarded by the existing active_trading_threads singleton set to prevent duplicates
  • _get_broker_balance() — deduplicates the Coinbase-specific zero-balance retry logic previously scattered across two methods

Once a trading thread starts, the existing adopt_existing_positions() call in run_user_broker_trading_loop() (runs every 2.5-min cycle) picks up any open positions and immediately attaches stop-loss, profit targets, and trailing stops — releasing held trades without manual intervention.

# Environment variable to tune retry cadence (default: 300 s)
NIJA_CONNECTION_RETRY_INTERVAL=120  # check every 2 minutes instead

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@railway-app railway-app bot temporarily deployed to patient-compassion / Nija-pr-786 February 21, 2026 13:03 Destroyed
@railway-app
Copy link

railway-app bot commented Feb 21, 2026

🚅 Deployed to the Nija-pr-786 environment in patient-compassion

Service Status Web Updated (UTC)
Nija ✅ Success (View Logs) Feb 21, 2026 at 1:13 pm

Co-authored-by: dantelrharrell-debug <232652186+dantelrharrell-debug@users.noreply.github.com>
@railway-app railway-app bot temporarily deployed to patient-compassion / Nija-pr-786 February 21, 2026 13:12 Destroyed
Copilot AI changed the title [WIP] Fix execution issues for held trades Add connection monitor to automatically release held trades on reconnect Feb 21, 2026
@dantelrharrell-debug dantelrharrell-debug marked this pull request as ready for review February 21, 2026 13:17
@dantelrharrell-debug dantelrharrell-debug merged commit a5f2365 into main Feb 21, 2026
28 of 29 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.

2 participants