Skip to content

Fix UnboundLocalError in _calculate_price_spreads (indentation bug, lines 760-775)#23

Open
OfficialGIGA wants to merge 1 commit into
cryptocj520:mainfrom
OfficialGIGA:fix/spread-abs-unbound-local
Open

Fix UnboundLocalError in _calculate_price_spreads (indentation bug, lines 760-775)#23
OfficialGIGA wants to merge 1 commit into
cryptocj520:mainfrom
OfficialGIGA:fix/spread-abs-unbound-local

Conversation

@OfficialGIGA

Copy link
Copy Markdown

Summary

Fixes a Python UnboundLocalError that fires continuously when running the arbitrage monitor with two or more exchange adapters.

Root cause

In core/services/arbitrage_monitor/implementations/arbitrage_monitor_impl.py around line 762, the spreads.append(...) call for the reverse-direction opportunity (buy exchange2 / sell exchange1) is indented outside its if book1["bid"] > book2["ask"]: guard.

When the condition is False, spread_abs and spread_pct are never assigned in that iteration, but the spreads.append(PriceSpread(... spread_abs=spread_abs ...)) call still executes and references them — raising UnboundLocalError.

This mirrors the structurally-correct block at lines 740-756 — the bug appears to be an accidental indentation slip.

Reproduction

  1. Configure config/arbitrage/monitor.yaml with two exchanges (e.g. edgex, backpack)
  2. Set monitor_only: true in config/arbitrage/arbitrage_segmented.yaml
  3. Run python3 run_arbitrage_monitor.py in public-data mode
  4. Within ~10 seconds, logs/arbitrage_monitor.log fills with error spam: ❌ 差价分析错误: cannot access local variable 'spread_abs' where it is not associated with a value — firing ~50 times per second on any pair where the bid/ask cross briefly.

Fix

Indent the spreads.append(...) block (and its timestamp=datetime.now() line) inside the if statement to match the mirror block at lines 740-756. Whitespace-only change — no logic modified.

Verification

After applying the patch:

  • Zero spread_abs errors over 5+ minutes of live monitoring
  • 27 overlapping pairs on edgex + backpack, ~20 opportunities/second detected cleanly
  • Tested on Python 3.12.10, ccxt 4.5.58, websockets 15.0.1, macOS

Notes

  • Comments in the modified block remain in Chinese (consistent with the rest of the file)
  • No tests included as the project doesn't have a test harness for this module — happy to add one if there's a preferred pattern

The second arbitrage opportunity block (lines 760-775) had incorrect
indentation, placing the spreads.append() call OUTSIDE the
'if book1["bid"] > book2["ask"]:' condition. When the condition was
False, spread_abs was never assigned, causing UnboundLocalError when
the PriceSpread object was constructed.

Fix: indent the spreads.append() block to live inside the if statement,
matching the mirror block at lines 740-756.

Reproducible by running run_arbitrage_monitor.py with edgex+backpack
adapters in public-data mode; produces ~50 errors/second in
logs/arbitrage_monitor.log when one venue's bid temporarily exceeds
the other's ask on any monitored pair.
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.

1 participant