From c98524704c939d9b4ad1a57069af8a38c77831e8 Mon Sep 17 00:00:00 2001 From: Hirotaka Aoki <113173839+aoki-h-jp@users.noreply.github.com> Date: Thu, 24 Aug 2023 23:32:56 +0900 Subject: [PATCH 1/2] All test case passed --- README.md | 52 +++--- downloader/downloader.py | 2 +- tests/test_bvolindex.py | 51 ++++++ tests/test_eohsummary.py | 51 ++++++ tests/test_indexpriceklines.py | 271 +++++++++++++++++++++++++++++++ tests/test_markpriceklines.py | 271 +++++++++++++++++++++++++++++++ tests/test_premiumindexklines.py | 271 +++++++++++++++++++++++++++++++ 7 files changed, 943 insertions(+), 26 deletions(-) create mode 100644 tests/test_bvolindex.py create mode 100644 tests/test_eohsummary.py create mode 100644 tests/test_indexpriceklines.py create mode 100644 tests/test_markpriceklines.py create mode 100644 tests/test_premiumindexklines.py diff --git a/README.md b/README.md index bdce4d2..6af2658 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Python library for bulk downloading Binance historical data A Python library to efficiently and concurrently download historical data files from Binance. -Supports multiple asset types (spot, futures, options) and various data frequencies. +Supports all asset types (spot, USDT-M, COIN-M, options) and all data frequencies. Status on available data types is shown below. @@ -56,37 +56,39 @@ This library is under development. Not all unit tests have been completed yet an | bookDepth | ❎ | ✅ | ✅ | ❎ | | bookTicker | ❎ | ✅ | ✅ | ❎ | | fundingRate | ❎ | ✅ | ✅ | ❎ | -| indexPriceKlines | ❎ | 🚧 | 🚧 | ❎ | -| klines | ✅ | ✅ | ❌ | ❎ | +| indexPriceKlines | ❎ | ✅ | ✅ | ❎ | +| klines | ✅ | ✅ | ✅ | ❎ | | liquidationSnapshot | ❎ | ✅ | ✅ | ❎ | -| markPriceKlines | ❎ | 🚧 | 🚧 | ❎ | +| markPriceKlines | ❎ | ✅ | ✅ | ❎ | | metrics | ❎ | ✅ | ✅ | ❎ | -| premiumIndexKlines | ❎ | 🚧 | 🚧 | ❎ | -| trades | ✅ | ✅ | ❌ | ❎ | -| BVOLIndex | ❎ | ❎ | ❎ | ❌ | -| EOHSummary | ❎ | ❎ | ❎ | ❌ | +| premiumIndexKlines | ❎ | ✅ | ✅ | ❎ | +| trades | ✅ | ✅ | ✅ | ❎ | +| BVOLIndex | ❎ | ❎ | ❎ | ✅ | +| EOHSummary | ❎ | ❎ | ❎ | ✅ | ### by data_frequency (klines, indexPriceKlines, markPriceKlines, premiumIndexKlines) | data_frequency | spot | um | cm | options | | :------------- | :--: | :--: | :--: | :-----: | -| 1s | ✅ | ❎ | ❎ | ❎ | -| 1m | ✅ | ✅ | ❌ | ❎ | -| 3m | ✅ | ✅ | ❌ | ❎ | -| 5m | ✅ | ✅ | ❌ | ❎ | -| 15m | ✅ | ✅ | ❌ | ❎ | -| 30m | ✅ | ✅ | ❌ | ❎ | -| 1h | ✅ | ✅ | ❌ | ❎ | -| 2h | ✅ | ✅ | ❌ | ❎ | -| 4h | ✅ | ✅ | ❌ | ❎ | -| 6h | ✅ | ✅ | ❌ | ❎ | -| 8h | ✅ | ✅ | ❌ | ❎ | -| 12h | ✅ | ✅ | ❌ | ❎ | -| 1d | ✅ | ✅ | ❌ | ❎ | -| 3d | ❌ | ❌ | ❌ | ❎ | -| 1w | ❌ | ❌ | ❌ | ❎ | -| 1mo | ❌ | ❌ | ❌ | ❎ | - +| 1s | ✅ | ❎ | ❎ | ❎ | +| 1m | ✅ | ✅ | ✅ | ❎ | +| 3m | ✅ | ✅ | ✅ | ❎ | +| 5m | ✅ | ✅ | ✅ | ❎ | +| 15m | ✅ | ✅ | ✅ | ❎ | +| 30m | ✅ | ✅ | ✅ | ❎ | +| 1h | ✅ | ✅ | ✅ | ❎ | +| 2h | ✅ | ✅ | ✅ | ❎ | +| 4h | ✅ | ✅ | ✅ | ❎ | +| 6h | ✅ | ✅ | ✅ | ❎ | +| 8h | ✅ | ✅ | ✅ | ❎ | +| 12h | ✅ | ✅ | ✅ | ❎ | +| 1d | ✅ | ✅ | ✅ | ❎ | +| 3d | ✅ | ✅ | ✅ | ❎ | +| 1w | ✅ | ✅ | ✅ | ❎ | +| 1mo | ✅ | ✅ | ✅ | ❎ | + +## If you want to report a bug or request a feature +Please create an issue on this repository! ## Disclaimer This project is for educational purposes only. You should not construe any such information or other material as legal, diff --git a/downloader/downloader.py b/downloader/downloader.py index 4d6a4aa..4f631d9 100644 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -227,7 +227,7 @@ def _make_asset_type(self) -> str: elif self._asset in "cm": asset_type = "futures/cm" elif self._asset in self._OPTIONS_ASSET: - asset_type = "options" + asset_type = "option" elif self._asset in self._ASSET: asset_type = "spot" else: diff --git a/tests/test_bvolindex.py b/tests/test_bvolindex.py new file mode 100644 index 0000000..b40b625 --- /dev/null +++ b/tests/test_bvolindex.py @@ -0,0 +1,51 @@ +# import standard libraries +import os + +# import third party libraries +import pytest + +# import my libraries +from downloader.downloader import * + + +def dynamic_bvolindex_test_params(): + """ + Generate params for BVOLIndex tests + :return: + """ + for asset in BinanceBulkDownloader._OPTIONS_ASSET: + for data_type in ["BVOLIndex"]: + for timeperiod_per_file in ["daily"]: + yield pytest.param( + asset, + data_type, + timeperiod_per_file, + id=f"{asset}-{data_type}-{timeperiod_per_file}", + ) + + +@pytest.mark.parametrize( + "asset, data_type, timeperiod_per_file", + dynamic_bvolindex_test_params() +) +def test_bvolindex(tmpdir, asset, data_type, timeperiod_per_file): + """ + Test BVOLIndex + :param tmpdir: + :param asset: asset (option) + :param data_type: data type (BVOLIndex) + :param timeperiod_per_file: time period per file (daily) + :return: + """ + downloader = BinanceBulkDownloader( + destination_dir=tmpdir, + asset=asset, + data_type=data_type, + timeperiod_per_file=timeperiod_per_file, + ) + prefix = downloader._build_prefix() + single_download_prefix = prefix + "/BTCBVOLUSDT/BTCBVOLUSDT-BVOLIndex-2023-07-01.zip" + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) diff --git a/tests/test_eohsummary.py b/tests/test_eohsummary.py new file mode 100644 index 0000000..fc948f7 --- /dev/null +++ b/tests/test_eohsummary.py @@ -0,0 +1,51 @@ +# import standard libraries +import os + +# import third party libraries +import pytest + +# import my libraries +from downloader.downloader import * + + +def dynamic_eohsummary_test_params(): + """ + Generate params for EOHSummary tests + :return: + """ + for asset in BinanceBulkDownloader._OPTIONS_ASSET: + for data_type in ["EOHSummary"]: + for timeperiod_per_file in ["daily"]: + yield pytest.param( + asset, + data_type, + timeperiod_per_file, + id=f"{asset}-{data_type}-{timeperiod_per_file}", + ) + + +@pytest.mark.parametrize( + "asset, data_type, timeperiod_per_file", + dynamic_eohsummary_test_params() +) +def test_eohsummary(tmpdir, asset, data_type, timeperiod_per_file): + """ + Test EOHSummary + :param tmpdir: + :param asset: asset (option) + :param data_type: data type (EOHSummary) + :param timeperiod_per_file: time period per file (daily) + :return: + """ + downloader = BinanceBulkDownloader( + destination_dir=tmpdir, + asset=asset, + data_type=data_type, + timeperiod_per_file=timeperiod_per_file, + ) + prefix = downloader._build_prefix() + single_download_prefix = prefix + "/BTCUSDT/BTCUSDT-EOHSummary-2023-07-01.zip" + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) diff --git a/tests/test_indexpriceklines.py b/tests/test_indexpriceklines.py new file mode 100644 index 0000000..c62b5a5 --- /dev/null +++ b/tests/test_indexpriceklines.py @@ -0,0 +1,271 @@ +# import standard libraries +import os + +# import third party libraries +import pytest + +# import my libraries +from downloader.downloader import * + + +def dynamic_indexpriceklines_test_params(): + """ + Generate params for indexpriceklines tests + :return: + """ + for asset in BinanceBulkDownloader._FUTURES_ASSET: + for data_type in ["indexPriceKlines"]: + for data_frequency in BinanceBulkDownloader._DATA_FREQUENCY: + for timeperiod_per_file in ["daily", "monthly"]: + yield pytest.param( + asset, + data_type, + data_frequency, + timeperiod_per_file, + id=f"{asset}-{data_type}-{data_frequency}-{timeperiod_per_file}", + ) + + +@pytest.mark.parametrize( + "asset, data_type, data_frequency, timeperiod_per_file", + dynamic_indexpriceklines_test_params() +) +def test_indexpriceklines(tmpdir, asset, data_type, data_frequency, timeperiod_per_file): + """ + Test indexpriceKlines + :param tmpdir: + :param asset: asset (spot, um, cm) + :param data_type: data type (indexPriceKlines) + :param data_frequency: data frequency (1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1mo) + :param timeperiod_per_file: time period per file (daily, monthly) + :return: + """ + downloader = BinanceBulkDownloader( + destination_dir=tmpdir, + asset=asset, + data_type=data_type, + timeperiod_per_file=timeperiod_per_file, + ) + prefix = downloader._build_prefix() + if timeperiod_per_file == "daily": + if data_frequency in [ + "1m", + "3m", + "5m", + "15m", + "30m", + "1h", + "2h", + "4h", + "6h", + "8h", + "12h", + "1d", + ]: + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2021-01-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "3d": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-14.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-14.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2022-10-21.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1w": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-12.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-12.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2022-10-17.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1mo": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-05-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2022-09-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2022-09-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1s": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + else: + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + # Not test. + return None + else: + raise ValueError(f"data_frequency {data_frequency} is not supported.") + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) + elif timeperiod_per_file == "monthly": + if data_frequency in [ + "1m", + "3m", + "5m", + "15m", + "30m", + "1h", + "2h", + "4h", + "6h", + "8h", + "12h", + "1d", + ]: + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "3d": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1w": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1mo": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD/{data_frequency}/BTCUSD-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1s": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + else: + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + else: + raise ValueError(f"data_frequency {data_frequency} is not supported.") + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) diff --git a/tests/test_markpriceklines.py b/tests/test_markpriceklines.py new file mode 100644 index 0000000..3d725ed --- /dev/null +++ b/tests/test_markpriceklines.py @@ -0,0 +1,271 @@ +# import standard libraries +import os + +# import third party libraries +import pytest + +# import my libraries +from downloader.downloader import * + + +def dynamic_markpriceklines_test_params(): + """ + Generate params for markpriceklines tests + :return: + """ + for asset in BinanceBulkDownloader._FUTURES_ASSET: + for data_type in ["markPriceKlines"]: + for data_frequency in BinanceBulkDownloader._DATA_FREQUENCY: + for timeperiod_per_file in ["daily", "monthly"]: + yield pytest.param( + asset, + data_type, + data_frequency, + timeperiod_per_file, + id=f"{asset}-{data_type}-{data_frequency}-{timeperiod_per_file}", + ) + + +@pytest.mark.parametrize( + "asset, data_type, data_frequency, timeperiod_per_file", + dynamic_markpriceklines_test_params() +) +def test_markpriceklines(tmpdir, asset, data_type, data_frequency, timeperiod_per_file): + """ + Test markpriceklines + :param tmpdir: + :param asset: asset (spot, um, cm) + :param data_type: data type (klines) + :param data_frequency: data frequency (1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1mo) + :param timeperiod_per_file: time period per file (daily, monthly) + :return: + """ + downloader = BinanceBulkDownloader( + destination_dir=tmpdir, + asset=asset, + data_type=data_type, + timeperiod_per_file=timeperiod_per_file, + ) + prefix = downloader._build_prefix() + if timeperiod_per_file == "daily": + if data_frequency in [ + "1m", + "3m", + "5m", + "15m", + "30m", + "1h", + "2h", + "4h", + "6h", + "8h", + "12h", + "1d", + ]: + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "3d": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-15.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-14.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2022-10-21.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1w": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-12.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-12.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2022-10-17.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1mo": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-05-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-05-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2022-09-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1s": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + else: + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + # Not test. + return None + else: + raise ValueError(f"data_frequency {data_frequency} is not supported.") + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) + elif timeperiod_per_file == "monthly": + if data_frequency in [ + "1m", + "3m", + "5m", + "15m", + "30m", + "1h", + "2h", + "4h", + "6h", + "8h", + "12h", + "1d", + ]: + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "3d": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1w": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1mo": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1s": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + else: + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + else: + raise ValueError(f"data_frequency {data_frequency} is not supported.") + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) diff --git a/tests/test_premiumindexklines.py b/tests/test_premiumindexklines.py new file mode 100644 index 0000000..fbf8e98 --- /dev/null +++ b/tests/test_premiumindexklines.py @@ -0,0 +1,271 @@ +# import standard libraries +import os + +# import third party libraries +import pytest + +# import my libraries +from downloader.downloader import * + + +def dynamic_premiumindexklines_test_params(): + """ + Generate params for premiumindexklines tests + :return: + """ + for asset in BinanceBulkDownloader._FUTURES_ASSET: + for data_type in ["premiumIndexKlines"]: + for data_frequency in BinanceBulkDownloader._DATA_FREQUENCY: + for timeperiod_per_file in ["daily", "monthly"]: + yield pytest.param( + asset, + data_type, + data_frequency, + timeperiod_per_file, + id=f"{asset}-{data_type}-{data_frequency}-{timeperiod_per_file}", + ) + + +@pytest.mark.parametrize( + "asset, data_type, data_frequency, timeperiod_per_file", + dynamic_premiumindexklines_test_params() +) +def test_premiumindexklines(tmpdir, asset, data_type, data_frequency, timeperiod_per_file): + """ + Test premiumindexklines + :param tmpdir: + :param asset: asset (spot, um, cm) + :param data_type: data type (premiumIndexKlines) + :param data_frequency: data frequency (1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1mo) + :param timeperiod_per_file: time period per file (daily, monthly) + :return: + """ + downloader = BinanceBulkDownloader( + destination_dir=tmpdir, + asset=asset, + data_type=data_type, + timeperiod_per_file=timeperiod_per_file, + ) + prefix = downloader._build_prefix() + if timeperiod_per_file == "daily": + if data_frequency in [ + "1m", + "3m", + "5m", + "15m", + "30m", + "1h", + "2h", + "4h", + "6h", + "8h", + "12h", + "1d", + ]: + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "3d": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-15.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-14.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2022-10-21.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1w": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-12.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-06-12.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2022-10-17.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1mo": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-05-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2023-05-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2022-09-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1s": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + else: + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01-01.zip" + ) + # Not test. + return None + else: + raise ValueError(f"data_frequency {data_frequency} is not supported.") + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) + elif timeperiod_per_file == "monthly": + if data_frequency in [ + "1m", + "3m", + "5m", + "15m", + "30m", + "1h", + "2h", + "4h", + "6h", + "8h", + "12h", + "1d", + ]: + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "3d": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1w": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1mo": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + elif asset == "um": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + elif asset == "cm": + single_download_prefix = ( + prefix + + f"/BTCUSD_PERP/{data_frequency}/BTCUSD_PERP-{data_frequency}-2021-01.zip" + ) + else: + raise ValueError(f"asset {asset} is not supported.") + elif data_frequency == "1s": + if asset == "spot": + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + else: + single_download_prefix = ( + prefix + + f"/BTCUSDT/{data_frequency}/BTCUSDT-{data_frequency}-2021-01.zip" + ) + # Not test. + return None + else: + raise ValueError(f"data_frequency {data_frequency} is not supported.") + destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) + downloader._download(single_download_prefix) + # If exists csv file on destination dir, test is passed. + assert os.path.exists(destination_path) From 3d5cccec2c2aaab5ae4377ace599687dbc6a7570 Mon Sep 17 00:00:00 2001 From: aoki-h-jp Date: Thu, 24 Aug 2023 14:33:37 +0000 Subject: [PATCH 2/2] Apply Code Formatter Change --- tests/test_bvolindex.py | 7 ++++--- tests/test_eohsummary.py | 3 +-- tests/test_indexpriceklines.py | 6 ++++-- tests/test_markpriceklines.py | 2 +- tests/test_premiumindexklines.py | 6 ++++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/test_bvolindex.py b/tests/test_bvolindex.py index b40b625..136801b 100644 --- a/tests/test_bvolindex.py +++ b/tests/test_bvolindex.py @@ -25,8 +25,7 @@ def dynamic_bvolindex_test_params(): @pytest.mark.parametrize( - "asset, data_type, timeperiod_per_file", - dynamic_bvolindex_test_params() + "asset, data_type, timeperiod_per_file", dynamic_bvolindex_test_params() ) def test_bvolindex(tmpdir, asset, data_type, timeperiod_per_file): """ @@ -44,7 +43,9 @@ def test_bvolindex(tmpdir, asset, data_type, timeperiod_per_file): timeperiod_per_file=timeperiod_per_file, ) prefix = downloader._build_prefix() - single_download_prefix = prefix + "/BTCBVOLUSDT/BTCBVOLUSDT-BVOLIndex-2023-07-01.zip" + single_download_prefix = ( + prefix + "/BTCBVOLUSDT/BTCBVOLUSDT-BVOLIndex-2023-07-01.zip" + ) destination_path = tmpdir.join(single_download_prefix.replace(".zip", ".csv")) downloader._download(single_download_prefix) # If exists csv file on destination dir, test is passed. diff --git a/tests/test_eohsummary.py b/tests/test_eohsummary.py index fc948f7..993733d 100644 --- a/tests/test_eohsummary.py +++ b/tests/test_eohsummary.py @@ -25,8 +25,7 @@ def dynamic_eohsummary_test_params(): @pytest.mark.parametrize( - "asset, data_type, timeperiod_per_file", - dynamic_eohsummary_test_params() + "asset, data_type, timeperiod_per_file", dynamic_eohsummary_test_params() ) def test_eohsummary(tmpdir, asset, data_type, timeperiod_per_file): """ diff --git a/tests/test_indexpriceklines.py b/tests/test_indexpriceklines.py index c62b5a5..2777262 100644 --- a/tests/test_indexpriceklines.py +++ b/tests/test_indexpriceklines.py @@ -28,9 +28,11 @@ def dynamic_indexpriceklines_test_params(): @pytest.mark.parametrize( "asset, data_type, data_frequency, timeperiod_per_file", - dynamic_indexpriceklines_test_params() + dynamic_indexpriceklines_test_params(), ) -def test_indexpriceklines(tmpdir, asset, data_type, data_frequency, timeperiod_per_file): +def test_indexpriceklines( + tmpdir, asset, data_type, data_frequency, timeperiod_per_file +): """ Test indexpriceKlines :param tmpdir: diff --git a/tests/test_markpriceklines.py b/tests/test_markpriceklines.py index 3d725ed..83bc6c0 100644 --- a/tests/test_markpriceklines.py +++ b/tests/test_markpriceklines.py @@ -28,7 +28,7 @@ def dynamic_markpriceklines_test_params(): @pytest.mark.parametrize( "asset, data_type, data_frequency, timeperiod_per_file", - dynamic_markpriceklines_test_params() + dynamic_markpriceklines_test_params(), ) def test_markpriceklines(tmpdir, asset, data_type, data_frequency, timeperiod_per_file): """ diff --git a/tests/test_premiumindexklines.py b/tests/test_premiumindexklines.py index fbf8e98..804bace 100644 --- a/tests/test_premiumindexklines.py +++ b/tests/test_premiumindexklines.py @@ -28,9 +28,11 @@ def dynamic_premiumindexklines_test_params(): @pytest.mark.parametrize( "asset, data_type, data_frequency, timeperiod_per_file", - dynamic_premiumindexklines_test_params() + dynamic_premiumindexklines_test_params(), ) -def test_premiumindexklines(tmpdir, asset, data_type, data_frequency, timeperiod_per_file): +def test_premiumindexklines( + tmpdir, asset, data_type, data_frequency, timeperiod_per_file +): """ Test premiumindexklines :param tmpdir: