Available languages: [Python](https://databento.com/docs/api-historical/timeseries/timeseries-get-range-to-file.python.md) · [C++](https://databento.com/docs/api-historical/timeseries/timeseries-get-range-to-file.cpp.md) · [Rust](https://databento.com/docs/api-historical/timeseries/timeseries-get-range-to-file.rust.md) · [HTTP](https://databento.com/docs/api-historical/timeseries/timeseries-get-range-to-file.http.md)

Source: https://databento.com/docs/api-historical/timeseries/timeseries-get-range-to-file



### Historical::TimeseriesGetRangeToFile

Make a streaming request for time series data from Databento.

Primary method for getting historical intraday and daily market data,
instrument definitions, and market status data directly into your application.

Data is streamed directly to the file at `file_path`.
If the file at `file_path` already exists, it will be overwritten.

#### API method

```cpp
DbnStore TimeseriesGetRangeToFile(
    const std::string& dataset,
    const DateTimeRange<UnixNanos>& datetime_range,
    const std::vector<std::string>& symbols, Schema schema,
    const std::filesystem::path& file_path);
DbnStore TimeseriesGetRangeToFile(
    const std::string& dataset,
    const DateTimeRange<std::string>& datetime_range,
    const std::vector<std::string>& symbols, Schema schema,
    const std::filesystem::path& file_path);
DbnStore TimeseriesGetRangeToFile(
    const std::string& dataset,
    const DateTimeRange<UnixNanos>& datetime_range,
    const std::vector<std::string>& symbols, Schema schema,
    SType stype_in, SType stype_out, std::uint64_t limit,
    const std::filesystem::path& file_path);
DbnStore TimeseriesGetRangeToFile(
    const std::string& dataset,
    const DateTimeRange<std::string>& datetime_range,
    const std::vector<std::string>& symbols, Schema schema,
    SType stype_in, SType stype_out, std::uint64_t limit,
    const std::filesystem::path& file_path);
```

#### Parameters

| Parameter | Type | Description |
| `dataset` | std::string | The [dataset](https://databento.com/docs/api-historical/basics/datasets.md) code (string identifier). Must be one of the values from [MetadataListDatasets](https://databento.com/docs/api-historical/metadata/metadata-list-datasets.md). Constants are provided in the `databento::dataset` namespace in ``. |
| `datetime_range` | databento::DateTimeRange\ or databento::DateTimeRange\ | The request range with an inclusive start and an optional exclusive end. Filters on `ts_recv` if it exists in the schema, otherwise `ts_event`. Takes an [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) string or a [UNIX timestamp](https://en.wikipedia.org/wiki/Unix_time) in nanoseconds. Assumes UTC as timezone unless otherwise specified. If not specified, end defaults to the [forward filled](https://databento.com/docs/conventions/common-fields-enums-types.md#forward-filling-end-parameters) value of start based on the resolution provided. |
| `symbols` | std::vector\ | The product symbols to filter for. Takes up to 2,000 symbols per request. Use `databento::kAllSymbols` from `` to request all symbols. |
| `schema` | databento::Schema | The data record [schema](https://databento.com/docs/schemas/whats-a-schema.md). Must be one of the values from [MetadataListSchemas](https://databento.com/docs/api-historical/metadata/metadata-list-schemas.md). |
| `stype_in` | databento::SType | The [symbology type](https://databento.com/docs/api-historical/basics/symbology.md) of input `symbols`. Defaults to `RawSymbol`. |
| `stype_out` | databento::SType | The [symbology type](https://databento.com/docs/api-historical/basics/symbology.md) of output symbols. Defaults to `InstrumentId`. Must be a valid symbology combination with `stype_in`. See [symbology combinations](https://databento.com/docs/conventions/symbology.md#supported-symbology-combinations). |
| `limit` | std::uint64_t | The maximum number of records to return. Use `0` or `{}` for no limit. Defaults to no limit. |
| `file_path` | std::filesystem::path | The path of the file where the data should be saved. If the file already exists, it will be overwritten. It is recommended to use the ".dbn.zst" suffix. |

#### Returns

A [DbnStore](https://databento.com/docs/api-historical/helpers/dbn-store.md) object.

A full list of fields for the records read through [NextRecord](https://databento.com/docs/api-historical/helpers/dbn-store-next-record.md)
or [Replay](https://databento.com/docs/api-historical/helpers/dbn-store-replay.md) for each schema is available through
[Historical.MetadataListFields](https://databento.com/docs/api-historical/metadata/metadata-list-fields.md)
or the struct definitions in `<databento/record.hpp>`.

#### Example usage

```cpp
#include <databento/historical.hpp>
#include <iostream>

using namespace databento;

auto client =
    Historical::Builder().SetKey("$YOUR_API_KEY").Build();
auto file_bento = client.TimeseriesGetRangeToFile(
    "GLBX.MDP3", {"2022-06-06T00:00", "2022-06-10T00:10"},
    {"ESM2"}, Schema::Trades, SType::RawSymbol,
    SType::InstrumentId, 1, "ESM2_20220606-20220610.dbn.zst");
file_bento.Replay([](const Record& record) {
  // Convert the generic `Record` to a `TradeMsg`, the
  // record type associated with `Schema::Trades`
  const auto& trade_msg = record.Get<TradeMsg>();
  std::cout << trade_msg << '\n';
  return KeepGoing::Continue;
});
```
#### Example response

```plaintext
TradeMsg {
    hd = RecordHeader { length = 12, rtype = mbp-0, publisher_id = 1, instrument_id = 3403, ts_event = 2022-06-06T00:00:00.070033767Z },
    price = 4108.500000000,
    size = 1,
    action = Trade,
    side = Ask,
    flags = 0,
    depth = 0,
    ts_recv = 2022-06-06T00:00:00.070314216Z,
    ts_in_delta = 18681,
    sequence = 157862
}
```
