Skip to content

feat(data): add klines_type to BinanceData for USD-M futures klines - #918

Open
QuantIntellect wants to merge 1 commit into
polakowo:masterfrom
QuantIntellect:feat/binance-futures-klines
Open

QuantIntellect wants to merge 1 commit into
polakowo:masterfrom
QuantIntellect:feat/binance-futures-klines

Conversation

@QuantIntellect

Copy link
Copy Markdown

Summary

BinanceData can only fetch spot klines. Passing klines_type to vbt.BinanceData.download(...), as in #398, raises TypeError: BinanceData.download_symbol() got an unexpected keyword argument 'klines_type'.

This PR adds a klines_type argument to BinanceData.download_symbol. It accepts 'spot' (the default) or 'futures' (USD-M futures), case-insensitively, and also binance.enums.HistoricalKlinesType.SPOT / .FUTURES. The snippet in the issue now runs as written and returns USD-M futures bars. Without klines_type, the requests and the output are unchanged. update() keeps the selected market, because the argument is stored in download_kwargs like every other download argument.

Root cause: download_symbol has no klines_type parameter. Both the first-valid-timestamp probe (L482) and the pagination loop (L498-L504) call the spot-only client.get_klines. BinanceData.download passes keyword arguments that match Client.__init__ to the client and forwards the rest to download_symbol, so klines_type lands 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_type maps to python-binance's public get_klines or futures_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.
  • An Enum is matched by member name, so HistoricalKlinesType works without vectorbt importing python-binance, which is an optional dependency.
  • Any other value, including FUTURES_COIN, raises ValueError before a request is made. The check sits outside the probe's bare except:, so the error is not swallowed.
  • The parameter is added last, so positional calls are unaffected. settings.data.binance is unchanged, because that dict is passed to Client(...).

Live /fapi/v1/klines responses 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::TestBinanceData is the first test coverage for BinanceData. 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 the test extra, is not needed. The tests cover a spot-default control (exact request sequence, columns, index and values); "spot", "futures", "FUTURES" and an Enum stand-in routing every request, the probe included, to the right method; futures history that starts before spot; invalid values raising ValueError with no request; and update() keeping the market. One more test runs the issue's flow with the real HistoricalKlinesType.FUTURES and 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 TypeError from 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 BTCUSDT bars and then ran .update(). The first bar matched /fapi/v1/klines exactly 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 with endTime=None as 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:

  • Python 3.12, pytest tests/test_data.py: 52 passed, 1 skipped (the python-binance-only test)
  • Python 3.12, pytest tests/: 1701 passed, 226 skipped (Rust-engine cases, pandas-ta and the python-binance-only test)
  • Python 3.11 and 3.14, pytest tests/test_data.py: 52 passed, 1 skipped

With python-binance 1.0.37 also installed, Python 3.12:

  • pytest tests/test_data.py: 53 passed

Formatting: black --check with the repo's line-length = 120 reports no changes needed on both files.

Not run: the Rust-engine job, Python 3.13, Linux and Windows.

Questions for review

  1. I dispatch to python-binance's public methods rather than the private Client._klines(klines_type=...). Happy to switch if you'd rather follow python-binance there.
  2. COIN-M (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/klines response. The current Volume, Quote volume, Taker base volume and Taker quote volume labels would therefore be wrong. Binance's COIN-M docs also describe a 200-day cap per request and returning the most recent limit rows before endTime (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 take pair instead of symbol (docs). I can follow up on any of these.

Fixes #398

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

No deployments
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.

vbt.BinanceData.download do not accept klines_type parameter

1 participant