From da87a3d99b1d7df991988bcfa28a2f6cd46b261f Mon Sep 17 00:00:00 2001 From: Benjamin Adovasio Date: Mon, 9 Feb 2026 18:30:21 -0500 Subject: [PATCH] added storage.py --- storage.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 storage.py diff --git a/storage.py b/storage.py new file mode 100644 index 0000000..72e22d2 --- /dev/null +++ b/storage.py @@ -0,0 +1,64 @@ +from __future__ import annotations + +import sqlite3 +import time +from typing import Any, Dict, Optional + + +class Storage: + def __init__(self, path: str = "storage.sqlite"): + self.conn = sqlite3.connect(path) + self.conn.execute("PRAGMA journal_mode=WAL;") + self._init() + + def _init(self) -> None: + self.conn.execute( + """ + CREATE TABLE IF NOT EXISTS decisions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ts REAL NOT NULL, + symbol TEXT NOT NULL, + series_ticker TEXT NOT NULL, + market_ticker TEXT NOT NULL, + close_time TEXT NOT NULL, + strike REAL NOT NULL, + side TEXT NOT NULL, + market_prob REAL NOT NULL, + fair_prob REAL NOT NULL, + edge REAL NOT NULL, + spread REAL NOT NULL, + jump REAL NOT NULL, + reason TEXT NOT NULL + ) + """ + ) + self.conn.execute( + """ + CREATE TABLE IF NOT EXISTS orders ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ts REAL NOT NULL, + market_ticker TEXT NOT NULL, + order_id TEXT, + mode TEXT NOT NULL, + status TEXT NOT NULL, + details TEXT + ) + """ + ) + self.conn.commit() + + def log_decision(self, **row: Any) -> None: + cols = ",".join(row.keys()) + qs = ",".join(["?"] * len(row)) + self.conn.execute(f"INSERT INTO decisions ({cols}) VALUES ({qs})", list(row.values())) + self.conn.commit() + + def log_order(self, market_ticker: str, order_id: Optional[str], mode: str, status: str, details: str = "") -> None: + self.conn.execute( + "INSERT INTO orders (ts, market_ticker, order_id, mode, status, details) VALUES (?, ?, ?, ?, ?, ?)", + (time.time(), market_ticker, order_id, mode, status, details), + ) + self.conn.commit() + + def close(self) -> None: + self.conn.close()