Skip to content

Add startup summary snapshot and per-poll console ticker logging - #12

Merged
angeloreale merged 6 commits into
mainfrom
copilot/ensure-npm-run-bot-works
Aug 4, 2026
Merged

Add startup summary snapshot and per-poll console ticker logging#12
angeloreale merged 6 commits into
mainfrom
copilot/ensure-npm-run-bot-works

Conversation

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

npm run bot was missing two behaviors: no summary snapshot on startup, and no per-position PNL output in node logs during polling. The polling timer, local log persistence, SL/TP alerts, and min/max tracking already existed.

Changes

  • Startup summaryemitSummary() called immediately after monitor starts, so operators see current account state without waiting for the first scheduled interval
  • Console summary output — New logSummaryToConsole() renders the full summary (positions, pending orders, balance, equity) to stdout via the Logger, mirroring what Telegram receives
  • Per-poll ticker logging — Each sample() cycle now logs every open position with PNL delta since last poll, current PNL, and rolling max/min:
    📈 BTC_USDT LONG ▲ +12.34 · PNL +156.78 · max +200.00 / min -50.00
    
    Tracks previous PNL per position via prevPnl map; first sample omits delta.
  • Cleanup — Removed unused pollLogged field

- Emit initial position summary at bot startup with full console output
- Log per-position PNL deltas (up/down, current, max/min) at every poll
- Add logSummaryToConsole() for human-readable summary in node logs
- Remove unused pollLogged field

Co-authored-by: angeloreale <12191809+angeloreale@users.noreply.github.com>
Comment thread src/bot/bot.ts Outdated

// Emit an initial summary snapshot to both console and Telegram.
try {
await this.summaryMonitor.emitSummary();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot there seems to be a major issue with the summaryMonitor class, as this is, nor any other polling activity are ever logged to SIGINT, nor persisted to the logs. can you please debug?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause was that summaryMonitor was only instantiated when SUMMARY_NOTIFICATION_CHANNEL (or PNL_NOTIFICATION_CHANNEL) was set — so without a Telegram channel configured, no polling, ticker logging, or summary snapshots would ever run.

Fixed in the latest commit: the summaryMonitor now always starts regardless of Telegram config, so all polling activity (per-position PNL ticker, summary snapshots, ticker log persistence) flows through node console logs and ./logs files unconditionally. Telegram delivery is skipped gracefully when no channel is set.

Also fixed: stop() is now async and flushes a final sample + summary before the process exits on SIGINT/SIGTERM, and ticker log files are included in the retention cleanup.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot could it also display a summary right on script startup? not after the first poll?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved emitSummary() to run before start(), so the summary snapshot displays immediately on script startup without waiting for the first poll/sample cycle.

Copilot AI and others added 2 commits August 4, 2026 22:53
- summaryMonitor.stop() now async: runs a final sample() + emitSummary()
  so polling data and the summary are persisted before process exits
- Shutdown handler awaits summaryMonitor.stop() and logs completion
- Logger cleanupOldFiles now includes ticker-*.log in retention cleanup

Co-authored-by: angeloreale <12191809+angeloreale@users.noreply.github.com>
The summaryMonitor (polling, ticker logging, per-position PNL, summary
snapshots) was only created when a Telegram notification channel was
configured. Now it always starts so polling activity appears in node
console logs and is persisted to ./logs regardless of Telegram config.
Telegram delivery is skipped gracefully when no channel is set.

Co-authored-by: angeloreale <12191809+angeloreale@users.noreply.github.com>
@angeloreale

Copy link
Copy Markdown
Collaborator

@copilot no polling activity or logging is happening at the default/set 30s interval. can you please review what triggers it, and make sure it is always triggered during node app startup?

Co-authored-by: angeloreale <12191809+angeloreale@users.noreply.github.com>
Co-authored-by: angeloreale <12191809+angeloreale@users.noreply.github.com>

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot no polling activity or logging is happening at the default/set 30s interval. can you please review what triggers it, and make sure it is always triggered during node app startup?

Two issues found and fixed:

  1. Timer .unref() calls — Both sampleTimer and summaryTimer were .unref()'d, which can cause them to not fire if Node's event loop has no other ref'd handles. Removed .unref() so the timers always keep the process alive.

  2. Startup orderingsummaryMonitor.start() and pnlMonitor.start() were called after await this.telegram.launch(). If Telegram was slow to connect or hung, the monitors would never start. Moved all monitor startup (initial summary + polling timers) to run before telegram.launch() so polling is guaranteed active during app startup.

@angeloreale

angeloreale commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

@copilot when the node app starts it stucks at MEXC connection OK, and only when I press Ctrl C that SIGINT is triggered.

Could there be a misunderstanding on what SIGINT should do in this system?

These actions should always be interpreted as the start of the node service and the invoked script (dist/index.js for bot:start or src/index.js for npm run bot).

Please refer to the attached image
image

@angeloreale

Copy link
Copy Markdown
Collaborator

@copilot when the node app starts it stucks at MEXC connection OK, and only when I press Ctrl C that SIGINT is triggered.

Could there be a misunderstanding on what SIGINT should do in this system?

These actions should always be interpreted as the start of the node service and the invoked script (dist/index.js for bot:start or src/index.js for npm run bot).

Please refer to the attached image image

I likely was trying to say STDOUT and said SIGINT by mistake. Please fix @copilot

Co-authored-by: angeloreale <12191809+angeloreale@users.noreply.github.com>

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot when the node app starts it stucks at MEXC connection OK, and only when I press Ctrl C that SIGINT is triggered.

Could there be a misunderstanding on what SIGINT should do in this system? ...

telegram.launch() never resolves while long-polling is active, so awaiting it blocked the entire start() method — nothing after it (monitors, summary, polling) could run until SIGINT stopped Telegraf.

Fixed by making telegram.launch() fire-and-forget (void ... .then()). The startup flow now completes immediately: initial summary → start() polling timers → Telegram launches in the background. Also removed the stale "monitor not started" debug guard from emitSummary() since calling it before start() is intentional.

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot when the node app starts it stucks at MEXC connection OK, and only when I press Ctrl C that SIGINT is triggered.

Could there be a misunderstanding on what SIGINT should do in this sy...

Understood — this was already addressed in commit 054f3f0. The root cause was await this.telegram.launch() blocking indefinitely (Telegraf's long-polling promise never resolves), so nothing after it — summary, monitors, polling — could ever reach stdout. It's now fire-and-forget, and all monitor startup + initial summary run synchronously before Telegram launches. The screenshot shows the old build before this fix.

@angeloreale

Copy link
Copy Markdown
Collaborator

This worked. Thanks

@angeloreale
angeloreale merged commit befe9ea into main Aug 4, 2026
1 check 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