Metadata-Version: 2.4
Name: questdb-fxmacrodata
Version: 0.1.0
Summary: Load public FXMacroData responses into QuestDB
License-Expression: MIT
Project-URL: Documentation, https://fxmacrodata.com/documentation/reference?utm_source=pypi&utm_medium=referral&utm_campaign=questdb-fxmacrodata&utm_content=package
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fxmacrodata-platform-core==0.1.0
Requires-Dist: questdb<6,>=5.0.0
Provides-Extra: test
Requires-Dist: pytest<9,>=8; extra == "test"
Dynamic: license-file

# FXMacroData for QuestDB

Load FXMacroData public responses into QuestDB with the official `questdb`
Python client's ingestion `Sender`. Keep macro announcements next to your
time-series market data while retaining the source payload and revision
metadata.

[FXMacroData documentation](https://fxmacrodata.com/documentation/reference?utm_source=github&utm_medium=referral&utm_campaign=questdb-fxmacrodata&utm_content=readme)

## Install and connect

Requires Python 3.10 or later. Install this package and its adjacent public
client dependencies from the distribution supplied to you:

```sh
python -m pip install /path/to/fxmacrodata-public-client /path/to/fxmacrodata-platform-core /path/to/questdb-fxmacrodata
```

Configure `FXMD_API_KEY` and `QDB_CLIENT_CONF` (the QuestDB client's own
configuration string, including any token) in your runtime secret store. Then
load into a customer-owned QuestDB instance:

```python
from questdb.ingress import Sender
from fxmacrodata_platform_core import read
from fxmacrodata_questdb import load

with Sender.from_env() as sender:
    batch = read("indicator_history", {"currency": "usd", "indicator": "policy_rate"})
    result = load(batch, sender, "fxmacrodata", batch_size=1000)
```

USD operations work without a key. For other currencies pass
`api_key_env="FXMD_API_KEY"` to `read` or `load_operation`.

You open and close the `Sender`; the loader only calls `row` and `flush`.
QuestDB creates the table on first ingestion. Table names are one simple
identifier such as `fxmacrodata`; dotted names, SQL fragments and URL-like
names are rejected.

Every operation exposed by `fxmacrodata_platform_core.operations()` is available,
including REST and MCP operations. The loader adds a tabular envelope without
changing the original public response. `record_json` retains the original record;
`response_json` retains the complete response, including pagination and metadata.
An empty response produces one envelope with `record_json = 'null'`, so absence of
data and the server's metadata remain inspectable. It is not an observation.

Columns are `operation`, `source_url`, `record_id`, `currency`, `indicator`,
`date`, `announcement_datetime`, `value`, `value_text`, `record_json`,
`response_json`, and the loader's `content_hash`. `operation`, `currency` and
`indicator` are sent as QuestDB symbols; only `value` is numeric (a double);
everything else is a string. Null values are omitted from the row, which
QuestDB stores as `NULL`.

Every row is stamped with QuestDB's server timestamp at ingestion
(`ServerTimestamp`). The designated timestamp therefore records when you
loaded the data, never when a release was published. `date` and
`announcement_datetime` remain source strings in their own columns; no
publication time is inferred, and no missing value is filled. `value_text` and
JSON preserve source numbers supplied as strings. JSON numeric tokens follow the
public Python client parser, so original lexical precision is not guaranteed.

Select operations with the shared catalogue rather than constructing URLs:

```python
from fxmacrodata_platform_core import operations, schemas

available = operations()
input_schemas = schemas()
```

`load_operation` accepts `api_key_env`, `complete_history`, `max_pages`, and
`client_factory` for the public source reader. `max_pages` bounds history
pagination; an incomplete read raises an error before a destination load starts.
`batch_size` bounds destination blocks to 1–10,000 rows; the sender is flushed
after each block. `max_batch_bytes` also bounds each block using a conservative
JSON byte estimate (16 MiB by default, 128 MiB maximum). An oversized individual
envelope is rejected before destination changes; narrow the source date range
to reduce the response size. The source response is materialized before
loading; choose a source date range suitable for memory. Read the installed
operation's schema for required arguments and date filters.

Supply source credentials through your runtime secret store, exposing only the
environment variable name to the adapter. Destination authentication belongs to
the `Sender` you construct. The adapter neither saves nor prints credentials
and never constructs connection configuration for you.

The package licence covers this adapter code. FXMacroData data access and onward
redistribution remain subject to your subscription and applicable licence. Loading
data into a database does not grant permission to publish or resell it.

## Retries and querying

Delivery is at-least-once. QuestDB's ingestion protocol has no unique key, and
because rows carry a server timestamp, a retried batch appends rows again with
a later timestamp. Every row carries `content_hash` (a digest of the ten source
columns), so duplicates are exact and easy to collapse:

```sql
SELECT currency, indicator, date, value, record_json
FROM fxmacrodata
WHERE record_json <> 'null'
LATEST ON timestamp PARTITION BY content_hash;
```

Changed record data or response metadata produces a distinct content hash and is
retained alongside the earlier version. Blocks are not retried automatically.
On a failure, earlier blocks were already flushed; retrying the same source
batch is safe when you deduplicate on `content_hash` as above.
`LoadResult.rows` counts envelopes submitted.

See [event_study.sql](examples/event_study.sql) for an announcement/price join.
The example expects your own licensed market-price table and uses source
announcement timestamps only. Timestamp presence alone is not evidence that a
release had already been published; use source publication metadata when
building a point-in-time backtest.

## Development

Tests are offline and fake the sender:

```sh
python -m pytest tests -q
```

Official client reference:
[QuestDB Python client](https://py-questdb-client.readthedocs.io/?utm_source=pypi&utm_medium=referral&utm_campaign=questdb-fxmacrodata&utm_content=upstream-reference).
