Metadata-Version: 2.4
Name: groai-fi-datastore-shared
Version: 0.3.0
Summary: Shared datastore utilities for GroAI.fi — Binance market data downloading, parquet storage, and trading infrastructure
Keywords: binance,crypto,market-data,parquet,trading,datastore
Author: Hamilton
Author-email: Hamilton <hamilton@aiart.io>
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Office/Business :: Financial :: Investment
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: python-binance>=1.0.34
Requires-Dist: pandas>=1.5.0
Requires-Dist: numpy>=1.23.0
Requires-Dist: dask[dataframe]>=2023.1.0
Requires-Dist: pyarrow>=10.0.0
Requires-Dist: pytz>=2022.1
Requires-Dist: python-dateutil>=2.8.2
Requires-Dist: duckdb>=0.10.0
Requires-Dist: boto3>=1.26.0
Requires-Dist: s3fs>=2023.6.0
Requires-Dist: joblib>=1.2.0
Requires-Dist: pytest>=7.0 ; extra == 'dev'
Requires-Dist: pytest-cov>=4.0 ; extra == 'dev'
Requires-Dist: python-dotenv>=1.0.0 ; extra == 'dev'
Requires-Python: >=3.9
Project-URL: Homepage, https://github.com/groai-fi/datastore.shared
Project-URL: Repository, https://github.com/groai-fi/datastore.shared
Project-URL: Issues, https://github.com/groai-fi/datastore.shared/issues
Provides-Extra: dev
Description-Content-Type: text/markdown

# groai-fi-datastore-shared

[![PyPI version](https://img.shields.io/pypi/v/groai-fi-datastore-shared.svg)](https://pypi.org/project/groai-fi-datastore-shared/)
[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-yellow.svg)](LICENSE)
[![CI](https://github.com/groai-fi/datastore.shared/actions/workflows/publish.yml/badge.svg)](https://github.com/groai-fi/datastore.shared/actions)

Shared datastore utilities for **GroAI.fi** — Binance market data downloading, partitioned Parquet storage, order execution, and backtesting infrastructure.

---

## Features

- **Market Data Downloading** — Incremental Binance Spot OHLCV, USD-M Futures extended klines, and funding rate history with automatic catch-up from where you left off
- **Parquet Storage** — Hive-partitioned Parquet files (`exchange=X/symbol=Y/part.N.parquet`) powered by Dask (local) and DuckDB (S3)
- **File Locking** — Safe concurrent writes via `.write.lock` file with stale-lock detection (Local filesystem only)
- **Order Execution** — `BinanceOrder`, `BinanceClient` with spot and margin support
- **Backtesting** — `BacktestOrder`, `BacktestOrderData` for strategy simulation
- **CLI Suite** — 16 distinct CLI commands enabling both isolated local-filesystem and cloud-native S3 workflows

---

## Installation

```bash
# Using pip
pip install groai-fi-datastore-shared

# Using uv (recommended)
uv add groai-fi-datastore-shared
```

---

## Environment Variables

The following environment variables are used across modules:

| Variable | Required | Description |
|---|---|---|
| `BINANCE_API_KEY` | Yes (trading/download) | Your Binance API key |
| `BINANCE_API_SECRET` | Yes (trading/download) | Your Binance API secret |
| `S3_ENDPOINT_URL` | Yes (if S3) | Your object storage endpoint (e.g. `https://t3.storageapi.dev`) |
| `S3_BUCKET_NAME` | Yes (if S3) | The target bucket name |
| `S3_ACCESS_KEY_ID` | Yes (if S3) | S3 access key ID |
| `S3_SECRET_ACCESS_KEY` | Yes (if S3) | S3 standard secret access key |
| `BINANCE_API_KEY_TEST` | No | Testnet API key |
| `BINANCE_API_SECRET_TEST` | No | Testnet API secret |
| `SEND_MAIL_RECEIVER` | No | Email address for trade alerts |

Create a `.env` file in your project root and load it with `python-dotenv` or export variables in your shell.

---

## CLI Usage

The package exposes identical pipelines for **Local Filesystems** and **S3 Storage**.

### Local Storage Commands
Run data engineering workflows directly onto a mounted drive via Dask and PyArrow files:
```bash
# 1. Download
binance-download-price --symbol BTCUSDT --tframe 1m --path /path/to/prices_v3.parquet
# 2. Merge shards (Spot-only — DO NOT run on usdm_perp_1m_v3.parquet or usdm_perp_funding_v3.parquet)
binance-merge-parquet --exchange Binance --symbol BTCUSDT --path /path/to/prices_v3.parquet --interval_base 1m
# 3. Auto-catchup all tracked symbols (Spot-only — prices_v3.parquet / exchange=Binance)
binance-auto-update --exchange Binance --path /path/to/prices_v3.parquet --tframe 1m
# 4. View tracked symbols
binance-list-symbols --path /path/to/prices_v3.parquet
# 5. Clean / remove
binance-remove-symbol --symbol BTCUSDT --path /path/to/prices_v3.parquet --yes
# 6. Download USD-M Perpetual extended 1m klines (Parquet v3, 1m-only, exchange=BinanceUSDM)
binance-download-usdm-perp-1m --symbol BTCUSDT --path /path/to/usdm_perp_1m_v3.parquet --start-date 2020/01/01 --pause 0.15 --chunk-rows 100000
# 7. Download USD-M Perpetual funding rates (Parquet v3, exchange=BinanceUSDM)
binance-download-usdm-perp-funding --symbol BTCUSDT --path /path/to/usdm_perp_funding_v3.parquet --start-date 2019/09/01 --pause 0.1 --chunk-rows 100000
```

### S3 Storage Commands (Native)
Run structurally identical commands operating directly on object storage leveraging DuckDB HTTPFS. Local storage volumes are not required.
```bash
# S3 1. Download
binance-download-price-s3 --symbol BTCUSDT --tframe 1m --bucket my-bucket
# S3 2. Merge shards (Memory optimized via DuckDB; preserves Spot, USD-M & funding schemas)
binance-merge-parquet-s3 --exchange Binance --symbol BTCUSDT --bucket my-bucket
# e.g. compact USD-M perp 1m klines or funding rates:
# binance-merge-parquet-s3 --price-root usdm_perp_1m_v3.parquet --exchange BinanceUSDM --symbol BTCUSDT --bucket my-bucket
# binance-merge-parquet-s3 --price-root usdm_perp_funding_v3.parquet --exchange BinanceUSDM --symbol BTCUSDT --bucket my-bucket
# S3 3. Auto-catchup all tracking (Spot-only — DO NOT run on usdm_perp_1m_v3.parquet or usdm_perp_funding_v3.parquet)
# Skips delisted/inactive symbols via Binance exchangeInfo (use --skip-symbols or --include-inactive to override)
binance-auto-update-s3 --exchange Binance --bucket my-bucket --tframe 1m
# S3 4. View S3 inventory
binance-list-symbols-s3 --bucket my-bucket
# S3 5. Clean / remove
binance-remove-symbol-s3 --symbol BTCUSDT --bucket my-bucket --yes
# S3 6. Download USD-M Perpetual extended 1m klines (1m-only; supports --end-date, --pause, --chunk-rows)
binance-download-usdm-perp-1m-s3 --symbol BTCUSDT --bucket my-bucket --price-root usdm_perp_1m_v3.parquet --start-date 2020/01/01 --exchange BinanceUSDM
# S3 7. Download USD-M Perpetual funding rates (supports --end-date, --pause, --chunk-rows)
binance-download-usdm-perp-funding-s3 --symbol BTCUSDT --bucket my-bucket --funding-root usdm_perp_funding_v3.parquet --start-date 2019/09/01 --exchange BinanceUSDM
# S3 8. Auto-catchup all tracked USD-M Perpetual symbols (1m klines + funding rates, skips delisted contracts)
binance-auto-update-usdm-s3 --bucket my-bucket --dataset all
```

### USD-M Perpetual & Funding Rate Defaults

| CLI Command | Default Root / Path | Default `--exchange` | Parquet v3 File Columns (`date` is UTC index) |
|---|---|---|---|
| `binance-download-usdm-perp-1m` / `binance-download-usdm-perp-1m-s3` | `usdm_perp_1m_v3.parquet` (S3, `1m`-only) | `BinanceUSDM` | `yymm`, `open`, `high`, `low`, `close`, `volume`, `quote_asset_volume`, `number_of_trades`, `taker_buy_base_asset_volume`, `taker_buy_quote_asset_volume` |
| `binance-download-usdm-perp-funding` / `binance-download-usdm-perp-funding-s3` | `usdm_perp_funding_v3.parquet` (S3) | `BinanceUSDM` | `yymm`, `funding_rate`, `mark_price` |

---

## Python API

```python
# Market data downloading
from groai_fi_datastore_shared.Binance import BinanceMarketDataDownloader
from datetime import datetime

logger = ...  # your logger

BinanceMarketDataDownloader.catchup_price_binance(
    symbol="BTCUSDT",
    kline_tframe="1m",
    default_download_start_date=datetime(2024, 1, 1),
    price_root_dir="/data/prices_v3.parquet",
    logger=logger,
)

# Reading config from environment
from groai_fi_datastore_shared.Binance.config import BinanceConfig
config = BinanceConfig.from_env()

# Trading client
from groai_fi_datastore_shared.Binance import BinanceClient
client = BinanceClient(config)
```

---

## Development

```bash
# Clone
git clone https://github.com/groai-fi/datastore.shared.git
cd datastore.shared

# Install with dev extras
make install-dev

# Run tests
make test

# Lint
make lint
```

---

## Publishing

```bash
# Build wheel + sdist
make build

# Publish to PyPI (requires UV_PUBLISH_TOKEN in env)
make publish

# Publish to TestPyPI first
make publish-test
```

To release a new version:
1. Bump the `version` field in `pyproject.toml`
2. Commit and push
3. Tag: `git tag v0.2.0 && git push origin v0.2.0`
4. GitHub Actions will automatically build and publish to PyPI

---

## License

[Apache License 2.0](LICENSE)

