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>
887 lines
35 KiB
HTML
887 lines
35 KiB
HTML
<!doctype html>
|
|
<html lang="en" data-bs-theme="dark">
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"/>
|
|
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
|
|
<title>Options Metrics Tracker</title>
|
|
|
|
<link rel="stylesheet" href="/assets/tabler.min.css"/>
|
|
<link rel="stylesheet" href="/assets/tabler-vendors.min.css"/>
|
|
<script src="/assets/apexcharts.min.js"></script>
|
|
<script src="/assets/viewstate-store.js"></script>
|
|
<script src="/assets/alpine.min.js" defer></script>
|
|
<script src="/assets/tabler.min.js" defer></script>
|
|
|
|
<style>
|
|
:root {
|
|
--chart-bg: #1a1c23;
|
|
--chart-grid: rgba(255, 255, 255, 0.06);
|
|
--chart-label: #8b95a1;
|
|
}
|
|
|
|
body {
|
|
background-color: #1a1c23;
|
|
}
|
|
|
|
.navbar-vertical.navbar-dark {
|
|
background-color: #14161c;
|
|
border-right: 1px solid rgba(255, 255, 255, 0.07);
|
|
}
|
|
|
|
.chart-card {
|
|
background-color: var(--chart-bg);
|
|
border: 1px solid rgba(255, 255, 255, 0.07);
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.apexcharts-canvas {
|
|
background: transparent !important;
|
|
}
|
|
|
|
.table-snapshots th {
|
|
font-size: 0.75rem;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.06em;
|
|
color: var(--chart-label);
|
|
font-weight: 600;
|
|
}
|
|
|
|
.toolbar-bar {
|
|
background-color: #14161c;
|
|
border-bottom: 1px solid rgba(255, 255, 255, 0.07);
|
|
}
|
|
|
|
.nav-link.active-page {
|
|
color: #4fc3f7 !important;
|
|
background-color: rgba(79, 195, 247, 0.1) !important;
|
|
border-radius: 6px;
|
|
}
|
|
|
|
.badge-fat-tails {
|
|
background-color: #f59f00;
|
|
color: #1a1c23;
|
|
}
|
|
|
|
.badge-thin-tails {
|
|
background-color: #d63939;
|
|
color: #fff;
|
|
}
|
|
|
|
.badge-normal {
|
|
background-color: rgba(255, 255, 255, 0.1);
|
|
color: #8b95a1;
|
|
}
|
|
|
|
.snapshot-count-badge {
|
|
font-size: 0.7rem;
|
|
padding: 2px 8px;
|
|
border-radius: 12px;
|
|
background-color: rgba(79, 195, 247, 0.15);
|
|
color: #4fc3f7;
|
|
border: 1px solid rgba(79, 195, 247, 0.3);
|
|
font-weight: 600;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
[x-cloak] { display: none !important; }
|
|
</style>
|
|
</head>
|
|
|
|
<body class="antialiased" x-data="trackerApp()" x-init="init()">
|
|
<div class="wrapper">
|
|
|
|
<!-- Vertical Sidebar -->
|
|
<aside class="navbar navbar-vertical navbar-expand-lg" data-bs-theme="dark">
|
|
<div class="container-fluid">
|
|
|
|
<!-- Toggle for mobile -->
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#sidebar-menu">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
|
|
<!-- Logo -->
|
|
<h1 class="navbar-brand navbar-brand-autodark">
|
|
<a href="index.html" class="text-decoration-none d-flex align-items-center gap-2">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" fill="none"
|
|
stroke="#4fc3f7" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<polyline points="22 7 13.5 15.5 8.5 10.5 2 17"></polyline>
|
|
<polyline points="16 7 22 7 22 13"></polyline>
|
|
</svg>
|
|
<span class="fw-bold" style="color:#4fc3f7; font-size:1rem; letter-spacing:0.04em;">OPTIONS PRICER</span>
|
|
</a>
|
|
</h1>
|
|
|
|
<!-- Nav links -->
|
|
<div class="collapse navbar-collapse" id="sidebar-menu">
|
|
<ul class="navbar-nav pt-lg-3">
|
|
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="index.html">
|
|
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect>
|
|
<rect x="14" y="14" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect>
|
|
</svg>
|
|
</span>
|
|
<span class="nav-link-title">Dashboard</span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="chain.html">
|
|
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<line x1="8" y1="6" x2="21" y2="6"></line><line x1="8" y1="12" x2="21" y2="12"></line>
|
|
<line x1="8" y1="18" x2="21" y2="18"></line>
|
|
<line x1="3" y1="6" x2="3.01" y2="6"></line><line x1="3" y1="12" x2="3.01" y2="12"></line>
|
|
<line x1="3" y1="18" x2="3.01" y2="18"></line>
|
|
</svg>
|
|
</span>
|
|
<span class="nav-link-title">Options Chain</span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="surface.html">
|
|
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<path d="M3 3l7 7 4-4 7 7"></path>
|
|
<path d="M21 17v4h-4"></path>
|
|
</svg>
|
|
</span>
|
|
<span class="nav-link-title">Vol Surface</span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="strategy.html">
|
|
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<path d="M4 19l4 -6l4 2l4 -8l4 5"></path><path d="M4 4v16h16"></path>
|
|
</svg>
|
|
</span>
|
|
<span class="nav-link-title">Strategy P/L</span>
|
|
</a>
|
|
</li>
|
|
|
|
<li class="nav-item">
|
|
<a class="nav-link active-page" href="tracker.html">
|
|
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<circle cx="12" cy="12" r="10"></circle>
|
|
<polyline points="12 6 12 12 16 14"></polyline>
|
|
</svg>
|
|
</span>
|
|
<span class="nav-link-title">Tracker</span>
|
|
</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</aside>
|
|
|
|
<!-- Main content -->
|
|
<div class="page-wrapper">
|
|
|
|
<!-- Toolbar -->
|
|
<div class="toolbar-bar py-2 px-3 mb-3">
|
|
<div class="container-xl">
|
|
<div class="d-flex flex-wrap align-items-center gap-3">
|
|
|
|
<!-- Symbol input -->
|
|
<div class="input-group input-group-sm" style="max-width:140px;">
|
|
<span class="input-group-text bg-dark border-secondary text-muted">Symbol</span>
|
|
<input
|
|
type="text"
|
|
class="form-control bg-dark border-secondary text-white"
|
|
placeholder="SPY"
|
|
x-model="symbol"
|
|
@keydown.enter="fetchExpirations()"
|
|
style="text-transform:uppercase;"
|
|
/>
|
|
</div>
|
|
|
|
<!-- Expiry select -->
|
|
<div class="input-group input-group-sm" style="max-width:200px;">
|
|
<span class="input-group-text bg-dark border-secondary text-muted">Expiry</span>
|
|
<select
|
|
class="form-select bg-dark border-secondary text-white"
|
|
x-model="expiry"
|
|
@change="renderCharts()"
|
|
>
|
|
<option value="">All expirations</option>
|
|
<template x-for="exp in expirations" :key="exp">
|
|
<option :value="exp" x-text="exp"></option>
|
|
</template>
|
|
</select>
|
|
</div>
|
|
|
|
<!-- Load button -->
|
|
<button
|
|
class="btn btn-sm btn-primary"
|
|
@click="loadHistory()"
|
|
:disabled="loading"
|
|
>
|
|
<span x-show="loading" class="spinner-border spinner-border-sm me-1" role="status"></span>
|
|
<span x-show="!loading">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24"
|
|
fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"
|
|
class="me-1" style="vertical-align:-2px;">
|
|
<polyline points="1 4 1 10 7 10"></polyline>
|
|
<path d="M3.51 15a9 9 0 1 0 .49-3.51"></path>
|
|
</svg>
|
|
Load History
|
|
</span>
|
|
<span x-show="loading">Loading…</span>
|
|
</button>
|
|
|
|
<!-- Snapshot count badge -->
|
|
<span class="snapshot-count-badge" x-text="filteredSnapshots.length + ' snapshots'"></span>
|
|
|
|
<!-- Error message -->
|
|
<span x-show="error" x-cloak class="text-danger small ms-2" x-text="error"></span>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Page body -->
|
|
<div class="page-body">
|
|
<div class="container-xl">
|
|
|
|
<!-- Tracked Orders (persisted strategy positions) -->
|
|
<div class="card mb-3" x-show="orders.length > 0 || ordersLoading" x-cloak style="background:#161824; border:1px solid #2d3045;">
|
|
<div class="card-header d-flex align-items-center justify-content-between" style="border-bottom:1px solid #2d3045;">
|
|
<h3 class="card-title text-white mb-0">
|
|
Tracked Orders
|
|
<span class="text-secondary small fw-normal" x-show="symbol">— <span x-text="symbol"></span></span>
|
|
</h3>
|
|
<div class="d-flex align-items-center gap-2">
|
|
<span x-show="ordersLoading" class="spinner-border spinner-border-sm text-secondary"></span>
|
|
<button class="btn btn-sm btn-outline-secondary" @click="loadOrders()" :disabled="ordersLoading">Refresh</button>
|
|
<span class="text-secondary small" x-text="orders.length + ' order' + (orders.length===1?'':'s')"></span>
|
|
</div>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-sm table-hover mb-0" style="color:#d0d5e0;">
|
|
<thead style="background:#1a1c2e;color:#8b95a7;font-size:.72rem;text-transform:uppercase;letter-spacing:.05em;">
|
|
<tr>
|
|
<th>Strategy</th>
|
|
<th>Legs</th>
|
|
<th class="text-end">Entered</th>
|
|
<th class="text-end">Current</th>
|
|
<th class="text-end">P/L $</th>
|
|
<th class="text-end">P/L %</th>
|
|
<th>Opened</th>
|
|
<th>Status</th>
|
|
<th></th><th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<template x-for="o in orders" :key="o.id">
|
|
<tr :style="o.status === 'closed' ? 'opacity:.55' : ''" style="font-size:.85rem;">
|
|
<td><span class="badge bg-purple-lt" x-text="o.name || ('Custom (' + o.legs.length + ' legs)')"></span></td>
|
|
<td class="text-secondary" style="font-family:'JetBrains Mono',monospace;font-size:.75rem;" x-text="legsSummary(o)"></td>
|
|
<td class="text-end" style="font-family:'JetBrains Mono',monospace;" x-text="fmtMoney(orderPL(o).entered)"></td>
|
|
<td class="text-end" style="font-family:'JetBrains Mono',monospace;" x-text="fmtMoney(orderPL(o).value)"></td>
|
|
<td class="text-end" style="font-family:'JetBrains Mono',monospace;" :class="orderPL(o).pl >= 0 ? 'text-success' : 'text-danger'" x-text="fmtMoney(orderPL(o).pl)"></td>
|
|
<td class="text-end" style="font-family:'JetBrains Mono',monospace;" :class="orderPL(o).pl >= 0 ? 'text-success' : 'text-danger'" x-text="orderPL(o).plPct.toFixed(1) + '%'"></td>
|
|
<td class="small text-secondary" x-text="(o.created_at||'').slice(0,10)"></td>
|
|
<td><span class="badge" :class="o.status === 'open' ? 'bg-success-lt text-success' : 'bg-secondary-lt text-secondary'" x-text="o.status"></span></td>
|
|
<td><button class="btn btn-sm" :class="o.status === 'open' ? 'btn-outline-warning' : 'btn-outline-success'" @click="toggleCloseOrder(o)" x-text="o.status === 'open' ? 'Close' : 'Reopen'"></button></td>
|
|
<td><button class="btn btn-sm btn-ghost-danger" @click="removeOrder(o.id)" aria-label="Remove order">✕</button></td>
|
|
</tr>
|
|
</template>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Empty state -->
|
|
<div x-show="!loading && filteredSnapshots.length === 0" x-cloak class="text-center py-5">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24"
|
|
fill="none" stroke="#8b95a1" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="mb-3">
|
|
<path d="M3 3l18 18"></path>
|
|
<path d="M21 21l-1.5-1.5M16.5 16.5L12 12M12 12L7.5 7.5M7.5 7.5L3 3"></path>
|
|
<circle cx="12" cy="12" r="10"></circle>
|
|
</svg>
|
|
<p class="text-muted">No snapshot data. Enter a symbol and click Load History.</p>
|
|
</div>
|
|
|
|
<!-- Charts row -->
|
|
<div x-show="filteredSnapshots.length > 0" x-cloak>
|
|
<div class="row g-3 mb-3">
|
|
|
|
<!-- Chart 1: ATM IV -->
|
|
<div class="col-12">
|
|
<div class="chart-card p-3">
|
|
<div class="d-flex align-items-center mb-2 gap-2">
|
|
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#00d4ff;"></span>
|
|
<span class="fw-semibold text-white" style="font-size:0.9rem;">ATM Implied Volatility History</span>
|
|
</div>
|
|
<div id="chart-atm-iv" style="min-height:220px;"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Chart 2: RR25 + Chart 3: Fly25 side by side on wider screens -->
|
|
<div class="col-12 col-lg-6">
|
|
<div class="chart-card p-3">
|
|
<div class="d-flex align-items-center mb-2 gap-2">
|
|
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#2fb344;"></span>
|
|
<span class="fw-semibold text-white" style="font-size:0.9rem;">25Δ Risk Reversal History</span>
|
|
</div>
|
|
<div id="chart-rr25" style="min-height:220px;"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-12 col-lg-6">
|
|
<div class="chart-card p-3">
|
|
<div class="d-flex align-items-center mb-2 gap-2">
|
|
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#f59f00;"></span>
|
|
<span class="fw-semibold text-white" style="font-size:0.9rem;">25Δ Butterfly History</span>
|
|
</div>
|
|
<div id="chart-fly25" style="min-height:220px;"></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<!-- History table -->
|
|
<div class="card" style="background-color:#14161c; border:1px solid rgba(255,255,255,0.07);">
|
|
<div class="card-header d-flex align-items-center justify-content-between"
|
|
style="border-bottom:1px solid rgba(255,255,255,0.07);">
|
|
<h3 class="card-title text-white mb-0" style="font-size:0.9rem; font-weight:600;">
|
|
Snapshot History
|
|
<span class="ms-2 text-muted" style="font-size:0.75rem; font-weight:400;">
|
|
(most recent first, showing up to 20)
|
|
</span>
|
|
</h3>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-sm table-hover table-snapshots mb-0"
|
|
style="color:#c8d3e0;">
|
|
<thead>
|
|
<tr>
|
|
<th>Timestamp</th>
|
|
<th>Expiry</th>
|
|
<th>Spot</th>
|
|
<th>ATM IV</th>
|
|
<th>RR25</th>
|
|
<th>RR10</th>
|
|
<th>Fly25</th>
|
|
<th>Butterfly Signal</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<template x-for="(snap, idx) in tableRows" :key="snap.timestamp + idx">
|
|
<tr>
|
|
<td class="text-muted" style="white-space:nowrap; font-size:0.8rem;"
|
|
x-text="formatTimestamp(snap.timestamp)"></td>
|
|
<td x-text="snap.expiry || '—'"></td>
|
|
<td x-text="snap.spot ? snap.spot.toFixed(2) : '—'"></td>
|
|
<td>
|
|
<span x-text="snap.atmIv ? (snap.atmIv * 100).toFixed(2) + '%' : '—'"
|
|
style="color:#4fc3f7; font-variant-numeric:tabular-nums;"></span>
|
|
</td>
|
|
<td>
|
|
<span
|
|
:style="{ color: snap.rr25 > 0 ? '#2fb344' : snap.rr25 < 0 ? '#d63939' : '#8b95a1' }"
|
|
style="font-variant-numeric:tabular-nums;"
|
|
x-text="snap.rr25 != null ? snap.rr25.toFixed(4) : '—'">
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<span
|
|
:style="{ color: snap.rr10 > 0 ? '#2fb344' : snap.rr10 < 0 ? '#d63939' : '#8b95a1' }"
|
|
style="font-variant-numeric:tabular-nums;"
|
|
x-text="snap.rr10 != null ? snap.rr10.toFixed(4) : '—'">
|
|
</span>
|
|
</td>
|
|
<td style="font-variant-numeric:tabular-nums;"
|
|
x-text="snap.fly25 != null ? snap.fly25.toFixed(4) : '—'"></td>
|
|
<td>
|
|
<template x-if="snap.fly25 > 0.002">
|
|
<span class="badge badge-fat-tails">Fat Tails</span>
|
|
</template>
|
|
<template x-if="snap.fly25 <= 0.002 && snap.fly25 < -0.002">
|
|
<span class="badge badge-thin-tails">Thin Tails</span>
|
|
</template>
|
|
<template x-if="snap.fly25 != null && snap.fly25 >= -0.002 && snap.fly25 <= 0.002">
|
|
<span class="badge badge-normal">Normal</span>
|
|
</template>
|
|
<template x-if="snap.fly25 == null">
|
|
<span class="text-muted">—</span>
|
|
</template>
|
|
</td>
|
|
</tr>
|
|
</template>
|
|
<tr x-show="tableRows.length === 0">
|
|
<td colspan="8" class="text-center text-muted py-3">No data available</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Alpine + ApexCharts logic -->
|
|
<script>
|
|
const CHART_DEFAULTS = {
|
|
background: 'transparent',
|
|
foreColor: '#8b95a1',
|
|
fontFamily: 'inherit',
|
|
};
|
|
|
|
const AXIS_STYLE = {
|
|
borderColor: 'rgba(255,255,255,0.06)',
|
|
labels: {
|
|
style: { colors: '#8b95a1', fontSize: '11px' }
|
|
},
|
|
axisBorder: { show: false },
|
|
axisTicks: { show: false },
|
|
};
|
|
|
|
const TOOLTIP_STYLE = {
|
|
theme: 'dark',
|
|
};
|
|
|
|
const GRID_STYLE = {
|
|
borderColor: 'rgba(255,255,255,0.06)',
|
|
strokeDashArray: 4,
|
|
};
|
|
|
|
// ── ATM IV chart ──────────────────────────────────────────────────────────
|
|
function buildAtmIvChart(data) {
|
|
const sorted = [...data].sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
|
|
const series = sorted.map(s => ({
|
|
x: new Date(s.timestamp).getTime(),
|
|
y: s.atmIv != null ? parseFloat((s.atmIv * 100).toFixed(4)) : null,
|
|
})).filter(p => p.y !== null);
|
|
|
|
const options = {
|
|
chart: {
|
|
type: 'area',
|
|
height: 220,
|
|
toolbar: { show: false },
|
|
...CHART_DEFAULTS,
|
|
animations: { enabled: true, speed: 400 },
|
|
zoom: { enabled: false },
|
|
},
|
|
series: [{ name: 'ATM IV', data: series }],
|
|
stroke: { curve: 'smooth', width: 2, colors: ['#00d4ff'] },
|
|
fill: {
|
|
type: 'gradient',
|
|
gradient: {
|
|
shadeIntensity: 1,
|
|
opacityFrom: 0.2,
|
|
opacityTo: 0.0,
|
|
stops: [0, 100],
|
|
colorStops: [{
|
|
offset: 0,
|
|
color: '#00d4ff',
|
|
opacity: 0.2,
|
|
}, {
|
|
offset: 100,
|
|
color: '#00d4ff',
|
|
opacity: 0,
|
|
}],
|
|
},
|
|
},
|
|
colors: ['#00d4ff'],
|
|
xaxis: {
|
|
type: 'datetime',
|
|
...AXIS_STYLE,
|
|
},
|
|
yaxis: {
|
|
...AXIS_STYLE,
|
|
labels: {
|
|
style: { colors: '#8b95a1', fontSize: '11px' },
|
|
formatter: v => v != null ? v.toFixed(1) + '%' : '',
|
|
},
|
|
title: { text: 'IV (%)', style: { color: '#8b95a1', fontSize: '11px', fontWeight: 400 } },
|
|
},
|
|
grid: GRID_STYLE,
|
|
tooltip: {
|
|
...TOOLTIP_STYLE,
|
|
x: { format: 'dd MMM yy HH:mm' },
|
|
y: { formatter: v => v != null ? v.toFixed(2) + '%' : 'N/A' },
|
|
},
|
|
markers: { size: series.length <= 15 ? 4 : 0, colors: ['#00d4ff'], strokeColors: '#1a1c23', strokeWidth: 2 },
|
|
dataLabels: { enabled: false },
|
|
};
|
|
|
|
return options;
|
|
}
|
|
|
|
// ── RR25 chart ────────────────────────────────────────────────────────────
|
|
function buildRr25Chart(data) {
|
|
const sorted = [...data].sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
|
|
const series = sorted.map(s => ({
|
|
x: new Date(s.timestamp).getTime(),
|
|
y: s.rr25 != null ? parseFloat(s.rr25.toFixed(6)) : null,
|
|
})).filter(p => p.y !== null);
|
|
|
|
const options = {
|
|
chart: {
|
|
type: 'line',
|
|
height: 220,
|
|
toolbar: { show: false },
|
|
...CHART_DEFAULTS,
|
|
animations: { enabled: true, speed: 400 },
|
|
zoom: { enabled: false },
|
|
},
|
|
series: [{ name: 'RR25', data: series }],
|
|
stroke: { curve: 'smooth', width: 2, colors: ['#2fb344'] },
|
|
colors: ['#2fb344'],
|
|
xaxis: {
|
|
type: 'datetime',
|
|
...AXIS_STYLE,
|
|
},
|
|
yaxis: {
|
|
...AXIS_STYLE,
|
|
labels: {
|
|
style: { colors: '#8b95a1', fontSize: '11px' },
|
|
formatter: v => v != null ? v.toFixed(4) : '',
|
|
},
|
|
title: { text: 'RR25', style: { color: '#8b95a1', fontSize: '11px', fontWeight: 400 } },
|
|
},
|
|
grid: GRID_STYLE,
|
|
annotations: {
|
|
yaxis: [{
|
|
y: 0,
|
|
borderColor: 'rgba(255, 255, 255, 0.25)',
|
|
strokeDashArray: 5,
|
|
label: {
|
|
text: 'Zero',
|
|
style: {
|
|
color: '#8b95a1',
|
|
background: 'transparent',
|
|
fontSize: '10px',
|
|
},
|
|
position: 'right',
|
|
},
|
|
}],
|
|
},
|
|
tooltip: {
|
|
...TOOLTIP_STYLE,
|
|
x: { format: 'dd MMM yy HH:mm' },
|
|
y: { formatter: v => v != null ? v.toFixed(4) : 'N/A' },
|
|
},
|
|
markers: { size: series.length <= 15 ? 4 : 0, colors: ['#2fb344'], strokeColors: '#1a1c23', strokeWidth: 2 },
|
|
dataLabels: { enabled: false },
|
|
};
|
|
|
|
return options;
|
|
}
|
|
|
|
// ── Fly25 chart ───────────────────────────────────────────────────────────
|
|
function buildFly25Chart(data) {
|
|
const sorted = [...data].sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
|
|
const filtered = sorted.filter(s => s.fly25 != null);
|
|
|
|
const values = filtered.map(s => parseFloat(s.fly25.toFixed(6)));
|
|
const timestamps = filtered.map(s => new Date(s.timestamp).getTime());
|
|
const barColors = values.map(v => v > 0 ? '#f59f00' : '#6c757d');
|
|
|
|
const options = {
|
|
chart: {
|
|
type: 'bar',
|
|
height: 220,
|
|
toolbar: { show: false },
|
|
...CHART_DEFAULTS,
|
|
animations: { enabled: true, speed: 400 },
|
|
zoom: { enabled: false },
|
|
},
|
|
series: [{
|
|
name: 'Fly25',
|
|
data: filtered.map((s, i) => ({
|
|
x: timestamps[i],
|
|
y: values[i],
|
|
})),
|
|
}],
|
|
plotOptions: {
|
|
bar: {
|
|
distributed: true,
|
|
borderRadius: 2,
|
|
columnWidth: filtered.length > 30 ? '90%' : '60%',
|
|
},
|
|
},
|
|
colors: barColors,
|
|
legend: { show: false },
|
|
xaxis: {
|
|
type: 'datetime',
|
|
...AXIS_STYLE,
|
|
},
|
|
yaxis: {
|
|
...AXIS_STYLE,
|
|
labels: {
|
|
style: { colors: '#8b95a1', fontSize: '11px' },
|
|
formatter: v => v != null ? v.toFixed(4) : '',
|
|
},
|
|
title: { text: 'Fly25', style: { color: '#8b95a1', fontSize: '11px', fontWeight: 400 } },
|
|
},
|
|
grid: GRID_STYLE,
|
|
annotations: {
|
|
yaxis: [{
|
|
y: 0,
|
|
borderColor: 'rgba(255, 255, 255, 0.2)',
|
|
strokeDashArray: 4,
|
|
}],
|
|
},
|
|
tooltip: {
|
|
...TOOLTIP_STYLE,
|
|
x: { format: 'dd MMM yy HH:mm' },
|
|
y: { formatter: v => v != null ? v.toFixed(4) : 'N/A' },
|
|
},
|
|
dataLabels: { enabled: false },
|
|
};
|
|
|
|
return options;
|
|
}
|
|
|
|
// ── Alpine component ──────────────────────────────────────────────────────
|
|
function trackerApp() {
|
|
return {
|
|
symbol: 'SPY',
|
|
expiry: '',
|
|
expirations: [],
|
|
snapshots: [],
|
|
loading: false,
|
|
error: '',
|
|
|
|
orders: [],
|
|
ordersLoading: false,
|
|
ordersChain: {}, // "SYMBOL@EXPIRY" -> { "strike@type": option }
|
|
|
|
_charts: { atmIv: null, rr25: null, fly25: null },
|
|
|
|
async init() {
|
|
// restore last loaded history (survives navigating away & back)
|
|
const vs = ViewState.load('tracker');
|
|
if (vs) {
|
|
this.symbol = vs.symbol ?? this.symbol;
|
|
this.expirations = vs.expirations ?? [];
|
|
this.expiry = vs.expiry ?? '';
|
|
this.snapshots = vs.snapshots ?? [];
|
|
if (this.snapshots.length) this.$nextTick(() => this.renderCharts());
|
|
}
|
|
if (this.symbol) this.loadOrders();
|
|
},
|
|
|
|
async loadOrders() {
|
|
if (!this.symbol) return;
|
|
this.ordersLoading = true;
|
|
try {
|
|
const r = await fetch('/api/orders?symbol=' + encodeURIComponent(this.symbol.trim().toUpperCase()));
|
|
const d = await r.json();
|
|
this.orders = d.data?.orders || [];
|
|
// fetch live chains for the unique expiries (one /api/chain call each)
|
|
const need = new Set();
|
|
for (const o of this.orders) for (const l of (o.legs||[])) if (l.expiry) need.add(o.symbol + '@' + l.expiry);
|
|
const cache = { ...this.ordersChain };
|
|
for (const key of need) {
|
|
if (cache[key]) continue;
|
|
const [sym, exp] = key.split('@');
|
|
try {
|
|
const cr = await fetch('/api/chain?symbol=' + encodeURIComponent(sym) + '&expiry=' + encodeURIComponent(exp));
|
|
if (!cr.ok) continue;
|
|
const ce = await cr.json();
|
|
const snap = ce.data?.snapshots?.[0];
|
|
if (!snap) continue;
|
|
const map = {};
|
|
for (const o of (snap.chain || [])) {
|
|
const t = (o.type || o.optionType || '').toLowerCase();
|
|
map[Number(o.strike) + '@' + t] = o;
|
|
}
|
|
cache[key] = map;
|
|
} catch {}
|
|
}
|
|
this.ordersChain = cache;
|
|
} catch (e) {
|
|
console.warn('loadOrders failed:', e);
|
|
} finally {
|
|
this.ordersLoading = false;
|
|
}
|
|
},
|
|
|
|
orderPL(o) {
|
|
let value = 0, entered = 0;
|
|
for (const l of (o.legs||[])) {
|
|
const sign = l.side === 'short' ? -1 : 1;
|
|
entered += sign * l.qty * 100 * (l.entryPrice || 0);
|
|
const m = this.ordersChain[o.symbol + '@' + l.expiry];
|
|
const oo = m && m[Number(l.strike) + '@' + l.type];
|
|
const mid = oo ? Number(oo.midPrice ?? oo.mid ?? oo.bsPrice ?? 0) : 0;
|
|
value += sign * l.qty * 100 * mid;
|
|
}
|
|
const baseCost = (o.entry_cost != null) ? Number(o.entry_cost) : entered;
|
|
const pl = value - baseCost;
|
|
const plPct = Math.abs(baseCost) > 1e-6 ? (pl / Math.abs(baseCost)) * 100 : 0;
|
|
return { value, entered: baseCost, pl, plPct };
|
|
},
|
|
|
|
async toggleCloseOrder(o) {
|
|
const newStatus = o.status === 'open' ? 'closed' : 'open';
|
|
try {
|
|
const r = await fetch('/api/orders/' + o.id, {
|
|
method:'PATCH', headers:{'Content-Type':'application/json'},
|
|
body: JSON.stringify({ status: newStatus }),
|
|
});
|
|
const d = await r.json();
|
|
if (d.ok) {
|
|
const idx = this.orders.findIndex(x => x.id === o.id);
|
|
if (idx >= 0) this.orders[idx] = d.data;
|
|
}
|
|
} catch (e) { /* silent */ }
|
|
},
|
|
|
|
async removeOrder(id) {
|
|
if (!confirm('Remove this tracked order?')) return;
|
|
try {
|
|
await fetch('/api/orders/' + id, { method:'DELETE' });
|
|
this.orders = this.orders.filter(o => o.id !== id);
|
|
} catch {}
|
|
},
|
|
|
|
legsSummary(o) {
|
|
return (o.legs||[]).map(l =>
|
|
(l.side === 'long' ? '+' : '-') + l.qty +
|
|
' ' + l.strike + (l.type === 'call' ? 'C' : 'P') +
|
|
'·' + (l.expiry || '').slice(5)
|
|
).join(' / ');
|
|
},
|
|
|
|
fmtMoney(v) {
|
|
if (v == null || !isFinite(v)) return '—';
|
|
const a = Math.abs(v), sign = v < 0 ? '-' : '';
|
|
return sign + '$' + a.toLocaleString('en-US', { maximumFractionDigits: a < 100 ? 2 : 0 });
|
|
},
|
|
|
|
_persist() {
|
|
ViewState.save('tracker', {
|
|
symbol: this.symbol, expirations: this.expirations,
|
|
expiry: this.expiry, snapshots: this.snapshots,
|
|
});
|
|
},
|
|
|
|
async fetchExpirations() {
|
|
if (!this.symbol.trim()) return;
|
|
try {
|
|
const sym = this.symbol.trim().toUpperCase();
|
|
const res = await fetch(`/api/expirations?symbol=${encodeURIComponent(sym)}`);
|
|
if (!res.ok) throw new Error('Failed to fetch expirations');
|
|
const env = await res.json();
|
|
const json = env.data ?? env;
|
|
this.expirations = Array.isArray(json) ? json : (json.expirations || []);
|
|
this._persist();
|
|
} catch (e) {
|
|
this.expirations = [];
|
|
}
|
|
},
|
|
|
|
async loadHistory() {
|
|
if (!this.symbol.trim()) return;
|
|
this.loading = true;
|
|
this.error = '';
|
|
try {
|
|
const sym = this.symbol.trim().toUpperCase();
|
|
const url = `/api/snapshots?symbol=${encodeURIComponent(sym)}&limit=100`;
|
|
const res = await fetch(url);
|
|
if (!res.ok) throw new Error(`API error ${res.status}: ${res.statusText}`);
|
|
const env = await res.json();
|
|
const json = env.data ?? env;
|
|
// Accept array or { snapshots: [...] }
|
|
this.snapshots = json.snapshots ?? (Array.isArray(json) ? json : []);
|
|
// Sort descending (most recent first)
|
|
this.snapshots.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
|
|
// Rebuild expiration list from data if API didn't provide it
|
|
if (this.expirations.length === 0) {
|
|
const expSet = new Set(this.snapshots.map(s => s.expiry).filter(Boolean));
|
|
this.expirations = [...expSet].sort();
|
|
}
|
|
this._persist();
|
|
this.$nextTick(() => this.renderCharts());
|
|
this.loadOrders();
|
|
} catch (e) {
|
|
this.error = e.message;
|
|
this.snapshots = [];
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
get filteredSnapshots() {
|
|
if (!this.expiry) return this.snapshots;
|
|
return this.snapshots.filter(s => s.expiry === this.expiry);
|
|
},
|
|
|
|
get tableRows() {
|
|
// Most recent first, limited to 20
|
|
return this.filteredSnapshots.slice(0, 20);
|
|
},
|
|
|
|
formatTimestamp(ts) {
|
|
if (!ts) return '—';
|
|
try {
|
|
const d = new Date(ts);
|
|
return d.toLocaleString('en-US', {
|
|
month: 'short', day: '2-digit',
|
|
hour: '2-digit', minute: '2-digit',
|
|
hour12: false,
|
|
});
|
|
} catch {
|
|
return ts;
|
|
}
|
|
},
|
|
|
|
renderCharts() {
|
|
const data = this.filteredSnapshots;
|
|
|
|
// Destroy existing instances
|
|
Object.keys(this._charts).forEach(k => {
|
|
if (this._charts[k]) {
|
|
try { this._charts[k].destroy(); } catch (_) {}
|
|
this._charts[k] = null;
|
|
}
|
|
});
|
|
|
|
if (data.length === 0) return;
|
|
|
|
this._charts.atmIv = new ApexCharts(
|
|
document.querySelector('#chart-atm-iv'),
|
|
buildAtmIvChart(data)
|
|
);
|
|
this._charts.atmIv.render();
|
|
|
|
this._charts.rr25 = new ApexCharts(
|
|
document.querySelector('#chart-rr25'),
|
|
buildRr25Chart(data)
|
|
);
|
|
this._charts.rr25.render();
|
|
|
|
this._charts.fly25 = new ApexCharts(
|
|
document.querySelector('#chart-fly25'),
|
|
buildFly25Chart(data)
|
|
);
|
|
this._charts.fly25.render();
|
|
},
|
|
|
|
// Re-render charts when expiry filter changes
|
|
// (triggered via @change on the select, which calls renderCharts() directly)
|
|
};
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|