initial import: etf strategy project

This commit is contained in:
2026-03-13 17:10:49 +08:00
commit 79ea983ca3
123 changed files with 6398 additions and 0 deletions

0
web/lab/__init__.py Normal file
View File

3
web/lab/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
web/lab/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class LabConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'lab'

View File

3
web/lab/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,43 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Best Artifacts</title>
<style>
table{border-collapse:collapse}
td,th{border:1px solid #ddd;padding:6px;font-family:ui-monospace,monospace;font-size:12px;vertical-align:top}
code{font-size:11px}
pre{margin:0;white-space:pre-wrap;max-width:900px}
</style>
</head>
<body>
<h1>Best Artifacts</h1>
<p>limit={{ limit }}</p>
<p><a href="/">Home</a></p>
<table>
<thead>
<tr>
<th>id</th><th>ts_utc</th><th>ann_return</th><th>ann_vol</th><th>max_dd</th><th>tpy</th>
<th>equity</th><th>weights</th><th>trades</th><th>config</th><th>params</th>
</tr>
</thead>
<tbody>
{% for r in rows %}
<tr>
<td>{{ r.id }}</td>
<td>{{ r.ts_utc }}</td>
<td>{{ r.ann_return }}</td>
<td>{{ r.ann_vol }}</td>
<td>{{ r.max_drawdown }}</td>
<td>{{ r.trades_per_year }}</td>
<td><code>{{ r.out_equity }}</code></td>
<td><code>{{ r.out_weights }}</code></td>
<td><code>{{ r.out_trades }}</code></td>
<td><code>{{ r.config_path }}</code></td>
<td><pre>{{ r.params_json }}</pre></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

View File

@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>QFR Lab</title>
</head>
<body>
<h1>QFR Lab</h1>
<p>Experiments DB: <code>{{ db_path }}</code></p>
<ul>
<li><a href="/lab/top-trials">Top trials</a></li>
<li><a href="/lab/best-artifacts">Best artifacts</a></li>
</ul>
</body>
</html>

View File

@@ -0,0 +1,42 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Top Trials</title>
<style>
table{border-collapse:collapse}
td,th{border:1px solid #ddd;padding:6px;font-family:ui-monospace,monospace;font-size:12px;vertical-align:top}
code{font-size:11px}
</style>
</head>
<body>
<h1>Top Trials (ann_return)</h1>
<p>Summary: {{ summary }}</p>
<p>limit={{ limit }}</p>
<p><a href="/">Home</a></p>
<table>
<thead>
<tr>
<th>id</th>
<th>ann_return</th><th>ann_vol</th><th>max_drawdown</th><th>sharpe</th><th>tpy</th>
<th>ts_utc</th><th>run_id</th><th>config</th>
</tr>
</thead>
<tbody>
{% for r in rows %}
<tr>
<td>{{ r.id }}</td>
<td>{{ r.ann_return }}</td>
<td>{{ r.ann_vol }}</td>
<td>{{ r.max_drawdown }}</td>
<td>{{ r.sharpe }}</td>
<td>{{ r.trades_per_year }}</td>
<td>{{ r.ts_utc }}</td>
<td><code>{{ r.run_id }}</code></td>
<td><code>{{ r.config_path }}</code></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

3
web/lab/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
web/lab/urls.py Normal file
View File

@@ -0,0 +1,11 @@
from __future__ import annotations
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("lab/top-trials", views.top_trials, name="top_trials"),
path("lab/best-artifacts", views.best_artifacts, name="best_artifacts"),
]

113
web/lab/views.py Normal file
View File

@@ -0,0 +1,113 @@
from __future__ import annotations
import sqlite3
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from django.conf import settings
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
@dataclass
class TrialRow:
ann_return: float | None
ann_vol: float | None
max_drawdown: float | None
sharpe: float | None
trades_per_year: float | None
ts_utc: str | None
run_id: str | None
config_path: str | None
def _db_path() -> Path:
return Path(getattr(settings, "QFR_EXPERIMENTS_DB"))
def _connect() -> sqlite3.Connection:
con = sqlite3.connect(str(_db_path()))
con.row_factory = sqlite3.Row
return con
def index(request: HttpRequest) -> HttpResponse:
return render(
request,
"lab/index.html",
{
"db_path": str(_db_path()),
},
)
def top_trials(request: HttpRequest) -> HttpResponse:
limit = int(request.GET.get("limit", "50"))
limit = max(10, min(500, limit))
rows: list[dict[str, Any]] = []
summary: dict[str, Any] = {}
with _connect() as con:
cur = con.execute(
"""
SELECT ann_return, ann_vol, max_drawdown, sharpe, trades_per_year, ts_utc, run_id, config_path
FROM trials
WHERE ann_return IS NOT NULL
ORDER BY ann_return DESC
LIMIT ?
""",
(limit,),
)
rows = [dict(r) for r in cur.fetchall()]
cur2 = con.execute(
"""
SELECT
COUNT(*) AS n,
MAX(ann_return) AS best_ann,
MIN(max_drawdown) AS worst_dd,
MIN(trades_per_year) AS min_tpy,
MAX(trades_per_year) AS max_tpy
FROM trials
"""
)
summary = dict(cur2.fetchone())
return render(
request,
"lab/top_trials.html",
{
"rows": rows,
"summary": summary,
"limit": limit,
},
)
def best_artifacts(request: HttpRequest) -> HttpResponse:
limit = int(request.GET.get("limit", "50"))
limit = max(10, min(500, limit))
rows: list[dict[str, Any]] = []
with _connect() as con:
cur = con.execute(
"""
SELECT id, ts_utc, config_path, out_equity, out_weights, out_trades,
ann_return, ann_vol, max_drawdown, trades_per_year, params_json
FROM best_artifacts
ORDER BY id DESC
LIMIT ?
""",
(limit,),
)
rows = [dict(r) for r in cur.fetchall()]
return render(
request,
"lab/best_artifacts.html",
{
"rows": rows,
"limit": limit,
},
)