# QuestDB Client Library

A comprehensive QuestDB client library for the Stock Bot trading platform, optimized for time-series data, market analytics, and high-performance queries.

## Features

- **Time-Series Optimized**: Built specifically for time-series data patterns
- **Dual Protocol Support**: HTTP REST API and PostgreSQL wire protocol
- **InfluxDB Line Protocol**: High-performance data ingestion
- **SQL Analytics**: Full SQL support for complex analytics
- **Schema Management**: Automatic table creation and partitioning
- **Performance Monitoring**: Query performance tracking and optimization
- **Health Monitoring**: Connection health monitoring and metrics

## Usage

```typescript
import { QuestDBClient } from '@stock-bot/questdb';

// Initialize client
const questClient = new QuestDBClient();
await questClient.connect();

// Insert market data using InfluxDB Line Protocol
await questClient.insert('ohlcv', {
  symbol: 'AAPL',
  open: 150.00,
  high: 152.00,
  low: 149.50,
  close: 151.50,
  volume: 1000000,
  timestamp: new Date()
});

// Query with SQL
const prices = await questClient.query(`
  SELECT symbol, close, timestamp 
  FROM ohlcv 
  WHERE symbol = 'AAPL' 
  AND timestamp > dateadd('d', -1, now())
  ORDER BY timestamp DESC
`);

// Time-series aggregations
const dailyStats = await questClient.aggregate('ohlcv')
  .select(['symbol', 'avg(close) as avg_price'])
  .where('symbol = ?', ['AAPL'])
  .groupBy('symbol')
  .sampleBy('1d', 'timestamp')
  .execute();
```

## Data Types

The client provides typed access to the following time-series data:

- **ohlcv**: OHLCV candlestick data
- **trades**: Individual trade executions
- **quotes**: Bid/ask quote data
- **indicators**: Technical indicator values
- **performance**: Portfolio performance metrics
- **risk_metrics**: Risk calculation results

## Configuration

Configure using environment variables:

```env
QUESTDB_HOST=localhost
QUESTDB_HTTP_PORT=9000
QUESTDB_PG_PORT=8812
QUESTDB_INFLUX_PORT=9009
```

## Time-Series Features

QuestDB excels at:

- **High-frequency data**: Millions of data points per second
- **Time-based partitioning**: Automatic partitioning by time
- **ASOF JOINs**: Time-series specific joins
- **SAMPLE BY**: Time-based aggregations
- **LATEST BY**: Get latest values by key

## Performance

The client includes performance optimizations:

- Connection pooling for HTTP and PostgreSQL protocols
- Batch insertions for high throughput
- Compressed data transfer
- Query result caching
- Automatic schema optimization

## Health Monitoring

Built-in health monitoring:

```typescript
const health = await questClient.getHealth();
console.log(health.status); // 'healthy' | 'degraded' | 'unhealthy'
```
