Fix UnboundLocalError in _calculate_price_spreads (indentation bug, lines 760-775)#23
Open
OfficialGIGA wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a Python
UnboundLocalErrorthat fires continuously when running the arbitrage monitor with two or more exchange adapters.Root cause
In
core/services/arbitrage_monitor/implementations/arbitrage_monitor_impl.pyaround line 762, thespreads.append(...)call for the reverse-direction opportunity (buyexchange2/ sellexchange1) is indented outside itsif book1["bid"] > book2["ask"]:guard.When the condition is
False,spread_absandspread_pctare never assigned in that iteration, but thespreads.append(PriceSpread(... spread_abs=spread_abs ...))call still executes and references them — raisingUnboundLocalError.This mirrors the structurally-correct block at lines 740-756 — the bug appears to be an accidental indentation slip.
Reproduction
config/arbitrage/monitor.yamlwith two exchanges (e.g.edgex,backpack)monitor_only: trueinconfig/arbitrage/arbitrage_segmented.yamlpython3 run_arbitrage_monitor.pyin public-data modelogs/arbitrage_monitor.logfills 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 itstimestamp=datetime.now()line) inside theifstatement to match the mirror block at lines 740-756. Whitespace-only change — no logic modified.Verification
After applying the patch:
spread_abserrors over 5+ minutes of live monitoringedgex+backpack, ~20 opportunities/second detected cleanlyNotes