database
db.py's DBHandler — the singleton every page and
every client script shares, imported as db_handler from
quantstrip. One general-purpose query method, plus canonical methods for the
order/execution/position/strategy-event schema.
Direct SQL
The single entry point for arbitrary SQL. Every other method on this class is built on top of it, and it is the method the Database / SQL page calls with the editor's query text.
pandas.DataFrame for result-set queries
(SELECT), or the affected row count (int) for
INSERT/UPDATE/DELETE. Runs in autocommit mode; no
explicit commit required.run_query executes whatever SQL you pass it, with no read-only guard. Prefer the
typed methods below for writes wherever one exists — they enforce the right columns and
conflict handling for you.
Orders
Generates a strictly increasing, time-based order ID. Thread-safe and process-safe. Call once per order placed.
int.Records a canonical order. Returns order_id.
Fetches order rows with flexible filtering by any combination of the three arguments.
pandas.DataFrame.Appends a new order-status event. Append-only — it does not overwrite prior status rows.
Executions & commissions
Inserts a canonical execution/fill record from individual arguments.
Inserts a commission/fee record tied to an execution. Duplicate inserts for the same
exec_id are silently ignored (ON CONFLICT DO NOTHING).
Positions & strategy events
Inserts a position snapshot resulting from a fill — the running position and average price
after this trade, classified with an event_type (OPEN_LONG,
PARTIAL_CLOSE, FLIP, etc.). Duplicate inserts for the same
exec_id are silently ignored.
The most recent position snapshot for a strategy/symbol, required to compute the next one. See the position-state-machine example on the IBKR template page.
dict, or None if the strategy has no
history for that symbol yet.The most recent strategy_event row for a strategy (optionally scoped to a
symbol) — this is what the Trade Operations
Strategy Events tab and Add Event panel are built on.
dict, or None.Reference data
All brokers in the database.
pandas.DataFrame (broker_id, name, full_name).All strategies in the database.
pandas.DataFrame (strategy_id, name, description).insert_order/insert_execution
must already exist in these reference tables — create them from the
Trade Operations Static Data tab first.