fix: honor PROXY_URL for live-trading REST calls - #229
Open
z20251130 wants to merge 1 commit into
Open
Conversation
env.example documents PROXY_URL as covering 'market data, exchange and broker API traffic'. Market data (CCXT) honors it, but the live-trading funnel BaseRestClient._request used plain requests without proxies, so on deployments that reach exchanges only through PROXY_URL, every private REST call (position snapshot, orders, account queries) failed with ConnectionResetError while signals kept working from cached market data. Add a cached _get_proxies() resolver next to _get_requests_verify(): PROXY_URL set -> explicit http/https proxies (socks5(h) works, PySocks is already a dependency); unset -> None, so standard HTTPS_PROXY / HTTP_PROXY / ALL_PROXY env vars and NO_PROXY bypasses keep applying via trust_env, unchanged from current behavior. Covered by unit tests for explicit/socks/unset/cached resolution.
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
env.exampledocumentsPROXY_URLas covering "market data, exchange and broker API traffic", and market data (CCXT) honors it. The live-trading funnelBaseRestClient._requestused plainrequestswithout proxies, so on deployments that reach exchanges only throughPROXY_URL(mainland CN / containerized setups), every private REST call failed — position snapshots, order placement, account queries — withConnectionResetError(104), while strategy signals kept working from cached market data. This made live auto-trading silently break in a way that looked like an exchange problem.This PR wires
PROXY_URLinto the funnel, following the existing_get_requests_verify()pattern in the same file (whose docstring already anticipated proxied calls).Root cause
BaseRestClient._request(the single HTTP entry point for all six exchange clients — binance spot/futures, okx, bitget, bybit, gate, htx) passed noproxiestorequests.request.requestsonly understands standardHTTPS_PROXY/HTTP_PROXY/ALL_PROXYenv vars, not the platform'sPROXY_URL, and live trading code never bridged the two.Observed in production logs (Binance demo, strategy live deployment):
Changes
app/services/live_trading/base.py: add cached_get_proxies()next to_get_requests_verify()and pass it torequests.requestin_request.PROXY_URLset → explicit{"http": url, "https": url}proxies (explicit wins over env, matching the documented precedence;socks5/socks5hwork — PySocks is already a dependency).PROXY_URLunset →None: nothing is forced, standardHTTPS_PROXY/HTTP_PROXY/ALL_PROXYenv vars andNO_PROXYbypasses keep applying viatrust_env— zero behavior change for deployments not usingPROXY_URL.Test plan
tests/test_live_rest_proxy.py): explicit http proxy applied, socks5h applied, unset forces nothing (trust_env stays in charge), per-process caching, funnel forwarding — 5/5 pass.PROXY_URLset):GET /fapi/v1/time→ 200,GET /fapi/v1/exchangeInfo→ 200 with 882 symbols throughBinanceFuturesClient._request.ConnectionResetError(104)when no proxy is configured at all (reproduces the reported symptom).API documentation
Backward compatibility
PROXY_URL: unchanged (helper resolves toNone,trust_envbehavior identical).PROXY_URL: live-trading traffic starts honoring the documented setting; if bothPROXY_URLand standard env vars are set,PROXY_URLwins for live trading (consistent with its documented scope).