|
| 1 | +--- |
| 2 | +id: download |
| 3 | +title: Download Market Data |
| 4 | +sidebar_label: Download Market Data |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The `download` function is a utility provided by the **Investing Algorithm Framework** that allows you to retrieve historical market data for specific symbols (assets), including OHLCV data (Open, High, Low, Close, Volume), ticker data, and more. This function is particularly useful for: |
| 10 | + |
| 11 | +- Backtesting trading strategies |
| 12 | +- Performing exploratory data analysis |
| 13 | +- Preprocessing input data for machine learning models |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Function Signature |
| 18 | + |
| 19 | +```python |
| 20 | +def download( |
| 21 | + symbol: str, |
| 22 | + market=None, |
| 23 | + date=None, |
| 24 | + time_frame: str = None, |
| 25 | + data_type: str = "ohlcv", |
| 26 | + start_date: str = None, |
| 27 | + end_date: str = None, |
| 28 | + window_size: int = 200, |
| 29 | + pandas: bool = True, |
| 30 | + save: bool = True, |
| 31 | + storage_path: str = None, |
| 32 | +) -> Union[pandas.DataFrame, polars.DataFrame] |
| 33 | +``` |
| 34 | + |
| 35 | +| Parameter | Type | Description | |
| 36 | +| -------------- | ------ | -------------------------------------------------------------------------------- | |
| 37 | +| `symbol` | `str` | The symbol (e.g., `"BTC/USDT"`) for which data is downloaded. | |
| 38 | +| `market` | `str` | (Optional) The market to download data from (e.g., `"binance"`). | |
| 39 | +| `date` | `str` | (Optional) Specific date to retrieve data for. | |
| 40 | +| `time_frame` | `str` | The time frame for data (e.g., `"1d"`, `"1h"`). | |
| 41 | +| `data_type` | `str` | Type of data to retrieve: `"ohlcv"` (default) or `"ticker"`. | |
| 42 | +| `start_date` | `str` | (Optional) Start of the date range to retrieve data. | |
| 43 | +| `end_date` | `str` | (Optional) End of the date range. | |
| 44 | +| `window_size` | `int` | Number of records to retrieve (default: 200). | |
| 45 | +| `pandas` | `bool` | If `True`, returns a `pandas.DataFrame`; otherwise returns a `polars.DataFrame`. | |
| 46 | +| `save` | `bool` | If `True`, saves the downloaded data to disk. | |
| 47 | +| `storage_path` | `str` | (Optional) Path to store the data when `save=True`. | |
| 48 | + |
| 49 | + |
| 50 | +## Returns |
| 51 | +The function returns a DataFrame (pandas or polars) containing the requested market data, ready for analysis, visualization, or model training. |
| 52 | + |
| 53 | +## Why It's Useful? |
| 54 | +This function streamlines the process of acquiring market data by: |
| 55 | +* Automatically selecting the correct data provider |
| 56 | +* Supporting multiple formats (pandas or polars) |
| 57 | +* Handling flexible input dates and ranges |
| 58 | +* Offering a simple way to persist downloaded data to disk |
| 59 | + |
| 60 | +## Example Use Cases |
| 61 | +📈 Backtesting a Strategy |
| 62 | + |
| 63 | +```python |
| 64 | +df = download("BTC/USDT", market="binance", time_frame="1d", start_date="2021-01-01", end_date="2022-01-01") |
| 65 | +``` |
| 66 | +Use the returned df to simulate your strategy’s performance over historical data. |
| 67 | + |
| 68 | +## 🧠 Preparing Data for Machine Learning |
| 69 | +```python |
| 70 | +df = download("ETH/USDT", market="binance", time_frame="1h", window_size=500, pandas=True) |
| 71 | +features = df[["close", "volume"]] |
| 72 | +``` |
| 73 | + |
| 74 | +This enables quick preparation of time-series datasets for supervised learning tasks. |
| 75 | + |
| 76 | +## 💾 Saving Data for Offline Analysis |
| 77 | + |
| 78 | +```python |
| 79 | +download("SOL/USDT", market="binance", time_frame="1d", save=True, storage_path="./data/") |
| 80 | +``` |
| 81 | + |
| 82 | +## Internals |
| 83 | +The function relies on: |
| 84 | +* ConfigurationService and MarketCredentialService to determine available providers and credentials. |
| 85 | +* DataProviderService to abstract away direct API calls. |
| 86 | +* dateutil.parser for robust datetime parsing. |
| 87 | +* Default providers registered via get_default_data_providers() and get_default_ohlcv_data_providers(). |
0 commit comments