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..136801b --- /dev/null +++ b/tests/test_bvolindex.py @@ -0,0 +1,52 @@ +# 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..993733d --- /dev/null +++ b/tests/test_eohsummary.py @@ -0,0 +1,50 @@ +# 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..2777262 --- /dev/null +++ b/tests/test_indexpriceklines.py @@ -0,0 +1,273 @@ +# 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..83bc6c0 --- /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..804bace --- /dev/null +++ b/tests/test_premiumindexklines.py @@ -0,0 +1,273 @@ +# 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)