> ## Documentation Index
> Fetch the complete documentation index at: https://knowledge.cloudquant.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Example Queries

> Practical examples of CloudQuant Data Liberator queries including last known value, time series, all symbols, live datasets, and stats.

# Example queries

<Note>
  As of release version 2.0, the P12 client certificate (`liberator.pfx`) is no longer required. The `--cert-type P12 --cert liberator.pfx` flags shown in the curl examples below are only needed for releases prior to 2.0.
</Note>

## Last known value

A query without date parameters retrieves the most recent available data.

<CodeGroup>
  ```python Python theme={null}
  import liberator

  res = liberator.query(name='daily_bars')
  df = liberator.get_dataframe(res)
  df[['symbol', 'timestamp', 'open', 'high', 'low', 'close', 'volume']]
  ```

  ```csharp C# theme={null}
  var res = liberator.query(new Dictionary<string, dynamic>() {
      {"name", "daily_bars"}
  });

  var df = liberator.get_dataframe(res);
  Console.WriteLine(DataFrameUtils.PrettyPrint(df));
  ```

  ```javascript JavaScript theme={null}
  let params = {
      symbols: "AAPL",
      name:    "daily_bars"
  };

  liberator.query(params).then(function(results) {
      // Your code
  });
  ```

  ```java Java theme={null}
  Liberator liberator = new Liberator();
  Object query_id = liberator.query(new HashMap<>() {{
      put("name", "daily_bars");
  }});

  for (Liberator.QueryResult res : liberator.Generate()) {
      Object data = res.GetData();
      if (data instanceof org.apache.arrow.vector.VectorSchemaRoot) {
          printRecordBatch((org.apache.arrow.vector.VectorSchemaRoot) data);
      } else if (data instanceof javax.json.JsonValue) {
          System.out.println((javax.json.JsonValue) data);
      }
  }
  ```

  ```r R theme={null}
  print(liberator::get_dataframe(liberator::query(
      name="daily_bars"
  )))
  ```

  ```bash RESTful (curl) theme={null}
  curl -qs --cert-type P12 --cert liberator.pfx \
    -H "Content-Type: application/json" --noproxy '*' \
    --data '{"json_xfer":true,"symbols":["AAPL","IBM"],"name":"daily_bars","user":"'"$USER"'","token":"'"$TOKEN"'","system":"API","debug_stream":false}' \
    $URL/liberator/query
  ```

  ```cpp C++ theme={null}
  auto table = liberator.get_table(liberator.query({
      {"name", "daily_bars"s}
  }));

  (void)arrow::PrettyPrint(*table, arrow::PrettyPrintOptions(0, 1), &std::cout);
  ```
</CodeGroup>

<Tip>
  Rather than displaying the entire result, you can select specific columns or pass a `fields` parameter to include only the data you need. Some key fields are returned automatically.
</Tip>

## Time series queries

Two date parameters control historical data retrieval:

* **`as_of`** — Retrieve data from any past date showing information as known on that date. Defaults to current time. Format: `YYYY-MM-DD HH:MM:SS` (time optional).
* **`back_to`** — Specifies where the returned dataset begins. Reads all data "back to" the specified date. Format: `YYYY-MM-DD HH:MM:SS` (time optional).

<Note>
  If `as_of` and `back_to` are identical, the result will not be a time series.
</Note>

<CodeGroup>
  ```python Python theme={null}
  df = liberator.get_dataframe(liberator.query(
      name='daily_bars',
      as_of='2020-11-15',
      symbols=['FB', 'AAPL', 'NFLX', 'GOOG', 'MSFT', 'IBM']))
  ```

  ```csharp C# theme={null}
  var res = liberator.query(new Dictionary<string, dynamic>() {
      {"name", "daily_bars"},
      {"as_of", "2021-04-10"},
      {"back_to", "2021-03-10"},
      {"symbols", "ES"}
  });

  foreach (RecordBatch batch in res())
  {
      var df = DataFrame.FromArrowRecordBatch(batch);
      Console.WriteLine(DataFrameUtils.PrettyPrint(df));
  }
  ```

  ```javascript JavaScript theme={null}
  let params = {
      back_to: "2021-01-12 00:00:00",
      as_of:   "2021-01-12 23:59:59",
      symbols: "AAPL",
      name:    "daily_bars"
  };

  liberator.query(params).then(function(results) {
      // Your code
  });
  ```

  ```java Java theme={null}
  Object query_id = liberator.query(new HashMap<>() {{
      put("name", "daily_bars");
      put("as_of", "2020-11-15");
      put("back_to", "2020-01-01");
      put("symbols", "AAPL");
  }});

  for (Liberator.QueryResult res : liberator.Generate()) {
      Object data = res.GetData();
      if (data instanceof org.apache.arrow.vector.VectorSchemaRoot) {
          printRecordBatch((org.apache.arrow.vector.VectorSchemaRoot) data);
      } else if (data instanceof javax.json.JsonValue) {
          System.out.println((javax.json.JsonValue) data);
      }
  }
  ```

  ```r R theme={null}
  print(liberator::get_dataframe(liberator::query(
      name="daily_bars",
      symbols="AAPL",
      as_of="2019-09-15",
      back_to="2019-09-10"
  )))
  ```

  ```bash RESTful (curl) theme={null}
  curl -qs --cert-type P12 --cert liberator.pfx \
    -H "Content-Type: application/json" --noproxy '*' \
    --data '{"json_xfer":true,"symbols":"AAPL","name":"daily_bars","back_to":"2022-12-01","user":"'"$USER"'","token":"'"$TOKEN"'","system":"API","debug_stream":false}' \
    $URL/liberator/query
  ```

  ```cpp C++ theme={null}
  auto table = liberator.get_table(liberator.query({
      {"symbols", std::vector<std::string>{"AAPL", "GOOGL"}},
      {"name", "daily_bars"s},
      {"as_of", "2021-04-10"s},
      {"back_to", "2021-03-10"s}
  }));
  ```
</CodeGroup>

## Query all symbols

Omit the `symbols` parameter to retrieve all symbols in a dataset.

<CodeGroup>
  ```python Python theme={null}
  df = liberator.get_dataframe(liberator.query(
      name='daily_bars',
      as_of='2020-11-15'))
  ```

  ```java Java theme={null}
  Object query_id = liberator.query(new HashMap<>() {{
      put("name", "daily_bars");
      put("as_of", "2020-11-15");
  }});
  ```

  ```bash RESTful (curl) theme={null}
  curl -qs --cert-type P12 --cert liberator.pfx \
    -H "Content-Type: application/json" --noproxy '*' \
    --data '{"json_xfer":true,"name":"daily_bars","as_of":"2020-11-15","user":"'"$USER"'","token":"'"$TOKEN"'","system":"API"}' \
    $URL/liberator/query
  ```
</CodeGroup>

## Query a live dataset

Set the `as_of` parameter to `"live"` for real-time data streaming.

<CodeGroup>
  ```python Python theme={null}
  res = liberator.query(name='live_nasdaq_composite', as_of='live')

  for batch in res:
      df = batch.to_pandas()
      print(df)
  ```

  ```csharp C# theme={null}
  var res = liberator.query(new Dictionary<string, dynamic>() {
      {"name", "live_nasdaq_composite"},
      {"as_of", "live"},
      {"symbols", "AAPL"}
  });

  foreach (RecordBatch batch in res())
  {
      var df = DataFrame.FromArrowRecordBatch(batch);
      Console.WriteLine(DataFrameUtils.PrettyPrint(df));
  }
  ```

  ```cpp C++ theme={null}
  Liberator::Result ptr = liberator.query({
      {"symbols", std::vector<std::string>{"AAPL"}},
      {"name", "live_nasdaq_composite"s},
      {"as_of", "live"s}
  });

  auto generator = *std::get_if<Liberator::Func>(&ptr);

  for (auto res = generator(); res.index(); res = generator())
  {
      auto batch = *std::get_if<2>(&res);
      (void)arrow::PrettyPrint(*batch, arrow::PrettyPrintOptions(0, 1), &std::cout);
  }
  ```
</CodeGroup>

## Query stats as JSON

Setting the `stats` parameter to `"total"` returns a JSON result with the count per symbol instead of data rows.

<CodeGroup>
  ```python Python theme={null}
  res = liberator.query(
      name='daily_bars',
      as_of='2021-04-10',
      back_to='2021-03-10',
      symbols=['ES'],
      stats='total'
  )
  ```

  ```csharp C# theme={null}
  var res = liberator.query(new Dictionary<string, dynamic>() {
      {"name", "daily_bars"},
      {"as_of", "2021-04-10"},
      {"back_to", "2021-03-10"},
      {"symbols", "ES"},
      {"stats", "total"}
  });
  Console.WriteLine(JsonDocumentToIndentedString(res));
  ```

  ```r R theme={null}
  print(liberator::query(
      symbols="AAPL",
      name="daily_bars",
      as_of="2019-09-15",
      back_to="2019-09-10",
      stats="total"
  ))
  ```
</CodeGroup>

## Get DataFrame / table

Convert query results into a tabular format native to your language.

<CodeGroup>
  ```python Python theme={null}
  df = liberator.get_dataframe(
      liberator.query(name='daily_bars', as_of='2020-11-15', symbols=['AAPL'])
  )
  ```

  ```csharp C# theme={null}
  var res = liberator.query(new Dictionary<string, dynamic>() {
      {"name", "daily_bars"},
      {"as_of", "2021-04-10"},
      {"back_to", "2021-03-10"},
      {"symbols", "ES"}
  });

  var df = liberator.get_dataframe(res);
  Console.WriteLine(DataFrameUtils.PrettyPrint(df));
  ```

  ```r R theme={null}
  res = liberator::get_dataframe(liberator::query(
      symbols="AAPL",
      name="daily_bars",
      as_of="2025-01-01",
      back_to="2024-12-01"
  ))

  print(res)
  ```

  ```cpp C++ theme={null}
  auto table = liberator.get_table(liberator.query({
      {"symbols", std::vector<std::string>{"AAPL", "GOOGL"}},
      {"name", "daily_bars"s},
  }));

  (void)arrow::PrettyPrint(*table, arrow::PrettyPrintOptions(0, 1), &std::cout);
  ```
</CodeGroup>
