# logwatch `logwatch.py` is a standalone, read-only Python log tailer for NT8 SDK session logs. It has no NinjaTrader assembly references, no DLL involvement, and does not write to any NinjaTrader directory. It tails the newest `session_*.log` under the configured log directory and emits JSON payloads to a webhook. Use `--dry-run` to print payloads to stdout instead of posting. ## Safety - Reads log files using shared normal file reads only. - Never opens logs for writing. - Never writes to the NinjaTrader directory. - Webhook failures are written to stderr and do not crash or block the watcher. - Default `config.json` ships with an empty `webhook_url`. ## Launch Dry run against the default log directory: ```powershell python tools\logwatch\logwatch.py --dry-run ``` Process the current newest session from the beginning once: ```powershell python tools\logwatch\logwatch.py --dry-run --from-start --once ``` Override the log directory: ```powershell python tools\logwatch\logwatch.py --dry-run --log-dir "C:\Users\billy\Documents\NinjaTrader 8\log\nt8-sdk" ``` Post to a webhook: ```powershell python tools\logwatch\logwatch.py --webhook-url "http://127.0.0.1:5678/webhook/nt8-logwatch" ``` ## Config Edit `tools/logwatch/config.json`: ```json { "webhook_url": "", "log_dir": "%USERPROFILE%\\Documents\\NinjaTrader 8\\log\\nt8-sdk", "orb_window_start": "09:30", "orb_window_end": "10:15", "timezone": "America/New_York", "events": { "session_start": true, "signal": true, "submit": true, "reject": true, "reject_summary": true, "warning": true, "connection": true, "silent_window_alert": true } } ``` ## n8n Webhook Example 1. Create a new n8n workflow. 2. Add a `Webhook` trigger node. 3. Set method to `POST`. 4. Set path to `nt8-logwatch`. 5. Add a `Switch` node using `{{$json.event_type}}`. 6. Add branches for `SILENT WINDOW ALERT`, `REJECT`, `WARNING`, and `SIGNAL`. 7. Send high-priority notifications for `SILENT WINDOW ALERT` and `CONNECTION LOST`. 8. Copy the test or production webhook URL into `config.json` or pass it with `--webhook-url`. ## Event Payloads ### SESSION START ```json { "tool": "logwatch", "event_type": "SESSION START", "session_file": "C:\\Users\\billy\\Documents\\NinjaTrader 8\\log\\nt8-sdk\\session_20260813_070512.log", "session_date": "2026-08-13", "started_at": "2026-08-13 07:05:12", "account": "SimSimple ORB", "instrument": "NQ SEP26", "bar_type": "HFTAlgoBarsType", "days_to_load": 3, "historical_bars_loaded": 750199, "warmup_status": "NarrowRangeFactorCalculator=SATISFIED (87/7 daily bars) OrbRangeVsAtrFactorCalculator=SATISFIED (87/5 daily bars)", "emitted_at_utc": "2026-08-16T04:00:00Z" } ``` ### SIGNAL ```json { "tool": "logwatch", "event_type": "SIGNAL", "side": "Buy", "grade": "A", "score": 0.83, "raw": "Buy NQ SEP26 | grade=A score=0.83", "session_date": "2026-08-10" } ``` ### SUBMIT ```json { "tool": "logwatch", "event_type": "SUBMIT", "mode": "Mode1DualLeg", "quantity": 2, "raw": "mode=Mode1DualLeg quantity=2" } ``` ### REJECT ```json { "tool": "logwatch", "event_type": "REJECT", "reason": "GradeFilter", "detail": "Score=4.300 Grade=C MinTradeGrade=B Factors=...", "bar": "2026-08-13 09:45:00" } ``` Only the first `REJECT` per reason is emitted each session. ### REJECT SUMMARY ```json { "tool": "logwatch", "event_type": "REJECT SUMMARY", "reason": "GradeFilter", "count": 311 } ``` ### WARNING ```json { "tool": "logwatch", "event_type": "WARNING", "message": "DataSeries[0] DaysToLoad below 30; DaysToLoad=3. Advisory only; trading is not blocked." } ``` ### CONNECTION LOST / RESTORED ```json { "tool": "logwatch", "event_type": "CONNECTION LOST", "status": "LOST" } ``` ### SILENT WINDOW ALERT ```json { "tool": "logwatch", "event_type": "SILENT WINDOW ALERT", "session_date": "2026-08-13", "orb_window_start": "09:30", "orb_window_end": "10:15", "timezone": "America/New_York", "message": "ORB window elapsed with no SIGNAL line in the active session log." } ``` This is the early-warning event for GAP-034: the ORB window has elapsed and the active session log has no `SIGNAL:` line. ## Robustness Notes - Newer `session_*.log` files are detected and tailed automatically. - Partial lines are buffered until complete. - If NT8 rotates/truncates a file, the read offset resets safely. - File-lock/read errors are reported to stderr and retried on the next poll.