Tracked orders — persist strategy positions in SQLite
Backend:
- New orders table (symbol, name, legs_json, entry_cost, created_at,
closed_at, status, note) sharing the snapshots.db
- Endpoints:
POST /api/orders save a position
GET /api/orders?symbol= list (most recent first)
GET /api/orders/:id single
PATCH /api/orders/:id { status:'open'|'closed', note? }
DELETE /api/orders/:id remove
Strategy page:
- "Save to Tracker" button posts the currently-active legs (with name,
net cost, side, qty, entry, IV, lock) as an order
Tracker page:
- New "Tracked Orders" section above the IV history charts: lists each
saved position for the current symbol with strategy name, leg summary,
entered cost, current value, P/L $ and %, opened date, status, and
Close/Reopen + Remove actions. Live P/L uses /api/chain mids for each
unique leg expiry.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
134
backend/src/db/orders.ts
Normal file
134
backend/src/db/orders.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* Tracked-order persistence — strategy positions the user wants to follow
|
||||
* over time. Single source of truth across browsers/devices.
|
||||
*
|
||||
* Schema:
|
||||
* orders(id, symbol, name, legs_json, entry_cost, created_at, closed_at, status, note)
|
||||
* legs_json holds the leg array exactly as the Strategy page knows it
|
||||
* (id, symbol, expiry, type, strike, side, qty, entryPrice, iv, locked).
|
||||
*/
|
||||
import { getDb } from "./snapshots.js";
|
||||
|
||||
const db = getDb();
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
symbol TEXT NOT NULL,
|
||||
name TEXT,
|
||||
legs_json TEXT NOT NULL,
|
||||
entry_cost REAL,
|
||||
created_at TEXT NOT NULL,
|
||||
closed_at TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'open',
|
||||
note TEXT
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_symbol_created
|
||||
ON orders(symbol, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_status
|
||||
ON orders(status);
|
||||
`);
|
||||
|
||||
export type OrderLeg = {
|
||||
symbol: string;
|
||||
expiry: string;
|
||||
type: "call" | "put";
|
||||
strike: number;
|
||||
side: "long" | "short";
|
||||
qty: number;
|
||||
entryPrice: number;
|
||||
iv: number;
|
||||
locked?: boolean;
|
||||
};
|
||||
|
||||
export type OrderRow = {
|
||||
id: number;
|
||||
symbol: string;
|
||||
name: string | null;
|
||||
legs_json: string;
|
||||
entry_cost: number | null;
|
||||
created_at: string;
|
||||
closed_at: string | null;
|
||||
status: "open" | "closed";
|
||||
note: string | null;
|
||||
};
|
||||
|
||||
export type Order = Omit<OrderRow, "legs_json"> & { legs: OrderLeg[] };
|
||||
|
||||
const stmtInsert = db.prepare(`
|
||||
INSERT INTO orders (symbol, name, legs_json, entry_cost, created_at, status, note)
|
||||
VALUES (?, ?, ?, ?, ?, 'open', ?)
|
||||
`);
|
||||
const stmtListBySymbol = db.prepare(`
|
||||
SELECT * FROM orders WHERE symbol = ? ORDER BY created_at DESC LIMIT ?
|
||||
`);
|
||||
const stmtListAll = db.prepare(`
|
||||
SELECT * FROM orders ORDER BY created_at DESC LIMIT ?
|
||||
`);
|
||||
const stmtGet = db.prepare(`SELECT * FROM orders WHERE id = ?`);
|
||||
const stmtDelete = db.prepare(`DELETE FROM orders WHERE id = ?`);
|
||||
const stmtClose = db.prepare(`
|
||||
UPDATE orders SET status = 'closed', closed_at = ? WHERE id = ?
|
||||
`);
|
||||
const stmtReopen = db.prepare(`
|
||||
UPDATE orders SET status = 'open', closed_at = NULL WHERE id = ?
|
||||
`);
|
||||
const stmtNote = db.prepare(`UPDATE orders SET note = ? WHERE id = ?`);
|
||||
|
||||
function rowToOrder(row: OrderRow): Order {
|
||||
let legs: OrderLeg[] = [];
|
||||
try { legs = JSON.parse(row.legs_json) as OrderLeg[]; } catch { /* ignore */ }
|
||||
const { legs_json: _legs, ...rest } = row;
|
||||
return { ...rest, legs };
|
||||
}
|
||||
|
||||
export function saveOrder(
|
||||
symbol: string,
|
||||
name: string | null,
|
||||
legs: OrderLeg[],
|
||||
entryCost: number | null,
|
||||
note: string | null = null
|
||||
): Order {
|
||||
const info = stmtInsert.run(
|
||||
symbol,
|
||||
name,
|
||||
JSON.stringify(legs),
|
||||
entryCost,
|
||||
new Date().toISOString(),
|
||||
note
|
||||
);
|
||||
const row = stmtGet.get(info.lastInsertRowid) as OrderRow;
|
||||
return rowToOrder(row);
|
||||
}
|
||||
|
||||
export function listOrders(symbol?: string, limit = 200): Order[] {
|
||||
const rows = (symbol
|
||||
? (stmtListBySymbol.all(symbol, limit) as OrderRow[])
|
||||
: (stmtListAll.all(limit) as OrderRow[]));
|
||||
return rows.map(rowToOrder);
|
||||
}
|
||||
|
||||
export function getOrder(id: number): Order | null {
|
||||
const row = stmtGet.get(id) as OrderRow | undefined;
|
||||
return row ? rowToOrder(row) : null;
|
||||
}
|
||||
|
||||
export function deleteOrder(id: number): boolean {
|
||||
const info = stmtDelete.run(id);
|
||||
return info.changes > 0;
|
||||
}
|
||||
|
||||
export function closeOrder(id: number): Order | null {
|
||||
stmtClose.run(new Date().toISOString(), id);
|
||||
return getOrder(id);
|
||||
}
|
||||
|
||||
export function reopenOrder(id: number): Order | null {
|
||||
stmtReopen.run(id);
|
||||
return getOrder(id);
|
||||
}
|
||||
|
||||
export function updateOrderNote(id: number, note: string | null): Order | null {
|
||||
stmtNote.run(note, id);
|
||||
return getOrder(id);
|
||||
}
|
||||
@@ -22,6 +22,16 @@ import {
|
||||
getSnapshots,
|
||||
getLatestSnapshot,
|
||||
} from "../db/snapshots.js";
|
||||
import {
|
||||
saveOrder,
|
||||
listOrders,
|
||||
getOrder,
|
||||
deleteOrder,
|
||||
closeOrder,
|
||||
reopenOrder,
|
||||
updateOrderNote,
|
||||
type OrderLeg,
|
||||
} from "../db/orders.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
@@ -425,3 +435,103 @@ optionsRouter.post("/refresh", async (c) => {
|
||||
return c.json(fail(msg), 502);
|
||||
}
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tracked orders — strategy positions persisted in SQLite
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function validLegs(raw: unknown): OrderLeg[] | null {
|
||||
if (!Array.isArray(raw)) return null;
|
||||
const out: OrderLeg[] = [];
|
||||
for (const l of raw) {
|
||||
if (!l || typeof l !== "object") return null;
|
||||
const o = l as Record<string, unknown>;
|
||||
const strike = Number(o.strike);
|
||||
const qty = Math.max(1, Math.round(Number(o.qty) || 1));
|
||||
if (!Number.isFinite(strike) || strike <= 0) return null;
|
||||
if (o.type !== "call" && o.type !== "put") return null;
|
||||
if (typeof o.symbol !== "string" || !o.symbol) return null;
|
||||
if (typeof o.expiry !== "string" || !o.expiry) return null;
|
||||
out.push({
|
||||
symbol: o.symbol,
|
||||
expiry: o.expiry,
|
||||
type: o.type,
|
||||
strike,
|
||||
side: o.side === "short" ? "short" : "long",
|
||||
qty,
|
||||
entryPrice: Number(o.entryPrice) || 0,
|
||||
iv: Number(o.iv) || 0,
|
||||
locked: o.locked === true,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// POST /api/orders { symbol, name?, legs:[...], entryCost?, note? }
|
||||
optionsRouter.post("/orders", async (c) => {
|
||||
let body: any;
|
||||
try { body = await c.req.json(); } catch { return c.json(fail("Invalid JSON body"), 400); }
|
||||
const symbol = typeof body.symbol === "string" ? body.symbol.toUpperCase().trim() : "";
|
||||
const legs = validLegs(body.legs);
|
||||
if (!symbol) return c.json(fail("Missing symbol"), 400);
|
||||
if (!legs || legs.length === 0) return c.json(fail("legs must be a non-empty array of {symbol,expiry,type,strike,side,qty,entryPrice,iv}"), 400);
|
||||
const name = typeof body.name === "string" ? body.name.slice(0, 80) : null;
|
||||
const note = typeof body.note === "string" ? body.note.slice(0, 500) : null;
|
||||
const entryCost = (typeof body.entryCost === "number" && Number.isFinite(body.entryCost)) ? body.entryCost : null;
|
||||
try {
|
||||
const order = saveOrder(symbol, name, legs, entryCost, note);
|
||||
return c.json(ok(order), 201);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error("[POST /api/orders]", msg);
|
||||
return c.json(fail(msg), 500);
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/orders?symbol=...&limit=...
|
||||
optionsRouter.get("/orders", (c) => {
|
||||
const sym = c.req.query("symbol")?.toUpperCase();
|
||||
const limit = Math.min(parseInt(c.req.query("limit") ?? "200", 10) || 200, 1000);
|
||||
try {
|
||||
const orders = listOrders(sym, limit);
|
||||
return c.json(ok({ count: orders.length, orders }));
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
return c.json(fail(msg), 500);
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/orders/:id
|
||||
optionsRouter.get("/orders/:id", (c) => {
|
||||
const id = Number(c.req.param("id"));
|
||||
if (!Number.isFinite(id)) return c.json(fail("Invalid id"), 400);
|
||||
const o = getOrder(id);
|
||||
return o ? c.json(ok(o)) : c.json(fail("Order not found"), 404);
|
||||
});
|
||||
|
||||
// DELETE /api/orders/:id
|
||||
optionsRouter.delete("/orders/:id", (c) => {
|
||||
const id = Number(c.req.param("id"));
|
||||
if (!Number.isFinite(id)) return c.json(fail("Invalid id"), 400);
|
||||
const removed = deleteOrder(id);
|
||||
return removed ? c.json(ok({ id, removed: true })) : c.json(fail("Order not found"), 404);
|
||||
});
|
||||
|
||||
// PATCH /api/orders/:id { status:'open'|'closed', note?:string }
|
||||
optionsRouter.patch("/orders/:id", async (c) => {
|
||||
const id = Number(c.req.param("id"));
|
||||
if (!Number.isFinite(id)) return c.json(fail("Invalid id"), 400);
|
||||
let body: any = {};
|
||||
try { body = await c.req.json(); } catch { /* allow empty patch */ }
|
||||
try {
|
||||
let order = getOrder(id);
|
||||
if (!order) return c.json(fail("Order not found"), 404);
|
||||
if (body.status === "closed" && order.status !== "closed") order = closeOrder(id) ?? order;
|
||||
else if (body.status === "open" && order.status !== "open") order = reopenOrder(id) ?? order;
|
||||
if (typeof body.note === "string") order = updateOrderNote(id, body.note.slice(0, 500)) ?? order;
|
||||
return c.json(ok(order));
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
return c.json(fail(msg), 500);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user