Repository navigation
feat(data): add klines_type to BinanceData for USD-M futures klines - #918
Open
QuantIntellect wants to merge 1 commit into
Open
QuantIntellect wants to merge 1 commit into
QuantIntellect wants to merge 1 commit into
Conversation
BinanceData.download_symbol always called the spot-only get_klines, so passing klines_type (as in polakowo#398) raised TypeError. Accept 'spot' (default) or 'futures' (USD-M), case-insensitively or as a HistoricalKlinesType member, and use the matching python-binance method for both the first-timestamp probe and pagination. Other values raise ValueError before any request. Fixes polakowo#398
This branch has not been deployed
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
BinanceDatacan only fetch spot klines. Passingklines_typetovbt.BinanceData.download(...), as in #398, raisesTypeError: BinanceData.download_symbol() got an unexpected keyword argument 'klines_type'.This PR adds a
klines_typeargument toBinanceData.download_symbol. It accepts'spot'(the default) or'futures'(USD-M futures), case-insensitively, and alsobinance.enums.HistoricalKlinesType.SPOT/.FUTURES. The snippet in the issue now runs as written and returns USD-M futures bars. Withoutklines_type, the requests and the output are unchanged.update()keeps the selected market, because the argument is stored indownload_kwargslike every other download argument.Root cause:
download_symbolhas noklines_typeparameter. Both the first-valid-timestamp probe (L482) and the pagination loop (L498-L504) call the spot-onlyclient.get_klines.BinanceData.downloadpasses keyword arguments that matchClient.__init__to the client and forwards the rest todownload_symbol, soklines_typelands there and is rejected. Routing it to the client would not help either: python-binance picks the market by calling a different client method (Client._klines), not through a constructor argument.Change (
vectorbt/data/custom.py):klines_typemaps to python-binance's publicget_klinesorfutures_klines. The selected method is used for both the probe and the pagination loop, so a futures download starts at the first futures bar, not at the spot listing date.Enumis matched by member name, soHistoricalKlinesTypeworks without vectorbt importing python-binance, which is an optional dependency.FUTURES_COIN, raisesValueErrorbefore a request is made. The check sits outside the probe's bareexcept:, so the error is not swallowed.settings.data.binanceis unchanged, because that dict is passed toClient(...).Live
/fapi/v1/klinesresponses have the same 12-field rows as spot, and Binance's USD-M kline stream model lists base-asset volume before quote-asset volume, as spot does. The existing parsing and column names therefore stay correct.Tests added
tests/test_data.py::TestBinanceDatais the first test coverage forBinanceData. It uses a fake client that serves in-memory 12-field kline rows per method and records every request, so python-binance, which is not in thetestextra, is not needed. The tests cover a spot-default control (exact request sequence, columns, index and values);"spot","futures","FUTURES"and anEnumstand-in routing every request, the probe included, to the right method; futures history that starts before spot; invalid values raisingValueErrorwith no request; andupdate()keeping the market. One more test runs the issue's flow with the realHistoricalKlinesType.FUTURESand is skipped when python-binance is missing (pytest.importorskip), as in CI.Validation
Regression evidence
On master (9ac1b69) with only the new tests applied, 11 of the 12 tests that run in CI fail with the
TypeErrorfrom the issue, and the spot control passes. With python-binance 1.0.37 installed, the issue-flow test fails the same way. After the fix, all of them pass.I also ran the issue's snippet, with only the progress bar turned off, through python-binance 1.0.37 against Binance's public market-data endpoints, with no API key. It downloaded about six years of hourly
BTCUSDTbars and then ran.update(). The first bar matched/fapi/v1/klinesexactly and differed from the spot bar at the same open time, and the index had no gaps or duplicates. In separate runs, a 1-minute bar fetched by.update()matched/fapi/v1/klines, not spot, and the probe, sent withendTime=Noneas vectorbt does, returned the first futures bar (2019-09-08) and paging started there.Checks
Installed like CI (
uv pip install -e ".[test]", no python-binance), on macOS:pytest tests/test_data.py: 52 passed, 1 skipped (the python-binance-only test)pytest tests/: 1701 passed, 226 skipped (Rust-engine cases, pandas-ta and the python-binance-only test)pytest tests/test_data.py: 52 passed, 1 skippedWith python-binance 1.0.37 also installed, Python 3.12:
pytest tests/test_data.py: 53 passedFormatting:
black --checkwith the repo'sline-length = 120reports no changes needed on both files.Not run: the Rust-engine job, Python 3.13, Linux and Windows.
Questions for review
Client._klines(klines_type=...). Happy to switch if you'd rather follow python-binance there.futures_coin) is rejected for now. Its kline volumes are in contracts and in the base asset, not in the base and quote assets, according to the COIN-M kline stream model and a live/dapi/v1/klinesresponse. The currentVolume,Quote volume,Taker base volumeandTaker quote volumelabels would therefore be wrong. Binance's COIN-M docs also describe a 200-day cap per request and returning the most recentlimitrows beforeendTime(docs). A live request paged forward anyway, but I'd rather not rely on undocumented behaviour. Mark-price and index-price klines are also out of scope; index-price klines takepairinstead ofsymbol(docs). I can follow up on any of these.Fixes #398