FastAPI application that ingests CFTC Commitments of Traders data into SQLite and exposes it via a REST API with analytics endpoints (screener, percentile rank, concentration). Includes CLI for historical and weekly data ingestion, Docker setup, and a frontend. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
27 lines
691 B
Python
27 lines
691 B
Python
import os
|
|
import sqlite3
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
|
|
DB_PATH = Path(os.environ.get("DB_PATH", Path(__file__).parent.parent / "data" / "cot.db"))
|
|
|
|
|
|
@contextmanager
|
|
def get_db():
|
|
conn = sqlite3.connect(str(DB_PATH), check_same_thread=False)
|
|
conn.row_factory = sqlite3.Row
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
try:
|
|
yield conn
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def init_db():
|
|
schema_path = Path(__file__).parent.parent / "schema.sql"
|
|
with get_db() as conn:
|
|
conn.executescript(schema_path.read_text())
|
|
conn.commit()
|
|
print(f"Database initialized at {DB_PATH}")
|