diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..4ba93d8 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,4 @@ +{ + // The C# extension provides the "coreclr" debugger used by launch.json. + "recommendations": ["ms-dotnettools.csharp"] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..60ff966 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,70 @@ +{ + // Debugging Anubis from VS Code (needs the C# extension, ms-dotnettools.csharp). + // Each configuration builds the server in Debug first and opens the browser once it listens. + "version": "0.2.0", + "configurations": [ + { + "name": "Anubis: server, dati di prova", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-server", + "program": "${workspaceFolder}/src/Anubis.Server/bin/Debug/net10.0/Anubis.Server.dll", + // A data folder of its own, ignored by git: the real one in %APPDATA%\Anubis stays untouched. + "args": ["--data-dir", "${workspaceFolder}/artifacts/debug-data"], + "cwd": "${workspaceFolder}/src/Anubis.Server", + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "console": "internalConsole", + "stopAtEntry": false, + "serverReadyAction": { + "action": "openExternally", + "pattern": "Now listening on: (https?://[^\\s\"]+)" + } + }, + { + "name": "Anubis: modalità dimostrativa", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-server", + "program": "${workspaceFolder}/src/Anubis.Server/bin/Debug/net10.0/Anubis.Server.dll", + // The data of SPEC appendix F, in a folder and a database apart (the same as "tasks seed-demo"). + "args": ["--demo"], + "cwd": "${workspaceFolder}/src/Anubis.Server", + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "console": "internalConsole", + "stopAtEntry": false, + "serverReadyAction": { + "action": "openExternally", + "pattern": "Now listening on: (https?://[^\\s\"]+)" + } + }, + { + "name": "Anubis: server, dati dell'utente", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-server", + "program": "${workspaceFolder}/src/Anubis.Server/bin/Debug/net10.0/Anubis.Server.dll", + // The desktop data folder (%APPDATA%\Anubis), the same one "tasks dev" uses. + "args": [], + "cwd": "${workspaceFolder}/src/Anubis.Server", + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "console": "internalConsole", + "stopAtEntry": false, + "serverReadyAction": { + "action": "openExternally", + "pattern": "Now listening on: (https?://[^\\s\"]+)" + } + }, + { + "name": "Anubis: aggancia a un processo", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..8995d4c --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,45 @@ +{ + // The build the debugger needs, plus the commands of tasks.ps1 (Terminal > Run Task). + "version": "2.0.0", + "tasks": [ + { + "label": "build-server", + "type": "process", + "command": "dotnet", + "args": ["build", "${workspaceFolder}/src/Anubis.Server", "-c", "Debug", "-nologo", "-consoleLoggerParameters:NoSummary"], + "group": { "kind": "build", "isDefault": true }, + "presentation": { "reveal": "silent", "clear": true }, + "problemMatcher": "$msCompile" + }, + { + "label": "check", + "type": "shell", + "command": "./tasks.ps1 check", + "windows": { "command": "./tasks.ps1 check" }, + "linux": { "command": "sh ./tasks.sh check" }, + "osx": { "command": "sh ./tasks.sh check" }, + "group": "test", + "problemMatcher": "$msCompile" + }, + { + "label": "test", + "type": "shell", + "command": "./tasks.ps1 test", + "windows": { "command": "./tasks.ps1 test" }, + "linux": { "command": "sh ./tasks.sh test" }, + "osx": { "command": "sh ./tasks.sh test" }, + "group": { "kind": "test", "isDefault": true }, + "problemMatcher": "$msCompile" + }, + { + "label": "dev", + "type": "shell", + "command": "./tasks.ps1 dev", + "windows": { "command": "./tasks.ps1 dev" }, + "linux": { "command": "sh ./tasks.sh dev" }, + "osx": { "command": "sh ./tasks.sh dev" }, + "isBackground": true, + "problemMatcher": [] + } + ] +} diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 53ac18b..ee7959f 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -136,17 +136,17 @@ Un motore di prova onesto, riproducibile e protetto dall'auto-inganno. **Lavori** -- [ ] [#34](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/34) Motore di backtest a eventi, modelli di costo e metriche -- [ ] [#35](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/35) Convalide: finestre che avanzano, K-fold purgato combinatorio, DSR, PBO; registro delle prove -- [ ] [#36](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/36) Rapporto di backtest e schermata Laboratorio -- [ ] [#37](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/37) Chiusura della fase, revisione indipendente e tag `fase-4` +- [x] [#34](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/34) Motore di backtest a eventi, modelli di costo e metriche +- [x] [#35](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/35) Convalide: finestre che avanzano, K-fold purgato combinatorio, DSR, PBO; registro delle prove +- [x] [#36](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/36) Rapporto di backtest e schermata Laboratorio +- [x] [#37](https://gitea.encke-hake.ts.net/BalboSoft/Anubis/issues/37) Chiusura della fase, revisione indipendente e tag `fase-4` **Criteri di accettazione** -- [ ] Una strategia giocattolo con risultato noto dà esattamente il risultato atteso -- [ ] Il test anti-sguardo-avanti esiste e fallisce su una strategia volutamente scorretta -- [ ] DSR e PBO riproducono i casi noti di 12.2 -- [ ] Il rapporto si apre dall'interfaccia +- [x] Una strategia giocattolo con risultato noto dà esattamente il risultato atteso (`AToyStrategyGivesExactlyTheResultComputedByHand`, `TheMetricsOfTheToyRunAreThoseComputedByHand`: patrimonio, commissioni ed esecuzioni calcolati a mano, al centesimo) +- [x] Il test anti-sguardo-avanti esiste e fallisce su una strategia volutamente scorretta (`TheLookAheadTestCatchesAStrategyThatReadsTomorrow`) +- [x] DSR e PBO riproducono i casi noti di 12.2 (`TheDeflatedSharpeRatioReproducesTheExampleOfTheArticle`: DSR 0,9004 dell'articolo; PBO circa 0,5 sul rumore, 0 con una variante dominante, 1 quando la migliore perde sempre fuori campione) +- [x] Il rapporto si apre dall'interfaccia (`TheReportOpensFromTheLaboratory`: test pianificato dal modulo, eseguito, rapporto e confronto aperti) ## Fase 5 — Strategie, portafoglio, rischio diff --git a/docs/STATE.md b/docs/STATE.md index c9572ed..535414c 100644 --- a/docs/STATE.md +++ b/docs/STATE.md @@ -1,10 +1,10 @@ # Stato del progetto -Aggiornato: 2026-11-10 +Aggiornato: 2026-11-11 ## Fase corrente -**Fase 3 — Conti, ordini, contabilità: completata**, con le prove sui conti di prova in attesa delle chiavi demo di OKX e di Alpaca. La prossima è la Fase 4 (Backtest e laboratorio), da iniziare con il via dell'utente. +**Fase 4 — Backtest e laboratorio: completata.** La prossima è la Fase 5 (Strategie, portafoglio, rischio), da iniziare con il via dell'utente. Restano in attesa delle chiavi demo di OKX e di Alpaca le prove sui conti di prova (Fase 3) e lo storico azionario (Fase 2). ## Fatto @@ -62,6 +62,14 @@ Aggiornato: 2026-11-10 - Riconciliazione ogni 5 minuti con modalità prudente, scelta "allinea" o "escludi", blocco delle chiavi che permettono i prelievi. - Schermata Conti e posizioni come il mockup, esportazione CSV delle posizioni; in Impostazioni chiavi (verificate col broker prima di salvarle) e capitale affidato; la Panoramica indica il capitale come primo passo. - Batteria di conformità sul simulatore e sulle risposte d'esempio della documentazione; sui conti di prova parte da sola quando ci sono le chiavi. +- **Fase 4** (scelte nell'ADR 0010): + - Motore di backtest a eventi, una seduta alla volta: si decide sulla chiusura e si esegue all'apertura dopo, con gli stessi costi del simulatore (moltiplicabili per 2 e 3), stop, frazionamenti e dividendi, nessun prestito. Le strategie sono le stesse classi che girano dal vivo; correttivi e rischio hanno già il loro posto e fino alla Fase 5 lasciano passare tutto. + - Metriche del rapporto (rendimento, volatilità, Sharpe, calo massimo, operazioni, esposizione, risultati per anno) e confronto con "comprare e tenere". + - Convalide scritte in casa: finestre che avanzano, K-fold purgato combinatorio, Deflated Sharpe Ratio, PBO, regola dell'altopiano, test contro lo sguardo in avanti. DSR e PBO riproducono i casi noti. + - Registro delle prove: il piano (con le soglie) e la sua impronta si scrivono prima dei risultati e non cambiano; il numero di prove del DSR conta tutti i tentativi della stessa strategia. + - Laboratorio su un thread a priorità bassa, una prova alla volta, con ripresa dopo un riavvio; dati dalle barre raccolte o dallo storico di ricerca Binance. + - Schermata Laboratorio: elenco delle prove, modulo per un nuovo test, rapporto completo, confronto tra due prove. Le sezioni "Migliorie in osservazione" e "Stime di costo da confermare" aspettano le Fasi 9 e 8. + - Strategia d'esempio `sma_cross` (incrocio di medie) per provare il laboratorio; le strategie di base arrivano con la Fase 5. ## In corso @@ -69,7 +77,7 @@ Nulla. ## Prossimo passo -Fase 4, backtest e laboratorio: motore di prova sullo storico, con gli stessi costi e le stesse regole d'ordine della Fase 3, e il confronto "senza bot". Con le chiavi dei conti di prova si possono chiudere i due criteri in attesa (azioni della Fase 2, conti di prova della Fase 3). +Fase 5, strategie, portafoglio e rischio: le tre strategie di base, i correttivi, la ripartizione del capitale e il motore del rischio completo, con i backtest onesti di ciascuna strategia fatti nel Laboratorio. Con le chiavi dei conti di prova si possono chiudere i due criteri in attesa (azioni della Fase 2, conti di prova della Fase 3). ## Da sapere @@ -80,7 +88,8 @@ Fase 4, backtest e laboratorio: motore di prova sullo storico, con gli stessi co - `tasks dev` fuori da Docker usa la cartella dei dati vera dell'utente: per prove usa `tasks seed-demo`. - Alpaca non ha più conti senza margine: il divieto di leva lo impone Anubis (API-NOTES A14). - OKX: WebSocket sulla porta 443, la 8443 è chiusa (API-NOTES O3). -- Le coppie in EUR di OKX esistono da novembre 2023: prima di allora la storia crypto viene dagli archivi Binance in USDT, da convertire in euro nella Fase 4. +- Le coppie in EUR di OKX esistono da novembre 2023: prima di allora la storia crypto viene dagli archivi Binance in USDT. Il Laboratorio li usa nella loro valuta, senza convertirli: una prova lavora in una sola valuta (ADR 0010, punto 13). +- Nella modalità dimostrativa il Laboratorio è spento: mostra l'elenco ma non esegue prove. - I dati di mercato si spengono con `ANUBIS_MARKET_DATA=off`; nella modalità dimostrativa sono sempre spenti. - Gli universi passati usano i flag ETF e "frazionabile" di oggi: è un limite delle fonti gratuite (API-NOTES, Fase 2). - Gitea ha un esecutore delle Actions attivo: la CI gira a ogni push (prima esecuzione: run 169 del branch `fase-0`, test verdi su SQLite e PostgreSQL). `tasks ci` resta per provarla in locale. @@ -90,7 +99,7 @@ Fase 4, backtest e laboratorio: motore di prova sullo storico, con gli stessi co | Cosa | Da quando serve | Senza | |---|---|---| -| Via libera per la Fase 4 | Subito | Non si parte | +| Via libera per la Fase 5 | Subito | Non si parte | | Chiavi del conto paper di Alpaca (`ALPACA_API_KEY_DEMO`, `ALPACA_API_SECRET_DEMO`) | Già dalla Fase 2 (dati azionari) | Parte azionaria spenta; storico azionario dal 2016 in attesa | | Chiavi demo di OKX Europe, senza permesso di prelievo | Fase 3 (ordini sui conti di prova) | Batteria di conformità su risposte d'esempio; criterio della Fase 3 in attesa | | Un token Gitea con permesso `write:package` come segreto `REGISTRY_TOKEN` | Fase 9 (pubblicazione dell'immagine) | Pubblicazione a mano | diff --git a/docs/SVILUPPO.md b/docs/SVILUPPO.md index 8bb0f83..38f922a 100644 --- a/docs/SVILUPPO.md +++ b/docs/SVILUPPO.md @@ -79,6 +79,17 @@ Un criterio che dipende solo da qualcosa che deve dare l'utente (chiavi dei cont Per ogni schermata: implementa, apri sui dati dimostrativi (`tasks seed-demo`), fotografa con Edge senza finestra (`msedge --headless --screenshot`) a 1440 e 390 pixel, confronta zona per zona con lo screen di `docs/design/`, correggi, ripeti finché restano differenze trascurabili. I testi fissi si leggono dalle immagini, i dati dall'appendice F, colori e misure dall'appendice C. Guarda uno screen per volta e ritaglia la zona che ti serve: le immagini sono a doppia densità. +## Debug con Visual Studio Code + +Apri la radice del repository in VS Code con l'estensione C# (`ms-dotnettools.csharp`, suggerita da `.vscode/extensions.json`), poi Esegui e debug (F5). Ogni voce compila il server in Debug (attività `build-server` di `.vscode/tasks.json`) e apre il browser quando il server ascolta: + +- **Anubis: server, dati di prova**: una cartella dati a parte, `artifacts/debug-data`, ignorata da git. Al primo avvio chiede la configurazione iniziale; per ripartire da zero basta cancellare la cartella. +- **Anubis: modalità dimostrativa**: i dati dell'appendice F, come `tasks seed-demo`. +- **Anubis: server, dati dell'utente**: la cartella vera, `%APPDATA%\Anubis`, come `tasks dev`. +- **Anubis: aggancia a un processo**: per un server già avviato, per esempio con `tasks dev`. + +Le attività `check`, `test` e `dev` sono anche in Terminale → Esegui attività. L'estensione è dell'editor, non del progetto: non entra nell'elenco dell'ADR 0004. + ## Pacchetti e consegne - **Docker**: immagine `gitea.encke-hake.ts.net/balbosoft/anubis`, pubblicata dalla CI sui tag `v*` nella sezione Packages del repository (ADR 0002). I tag `fase-N` non pubblicano nulla. diff --git a/docs/adr/0010-scelte-fase-4.md b/docs/adr/0010-scelte-fase-4.md new file mode 100644 index 0000000..cce86b0 --- /dev/null +++ b/docs/adr/0010-scelte-fase-4.md @@ -0,0 +1,68 @@ +# 0010 — Scelte della Fase 4: backtest e laboratorio + +Data: 2026-11-11 · Stato: accettata · Precisa la specifica (sezioni 5.3, 8.7, 12.1, 12.2, 13) e l'ADR 0009, senza aggiungere componenti esterni + +## Contesto + +La Fase 4 costruisce il motore di prova sullo storico, le convalide del protocollo anti-illusioni (12.2), il registro delle prove e la schermata Laboratorio. Le strategie di base, i correttivi e il motore del rischio arrivano con la Fase 5: per provare il laboratorio c'è una strategia d'esempio, l'incrocio di due medie (`sma_cross`). Alcune scelte non sono scritte nella specifica, o la precisano. Le raccolgo qui. + +## Decisione + +**1. Il motore va a eventi, una seduta alla volta.** +- Ordine degli eventi in una seduta: azioni societarie prima dell'apertura; all'apertura gli stop che aprono sotto il livello, poi gli ordini decisi alla chiusura prima, vendite per prime; durante la seduta gli stop toccati dal minimo; alla chiusura la valutazione e la decisione. +- Si decide sulla chiusura e si esegue all'apertura dopo: è il ritardo tra decidere e agire. Niente inseguimento con ordini limite (quello lo fa il gestore degli ordini dal vivo). +- Le strategie sono le stesse classi che girano dal vivo e non sanno se sono in prova. +- Il denaro è `decimal`; le barre sono `double` (dati d'analisi) e diventano `decimal` una volta sola, quando servono come prezzo. +- Il motore ha una versione (`BacktestEngine.Version`) scritta nel piano: un rapporto dice quale motore l'ha prodotto. + +**2. Gli stessi costi del simulatore.** Commissioni e scostamento vengono dai listini del simulatore interno (ADR 0009 punto 5), arrotondati a nostro sfavore. Un moltiplicatore li scala tutti insieme per la sensibilità ai costi (×1, ×2, ×3). Gli acquisti non prendono mai in prestito; un ordine sotto il minimo non parte e lascia una nota. + +**3. Correttivi e rischio sono già al loro posto, ma lasciano passare tutto.** Il motore chiama i correttivi (8.5) e il motore del rischio (9) per ogni intenzione; fino alla Fase 5 sono "passanti". Un test controlla che vedano ogni intenzione e possano cambiarla. Il rapporto lo scrive tra le cose che il test non considera. + +**4. Statistica scritta in casa.** Nessuna libreria esterna (ADR 0004): +- funzione di ripartizione normale con l'algoritmo 5666 di Hart, quantile con il metodo di Acklam più un passo di Halley; +- momenti del campione: deviazione standard campionaria, asimmetria e curtosi (non in eccesso) di popolazione, come nell'articolo di Bailey e López de Prado; +- il Deflated Sharpe Ratio riproduce l'esempio dell'articolo (soglia 0,1132, DSR 0,9004). + +**5. Sharpe senza tasso privo di rischio.** Sharpe e volatilità sono annualizzati con le sedute di un anno (365 per le crypto, 252 per le azioni) e non tolgono un tasso privo di rischio. Il rendimento annuo usa i giorni di calendario (365,25). Il rapporto guadagni/perdite non ha valore senza un'operazione in perdita: un "0" o un "infinito" ingannerebbero. + +**6. Le convalide lavorano sui rendimenti giornalieri delle varianti.** Ogni variante della griglia gira una volta sull'intero periodo; finestre che avanzano, K-fold purgato combinatorio e PBO scelgono e giudicano su pezzi di quelle serie. È più semplice e veloce che rifare ogni volta il backtest su ogni pezzo, e la scelta nel pezzo d'addestramento vede comunque solo i rendimenti di quel pezzo. Il prezzo: le posizioni aperte prima dell'inizio di un pezzo di prova restano aperte, come lo sarebbero dal vivo. + +**7. La regola dell'altopiano.** Il punteggio di una variante è il più basso tra il suo Sharpe e la media del suo intorno (lei e le vicine a distanza 1 su ogni parametro della griglia). Con la sola media dell'intorno vincevano gli angoli della griglia accanto a un picco isolato; con il minimo vince una collina larga, non una punta. A parità vince la prima variante nell'ordine della griglia. + +**8. Le misure delle convalide.** +- Finestre che avanzano: addestramento che si allarga, 4 finestre di prova. +- K-fold purgato combinatorio: 6 gruppi, 2 in prova per volta, quindi 15 divisioni e 5 percorsi. +- Purga ed embargo: l'1% delle sedute, almeno una. +- PBO con il metodo CSCV: 16 gruppi, ridotti a un numero pari con almeno 4 sedute per gruppo. Un logit uguale o sotto zero conta come sovra-adattamento; nei pareggi il rango è la media. Con una sola variante il PBO non si applica e il rapporto lo dice. + +**9. Il numero di prove del DSR.** N è la somma delle varianti di tutte le prove concluse della stessa strategia più quelle della prova corrente; la varianza degli Sharpe è quella di tutte queste varianti. Ogni nuovo tentativo alza l'asticella dei successivi. + +**10. Il piano si scrive prima e non cambia.** +- Prima di qualsiasi calcolo si salvano il piano in JSON (strategia e versione, griglia, dati, capitale, moltiplicatori dei costi, misure delle convalide, soglie, versione del motore) e la sua impronta SHA-256. Il codice non aggiorna mai piano e impronta: un test controlla che dopo la corsa testo e impronta siano identici. Prima di partire la prova ricalcola l'impronta: se il piano non le corrisponde più, non parte. +- Su PostgreSQL la colonna è `json`, non `jsonb`, così il testo resta identico e l'impronta si ricontrolla. +- Le soglie sono nel piano: risultato netto positivo con costi doppi, DSR almeno 0,90, PBO non oltre 0,40, calo massimo non oltre il 25%, nessuna decisione cambiata dai dati futuri. La prova è superata se nessun controllo è fallito; un controllo che non si applica non la fa fallire. + +**11. Il test contro lo sguardo in avanti.** Si taglia lo storico a 6 date e si rifà la decisione su dati che finiscono lì: le intenzioni devono essere identiche a quelle della corsa completa. Una strategia volutamente scorretta, che legge il giorno dopo, viene presa. Lo stesso confronto "un passo" servirà nella Fase 6 per paragonare dal vivo e backtest (12.3), con una tolleranza del 10% sulla dimensione. + +**12. Il laboratorio non disturba il bot.** +- Una prova alla volta, su un thread suo a priorità bassa. Solo la parte asincrona tocca il database. +- All'avvio le prove rimaste "in corso" tornano "pianificate" e ripartono con lo stesso piano. +- Una prova fallita tiene il motivo. +- Massimo 20 strumenti e 200 varianti per prova, almeno 30 sedute, capitale tra 1 e 10.000.000. I parametri che contano giorni accettano solo numeri interi: 10,5 girerebbe come 10 e conterebbe due volte nel DSR. +- Un errore del database (per esempio PostgreSQL che riparte) non ferma la coda: si riprova dopo 30 secondi. +- Nella modalità dimostrativa il laboratorio è spento. + +**13. Le due origini dei dati.** +- "Barre raccolte": le barre giornaliere del database, con frazionamenti e dividendi per le azioni. +- "Storico di ricerca": gli archivi Binance (Fase 2), nella loro valuta (USDT). Le regole d'ordine non ci sono negli archivi: passo di prezzo e di quantità di 0,00000001 e controvalore minimo di 5 (come i minimi tipici delle borse crypto: un ordine più piccolo non parte e un residuo sotto i 5 si perde all'uscita); i costi sono quelli di OKX. Il rapporto lo scrive. La conversione in euro (annunciata nello STATE della Fase 2) non serve: una prova lavora in una sola valuta e rifiuta strumenti quotati in valute diverse. + +**14. Il rapporto.** Contiene i controlli del protocollo, la variante scelta contro "comprare e tenere" (pesi uguali, stessi costi; uno strumento la cui storia comincia dopo si compra alla sua prima chiusura, con la quota rimasta libera per lui), i risultati per anno, la sensibilità ai costi, DSR, PBO, finestre, percorsi, sguardo in avanti, tutte le varianti con il loro punteggio, ciò che il test non considera, l'impronta dei dati e le operazioni. La curva tiene al massimo 500 punti e l'elenco le ultime 300 operazioni, per restare leggero nel database. Il confronto tra due prove dice se hanno letto gli stessi dati. + +**15. Le sezioni che arrivano dopo.** "Migliorie in osservazione" (8.7) e "Stime di costo da confermare" (11.6) sono già nella schermata, vuote, con la fase che le riempie (9 e 8). + +## Conseguenze + +- Una prova è riproducibile: stesso piano e stessi dati danno lo stesso rapporto, e il registro non si può ritoccare dopo aver visto i risultati. +- I backtest delle tre strategie di base (Fase 5) useranno questo laboratorio così com'è; i correttivi e il rischio veri entreranno nei punti già pronti. +- La prova su PostgreSQL della tabella delle prove passa dalla CI, perché su questo PC Docker non era disponibile. diff --git a/src/Anubis.Server/AccountsView.cs b/src/Anubis.Server/AccountsView.cs index 15f6f3b..5e7e8f0 100644 --- a/src/Anubis.Server/AccountsView.cs +++ b/src/Anubis.Server/AccountsView.cs @@ -136,7 +136,7 @@ internal sealed record AccountsView( var (pending, others, alpacaStops) = PendingRows(orders.Where(o => shown.Contains(o.Account)).ToList(), byId, instruments.ToDictionary(i => i.Key, i => i.Value.Symbol), positions, market.Open, market.Next, clock.UtcNow, zone); - var slices = Strategies.Base.Select(b => new StrategySlice(Strategies.Label(b.Name, withMarket: true), "var(--s-" + b.Name.Replace('_', '-') + ")", + var slices = StrategyNames.Base.Select(b => new StrategySlice(StrategyNames.Label(b.Name, withMarket: true), "var(--s-" + b.Name.Replace('_', '-') + ")", rows.Where(r => r.Row.Strategy == b.Name).Sum(r => r.Row.ValueEur))).ToList(); decimal? fxEffect = null; if (cards.Count > 0) diff --git a/src/Anubis.Server/AnubisHost.cs b/src/Anubis.Server/AnubisHost.cs index cf6e46f..56e6b63 100644 --- a/src/Anubis.Server/AnubisHost.cs +++ b/src/Anubis.Server/AnubisHost.cs @@ -1,4 +1,5 @@ using Anubis.Access; +using Anubis.Backtest; using Anubis.Core; using Anubis.Data; using Anubis.Db; @@ -37,7 +38,7 @@ internal sealed record HostChanged(HostMode Mode); /// Everything that needs the database, built once it is reachable and the installation checks out. internal sealed record AppServices(Database Db, SettingsStore Settings, SecretStore Secrets, AccessStore Access, Backups Backups, Scheduler Scheduler, - MarketDataService? MarketData = null, TradingService? Trading = null); + MarketDataService? MarketData = null, TradingService? Trading = null, Laboratory? Lab = null); /// /// The startup state machine of the server (SPEC 15.1, 15.6): reads the startup configuration, opens the configured @@ -65,6 +66,7 @@ internal sealed partial class AnubisHost( private CancellationTokenSource? _schedulerStop; private MarketDataService? _market; private TradingService? _trading; + private Laboratory? _lab; private volatile HostStatus _status = new(HostMode.Starting, null, null, clock.UtcNow, 0); private volatile AppServices? _services; @@ -292,7 +294,9 @@ internal sealed partial class AnubisHost( ? new TradingService(_db, clock, bus, secrets, _market, new FxStore(_db, clock), TradingConsent.From(Config), file.InstallationId, overrides?.MarketHttp, overrides?.MarketSockets, logger) : null; - _services = new AppServices(_db, settings, secrets, access, backups, scheduler, _market, _trading); + // The laboratory computes on its own thread and reads only local data; the demo shows appendix F and runs none. + _lab = options.Demo ? null : new Laboratory(_db, clock, Config.Data.Research, logger); + _services = new AppServices(_db, settings, secrets, access, backups, scheduler, _market, _trading, _lab); StartScheduler(scheduler, backups); SetStatus(HostMode.Ready, null); } @@ -426,12 +430,14 @@ internal sealed partial class AnubisHost( var token = _schedulerStop.Token; var market = _market; var trading = _trading; + var lab = _lab; _schedulerLoop = Task.Run(async () => { using var runs = bus.Subscribe(); var recorder = RecordJobsAsync(runs, token); var collecting = market is null ? Task.CompletedTask : RunMarketDataAsync(market, token); var dealing = trading is null ? Task.CompletedTask : RunTradingAsync(trading, token); + var testing = lab is null ? Task.CompletedTask : RunLabAsync(lab, token); try { await scheduler.RunAsync(token).ConfigureAwait(false); @@ -442,6 +448,7 @@ internal sealed partial class AnubisHost( await collecting.ConfigureAwait(false); await dealing.ConfigureAwait(false); + await testing.ConfigureAwait(false); await recorder.ConfigureAwait(false); }, CancellationToken.None); } @@ -461,6 +468,21 @@ internal sealed partial class AnubisHost( } } + private async Task RunLabAsync(Laboratory lab, CancellationToken cancellationToken) + { + try + { + await lab.RunAsync(cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + } + catch (Exception ex) + { + LogLabFailed(logger, ex); + } + } + private async Task RunTradingAsync(TradingService trading, CancellationToken cancellationToken) { try @@ -532,6 +554,8 @@ internal sealed partial class AnubisHost( _market = null; _trading?.Dispose(); _trading = null; + _lab?.Dispose(); + _lab = null; } // Values of the startup configuration that also exist as settings are read-only in the interface (SPEC 13.5). @@ -571,6 +595,9 @@ internal sealed partial class AnubisHost( [LoggerMessage(Level = LogLevel.Error, Message = "Market data stopped")] private static partial void LogMarketDataFailed(ILogger logger, Exception exception); + [LoggerMessage(Level = LogLevel.Error, Message = "Laboratory stopped")] + private static partial void LogLabFailed(ILogger logger, Exception exception); + [LoggerMessage(Level = LogLevel.Error, Message = "Trading stopped")] private static partial void LogTradingFailed(ILogger logger, Exception exception); diff --git a/src/Anubis.Server/Assets/app.css b/src/Anubis.Server/Assets/app.css index 4d3bab2..2e81dd9 100644 --- a/src/Anubis.Server/Assets/app.css +++ b/src/Anubis.Server/Assets/app.css @@ -256,6 +256,7 @@ textarea { .form { display: flex; flex-direction: column; gap: 8px; max-width: 420px; } .form label { font-weight: 550; margin-top: 6px; } .form .btn { align-self: flex-start; margin-top: 10px; } +.form fieldset { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; } .form-inline { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 8px 0; } .form-inline label { min-width: 80px; font-weight: 550; } .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; } @@ -349,6 +350,8 @@ legend { display: flex; align-items: center; gap: 10px; font-size: 17px; font-we .account-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; } .account-head h2 { display: flex; align-items: center; gap: 10px; } .tag { padding: 2px 8px; border-radius: 6px; background: var(--surface-2); color: var(--text-2); font-size: 13px; font-weight: 500; line-height: 20px; } +.tag.gain { background: var(--gain-bg); color: var(--gain); } +.tag.loss { background: var(--loss-bg); color: var(--loss); } .link-state { display: inline-flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 13.5px; } .link-state.down .dot { background: var(--loss); } .account-value { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; margin: 14px 0 12px; } diff --git a/src/Anubis.Server/Components/EquityChart.razor b/src/Anubis.Server/Components/EquityChart.razor index 7d32e9c..3039dcb 100644 --- a/src/Anubis.Server/Components/EquityChart.razor +++ b/src/Anubis.Server/Components/EquityChart.razor @@ -5,8 +5,8 @@ HTML placed in percent, so nothing gets distorted at any width. *@ @if (Points.Count >= 2) { - var values = Points.Select(p => Shell.Convert(p.Equity, Currency)).ToList(); - var bench = Points.Select(p => p.Benchmark is { } b ? Shell.Convert(b, Currency) : (decimal?)null).ToList(); + var values = Points.Select(p => Value(p.Equity)).ToList(); + var bench = Points.Select(p => p.Benchmark is { } b ? Value(b) : (decimal?)null).ToList(); var all = values.Concat(bench.OfType()).ToList(); var (lo, hi, step) = Scale(all.Min(), all.Max()); double X(int i) => i * 1000.0 / (Points.Count - 1); @@ -15,7 +15,7 @@ var equityLine = Line(values.Select((v, i) => (i, v))); var benchLine = Line(bench.Select((v, i) => (i, v)).Where(p => p.v is not null).Select(p => (p.i, p.v!.Value))); var last = values[^1]; -
+
- Con Anubis + @EquityLabel @if (benchLine.Length > 0) { - Senza bot: Bitcoin e S&P 500 comprati e tenuti + @BenchmarkLabel }
Vedi i numeri in una tabella
- + @foreach (var p in Points.AsEnumerable().Reverse()) { - + }
GiornoCon il botSenza bot
Giorno@EquityColumn@BenchmarkColumn
@Format.LongDate(p.Day)@Shell.Money(p.Equity, Currency)@(p.Benchmark is { } b ? Shell.Money(b, Currency) : "—")
@Format.LongDate(p.Day)@Text(p.Equity)@(p.Benchmark is { } b ? Text(b) : "—")
@@ -79,6 +79,29 @@ [Parameter] public string Currency { get; set; } = "EUR"; + /// True when the amounts are already in (a backtest report): no conversion from euro. + [Parameter] + public bool Native { get; set; } + + [Parameter] + public string Label { get; set; } = "Patrimonio nel tempo, con il bot e senza"; + + [Parameter] + public string EquityLabel { get; set; } = "Con Anubis"; + + [Parameter] + public string BenchmarkLabel { get; set; } = "Senza bot: Bitcoin e S&P 500 comprati e tenuti"; + + [Parameter] + public string EquityColumn { get; set; } = "Con il bot"; + + [Parameter] + public string BenchmarkColumn { get; set; } = "Senza bot"; + + private decimal Value(decimal amount) => Native ? amount : Shell.Convert(amount, Currency); + + private string Text(decimal amount, int decimals = 2) => Native ? Format.Money(amount, Currency, decimals: decimals) : Shell.Money(amount, Currency, decimals: decimals); + private static string Fmt(double v) => v.ToString("0.##", CultureInfo.InvariantCulture); private static string Pct(double v) => v.ToString("0.###", CultureInfo.InvariantCulture) + "%"; diff --git a/src/Anubis.Server/Components/Pages/Conti.razor b/src/Anubis.Server/Components/Pages/Conti.razor index 9aa295e..8e3821a 100644 --- a/src/Anubis.Server/Components/Pages/Conti.razor +++ b/src/Anubis.Server/Components/Pages/Conti.razor @@ -221,7 +221,7 @@ else @p.Symbol @p.Name @(p.Broker == Venues.Alpaca ? "Alpaca" : "OKX") - @Strategies.Label(p.Strategy) + @StrategyNames.Label(p.Strategy) @Format.Quantity(p.Quantity) @Format.Money(p.Average, p.Currency) @Format.Money(p.Last, p.Currency) diff --git a/src/Anubis.Server/Components/Pages/Impostazioni.razor b/src/Anubis.Server/Components/Pages/Impostazioni.razor index 885e38d..52a35e8 100644 --- a/src/Anubis.Server/Components/Pages/Impostazioni.razor +++ b/src/Anubis.Server/Components/Pages/Impostazioni.razor @@ -118,7 +118,7 @@
    @foreach (var s in b.Strategies) { -
  • @Strategies.Label(s.Strategy, withMarket: true): quota @Format.Percent(s.Share * 100m, 0), capitale @Format.Money(s.Capital, s.Currency), liquidità @Format.Money(s.Cash, s.Currency)
  • +
  • @StrategyNames.Label(s.Strategy, withMarket: true): quota @Format.Percent(s.Share * 100m, 0), capitale @Format.Money(s.Capital, s.Currency), liquidità @Format.Money(s.Cash, s.Currency)
  • }
} diff --git a/src/Anubis.Server/Components/Pages/Laboratorio.razor b/src/Anubis.Server/Components/Pages/Laboratorio.razor index de56aad..688cc29 100644 --- a/src/Anubis.Server/Components/Pages/Laboratorio.razor +++ b/src/Anubis.Server/Components/Pages/Laboratorio.razor @@ -1,5 +1,239 @@ +@inherits LoadedComponent @page "/laboratorio" +@inject AnubisHost Host +@inject ShellData Shell +@inject IClock Clock +@using Anubis.Backtest +@using Anubis.Data +@using Anubis.Strategies Laboratorio · Anubis -Il laboratorio dei test sul passato arriva con il motore dei backtest. +@if (_running) +{ + +} + +@if (_message is { } message) +{ + +} + +@if (_lab is null) +{ + Nella modalità dimostrativa i test sul passato non partono: avvia Anubis normalmente per provare una strategia. +} + +
+
+

Test eseguiti

+ @if (_trials.Count(t => t.Status == TrialStatuses.Done) >= 2) + { +
+ + + + + +
+ } +
+ @if (_trials.Count == 0) + { +
+

Nessun test eseguito.

+

Ogni test prova una strategia su un periodo passato, con tutte le combinazioni della sua griglia di parametri, costi realistici e poi doppi e tripli, e le convalide del protocollo anti-illusioni. Il piano si scrive prima di vedere i risultati e resta nel registro.

+
+ } + else + { +
+ + + + + + + + + + + + + + + + @foreach (var t in _trials) + { + var s = t.Summary; + + + + + + + + + + + + } + +
PianificatoStrategiaDatiVariantiStatoEsitoRisultato nettoSharpeCalo massimo
@Format.ShortDate(t.CreatedAt, Shell.Now, Shell.Zone)@Name(t.Strategy)@LabText.Symbols(t.Plan.Data.Symbols)
@LabText.Period(t.Plan.Data.From, t.Plan.Data.To)
@t.Variants@LabText.Status(t, _lab?.Progress(t.Id)) + @if (t.Verdict is { } verdict) + { + @LabText.Verdict(verdict) + } + else + { + @LabText.Missing + } + @(s is null ? LabText.Missing : LabText.Money(s.NetResult, s.Currency, withSign: true))@(s is null ? LabText.Missing : LabText.Number(s.Sharpe))@(s is null ? LabText.Missing : LabText.Percent(s.MaxDrawdown))
+
+ } +
+ +@if (_lab is not null) +{ + var family = _family; +
+
+

Nuovo test

+ +
+

@family.Description

+
+ + + + + + + + +

Simboli del mercato scelto: BTC-EUR su OKX, AAPL su Alpaca, BTCUSDT nello storico di ricerca. @Research()

+
+
+ + +
+
+ + +
+
+ + + @if (family.Parameters.Count > 0) + { +
+ Griglia dei parametri + @foreach (var p in family.Parameters) + { + + v.ToString(CultureInfo.InvariantCulture)))" /> +

Da @p.Min.ToString(CultureInfo.InvariantCulture) a @p.Max.ToString(CultureInfo.InvariantCulture), valori separati da virgole; ogni combinazione è una variante.

+ } +
+ } +

Soglie del protocollo, fissate nel piano: risultato netto positivo anche con costi doppi, Deflated Sharpe Ratio almeno 0,90, probabilità di sovra-adattamento non oltre 0,40 con più varianti, calo massimo non oltre il 25%.

+ + +
+} + +
+

Migliorie in osservazione

+
+

Nessuna miglioria in osservazione.

+

Le modifiche che potrebbero migliorare il risultato o ridurre i costi si calcolano in ombra accanto alle regole in vigore e compaiono qui con l'effetto che avrebbero avuto. Arrivano con la Fase 9.

+
+
+ +
+

Stime di costo da confermare

+
+

Nessuna stima da confermare.

+

Quando le esecuzioni con soldi veri diranno che lo scostamento è più basso di quello previsto, la nuova stima aspetterà qui la tua conferma. Arrivano con la Fase 8.

+
+
+ +@code { + private Laboratory? _lab; + private IReadOnlyList _trials = []; + private IStrategyFamily _family = StrategyCatalog.All[0]; + private bool _running; + private string? _message; + private DateOnly _from; + private DateOnly _to; + private IReadOnlyList _research = []; + + [CascadingParameter] + public HttpContext? HttpContext { get; set; } + + [SupplyParameterFromQuery(Name = "strategia")] + public string? Strategia { get; set; } + + [SupplyParameterFromQuery(Name = "errore")] + public string? Errore { get; set; } + + protected override async Task LoadAsync() + { + var ct = HttpContext?.RequestAborted ?? CancellationToken.None; + var services = Host.Services!; + _lab = services.Lab; + _trials = await new TrialStore(services.Db, Clock).ListAsync(100, ct); + _running = _trials.Any(t => !t.IsFinished); + _family = StrategyCatalog.Find(Strategia ?? "") ?? StrategyCatalog.All[0]; + var today = DateOnly.FromDateTime(Clock.UtcNow.UtcDateTime); + _to = today.AddDays(-1); + _from = _to.AddYears(-3); + _research = new ResearchStore(Host.Config.Data.Research).Series() + .Where(s => s.Timeframe == Timeframes.Day) + .Select(s => s.Symbol) + .Order(StringComparer.Ordinal) + .ToList(); + _message = Errore switch + { + null => null, + "motore" => "Il laboratorio non è attivo in questo momento: riprova tra poco.", + TrialRequestErrors.Strategy => "Strategia sconosciuta: scegline una dall'elenco.", + TrialRequestErrors.Source => "Scegli da dove prendere i dati e, per le barre raccolte, il mercato.", + TrialRequestErrors.Symbols => $"Indica da 1 a {Laboratory.MaxSymbols} strumenti, separati da virgole.", + TrialRequestErrors.Dates => "Controlla le date: l'inizio deve venire prima della fine.", + TrialRequestErrors.Capital => "Il capitale iniziale deve essere un importo tra 1 e 10.000.000.", + TrialRequestErrors.Grid => "Un valore della griglia non si legge o è fuori dall'intervallo del parametro: usa numeri separati da virgole, con il punto per i decimali.", + TrialRequestErrors.Combinations => $"La griglia non dà combinazioni valide, oppure ne dà più di {Laboratory.MaxVariants}: restringila.", + _ => "Non è stato possibile avviare il test: riprova.", + }; + } + + private string Research() => _research.Count == 0 + ? "Lo storico di ricerca è vuoto: lo riempie la raccolta dei dati." + : "Nello storico di ricerca: " + LabText.Symbols(_research) + "."; + + private static string Name(string strategy) => StrategyCatalog.Find(strategy)?.Name ?? strategy; + + private string Title(Trial t) => $"{Name(t.Strategy)}, {LabText.Symbols(t.Plan.Data.Symbols)}, {Format.ShortDate(t.CreatedAt, Shell.Now, Shell.Zone)}"; +} diff --git a/src/Anubis.Server/Components/Pages/LaboratorioConfronto.razor b/src/Anubis.Server/Components/Pages/LaboratorioConfronto.razor new file mode 100644 index 0000000..73940cf --- /dev/null +++ b/src/Anubis.Server/Components/Pages/LaboratorioConfronto.razor @@ -0,0 +1,106 @@ +@inherits LoadedComponent +@page "/laboratorio/confronto" +@inject AnubisHost Host +@inject ShellData Shell +@inject IClock Clock +@using Anubis.Backtest +@using Anubis.Strategies + +Confronto tra test · Anubis + +

← Laboratorio

+ +@if (_a is null || _b is null) +{ + Scegli due test conclusi dall'elenco del Laboratorio. + return; +} + +@{ + var a = _a.Value; + var b = _b.Value; + var ra = a.Report; + var rb = b.Report; +} +
+

Confronto tra due test

+ @if (ra.DataFingerprint != rb.DataFingerprint) + { +

I due test hanno letto dati diversi (strumenti, periodo o valuta): il confronto è indicativo.

+ } + else + { +

Stessi dati per entrambi: le differenze vengono dalla strategia, dalla griglia o dalle soglie.

+ } +
+ + + + + + + + + + @Row("Esito", x => LabText.Verdict(x.Trial.Verdict)) + @Row("Dati", x => LabText.Symbols(x.Trial.Plan.Data.Symbols) + ", " + LabText.Period(x.Trial.Plan.Data.From, x.Trial.Plan.Data.To)) + @Row("Variante scelta", x => LabText.Parameters(x.Report.Chosen) + $" (di {x.Trial.Variants})") + @Row("Risultato netto", x => LabText.Money(x.Report.Metrics.NetResult, x.Report.Currency, withSign: true)) + @Row("Con costi doppi", x => x.Report.Costs.FirstOrDefault(k => k.Multiplier == 2m) is { } k ? LabText.Money(k.NetResult, x.Report.Currency, withSign: true) : LabText.Missing) + @Row("Rendimento annuo", x => LabText.Percent(x.Report.Metrics.AnnualReturn, 1, true)) + @Row("Comprare e tenere, annuo", x => LabText.Percent(x.Report.Benchmark.AnnualReturn, 1, true)) + @Row("Volatilità annua", x => LabText.Percent(x.Report.Metrics.Volatility)) + @Row("Sharpe annuo", x => LabText.Number(x.Report.Metrics.Sharpe)) + @Row("Calo massimo", x => LabText.Percent(x.Report.Metrics.MaxDrawdown)) + @Row("Operazioni chiuse", x => x.Report.Metrics.Trades.ToString(CultureInfo.InvariantCulture)) + @Row("Operazioni in guadagno", x => x.Report.Metrics.Trades == 0 ? LabText.Missing : LabText.Percent(x.Report.Metrics.WinRate, 0)) + @Row("Rapporto guadagni/perdite", x => LabText.Number(x.Report.Metrics.ProfitFactor)) + @Row("Deflated Sharpe Ratio", x => LabText.Number(x.Report.DeflatedSharpe.Value)) + @Row("Probabilità di sovra-adattamento", x => x.Report.Overfitting is { } p ? LabText.Number(p.Probability) : "non si applica") + @Row("Finestre che avanzano, rendimento", x => x.Report.WalkForward is { } w ? LabText.Percent(w.Return, 1, true) : LabText.Missing) + @Row("Decisioni cambiate dai dati futuri", x => x.Report.LookAhead.Changed.ToString(CultureInfo.InvariantCulture)) + +
@Name(a)@Name(b)
+
+
+ +@code { + private (Trial Trial, BacktestReport Report)? _a; + private (Trial Trial, BacktestReport Report)? _b; + + [CascadingParameter] + public HttpContext? HttpContext { get; set; } + + // Text, not Guid: a malformed value must give "servono due test", not an error page. + [SupplyParameterFromQuery(Name = "a")] + public string? A { get; set; } + + [SupplyParameterFromQuery(Name = "b")] + public string? B { get; set; } + + protected override async Task LoadAsync() + { + var ct = HttpContext?.RequestAborted ?? CancellationToken.None; + var store = new TrialStore(Host.Services!.Db, Clock); + _a = await ReadAsync(store, A, ct); + _b = await ReadAsync(store, B, ct); + } + + private static async Task<(Trial, BacktestReport)?> ReadAsync(TrialStore store, string? id, CancellationToken ct) + { + if (!Guid.TryParse(id, out var key) || await store.GetAsync(key, ct) is not { } trial || await store.ReportJsonAsync(key, ct) is not { } json) + { + return null; + } + + return (trial, BacktestReport.FromJson(json)); + } + + private string Name((Trial Trial, BacktestReport Report) x) => + $"{StrategyCatalog.Find(x.Trial.Strategy)?.Name ?? x.Trial.Strategy}, {Format.ShortDate(x.Trial.CreatedAt, Shell.Now, Shell.Zone)}"; + + private RenderFragment Row(string label, Func<(Trial Trial, BacktestReport Report), string> value) => __builder => + { + @label@value(_a!.Value)@value(_b!.Value) + }; +} diff --git a/src/Anubis.Server/Components/Pages/LaboratorioProva.razor b/src/Anubis.Server/Components/Pages/LaboratorioProva.razor new file mode 100644 index 0000000..723ebc7 --- /dev/null +++ b/src/Anubis.Server/Components/Pages/LaboratorioProva.razor @@ -0,0 +1,336 @@ +@inherits LoadedComponent +@page "/laboratorio/{Id:guid}" +@inject AnubisHost Host +@inject ShellData Shell +@inject IClock Clock +@using Anubis.Backtest +@using Anubis.Strategies + +Test del laboratorio · Anubis + +@if (_trial is null) +{ + Torna al Laboratorio per l'elenco dei test. + return; +} + +@{ + var t = _trial; + var plan = t.Plan; + var r = _report; +} +@if (!t.IsFinished) +{ + +} + +

← Laboratorio

+ +
+
+

@Name(t.Strategy) su @LabText.Symbols(plan.Data.Symbols)

+ @if (t.Verdict is { } verdict) + { + @LabText.Verdict(verdict) + } + else + { + @LabText.Status(t, _progress) + } +
+
+
Dati
+
@LabText.Source(plan.Data), @LabText.Period(plan.Data.From, plan.Data.To)
+
Varianti provate
+
@t.Variants (@string.Join("; ", plan.Grid.Select(g => g.Key + " " + string.Join(", ", g.Value.Select(v => v.ToString(CultureInfo.InvariantCulture))))))
+
Capitale iniziale
+
@(r is null ? Format.Number(plan.InitialCapital, 2) : Format.Money(plan.InitialCapital, r.Currency))
+
Piano scritto
+
@Format.LongDateTime(t.CreatedAt, Shell.Zone), prima dei risultati. Impronta @Short(t.PlanHash)
+ @if (t.FinishedAt is { } finished) + { +
Concluso
+
@Format.LongDateTime(finished, Shell.Zone)@(r is null ? "" : $", in {LabText.Number(r.Seconds, 1)} secondi")
+ } +
+ @if (t.Status == TrialStatuses.Failed) + { + + } + else if (!t.IsFinished) + { +

@(t.Status == TrialStatuses.Planned ? "In coda: parte appena finisce il test precedente." : "In corso" + (_progress is null ? "." : ": " + _progress + ".")) La pagina si aggiorna da sola.

+ } +
+ +@if (r is not null) +{ + var c = r.Currency; + var m = r.Metrics; + var b = r.Benchmark; +
+

Esito del protocollo

+

Variante scelta: @LabText.Parameters(r.Chosen), la migliore zona della griglia e non il singolo punto migliore.

+
+ + + + @foreach (var check in r.Checks) + { + + + + + + + } + +
ControlloValoreSogliaEsito
@check.Name@LabText.CheckValue(check, c)@LabText.CheckThreshold(check, c) + @switch (check.Passed) + { + case true: + Superato + break; + case false: + Non superato + break; + default: + Non si applica + break; + } +
+
+
+ +
+

Risultato della variante scelta

+
+ + + + + + + + + + + + + + + + +
Variante sceltaComprare e tenere
Risultato netto@LabText.Money(m.NetResult, c, withSign: true)@LabText.Money(b.NetResult, c, withSign: true)
Rendimento totale@LabText.Percent(m.TotalReturn, 1, true)@LabText.Percent(b.TotalReturn, 1, true)
Rendimento annuo@LabText.Percent(m.AnnualReturn, 1, true)@LabText.Percent(b.AnnualReturn, 1, true)
Volatilità annua@LabText.Percent(m.Volatility)@LabText.Percent(b.Volatility)
Sharpe annuo@LabText.Number(m.Sharpe)@LabText.Number(b.Sharpe)
Calo massimo@LabText.Percent(m.MaxDrawdown)@LabText.Percent(b.MaxDrawdown)
Operazioni chiuse@m.Trades@LabText.Missing
Operazioni in guadagno@(m.Trades == 0 ? LabText.Missing : LabText.Percent(m.WinRate, 0))@LabText.Missing
Rapporto guadagni/perdite@(m.ProfitFactor is { } pf ? LabText.Number(pf) : m.Trades > 0 ? "nessuna perdita" : LabText.Missing)@LabText.Missing
Guadagno e perdita medi@LabText.Money(m.AverageWin, c, true) / @LabText.Money(m.AverageLoss, c, true)@LabText.Missing
Commissioni@LabText.Money(m.Fees, c)@LabText.Money(b.Fees, c)
Tempo investito, in media@LabText.Percent(m.Exposure, 0)@LabText.Percent(b.Exposure, 0)
+
+ +
+ +
+

Risultato per anno

+
+ + + + @foreach (var y in m.Years) + { + var bh = b.Years.FirstOrDefault(x => x.Year == y.Year); + + } + +
AnnoRisultatoRendimentoComprare e tenere
@y.Year@LabText.Money(y.Result, c, true)@LabText.Percent(y.Return, 1, true)@(bh is null ? LabText.Missing : LabText.Percent(bh.Return, 1, true))
+
+
+ +
+

Sensibilità ai costi

+

Commissioni e scostamento moltiplicati: una strategia che muore con i costi doppi non passa.

+
+ + + + @foreach (var k in r.Costs) + { + + } + +
CostiRisultato nettoRendimentoSharpeCalo massimoCommissioni
@(k.Multiplier == 1m ? "realistici" : $"× {Format.Number(k.Multiplier)}")@LabText.Money(k.NetResult, c, true)@LabText.Percent(k.TotalReturn, 1, true)@LabText.Number(k.Sharpe)@LabText.Percent(k.MaxDrawdown)@LabText.Money(k.Fees, c)
+
+
+ +
+

Convalide

+

Sharpe corretto per il numero di prove

+ @{ var d = r.DeflatedSharpe; var annual = Math.Sqrt(r.PeriodsPerYear); } +

Deflated Sharpe Ratio @LabText.Number(d.Value): la probabilità che lo Sharpe della variante scelta (@LabText.Number(d.Sharpe * annual) all'anno) superi quello che il migliore di @d.Trials tentativi mostrerebbe per caso (@LabText.Number(d.Benchmark * annual) all'anno). Contati tutti i test di questa strategia: @r.TrialsCounted varianti. Su @d.Observations sedute, asimmetria @LabText.Number(d.Skewness), curtosi @LabText.Number(d.Kurtosis).

+ +

Probabilità di sovra-adattamento

+ @if (r.Overfitting is { } pbo) + { +

PBO @LabText.Number(pbo.Probability): in @Format.Number(pbo.Combinations) divisioni del periodo in due metà, la quota di volte in cui la variante migliore in una metà è finita nella metà peggiore dell'altra. Con @pbo.Variants varianti e @pbo.Groups blocchi.

+ } + else + { +

Con una sola variante non c'è nulla da scegliere, quindi nulla da sovra-adattare: il controllo non si applica.

+ } + +

Finestre che avanzano

+ @if (r.WalkForward is { } wf) + { +

Ogni finestra sceglie la variante sui dati precedenti e la giudica su quelli successivi, mai visti. Rendimento fuori campione complessivo @LabText.Percent(wf.Return, 1, true), Sharpe @LabText.Number(wf.Sharpe * annual) all'anno; @wf.Purge sedute tolte tra addestramento e prova.

+
+ + + + @foreach (var w in wf.Windows) + { + + } + +
FinestraScelta suProvata suVarianteRendimentoSharpe
@w.Window@LabText.Period(w.TrainFrom, w.TrainTo)@LabText.Period(w.TestFrom, w.TestTo)@LabText.Parameters(w.Chosen)@LabText.Percent(w.Return, 1, true)@LabText.Number(w.Sharpe * annual)
+
+ } + else + { +

Periodo troppo breve per le finestre.

+ } + +

K-fold purgato combinatorio

+ @if (r.Combinatorial is { } cv) + { +

@cv.Groups blocchi, @cv.TestGroups alla volta in prova: @cv.Splits divisioni e @cv.PathSharpes.Count percorsi completi fuori campione, con @cv.Purge sedute tolte prima di ogni blocco di prova e @cv.Embargo dopo. Sharpe medio dei percorsi @LabText.Number(cv.MeanSharpe * annual) all'anno, il peggiore @LabText.Number(cv.WorstSharpe * annual); percorsi in positivo @LabText.Percent(cv.PositiveShare, 0).

+ } + else + { +

Periodo troppo breve per i blocchi.

+ } + +

Nessuno sguardo in avanti

+

+ Il test è stato rifatto con i dati tagliati al @string.Join(", ", r.LookAhead.Cutoffs.Select(x => LabText.Day(x))): delle @r.LookAhead.Decisions decisioni confrontate, + @(r.LookAhead.Changed == 0 ? "nessuna è cambiata." : $"{r.LookAhead.Changed} sono cambiate: la strategia guarda dati che al momento della decisione non esistevano.") +

+ @if (r.LookAhead.Examples.Count > 0) + { +
    + @foreach (var e in r.LookAhead.Examples) + { +
  • @e
  • + } +
+ } +
+ +
+

Tutte le varianti

+

Ogni variante vale il suo Sharpe, ma non più della media della sua zona (lei e le vicine nella griglia): si preferisce una collina larga a un picco isolato.

+
+ + + + @foreach (var v in r.Variants) + { + + + + + + + + + } + +
ParametriSharpe annuoPunteggio di zonaRisultato nettoCalo massimoOperazioni
@LabText.Parameters(v.Parameters)@if (v.Chosen) { scelta}@LabText.Number(v.Sharpe * annual)@LabText.Number(v.PlateauScore * annual)@LabText.Money(v.NetResult, c, true)@LabText.Percent(v.MaxDrawdown)@v.Trades
+
+
+ +
+

Cosa questo test non considera

+
    + @foreach (var l in r.Limitations) + { +
  • @l
  • + } +
+ @if (r.Notes.Count > 0) + { +

Ordini non eseguiti come chiesto

+
    + @foreach (var (reason, count) in r.Notes.OrderByDescending(n => n.Value)) + { +
  • @LabText.Note(reason): @count
  • + } +
+ } +

Dati usati

+
+ + + + @foreach (var i in r.Instruments) + { + + } + +
StrumentoOriginePeriodoBarreAzioni societarie
@i.Symbol@i.Source@LabText.Day(i.First) – @LabText.Day(i.Last)@i.Bars@i.Actions
+
+

Impronta dei dati @Short(r.DataFingerprint): due test con la stessa impronta hanno letto esattamente gli stessi dati. Motore versione @plan.Engine, strategia versione @r.StrategyVersion.

+
+ + @if (r.Trades.Count > 0) + { +
+

Operazioni della variante scelta

+
+ Vedi le ultime @r.Trades.Count operazioni +
+ + + + @foreach (var tr in r.Trades.Reverse()) + { + + } + +
StrumentoApertaChiusaInvestitoRisultatoCommissioni
@tr.Symbol@LabText.Day(tr.Opened)@(tr.Closed is { } closed ? LabText.Day(closed) : "aperta")@LabText.Money(tr.Invested, c)@LabText.Money(tr.Result, c, true)@LabText.Money(tr.Fees, c)
+
+
+
+ } +} + +@code { + private Trial? _trial; + private BacktestReport? _report; + private string? _progress; + private IReadOnlyList _curve = []; + + [Parameter] + public Guid Id { get; set; } + + [CascadingParameter] + public HttpContext? HttpContext { get; set; } + + protected override async Task LoadAsync() + { + var ct = HttpContext?.RequestAborted ?? CancellationToken.None; + var services = Host.Services!; + var store = new TrialStore(services.Db, Clock); + _trial = await store.GetAsync(Id, ct); + if (_trial is null) + { + return; + } + + _progress = services.Lab?.Progress(Id); + if (await store.ReportJsonAsync(Id, ct) is { } json) + { + _report = BacktestReport.FromJson(json); + _curve = _report.Curve.Select(p => new Anubis.Server.EquityPoint(new DateTimeOffset(p.Day.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero), p.Day, p.Equity, p.Benchmark)).ToList(); + } + } + + private static string Name(string strategy) => StrategyCatalog.Find(strategy)?.Name ?? strategy; + + private static string Short(string hash) => hash.Length > 12 ? hash[..12] : hash; +} diff --git a/src/Anubis.Server/Forms.cs b/src/Anubis.Server/Forms.cs index 816fd1e..e13a5ed 100644 --- a/src/Anubis.Server/Forms.cs +++ b/src/Anubis.Server/Forms.cs @@ -34,6 +34,7 @@ internal static partial class Forms signed.MapPost("/chiavi/{broker}", SaveKeysAsync); signed.MapPost("/chiavi/{broker}/togli", RemoveKeysAsync); signed.MapPost("/capitale/{broker}", SetCapitalAsync); + signed.MapPost("/laboratorio", PlanTrialAsync); } private static string Back(IFormCollection form, string fallback) @@ -324,6 +325,93 @@ internal static partial class Forms return Results.Redirect(back + "?ok=capitale#conti"); } + /// + /// A new backtest trial (SPEC 12.2): the plan is written and queued at once, then the page of the trial follows it. + /// A field that cannot be read goes back as a code; the chosen strategy is kept, nothing typed travels in the address. + /// + private static async Task PlanTrialAsync(IFormCollection form, AnubisHost host, CancellationToken ct) + { + var family = Anubis.Strategies.StrategyCatalog.Find(form["strategia"].ToString()); + string Back(string code) => "/laboratorio?errore=" + code + (family is null ? "" : "&strategia=" + Uri.EscapeDataString(family.Id)) + "#nuovo"; + if (host.IsDemo || host.Services?.Lab is not { } lab) + { + return Results.Redirect(Back("motore")); + } + + if (family is null) + { + return Results.Redirect(Back(Anubis.Backtest.TrialRequestErrors.Strategy)); + } + + var source = form["origine"].ToString(); + var venue = source == Anubis.Backtest.TrialSources.Research ? Anubis.Data.Venues.Binance : form["mercato"].ToString(); + if (source == Anubis.Backtest.TrialSources.Database && venue is not (Anubis.Data.Venues.Okx or Anubis.Data.Venues.Alpaca)) + { + return Results.Redirect(Back(Anubis.Backtest.TrialRequestErrors.Source)); + } + + if (ParseDay(form["dal"].ToString()) is not { } from || ParseDay(form["al"].ToString()) is not { } to) + { + return Results.Redirect(Back(Anubis.Backtest.TrialRequestErrors.Dates)); + } + + if (ParseEuro(form["capitale"].ToString()) is not { } capital) + { + return Results.Redirect(Back(Anubis.Backtest.TrialRequestErrors.Capital)); + } + + var grid = new Dictionary>(StringComparer.Ordinal); + foreach (var p in family.Parameters) + { + var text = form["griglia_" + p.Name].ToString(); + if (string.IsNullOrWhiteSpace(text)) + { + continue; + } + + if (ParseList(text) is not { } values) + { + return Results.Redirect(Back(Anubis.Backtest.TrialRequestErrors.Grid)); + } + + grid[p.Name] = values; + } + + var symbols = form["strumenti"].ToString() + .Split([',', ';', ' ', '\n', '\r', '\t'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(x => x.ToUpperInvariant()) + .ToList(); + try + { + var trial = await lab.PlanAsync(new Anubis.Backtest.TrialRequest(family.Id, source, venue, symbols, from, to, capital, grid), ct).ConfigureAwait(false); + return Results.Redirect("/laboratorio/" + trial.Id.ToString("D")); + } + catch (Anubis.Backtest.TrialRequestException ex) + { + return Results.Redirect(Back(ex.Code)); + } + } + + private static DateOnly? ParseDay(string text) => + DateOnly.TryParseExact(text, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out var day) ? day : null; + + /// Values of a grid as people type them: "10, 20, 30" or "10 20 30"; decimals with a point. + internal static IReadOnlyList? ParseList(string text) + { + var result = new List(); + foreach (var item in (text ?? "").Split([',', ';', ' '], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + if (!decimal.TryParse(item, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out var value)) + { + return null; + } + + result.Add(value); + } + + return result.Count is > 0 and <= 20 ? result : null; + } + /// An amount as people type it: "1500", "1.500", "1500,50", "1.500,50", "1500.50". internal static decimal? ParseEuro(string text) { diff --git a/src/Anubis.Server/LabText.cs b/src/Anubis.Server/LabText.cs new file mode 100644 index 0000000..97015f2 --- /dev/null +++ b/src/Anubis.Server/LabText.cs @@ -0,0 +1,88 @@ +using Anubis.Backtest; +using Anubis.Core; + +namespace Anubis.Server; + +/// How the laboratory pages write the numbers of a backtest, in plain Italian (SPEC 13.3). +internal static class LabText +{ + public const string Missing = "—"; + + /// A fraction as a percentage: 0,1234 is "12,3%". + public static string Percent(double? fraction, int decimals = 1, bool withSign = false) => + fraction is { } x && double.IsFinite(x) ? Format.Percent((decimal)(x * 100), decimals, withSign) : Missing; + + public static string Number(double? value, int decimals = 2) => + value is { } x && double.IsFinite(x) ? Format.Number((decimal)x, decimals) : Missing; + + public static string Money(decimal value, string currency, bool withSign = false) => Format.Money(value, currency, withSign); + + public static string Day(DateOnly? day) => day is { } d ? d.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture) : Missing; + + public static string Period(DateOnly from, DateOnly to) => $"{Day(from)} – {Day(to)}"; + + /// Parameters as the user reads them: "fast=20;slow=100" becomes "fast 20, slow 100". + public static string Parameters(string text) => + string.IsNullOrEmpty(text) ? "nessun parametro" : string.Join(", ", text.Split(';').Select(p => p.Replace('=', ' ').Replace('.', ','))); + + public static string Status(Trial trial, string? progress) => trial.Status switch + { + TrialStatuses.Planned => "In coda", + TrialStatuses.Running => progress is null ? "In corso" : "In corso: " + progress, + TrialStatuses.Failed => "Non riuscito", + _ => "Completato", + }; + + public static string Verdict(string? verdict) => verdict switch + { + TrialVerdicts.Passed => "Superato", + TrialVerdicts.Rejected => "Non superato", + _ => Missing, + }; + + public static string Source(TrialData data) => data.Source == TrialSources.Research + ? "storico di ricerca " + Venue(data.Venue) + : "barre raccolte di " + Venue(data.Venue); + + public static string Venue(string venue) => venue switch + { + "okx" => "OKX", + "alpaca" => "Alpaca", + "binance" => "Binance", + _ => venue, + }; + + public static string Symbols(IReadOnlyList symbols) => + symbols.Count <= 4 ? string.Join(", ", symbols) : string.Join(", ", symbols.Take(3)) + $" e altri {symbols.Count - 3}"; + + /// The value of a check, written in its unit. + public static string CheckValue(ReportCheck check, string currency) => check.Value is not { } v + ? "non si applica" + : check.Unit switch + { + "money" => Format.Money((decimal)v, currency, withSign: true), + "percent" => Percent(v), + "count" => Format.Number((decimal)v), + _ => Number(v), + }; + + public static string CheckThreshold(ReportCheck check, string currency) => check.Comparison + " " + check.Unit switch + { + "money" => Format.Money((decimal)check.Threshold, currency), + "percent" => Percent(check.Threshold, 0), + "count" => Format.Number((decimal)check.Threshold), + _ => Number(check.Threshold), + }; + + /// What the engine did not do as asked, in words. + public static string Note(string reason) => reason switch + { + BacktestReasons.NoCash => "Acquisti ridotti per mancanza di liquidità", + BacktestReasons.NoPrice => "Ordini senza prezzo di apertura", + BacktestReasons.UnknownSymbol => "Strumenti chiesti dalla strategia ma assenti dai dati", + BacktestReasons.NoShortSelling => "Vendite allo scoperto rifiutate", + BacktestReasons.WrittenOff => "Residui sotto il minimo lasciati dopo un'uscita", + BacktestReasons.Dividend => "Dividendi incassati", + _ => "Ordini sotto il minimo dello strumento, non inviati", + }; +} diff --git a/src/Anubis/Backtest/Costs.cs b/src/Anubis/Backtest/Costs.cs new file mode 100644 index 0000000..7f26ec2 --- /dev/null +++ b/src/Anubis/Backtest/Costs.cs @@ -0,0 +1,94 @@ +using Anubis.Data; +using Anubis.Db; +using Anubis.Trading; + +namespace Anubis.Backtest; + +/// A fill of the backtest: quantity received or given, cash moved and fee, all in the dataset currency. +public sealed record CostedFill(decimal Quantity, decimal Price, decimal Gross, decimal CashFee, decimal CoinFee, decimal CashDelta) +{ + /// Every fee valued in the dataset currency: cash fees plus the coin kept by the venue at the fill price. + public decimal FeeValue => CashFee + DbValues.Money(CoinFee * Price); +} + +/// +/// The costs of SPEC 12.1 for a market order: the same price lists and slippage tiers as the internal simulator +/// (SPEC 7.3, ADR 0009 point 5), rounded the cautious way. scales fees and slippage +/// together, for the runs at double and triple costs of SPEC 12.2, step 3. +/// +public sealed record CostModel(decimal Multiplier = 1m) +{ + public static CostModel Standard { get; } = new(); + + /// The price list of the venue where the instrument would trade. Research archives trade like OKX. + public static PaperOptions PriceList(InstrumentSpec spec) + { + ArgumentNullException.ThrowIfNull(spec); + return spec.Venue == Venues.Alpaca ? PaperOptions.Alpaca() : PaperOptions.Okx(spec.QuoteCurrency); + } + + public decimal SlippageRate(InstrumentSpec spec) => PaperBroker.SlippageBp(spec) / 10_000m * Multiplier; + + /// The price paid or received: the reference moved by the slippage, rounded to the tick against us. + public decimal ExecutionPrice(InstrumentSpec spec, OrderSide side, decimal reference) + { + ArgumentNullException.ThrowIfNull(spec); + var slip = SlippageRate(spec); + var price = side == OrderSide.Buy ? reference * (1m + slip) : reference * (1m - slip); + var tick = OrderRules.TickOf(spec, price); + price = tick is > 0m ? (side == OrderSide.Buy ? OrderRules.RoundUp(price, tick.Value) : OrderRules.RoundDown(price, tick.Value)) : price; + return DbValues.Money(price); + } + + /// The cost of a market order of at , as the venue would charge it. + public CostedFill Fill(InstrumentSpec spec, OrderSide side, decimal quantity, decimal price) + { + var list = PriceList(spec); + var taker = list.TakerFee * Multiplier; + var gross = DbValues.Money(quantity * price); + var share = list.ShareFee > 0m ? OrderRules.RoundUp(quantity * list.ShareFee * Multiplier, 0.01m) : 0m; + if (side == OrderSide.Buy) + { + if (list.FeeInKind) + { + var coin = DbValues.Money(quantity * taker); + return new CostedFill(quantity - coin, price, gross, 0m, coin, -gross); + } + + var fee = DbValues.Money(gross * taker) + share; + return new CostedFill(quantity, price, gross, fee, 0m, -(gross + fee)); + } + + var sellFee = DbValues.Money(gross * taker) + share + + (list.SellValueFee > 0m ? OrderRules.RoundUp(gross * list.SellValueFee * Multiplier, 0.01m) : 0m); + return new CostedFill(quantity, price, gross, sellFee, 0m, gross - sellFee); + } + + /// The largest quantity whose purchase, fees included, fits in : no leverage (SPEC 9.1). + public decimal Affordable(InstrumentSpec spec, decimal cash, decimal price) + { + ArgumentNullException.ThrowIfNull(spec); + if (cash <= 0m || price <= 0m) + { + return 0m; + } + + var list = PriceList(spec); + var perUnit = price * (1m + (list.FeeInKind ? 0m : list.TakerFee * Multiplier)) + (list.ShareFee * Multiplier); + var lot = OrderRules.LotOf(spec); + var quantity = OrderRules.RoundDown(cash / perUnit, lot); + // Fees rounded up to the cent can push the total a little past the cash: step back by the excess. + for (var guard = 0; guard < 8 && quantity > 0m; guard++) + { + var excess = -Fill(spec, OrderSide.Buy, quantity, price).CashDelta - cash; + if (excess <= 0m) + { + break; + } + + quantity = OrderRules.RoundDown(quantity - (excess / perUnit) - lot, lot); + } + + return Math.Max(quantity, 0m); + } +} diff --git a/src/Anubis/Backtest/Dataset.cs b/src/Anubis/Backtest/Dataset.cs new file mode 100644 index 0000000..1d2247e --- /dev/null +++ b/src/Anubis/Backtest/Dataset.cs @@ -0,0 +1,186 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text; +using Anubis.Data; +using Anubis.Strategies; + +namespace Anubis.Backtest; + +/// +/// One instrument of a backtest: its rules, its real daily bars (oldest first, one per day) and its corporate +/// actions. Signals read the bars adjusted and anchored to the decision day; executions use the real prices. +/// +public sealed class BacktestInstrument +{ + private readonly Dictionary _index; + + public BacktestInstrument(InstrumentSpec spec, IReadOnlyList bars, IReadOnlyList? actions = null, string source = "") + { + ArgumentNullException.ThrowIfNull(spec); + ArgumentNullException.ThrowIfNull(bars); + Spec = spec; + Bars = [.. bars.OrderBy(b => b.Day)]; + Actions = [.. (actions ?? []).Where(a => a.Symbol == spec.Symbol).OrderBy(a => a.ExDate)]; + Source = source; + _index = new Dictionary(Bars.Count); + for (var i = 0; i < Bars.Count; i++) + { + if (!_index.TryAdd(Bars[i].Day, i)) + { + throw new ArgumentException("Two bars on the same day for " + spec.Symbol, nameof(bars)); + } + } + + // Cumulative price factor of every bar: the product of the factors of the actions that come after it. A bar + // anchored to day A is the real bar times Factors[i] / Factors[A] (SPEC 6.10). + Factors = new double[Bars.Count]; + var running = 1d; + var next = Actions.Count - 1; + while (next >= 0 && Bars.Count > 0 && Actions[next].ExDate > Bars[^1].Day) + { + next--; + } + + for (var i = Bars.Count - 1; i >= 0; i--) + { + Factors[i] = running; + while (next >= 0 && Actions[next].ExDate > (i > 0 ? Bars[i - 1].Day : DateOnly.MinValue) && Actions[next].ExDate <= Bars[i].Day) + { + running *= Adjustments.PriceFactor(Actions[next], i > 0 ? Bars[i - 1].Close : null); + next--; + } + } + } + + public InstrumentSpec Spec { get; } + + public string Symbol => Spec.Symbol; + + public IReadOnlyList Bars { get; } + + public IReadOnlyList Actions { get; } + + /// Where the bars come from, in words for the report. + public string Source { get; } + + internal double[] Factors { get; } + + public int IndexOf(DateOnly day) => _index.TryGetValue(day, out var i) ? i : -1; + + /// Number of bars up to and including . + public int CountThrough(DateOnly day) + { + var lo = 0; + var hi = Bars.Count; + while (lo < hi) + { + var mid = (lo + hi) / 2; + if (Bars[mid].Day <= day) + { + lo = mid + 1; + } + else + { + hi = mid; + } + } + + return lo; + } + + public BarSeries SeriesThrough(DateOnly day) => new(Bars, Factors, CountThrough(day)); + + internal BacktestInstrument Through(DateOnly day) => + new(Spec, [.. Bars.Where(b => b.Day <= day)], [.. Actions.Where(a => a.ExDate <= day)], Source); +} + +/// +/// The data of a backtest: instruments in one currency, and the sessions (every day with at least one bar). Built +/// once and never changed, so the same dataset and parameters always give the same result (SPEC 12.1). +/// +public sealed class BacktestDataset +{ + private readonly Dictionary _bySymbol; + + public BacktestDataset(IEnumerable instruments, string currency) + { + ArgumentNullException.ThrowIfNull(instruments); + Instruments = [.. instruments.OrderBy(i => i.Symbol, StringComparer.Ordinal)]; + Currency = currency; + _bySymbol = Instruments.ToDictionary(i => i.Symbol, StringComparer.Ordinal); + Sessions = [.. Instruments.SelectMany(i => i.Bars.Select(b => b.Day)).Distinct().Order()]; + var wrong = Instruments.FirstOrDefault(i => i.Spec.QuoteCurrency != currency); + if (wrong is not null) + { + throw new ArgumentException($"{wrong.Symbol} is in {wrong.Spec.QuoteCurrency}, the backtest is in {currency}", nameof(instruments)); + } + } + + public IReadOnlyList Instruments { get; } + + public string Currency { get; } + + public IReadOnlyList Sessions { get; } + + public bool IsCrypto => Instruments.Count > 0 && Instruments.All(i => i.Spec.AssetClass == AssetClasses.Crypto); + + /// Sessions in a year, to annualise: crypto trades every day, stocks about 252 days. + public int PeriodsPerYear => IsCrypto ? 365 : 252; + + public BacktestInstrument? this[string symbol] => _bySymbol.GetValueOrDefault(symbol); + + /// The same data cut after : what was known on that day (SPEC 12.2, step 2). + public BacktestDataset Through(DateOnly day) => new(Instruments.Select(i => i.Through(day)), Currency); + + /// The same data between two days, both included. + public BacktestDataset Between(DateOnly from, DateOnly to) => new( + Instruments.Select(i => new BacktestInstrument(i.Spec, [.. i.Bars.Where(b => b.Day >= from && b.Day <= to)], [.. i.Actions.Where(a => a.ExDate > from && a.ExDate <= to)], i.Source)) + .Where(i => i.Bars.Count > 0), + Currency); + + /// + /// A SHA-256 of every bar, action and rule used: two reports with the same fingerprint and the same plan were + /// computed on exactly the same data. + /// + public string Fingerprint() + { + var text = new StringBuilder(); + text.Append(Currency).Append('\n'); + foreach (var i in Instruments) + { + var s = i.Spec; + text.Append(CultureInfo.InvariantCulture, $"{s.Venue}|{s.Symbol}|{s.AssetClass}|{s.QuoteCurrency}|{s.TickSize}|{s.LotSize}|{s.MinSize}|{s.MinNotional}|{s.Fractionable}\n"); + foreach (var b in i.Bars) + { + text.Append(CultureInfo.InvariantCulture, $"{b.Day:yyyy-MM-dd},{b.Open:R},{b.High:R},{b.Low:R},{b.Close:R},{b.Volume:R}\n"); + } + + foreach (var a in i.Actions) + { + text.Append(CultureInfo.InvariantCulture, $"{a.Kind},{a.ExDate:yyyy-MM-dd},{a.Ratio},{a.CashAmount}\n"); + } + } + + return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(text.ToString()))); + } + + /// When a session opens and closes: crypto at midnight UTC (trap 23), stocks 09:30–16:00 in New York. + public (DateTimeOffset Opens, DateTimeOffset Closes) SessionTimes(DateOnly day) + { + if (IsCrypto) + { + var open = new DateTimeOffset(day.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero); + return (open, open.AddDays(1)); + } + + return (NewYork(day, new TimeOnly(9, 30)), NewYork(day, new TimeOnly(16, 0))); + } + + private static readonly TimeZoneInfo NewYorkZone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York"); + + private static DateTimeOffset NewYork(DateOnly day, TimeOnly time) + { + var local = day.ToDateTime(time); + return new DateTimeOffset(local, NewYorkZone.GetUtcOffset(local)).ToUniversalTime(); + } +} diff --git a/src/Anubis/Backtest/Engine.cs b/src/Anubis/Backtest/Engine.cs new file mode 100644 index 0000000..4c24d5f --- /dev/null +++ b/src/Anubis/Backtest/Engine.cs @@ -0,0 +1,563 @@ +using Anubis.Data; +using Anubis.Db; +using Anubis.Strategies; +using Anubis.Trading; + +namespace Anubis.Backtest; + +/// How a backtest runs (SPEC 12.1). +public sealed record BacktestOptions +{ + public decimal InitialCapital { get; init; } = 10_000m; + + public CostModel Costs { get; init; } = CostModel.Standard; + + /// First and last session simulated; null means the whole dataset. + public DateOnly? From { get; init; } + + public DateOnly? To { get; init; } + + /// The correctives of SPEC 8.5. Until Phase 5 they let everything through. + public IReadOnlyList Correctives { get; init; } = [PassThroughStage.Correctives]; + + /// The risk engine of SPEC 9. Until Phase 5 it lets everything through. + public IIntentStage Risk { get; init; } = PassThroughStage.Risk; +} + +/// A book to start from: a state recorded live, for the one-step comparison of SPEC 12.3. +public sealed record PortfolioState(decimal Cash, IReadOnlyDictionary Holdings) +{ + public static PortfolioState OnlyCash(decimal cash) => new(cash, new Dictionary()); +} + +public sealed record EquityPoint(DateOnly Day, decimal Equity, decimal Cash, decimal Invested); + +/// An execution. is every fee valued in the dataset currency. +public sealed record BacktestFill(DateOnly Day, string Symbol, OrderSide Side, decimal Quantity, decimal Price, decimal Gross, decimal Fee, string Reason); + +/// An operation from zero to zero (SPEC 7.5). is net of every cost; an open one is valued at the last close. +public sealed record BacktestTrade(string Symbol, DateOnly Opened, DateOnly? Closed, decimal Invested, decimal Result, decimal Fees) +{ + public bool IsClosed => Closed is not null; +} + +/// What the strategy proposed on a day, and what was left after correctives and risk. +public sealed record DecisionRecord(DateOnly Day, IReadOnlyList Proposed, IReadOnlyList Final); + +/// Something the engine did not do as asked, and why: an order below the minimum, no cash, no price. +public sealed record BacktestNote(DateOnly Day, string Symbol, string Reason); + +/// The outcome of one run of one strategy on one dataset. +public sealed record BacktestRun( + string Currency, + decimal InitialCapital, + IReadOnlyList Equity, + IReadOnlyList Fills, + IReadOnlyList Trades, + IReadOnlyList Decisions, + IReadOnlyList Notes) +{ + public decimal FinalEquity => Equity.Count > 0 ? Equity[^1].Equity : InitialCapital; + + public decimal NetResult => FinalEquity - InitialCapital; + + public decimal Fees => Fills.Sum(f => f.Fee); + + public decimal Dividends { get; init; } + + /// Value of the leftovers below the minimum written off after an exit (SPEC 7.2). + public decimal WrittenOff { get; init; } + + /// Daily returns of the equity, one per session after the first. + public double[] DailyReturns() + { + var result = new double[Math.Max(Equity.Count - 1, 0)]; + for (var i = 1; i < Equity.Count; i++) + { + var before = Equity[i - 1].Equity; + result[i - 1] = before > 0m ? (double)(Equity[i].Equity / before) - 1d : 0d; + } + + return result; + } +} + +/// Reasons the engine writes in fills and notes. +public static class BacktestReasons +{ + public const string Stop = "stop"; + public const string StopGap = "stop_gap"; + public const string WrittenOff = "residue_written_off"; + public const string NoCash = "no_cash"; + public const string NoPrice = "no_price"; + public const string UnknownSymbol = "unknown_symbol"; + public const string NoShortSelling = "no_short_selling"; + public const string Dividend = "dividend"; +} + +/// +/// The event-driven backtest engine (SPEC 12.1). Each session is a sequence of events in time order: corporate +/// actions before the open, the open (stops that open below their level, then the orders decided at the previous +/// close, sales first), the session (stops touched by the low), the close (valuation and decision). A decision sees +/// the data up to its close and its orders go at the next open: the delay between deciding and executing. The same +/// classes run live: the strategy cannot tell the difference. +/// +public static class BacktestEngine +{ + /// Changes when the engine computes differently: plans record it, so a report says which engine made it. + public const string Version = "1"; + + public static BacktestRun Run(IStrategy strategy, BacktestDataset data, BacktestOptions? options = null, PortfolioState? start = null) + { + ArgumentNullException.ThrowIfNull(strategy); + ArgumentNullException.ThrowIfNull(data); + options ??= new BacktestOptions(); + CheckSchedule(strategy); + var simulation = new Simulation(strategy, data, options, start ?? PortfolioState.OnlyCash(options.InitialCapital)); + // Ordered by time, then by day: a crypto close and the next open fall on the same midnight, and the close of + // the earlier day comes first. + var events = new PriorityQueue<(SessionEvent Kind, DateOnly Day), (DateTimeOffset At, int Day, int Order)>(); + foreach (var day in data.Sessions.Where(d => (options.From is null || d >= options.From) && (options.To is null || d <= options.To))) + { + var (opens, closes) = data.SessionTimes(day); + events.Enqueue((SessionEvent.CorporateActions, day), (opens, day.DayNumber, 0)); + events.Enqueue((SessionEvent.Open, day), (opens, day.DayNumber, 1)); + events.Enqueue((SessionEvent.Session, day), (opens, day.DayNumber, 2)); + events.Enqueue((SessionEvent.Close, day), (closes, day.DayNumber, 3)); + } + + while (events.TryDequeue(out var e, out var when)) + { + switch (e.Kind) + { + case SessionEvent.CorporateActions: + simulation.CorporateActions(e.Day); + break; + case SessionEvent.Open: + simulation.Open(e.Day); + break; + case SessionEvent.Session: + simulation.Session(e.Day); + break; + default: + simulation.Close(e.Day, when.At); + break; + } + } + + return simulation.Finish(); + } + + /// + /// One decision cycle from a recorded book (SPEC 12.1, 12.3): the strategy decides on the close of + /// with the data known then and the book given, and nothing else runs. + /// + public static DecisionRecord DecideOnce(IStrategy strategy, BacktestDataset data, PortfolioState state, DateOnly day, BacktestOptions? options = null) + { + ArgumentNullException.ThrowIfNull(strategy); + ArgumentNullException.ThrowIfNull(data); + ArgumentNullException.ThrowIfNull(state); + CheckSchedule(strategy); + var simulation = new Simulation(strategy, data.Through(day), options ?? new BacktestOptions(), state); + simulation.MarkThrough(day); + return simulation.Decide(day, data.SessionTimes(day).Closes); + } + + private static void CheckSchedule(IStrategy strategy) + { + if (strategy.Schedule().Any(t => t.Anchor != DecisionAnchor.Close)) + { + throw new NotSupportedException($"{strategy.Id}: the daily backtest decides only on the close of a session"); + } + } + + private enum SessionEvent + { + CorporateActions, + Open, + Session, + Close, + } + + private sealed class Position + { + public decimal Quantity { get; set; } + + public decimal AveragePrice { get; set; } + + public decimal? Stop { get; set; } + + public DateOnly EnteredOn { get; init; } + + public required OpenTrade Trade { get; init; } + } + + private sealed class OpenTrade + { + public DateOnly Opened { get; init; } + + public decimal Bought { get; set; } + + public decimal CashFlow { get; set; } + + public decimal Fees { get; set; } + } + + private sealed record Pending(string Symbol, OrderSide Side, decimal Quantity, bool Exit, decimal? Stop, string Reason); + + private sealed class Simulation + { + private readonly IStrategy _strategy; + private readonly BacktestDataset _data; + private readonly BacktestOptions _options; + private readonly CostModel _costs; + private readonly SortedDictionary _positions = new(StringComparer.Ordinal); + private readonly SortedDictionary _pending = new(StringComparer.Ordinal); + private readonly Dictionary _lastClose = new(StringComparer.Ordinal); + private readonly Dictionary _actionsDoneThrough = new(StringComparer.Ordinal); + private readonly List _equity = []; + private readonly List _fills = []; + private readonly List _trades = []; + private readonly List _decisions = []; + private readonly List _notes = []; + private readonly decimal _initial; + private decimal _cash; + private decimal _dividends; + private decimal _writtenOff; + + public Simulation(IStrategy strategy, BacktestDataset data, BacktestOptions options, PortfolioState start) + { + _strategy = strategy; + _data = data; + _options = options; + _costs = options.Costs; + _cash = start.Cash; + var first = data.Sessions.FirstOrDefault(d => options.From is null || d >= options.From); + foreach (var (symbol, h) in start.Holdings.Where(h => h.Value.Quantity > 0m)) + { + var trade = new OpenTrade { Opened = h.EnteredOn, Bought = DbValues.Money(h.Quantity * h.AveragePrice), CashFlow = -DbValues.Money(h.Quantity * h.AveragePrice) }; + _positions[symbol] = new Position { Quantity = h.Quantity, AveragePrice = h.AveragePrice, Stop = h.Stop, EnteredOn = h.EnteredOn, Trade = trade }; + _lastClose[symbol] = h.AveragePrice; + } + + _initial = start.Cash + _positions.Values.Sum(p => p.Trade.Bought); + foreach (var i in data.Instruments) + { + _actionsDoneThrough[i.Symbol] = first == default ? DateOnly.MinValue : first.AddDays(-1); + } + } + + public void MarkThrough(DateOnly day) + { + foreach (var i in _data.Instruments) + { + var n = i.CountThrough(day); + if (n > 0) + { + _lastClose[i.Symbol] = Price(i.Bars[n - 1].Close); + } + } + } + + public void CorporateActions(DateOnly day) + { + foreach (var i in _data.Instruments) + { + var done = _actionsDoneThrough[i.Symbol]; + foreach (var action in i.Actions.Where(a => a.ExDate > done && a.ExDate <= day)) + { + Apply(i, action); + } + + _actionsDoneThrough[i.Symbol] = day; + } + } + + private void Apply(BacktestInstrument instrument, CorporateAction action) + { + var symbol = instrument.Symbol; + if (action.ChangesShares) + { + if (_positions.TryGetValue(symbol, out var p)) + { + p.Quantity = Adjustments.ConvertQuantity(p.Quantity, action); + p.AveragePrice = DbValues.Money(Adjustments.ConvertLevel(p.AveragePrice, action, null)); + p.Stop = p.Stop is { } s ? DbValues.Money(Adjustments.ConvertLevel(s, action, null)) : null; + } + + if (_pending.TryGetValue(symbol, out var order)) + { + _pending[symbol] = order with + { + Quantity = Adjustments.ConvertQuantity(order.Quantity, action), + Stop = order.Stop is { } s ? DbValues.Money(Adjustments.ConvertLevel(s, action, null)) : null, + }; + } + + if (_lastClose.TryGetValue(symbol, out var close)) + { + _lastClose[symbol] = DbValues.Money(Adjustments.ConvertLevel(close, action, null)); + } + } + else if (action.Kind == CorporateActionKinds.CashDividend && action.CashAmount is > 0m && _positions.TryGetValue(symbol, out var held) + && (action.Currency is null || action.Currency == _data.Currency)) + { + // To whoever held the shares at the close before the ex-date: that is the book now, before the open. + var amount = DbValues.Money(held.Quantity * action.CashAmount.Value); + _cash += amount; + _dividends += amount; + held.Trade.CashFlow += amount; + } + } + + public void Open(DateOnly day) + { + // Stops resting on the broker fill first when the price opens below them (SPEC 7.2, trap 24). + foreach (var (symbol, p) in _positions.ToList()) + { + if (Bar(symbol, day) is { } bar && p.Stop is { } stop && Price(bar.Open) <= stop) + { + Sell(day, symbol, p.Quantity, Price(bar.Open), BacktestReasons.StopGap, exit: true); + _pending.Remove(symbol); + } + } + + var due = _pending.Values.Where(o => Bar(o.Symbol, day) is not null).OrderBy(o => o.Side == OrderSide.Buy).ToList(); + foreach (var order in due) + { + _pending.Remove(order.Symbol); + var open = Price(Bar(order.Symbol, day)!.Open); + if (order.Side == OrderSide.Sell) + { + if (_positions.TryGetValue(order.Symbol, out var p)) + { + Sell(day, order.Symbol, order.Exit ? p.Quantity : Math.Min(order.Quantity, p.Quantity), open, order.Reason, order.Exit); + } + } + else + { + Buy(day, order, open); + } + } + } + + public void Session(DateOnly day) + { + foreach (var (symbol, p) in _positions.ToList()) + { + if (Bar(symbol, day) is { } bar && p.Stop is { } stop && Price(bar.Low) <= stop) + { + Sell(day, symbol, p.Quantity, Math.Min(stop, Price(bar.Open)), BacktestReasons.Stop, exit: true); + _pending.Remove(symbol); + } + } + } + + public void Close(DateOnly day, DateTimeOffset at) + { + MarkOn(day); + var invested = Invested(); + _equity.Add(new EquityPoint(day, _cash + invested, _cash, invested)); + var record = Decide(day, at); + _decisions.Add(record); + Plan(day, record.Final); + } + + public DecisionRecord Decide(DateOnly day, DateTimeOffset at) + { + var available = _data.Instruments.Where(i => i.CountThrough(day) > 0).Select(i => i.Symbol).ToList(); + var universe = _strategy.Universe(new UniverseContext(day, available)); + var equity = _cash + Invested(); + var holdings = _positions.ToDictionary(p => p.Key, p => new Holding(p.Value.Quantity, p.Value.AveragePrice, p.Value.Stop, p.Value.EnteredOn), StringComparer.Ordinal); + var context = new StrategyContext(day, at, universe, new PortfolioView(_cash, equity, holdings), + s => _data[s]?.SeriesThrough(day) ?? BarSeries.Empty, s => _data[s]?.Spec); + var proposed = _strategy.Decide(context); + var final = proposed; + foreach (var stage in _options.Correctives) + { + final = stage.Apply(context, final); + } + + final = _options.Risk.Apply(context, final); + return new DecisionRecord(day, proposed, final); + } + + /// Turns the intents of a close into orders for the next open, sized on the equity of that close. + private void Plan(DateOnly day, IReadOnlyList intents) + { + var equity = _cash + Invested(); + foreach (var intent in intents.GroupBy(i => i.Symbol, StringComparer.Ordinal).Select(g => g.Last())) + { + var instrument = _data[intent.Symbol]; + if (instrument is null) + { + _notes.Add(new BacktestNote(day, intent.Symbol, BacktestReasons.UnknownSymbol)); + continue; + } + + _positions.TryGetValue(intent.Symbol, out var p); + var stop = intent.Stop is { } level ? OrderRules.StopPrice(level, OrderRules.TickOf(instrument.Spec, level)) : (decimal?)null; + if (p is not null && stop is not null) + { + p.Stop = stop; + } + + if (intent.TargetWeight is not { } weight) + { + continue; + } + + if (weight < 0m) + { + _notes.Add(new BacktestNote(day, intent.Symbol, BacktestReasons.NoShortSelling)); + continue; + } + + if (!_lastClose.TryGetValue(intent.Symbol, out var close) || close <= 0m) + { + _notes.Add(new BacktestNote(day, intent.Symbol, BacktestReasons.NoPrice)); + continue; + } + + var current = p?.Quantity ?? 0m; + if (weight == 0m) + { + if (current > 0m) + { + _pending[intent.Symbol] = new Pending(intent.Symbol, OrderSide.Sell, current, Exit: true, null, intent.Reason); + } + else + { + _pending.Remove(intent.Symbol); + } + + continue; + } + + var target = weight * equity / close; + var difference = target - current; + if (difference > 0m) + { + _pending[intent.Symbol] = new Pending(intent.Symbol, OrderSide.Buy, difference, Exit: false, stop, intent.Reason); + } + else if (difference < 0m) + { + _pending[intent.Symbol] = new Pending(intent.Symbol, OrderSide.Sell, -difference, Exit: false, null, intent.Reason); + } + } + } + + private void Buy(DateOnly day, Pending order, decimal reference) + { + var spec = _data[order.Symbol]!.Spec; + var price = _costs.ExecutionPrice(spec, OrderSide.Buy, reference); + var quantity = Math.Min(order.Quantity, _costs.Affordable(spec, _cash, price)); + if (quantity < order.Quantity) + { + // Less cash than the decision assumed (the open moved): the purchase shrinks, never borrows (SPEC 9.1). + _notes.Add(new BacktestNote(day, order.Symbol, BacktestReasons.NoCash)); + } + + var rounded = OrderRules.Round(spec, OrderSide.Buy, OrderKind.Market, quantity, price); + if (!rounded.IsValid) + { + if (quantity >= order.Quantity) + { + _notes.Add(new BacktestNote(day, order.Symbol, rounded.Rejection!)); + } + + return; + } + + var fill = _costs.Fill(spec, OrderSide.Buy, rounded.Quantity, price); + _cash += fill.CashDelta; + if (!_positions.TryGetValue(order.Symbol, out var p)) + { + p = new Position { EnteredOn = day, Trade = new OpenTrade { Opened = day } }; + _positions[order.Symbol] = p; + } + + p.AveragePrice = DbValues.Money(((p.Quantity * p.AveragePrice) + fill.Gross) / (p.Quantity + fill.Quantity)); + p.Quantity += fill.Quantity; + p.Stop = order.Stop ?? p.Stop; + p.Trade.Bought += fill.Gross; + p.Trade.CashFlow += fill.CashDelta; + p.Trade.Fees += fill.FeeValue; + _fills.Add(new BacktestFill(day, order.Symbol, OrderSide.Buy, rounded.Quantity, price, fill.Gross, fill.FeeValue, order.Reason)); + } + + private void Sell(DateOnly day, string symbol, decimal quantity, decimal reference, string reason, bool exit) + { + var p = _positions[symbol]; + var spec = _data[symbol]!.Spec; + var price = _costs.ExecutionPrice(spec, OrderSide.Sell, reference); + var rounded = OrderRules.Round(spec, OrderSide.Sell, OrderKind.Market, Math.Min(quantity, p.Quantity), price); + if (rounded.IsValid) + { + var fill = _costs.Fill(spec, OrderSide.Sell, rounded.Quantity, price); + _cash += fill.CashDelta; + p.Quantity -= rounded.Quantity; + p.Trade.CashFlow += fill.CashDelta; + p.Trade.Fees += fill.FeeValue; + _fills.Add(new BacktestFill(day, symbol, OrderSide.Sell, rounded.Quantity, price, fill.Gross, fill.FeeValue, reason)); + } + else if (!exit) + { + _notes.Add(new BacktestNote(day, symbol, rounded.Rejection!)); + return; + } + + // After an exit, what is below the minimum cannot be sold on its own: it is written off (SPEC 7.2). + if (p.Quantity > 0m && (exit || OrderRules.IsUnsellable(spec, p.Quantity, price))) + { + _writtenOff += DbValues.Money(p.Quantity * price); + _notes.Add(new BacktestNote(day, symbol, BacktestReasons.WrittenOff)); + p.Quantity = 0m; + } + + if (p.Quantity <= 0m) + { + _positions.Remove(symbol); + _trades.Add(new BacktestTrade(symbol, p.Trade.Opened, day, p.Trade.Bought, p.Trade.CashFlow, p.Trade.Fees)); + } + } + + public BacktestRun Finish() + { + foreach (var (symbol, p) in _positions) + { + var value = DbValues.Money(p.Quantity * _lastClose.GetValueOrDefault(symbol)); + _trades.Add(new BacktestTrade(symbol, p.Trade.Opened, null, p.Trade.Bought, p.Trade.CashFlow + value, p.Trade.Fees)); + } + + return new BacktestRun(_data.Currency, _initial, _equity, _fills, [.. _trades.OrderBy(t => t.Opened).ThenBy(t => t.Symbol, StringComparer.Ordinal)], _decisions, _notes) + { + Dividends = _dividends, + WrittenOff = _writtenOff, + }; + } + + private void MarkOn(DateOnly day) + { + foreach (var i in _data.Instruments) + { + if (Bar(i.Symbol, day) is { } bar) + { + _lastClose[i.Symbol] = Price(bar.Close); + } + } + } + + private decimal Invested() => _positions.Sum(p => DbValues.Money(p.Value.Quantity * _lastClose.GetValueOrDefault(p.Key))); + + private DailyBar? Bar(string symbol, DateOnly day) + { + var instrument = _data[symbol]; + var index = instrument?.IndexOf(day) ?? -1; + return index >= 0 ? instrument!.Bars[index] : null; + } + + /// Bars are analysis data in floating point; prices used for money become decimal here, once. + private static decimal Price(double value) => DbValues.Money((decimal)value); + } +} diff --git a/src/Anubis/Backtest/Laboratory.cs b/src/Anubis/Backtest/Laboratory.cs new file mode 100644 index 0000000..4787daa --- /dev/null +++ b/src/Anubis/Backtest/Laboratory.cs @@ -0,0 +1,381 @@ +using System.Collections.Concurrent; +using System.Globalization; +using System.Threading.Channels; +using Anubis.Core; +using Anubis.Data; +using Anubis.Db; +using Anubis.Strategies; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Anubis.Backtest; + +/// What the user asks for in the new-trial form of the laboratory. +public sealed record TrialRequest( + string Strategy, + string Source, + string Venue, + IReadOnlyList Symbols, + DateOnly From, + DateOnly To, + decimal InitialCapital, + IReadOnlyDictionary>? Grid = null); + +/// A request the laboratory refuses: a short code for the address of the form, and the reason in Italian. +public sealed class TrialRequestException(string code, string message) : Exception(message) +{ + public string Code { get; } = code; +} + +/// Codes of . +public static class TrialRequestErrors +{ + public const string Strategy = "strategia"; + public const string Source = "origine"; + public const string Symbols = "strumenti"; + public const string Dates = "date"; + public const string Capital = "capitale"; + public const string Grid = "griglia"; + public const string Combinations = "combinazioni"; +} + +/// +/// The laboratory (SPEC 12.2, 13.4): it writes the plan of a trial before running it, runs one trial at a time on a +/// dedicated thread with low priority, so trading and the interface never wait for a backtest, and writes the report +/// back. Only the asynchronous side touches the database. After a restart, trials left halfway start again. +/// +public sealed partial class Laboratory : IDisposable +{ + public const int MaxSymbols = 20; + + public const int MaxVariants = 200; + + /// The pause after an error of the queue itself (the database out of reach), before trying again. + public static readonly TimeSpan RetryDelay = TimeSpan.FromSeconds(30); + + private readonly Database _db; + private readonly IClock _clock; + private readonly string _researchRoot; + private readonly ILogger _logger; + private readonly Channel _queue = Channel.CreateUnbounded(new UnboundedChannelOptions { SingleReader = true }); + private readonly ConcurrentDictionary _progress = new(); + private readonly LowPriorityWorker _worker = new("anubis-laboratory"); + + public Laboratory(Database db, IClock clock, string researchRoot, ILogger? logger = null) + { + _db = db; + _clock = clock; + _researchRoot = researchRoot; + _logger = logger ?? NullLogger.Instance; + Trials = new TrialStore(db, clock); + } + + public TrialStore Trials { get; } + + /// What the running trial is doing, in words; null when it is not running here. + public string? Progress(Guid id) => _progress.GetValueOrDefault(id); + + /// Checks the request, writes the plan and queues the trial. The plan is final from here on. + public async Task PlanAsync(TrialRequest request, CancellationToken cancellationToken) + { + var (plan, variants) = BuildPlan(request, _clock.UtcNow); + var trial = await Trials.PlanAsync(plan, variants, cancellationToken).ConfigureAwait(false); + await _queue.Writer.WriteAsync(trial.Id, cancellationToken).ConfigureAwait(false); + return trial; + } + + /// The plan of a request: the family's own grid where the request gives none, the default costs, checks and thresholds. + public static (TrialPlan Plan, int Variants) BuildPlan(TrialRequest request, DateTimeOffset now) + { + ArgumentNullException.ThrowIfNull(request); + var family = StrategyCatalog.Find(request.Strategy) ?? throw new TrialRequestException(TrialRequestErrors.Strategy, "Strategia sconosciuta."); + if (request.Source is not (TrialSources.Database or TrialSources.Research)) + { + throw new TrialRequestException(TrialRequestErrors.Source, "Origine dei dati sconosciuta."); + } + + var symbols = request.Symbols.Select(s => s.Trim()).Where(s => s.Length > 0).Distinct(StringComparer.Ordinal).ToList(); + if (symbols.Count == 0) + { + throw new TrialRequestException(TrialRequestErrors.Symbols, "Indica almeno uno strumento."); + } + + if (symbols.Count > MaxSymbols) + { + throw new TrialRequestException(TrialRequestErrors.Symbols, $"Al massimo {MaxSymbols} strumenti per prova."); + } + + if (request.From >= request.To) + { + throw new TrialRequestException(TrialRequestErrors.Dates, "La data di inizio deve venire prima di quella di fine."); + } + + if (request.InitialCapital is < 1m or > 10_000_000m) + { + throw new TrialRequestException(TrialRequestErrors.Capital, "Il capitale iniziale deve essere tra 1 e 10.000.000."); + } + + var grid = new SortedDictionary>(StringComparer.Ordinal); + foreach (var p in family.Parameters) + { + var values = request.Grid?.GetValueOrDefault(p.Name) is { Count: > 0 } given ? given : p.Grid; + if (values.Any(x => x < p.Min || x > p.Max)) + { + throw new TrialRequestException(TrialRequestErrors.Grid, string.Create(CultureInfo.GetCultureInfo("it-IT"), $"{p.Label}: i valori vanno da {p.Min} a {p.Max}.")); + } + + // A whole parameter given 10.5 would run as 10: two variants that are the same would count twice in the DSR. + if (p.Whole && values.Any(x => x != decimal.Truncate(x))) + { + throw new TrialRequestException(TrialRequestErrors.Grid, p.Label + ": servono numeri interi."); + } + + grid[p.Name] = [.. values.Distinct().Order()]; + } + + var plan = new TrialPlan( + family.Id, + family.Version, + grid, + new TrialData(request.Source, request.Venue, symbols, request.From, request.To), + request.InitialCapital, + [1m, 2m, 3m], + TrialValidation.Default, + TrialThresholds.Default, + BacktestEngine.Version, + now); + var variants = ParameterGrid.Of(family, plan.Grid).Count; + if (variants == 0) + { + throw new TrialRequestException(TrialRequestErrors.Combinations, "Nessuna combinazione dei parametri è valida."); + } + + if (variants > MaxVariants) + { + throw new TrialRequestException(TrialRequestErrors.Combinations, $"Troppe combinazioni ({variants}): al massimo {MaxVariants}."); + } + + return (plan, variants); + } + + /// Runs the queue until stopped: first the trials left planned or halfway by the last run, then the new ones. + /// + /// An error of the queue itself (the database out of reach) never stops it: it waits and + /// tries the same trial again. A trial left running by such an error is planned again at the next start. + /// + public async Task RunAsync(CancellationToken cancellationToken) + { + while (true) + { + try + { + foreach (var id in await Trials.ResumeAsync(cancellationToken).ConfigureAwait(false)) + { + await _queue.Writer.WriteAsync(id, cancellationToken).ConfigureAwait(false); + } + + break; + } + catch (Exception ex) when (!cancellationToken.IsCancellationRequested) + { + LogQueueFailed(_logger, ex); + await _clock.WaitUntilAsync(_clock.UtcNow + RetryDelay, cancellationToken).ConfigureAwait(false); + } + } + + await foreach (var id in _queue.Reader.ReadAllAsync(cancellationToken).ConfigureAwait(false)) + { + try + { + await ProcessAsync(id, cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) when (!cancellationToken.IsCancellationRequested) + { + LogQueueFailed(_logger, ex); + await _clock.WaitUntilAsync(_clock.UtcNow + RetryDelay, cancellationToken).ConfigureAwait(false); + _queue.Writer.TryWrite(id); + } + } + } + + /// One trial: load its data, compute on the worker thread, write the report. A failure is recorded, not thrown. + public async Task ProcessAsync(Guid id, CancellationToken cancellationToken) + { + if (!await Trials.StartAsync(id, cancellationToken).ConfigureAwait(false)) + { + return; + } + + _progress[id] = "lettura dei dati"; + try + { + var trial = await Trials.GetAsync(id, cancellationToken).ConfigureAwait(false) ?? throw new InvalidOperationException("Prova non trovata."); + if (!trial.PlanIntact) + { + throw new InvalidOperationException("Il piano registrato non corrisponde più alla sua impronta: la prova non parte."); + } + + var family = StrategyCatalog.Find(trial.Strategy) ?? throw new InvalidOperationException("Strategia non più disponibile: " + trial.Strategy); + if (family.Version != trial.StrategyVersion) + { + throw new InvalidOperationException($"La strategia è cambiata dalla versione {trial.StrategyVersion} alla {family.Version}: pianifica una prova nuova."); + } + + var data = await LoadAsync(_db, _clock, _researchRoot, trial.Plan.Data, cancellationToken).ConfigureAwait(false); + var prior = await Trials.PriorAsync(trial.Strategy, id, cancellationToken).ConfigureAwait(false); + var now = _clock.UtcNow; + var (report, summary) = await _worker.RunAsync( + () => TrialRunner.Run(id, trial.Plan, trial.PlanHash, family, data, prior, now, text => _progress[id] = text, cancellationToken)).ConfigureAwait(false); + await Trials.FinishAsync(id, report.DataFingerprint, summary, report.ToJson(), report.Passed, cancellationToken).ConfigureAwait(false); + LogFinished(_logger, id, trial.Strategy, report.Passed); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; // Left running: the next start plans it again. + } + catch (Exception ex) + { + LogFailed(_logger, id, ex); + await Trials.FailAsync(id, Redactor.Redact(ex.Message), CancellationToken.None).ConfigureAwait(false); + } + finally + { + _progress.TryRemove(id, out _); + } + } + + /// + /// The bars of a trial. From the database: daily bars and, for stocks, corporate actions. From the research history: + /// Binance daily archives, with estimated order rules (the archive does not carry them). + /// + public static async Task LoadAsync(Database db, IClock clock, string researchRoot, TrialData data, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(data); + var instruments = new List(); + if (data.Source == TrialSources.Database) + { + var catalog = new InstrumentCatalog(db, clock); + var bars = new BarStore(db, clock); + var actions = new CorporateActionStore(db, clock); + foreach (var symbol in data.Symbols) + { + var instrument = await catalog.FindAsync(data.Venue, symbol, cancellationToken).ConfigureAwait(false) + ?? throw new InvalidOperationException($"Strumento sconosciuto: {symbol} su {data.Venue}."); + var daily = await bars.DailyAsync(instrument.Id, data.From, data.To, cancellationToken).ConfigureAwait(false); + var events = instrument.IsCrypto ? [] : await actions.ForSymbolAsync(symbol, cancellationToken).ConfigureAwait(false); + instruments.Add(new BacktestInstrument(instrument.Spec, daily, [.. events.Where(a => a.ExDate > data.From && a.ExDate <= data.To)], "barre raccolte")); + } + } + else + { + var store = new ResearchStore(researchRoot); + var from = new DateTimeOffset(data.From.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero); + var to = new DateTimeOffset(data.To.AddDays(1).ToDateTime(TimeOnly.MinValue), TimeSpan.Zero); + foreach (var symbol in data.Symbols) + { + var daily = new List(); + await foreach (var b in store.ReadAsync(data.Venue, symbol, Timeframes.Day, from, to, cancellationToken).ConfigureAwait(false)) + { + daily.Add(new DailyBar(DateOnly.FromDateTime(b.OpensAt.UtcDateTime), (double)b.Open, (double)b.High, (double)b.Low, (double)b.Close, (double)b.Volume)); + } + + instruments.Add(new BacktestInstrument(ResearchSpec(data.Venue, symbol), daily, null, "storico di ricerca")); + } + } + + var empty = instruments.FirstOrDefault(i => i.Bars.Count == 0); + if (empty is not null) + { + throw new InvalidOperationException($"Nessuna barra giornaliera per {empty.Symbol} tra il {data.From:dd/MM/yyyy} e il {data.To:dd/MM/yyyy}."); + } + + var currencies = instruments.Select(i => i.Spec.QuoteCurrency).Distinct(StringComparer.Ordinal).ToList(); + if (currencies.Count != 1) + { + throw new InvalidOperationException("Gli strumenti di una prova devono essere quotati nella stessa valuta: " + string.Join(", ", currencies) + "."); + } + + return new BacktestDataset(instruments, currencies[0]); + } + + /// Order rules for a research series: the archive has none, so small steps that never bind are assumed. + public static InstrumentSpec ResearchSpec(string venue, string symbol) + { + ArgumentNullException.ThrowIfNull(symbol); + string[] quotes = ["USDT", "USDC", "EUR", "USD"]; + var quote = quotes.FirstOrDefault(q => symbol.EndsWith(q, StringComparison.Ordinal) && symbol.Length > q.Length) + ?? throw new InvalidOperationException("Valuta di quotazione non riconosciuta per " + symbol + "."); + return new InstrumentSpec(venue, symbol, symbol, AssetClasses.Crypto, quote) + { + BaseCurrency = symbol[..^quote.Length], + TickSize = 0.00000001m, + LotSize = 0.00000001m, + MinSize = 0.00000001m, + MinNotional = 5m, + Status = "live", + }; + } + + public void Dispose() + { + _queue.Writer.TryComplete(); + _worker.Dispose(); + } + + [LoggerMessage(Level = LogLevel.Information, Message = "Backtest trial {Id} of {Strategy} finished, passed: {Passed}")] + private static partial void LogFinished(ILogger logger, Guid id, string strategy, bool passed); + + [LoggerMessage(Level = LogLevel.Warning, Message = "Laboratory queue error, trying again shortly")] + private static partial void LogQueueFailed(ILogger logger, Exception exception); + + [LoggerMessage(Level = LogLevel.Warning, Message = "Backtest trial {Id} failed")] + private static partial void LogFailed(ILogger logger, Guid id, Exception exception); +} + +/// +/// One thread with low priority that runs heavy work in order. Its callers await the result on their own side: the +/// thread never touches the database. +/// +internal sealed class LowPriorityWorker : IDisposable +{ + private readonly BlockingCollection _work = []; + private readonly Thread _thread; + + public LowPriorityWorker(string name) + { + _thread = new Thread(Loop) { IsBackground = true, Name = name, Priority = ThreadPriority.BelowNormal }; + _thread.Start(); + } + + public Task RunAsync(Func work) + { + var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _work.Add(() => + { + try + { + done.SetResult(work()); + } + catch (Exception ex) + { + done.SetException(ex); + } + }); + return done.Task; + } + + private void Loop() + { + foreach (var item in _work.GetConsumingEnumerable()) + { + item(); + } + } + + public void Dispose() + { + _work.CompleteAdding(); + _thread.Join(TimeSpan.FromSeconds(10)); + _work.Dispose(); + } +} diff --git a/src/Anubis/Backtest/LookAhead.cs b/src/Anubis/Backtest/LookAhead.cs new file mode 100644 index 0000000..7651682 --- /dev/null +++ b/src/Anubis/Backtest/LookAhead.cs @@ -0,0 +1,127 @@ +using System.Globalization; +using Anubis.Strategies; + +namespace Anubis.Backtest; + +/// A decision that changed when data after it was added: the strategy looked ahead. +public sealed record LookAheadMismatch(DateOnly Cutoff, DateOnly Day, string Full, string Cut); + +public sealed record LookAheadResult(IReadOnlyList Cutoffs, int Decisions, IReadOnlyList Mismatches) +{ + public bool Passed => Mismatches.Count == 0; +} + +/// +/// The automatic test of SPEC 12.2 point 2: the signal of day t must not change when data after t is added. The +/// strategy runs on the full data and again on the data cut at a few days; every decision up to each cut must be the +/// same in both runs. The strategy is built afresh for each run from the data it receives, so one that keeps a +/// reference to the data and reads tomorrow from it is caught. +/// +public static class LookAheadCheck +{ + public static LookAheadResult Run(Func create, BacktestDataset data, BacktestOptions? options = null, int cuts = 6) + { + ArgumentNullException.ThrowIfNull(create); + ArgumentNullException.ThrowIfNull(data); + options ??= new BacktestOptions(); + var sessions = data.Sessions + .Where(d => (options.From is null || d >= options.From) && (options.To is null || d <= options.To)) + .ToList(); + // Cut days spread over the run, never the last day (cutting there changes nothing). + var cutoffs = Enumerable.Range(1, cuts) + .Select(k => (int)((long)(sessions.Count - 1) * k / (cuts + 1))) + .Where(i => i > 0) + .Distinct() + .Select(i => sessions[i]) + .ToList(); + + var full = BacktestEngine.Run(create(data), data, options); + var mismatches = new List(); + var compared = 0; + foreach (var cutoff in cutoffs) + { + var cutData = data.Through(cutoff); + var cut = BacktestEngine.Run(create(cutData), cutData, options); + var before = full.Decisions.Where(d => d.Day <= cutoff).ToDictionary(d => d.Day); + var after = cut.Decisions.ToDictionary(d => d.Day); + foreach (var day in before.Keys.Union(after.Keys).Order()) + { + compared++; + var a = before.TryGetValue(day, out var x) ? Describe(x.Proposed) : "-"; + var b = after.TryGetValue(day, out var y) ? Describe(y.Proposed) : "-"; + if (a != b) + { + mismatches.Add(new LookAheadMismatch(cutoff, day, a, b)); + } + } + } + + return new LookAheadResult(cutoffs, compared, mismatches); + } + + /// Intents as text, with stops to nine significant digits: adjusted prices may differ in the last bit. + private static string Describe(IReadOnlyList intents) => string.Join(" | ", intents.Select(i => string.Create( + CultureInfo.InvariantCulture, + $"{i.Symbol} {i.TargetWeight?.ToString(CultureInfo.InvariantCulture) ?? "keep"} {(i.Stop is { } s ? ((double)s).ToString("G9", CultureInfo.InvariantCulture) : "-")} {i.Reason}"))); +} + +/// How the live intent and the backtest intent for one instrument compare (SPEC 12.3). +public sealed record IntentComparison(string Symbol, IntentAction Live, IntentAction Backtest, decimal? LiveWeight, decimal? BacktestWeight, bool Differs); + +/// +/// The one-step comparison of SPEC 12.3: the intents of the strategy (before correctives and risk) for the instruments +/// held and those either side wants to enter. Two intents differ if the action differs or the size differs by more +/// than 10%. +/// +public static class OneStepComparison +{ + public const decimal SizeTolerance = 0.10m; + + public static IntentAction ActionOf(Intent? intent, bool held) => intent?.TargetWeight switch + { + null => IntentAction.Keep, + <= 0m => held ? IntentAction.Exit : IntentAction.Keep, + _ => held ? IntentAction.Resize : IntentAction.Enter, + }; + + public static IReadOnlyList Compare(IReadOnlyList live, IReadOnlyList backtest, IReadOnlySet held) + { + ArgumentNullException.ThrowIfNull(live); + ArgumentNullException.ThrowIfNull(backtest); + ArgumentNullException.ThrowIfNull(held); + var a = live.GroupBy(i => i.Symbol).ToDictionary(g => g.Key, g => g.Last(), StringComparer.Ordinal); + var b = backtest.GroupBy(i => i.Symbol).ToDictionary(g => g.Key, g => g.Last(), StringComparer.Ordinal); + var symbols = new SortedSet(held, StringComparer.Ordinal); + foreach (var (symbol, intent) in a.Concat(b)) + { + if (ActionOf(intent, held.Contains(symbol)) == IntentAction.Enter) + { + symbols.Add(symbol); + } + } + + var result = new List(); + foreach (var symbol in symbols) + { + var isHeld = held.Contains(symbol); + var x = a.GetValueOrDefault(symbol); + var y = b.GetValueOrDefault(symbol); + var actionX = ActionOf(x, isHeld); + var actionY = ActionOf(y, isHeld); + var differs = actionX != actionY || SizeDiffers(x?.TargetWeight, y?.TargetWeight, actionX); + result.Add(new IntentComparison(symbol, actionX, actionY, x?.TargetWeight, y?.TargetWeight, differs)); + } + + return result; + } + + private static bool SizeDiffers(decimal? x, decimal? y, IntentAction action) + { + if (action is not (IntentAction.Enter or IntentAction.Resize) || x is not { } p || y is not { } q) + { + return false; + } + + return Math.Abs(p - q) > SizeTolerance * Math.Max(Math.Abs(p), Math.Abs(q)); + } +} diff --git a/src/Anubis/Backtest/Metrics.cs b/src/Anubis/Backtest/Metrics.cs new file mode 100644 index 0000000..053013f --- /dev/null +++ b/src/Anubis/Backtest/Metrics.cs @@ -0,0 +1,140 @@ +namespace Anubis.Backtest; + +/// The result of one calendar year: equity at the end of the year before (or the start) and at the end of this one. +public sealed record YearResult(int Year, decimal Start, decimal End) +{ + public decimal Result => End - Start; + + public double Return => Start > 0m ? (double)(End / Start) - 1d : 0d; +} + +/// +/// The figures of a run that the report of SPEC 12.2 point 6 shows. Returns and drawdowns are fractions (0,12 is +/// 12%); Sharpe and volatility are annualised with the sessions of a year (365 for crypto, 252 for stocks), without +/// a risk-free rate. +/// +public sealed record BacktestMetrics( + DateOnly? From, + DateOnly? To, + decimal InitialCapital, + decimal FinalEquity, + double TotalReturn, + double AnnualReturn, + double Volatility, + double Sharpe, + double DailySharpe, + double MaxDrawdown, + DateOnly? DrawdownPeak, + DateOnly? DrawdownLow, + int Trades, + int Winners, + double? ProfitFactor, + decimal AverageWin, + decimal AverageLoss, + decimal Fees, + double Exposure, + IReadOnlyList Years) +{ + public decimal NetResult => FinalEquity - InitialCapital; + + public double WinRate => Trades > 0 ? (double)Winners / Trades : 0d; + + public static BacktestMetrics Of(BacktestRun run, int periodsPerYear) + { + ArgumentNullException.ThrowIfNull(run); + var equity = run.Equity; + var returns = run.DailyReturns(); + var moments = Moments.Of(returns); + var annualisation = Math.Sqrt(periodsPerYear); + + var total = run.InitialCapital > 0m ? (double)(run.FinalEquity / run.InitialCapital) - 1d : 0d; + var annual = 0d; + if (equity.Count > 1 && run.InitialCapital > 0m && run.FinalEquity > 0m) + { + // Calendar time, the same for markets that trade every day and markets that rest at the weekend. + var years = (equity[^1].Day.DayNumber - equity[0].Day.DayNumber) / 365.25; + annual = years > 0 ? Math.Pow(1 + total, 1 / years) - 1 : 0d; + } + + var (drawdown, peak, low) = Drawdown(equity); + + var closed = run.Trades.Where(t => t.IsClosed).ToList(); + var wins = closed.Where(t => t.Result > 0m).ToList(); + var losses = closed.Where(t => t.Result <= 0m).ToList(); + var gains = wins.Sum(t => t.Result); + var lost = -losses.Sum(t => t.Result); + // Gains over losses of the closed trades; no value without a losing trade (nothing to divide by). + double? profitFactor = lost > 0m ? (double)(gains / lost) : null; + + var exposure = equity.Count > 0 + ? equity.Average(p => p.Equity > 0m ? (double)(p.Invested / p.Equity) : 0d) + : 0d; + + return new BacktestMetrics( + equity.Count > 0 ? equity[0].Day : null, + equity.Count > 0 ? equity[^1].Day : null, + run.InitialCapital, + run.FinalEquity, + total, + annual, + moments.StandardDeviation * annualisation, + moments.Sharpe * annualisation, + moments.Sharpe, + drawdown, + peak, + low, + closed.Count, + wins.Count, + profitFactor, + wins.Count > 0 ? gains / wins.Count : 0m, + losses.Count > 0 ? -lost / losses.Count : 0m, + run.Fees, + exposure, + Yearly(run)); + } + + /// The deepest fall of the equity from a previous high, as a fraction of that high, with its two days. + public static (double Depth, DateOnly? Peak, DateOnly? Low) Drawdown(IReadOnlyList equity) + { + ArgumentNullException.ThrowIfNull(equity); + var depth = 0d; + DateOnly? peakDay = null, lowDay = null; + var high = decimal.MinValue; + DateOnly highDay = default; + foreach (var p in equity) + { + if (p.Equity > high) + { + high = p.Equity; + highDay = p.Day; + } + + if (high > 0m) + { + var fall = (double)((high - p.Equity) / high); + if (fall > depth) + { + depth = fall; + peakDay = highDay; + lowDay = p.Day; + } + } + } + + return (depth, peakDay, lowDay); + } + + private static List Yearly(BacktestRun run) + { + var result = new List(); + var start = run.InitialCapital; + foreach (var year in run.Equity.GroupBy(p => p.Day.Year).OrderBy(g => g.Key)) + { + var end = year.Last().Equity; + result.Add(new YearResult(year.Key, start, end)); + start = end; + } + + return result; + } +} diff --git a/src/Anubis/Backtest/Report.cs b/src/Anubis/Backtest/Report.cs new file mode 100644 index 0000000..01e4274 --- /dev/null +++ b/src/Anubis/Backtest/Report.cs @@ -0,0 +1,265 @@ +using System.Diagnostics; +using System.Text.Json; +using Anubis.Data; +using Anubis.Strategies; + +namespace Anubis.Backtest; + +/// An instrument of the report: where its bars come from and what period they cover. +public sealed record ReportInstrument(string Symbol, string Venue, DateOnly? First, DateOnly? Last, int Bars, int Actions, string Source); + +/// A point of the equity curve of the chosen variant and of buy and hold, on the same day. +public sealed record ReportPoint(DateOnly Day, decimal Equity, decimal Benchmark); + +/// A variant of the grid with its full-period figures. +public sealed record ReportVariant(string Parameters, double Sharpe, double PlateauScore, decimal NetResult, double MaxDrawdown, int Trades, bool Chosen); + +/// The chosen variant with costs multiplied (SPEC 12.2 point 3). +public sealed record ReportCosts(decimal Multiplier, decimal NetResult, double TotalReturn, double Sharpe, double MaxDrawdown, decimal Fees); + +public sealed record ReportWindow(int Window, DateOnly TrainFrom, DateOnly TrainTo, DateOnly TestFrom, DateOnly TestTo, string Chosen, double Return, double Sharpe); + +public sealed record ReportWalkForward(IReadOnlyList Windows, int Purge, double Return, double Sharpe); + +public sealed record ReportCombinatorial(int Groups, int TestGroups, int Splits, int Purge, int Embargo, IReadOnlyList PathSharpes, double MeanSharpe, double WorstSharpe, double PositiveShare); + +public sealed record ReportOverfitting(int Groups, int Combinations, int Variants, double Probability); + +public sealed record ReportLookAhead(IReadOnlyList Cutoffs, int Decisions, int Changed, IReadOnlyList Examples) +{ + public bool Passed => Changed == 0; +} + +/// +/// A threshold of SPEC 12.2 checked on the result. is null when the check does not apply (PBO with +/// a single variant). +/// +public sealed record ReportCheck(string Key, string Name, double? Value, string Comparison, double Threshold, string Unit, bool? Passed); + +public static class ReportCheckKeys +{ + public const string DoubleCosts = "double_costs"; + public const string DeflatedSharpe = "deflated_sharpe"; + public const string Overfitting = "overfitting"; + public const string Drawdown = "drawdown"; + public const string LookAhead = "look_ahead"; +} + +/// +/// The final report of a trial (SPEC 12.2 point 6): figures of the chosen variant, buy and hold, cost sensitivity, the +/// grid, the validations, the checks and what the backtest does not model. +/// +public sealed record BacktestReport( + Guid TrialId, + string Strategy, + string StrategyName, + string StrategyVersion, + string PlanHash, + string DataFingerprint, + string Currency, + int PeriodsPerYear, + int Sessions, + IReadOnlyList Instruments, + string Chosen, + BacktestMetrics Metrics, + BacktestMetrics Benchmark, + IReadOnlyList Costs, + IReadOnlyList Variants, + ReportWalkForward? WalkForward, + ReportCombinatorial? Combinatorial, + ReportOverfitting? Overfitting, + DeflatedSharpe DeflatedSharpe, + int TrialsCounted, + ReportLookAhead LookAhead, + IReadOnlyList Checks, + bool Passed, + IReadOnlyList Limitations, + IReadOnlyList Curve, + IReadOnlyList Trades, + IReadOnlyDictionary Notes, + DateTimeOffset ComputedAt, + double Seconds) +{ + public string ToJson() => JsonSerializer.Serialize(this, TrialPlan.Json); + + public static BacktestReport FromJson(string text) => JsonSerializer.Deserialize(text, TrialPlan.Json) ?? throw new JsonException("Empty report"); +} + +/// +/// Runs a trial from its plan, on data already loaded (pure computation, no database: the laboratory runs it on its own +/// low-priority thread). Every variant of the grid runs once on the whole period; the validations work on their daily +/// returns; the chosen variant (plateau rule on the whole period) is then run again with doubled and tripled costs. +/// +public static class TrialRunner +{ + public const int MaxCurvePoints = 500; + + public const int MaxTrades = 300; + + public static (BacktestReport Report, TrialSummary Summary) Run( + Guid trialId, TrialPlan plan, string planHash, IStrategyFamily family, BacktestDataset data, PriorTrials prior, DateTimeOffset now, + Action? progress = null, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(plan); + ArgumentNullException.ThrowIfNull(family); + ArgumentNullException.ThrowIfNull(data); + ArgumentNullException.ThrowIfNull(prior); + var watch = Stopwatch.StartNew(); + if (data.Sessions.Count < 30) + { + throw new InvalidOperationException($"Dati insufficienti: {data.Sessions.Count} sedute, ne servono almeno 30."); + } + + var variants = ParameterGrid.Of(family, plan.Grid); + if (variants.Count == 0) + { + throw new InvalidOperationException("Nessuna combinazione valida nella griglia dei parametri."); + } + + var options = new BacktestOptions { InitialCapital = plan.InitialCapital }; + var runs = new BacktestRun[variants.Count]; + for (var i = 0; i < variants.Count; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Invoke($"variante {i + 1} di {variants.Count}"); + runs[i] = BacktestEngine.Run(family.Create(variants[i].Parameters), data, options); + } + + var returns = runs.Select(r => r.DailyReturns()).ToArray(); + var sharpes = returns.Select(r => Moments.Of(r).Sharpe).ToArray(); + var scores = Validation.PlateauScores(variants, sharpes); + var chosenIndex = Validation.ChoosePlateau(variants, sharpes); + var chosen = variants[chosenIndex]; + var run = runs[chosenIndex]; + var metrics = BacktestMetrics.Of(run, data.PeriodsPerYear); + + progress?.Invoke("costi doppi e tripli, comprare e tenere"); + var costs = new List(); + foreach (var multiplier in plan.CostMultipliers.Distinct().Order()) + { + var costed = multiplier == 1m ? run : BacktestEngine.Run(family.Create(chosen.Parameters), data, options with { Costs = new CostModel(multiplier) }); + var m = multiplier == 1m ? metrics : BacktestMetrics.Of(costed, data.PeriodsPerYear); + costs.Add(new ReportCosts(multiplier, costed.NetResult, m.TotalReturn, m.Sharpe, m.MaxDrawdown, costed.Fees)); + } + + var benchmarkRun = BacktestEngine.Run(new BuyAndHold(data.Instruments.Count), data, options); + var benchmark = BacktestMetrics.Of(benchmarkRun, data.PeriodsPerYear); + + cancellationToken.ThrowIfCancellationRequested(); + progress?.Invoke("convalide"); + var days = run.Equity.Select(p => p.Day).ToArray(); + DateOnly DayOf(int returnIndex) => days[Math.Clamp(returnIndex + 1, 0, days.Length - 1)]; + var v = plan.Validation; + var wf = Validation.WalkForward(variants, returns, v.WalkForwardWindows); + var walkForward = wf is null ? null : new ReportWalkForward( + [.. wf.Windows.Select(w => new ReportWindow(w.Window, DayOf(w.TrainFrom), DayOf(w.TrainTo - 1), DayOf(w.TestFrom), DayOf(w.TestTo - 1), + variants[w.Chosen].ToString(), w.TestReturn, w.TestSharpe))], + wf.Purge, wf.Return, wf.Sharpe); + var cv = Validation.Combinatorial(variants, returns, v.CombinatorialGroups, v.CombinatorialTestGroups); + var combinatorial = cv is null ? null : new ReportCombinatorial(cv.Groups, cv.TestGroups, cv.Splits, cv.Purge, cv.Embargo, cv.PathSharpes, + cv.MeanSharpe, cv.WorstSharpe, cv.PositiveShare); + var pbo = Validation.Overfitting(returns, v.OverfittingGroups); + var overfitting = pbo is null ? null : new ReportOverfitting(pbo.Groups, pbo.Combinations, pbo.Variants, pbo.Probability); + + // Every variant of every finished trial of this strategy counts as a try (SPEC 12.2 point 1). + var trials = prior.Variants + variants.Count; + var spread = Moments.Of([.. prior.Sharpes, .. sharpes]).StandardDeviation; + var dsr = SharpeTests.Deflated(returns[chosenIndex], trials, spread * spread); + + progress?.Invoke("test anti sguardo in avanti"); + var look = LookAheadCheck.Run(_ => family.Create(chosen.Parameters), data, options, v.LookAheadCuts); + var lookAhead = new ReportLookAhead(look.Cutoffs, look.Decisions, look.Mismatches.Count, + [.. look.Mismatches.Take(5).Select(m => $"{m.Day:yyyy-MM-dd}: {m.Full} / {m.Cut}")]); + + var t = plan.Thresholds; + var doubled = costs.FirstOrDefault(c => c.Multiplier == 2m); + List checks = + [ + new(ReportCheckKeys.DoubleCosts, "Risultato netto con costi doppi", doubled is null ? null : (double)doubled.NetResult, ">", (double)t.MinNetAtDoubleCosts, "money", + doubled is null ? false : doubled.NetResult > t.MinNetAtDoubleCosts), + new(ReportCheckKeys.DeflatedSharpe, "Deflated Sharpe Ratio", dsr.Value, "≥", t.MinDeflatedSharpe, "ratio", dsr.Value >= t.MinDeflatedSharpe), + new(ReportCheckKeys.Overfitting, "Probabilità di sovra-adattamento (PBO)", pbo?.Probability, "≤", t.MaxOverfitting, "ratio", + pbo is null ? null : pbo.Probability <= t.MaxOverfitting), + new(ReportCheckKeys.Drawdown, "Calo massimo", metrics.MaxDrawdown, "≤", t.MaxDrawdown, "percent", metrics.MaxDrawdown <= t.MaxDrawdown), + new(ReportCheckKeys.LookAhead, "Decisioni cambiate aggiungendo dati successivi", look.Mismatches.Count, "=", 0, "count", look.Passed), + ]; + var passed = checks.All(c => c.Passed != false); + + var benchmarkByDay = benchmarkRun.Equity.ToDictionary(p => p.Day, p => p.Equity); + var step = Math.Max(1, (int)Math.Ceiling(run.Equity.Count / (double)MaxCurvePoints)); + var curve = run.Equity + .Where((_, i) => i % step == 0 || i == run.Equity.Count - 1) + .Select(p => new ReportPoint(p.Day, p.Equity, benchmarkByDay.GetValueOrDefault(p.Day, plan.InitialCapital))) + .ToList(); + + var report = new BacktestReport( + trialId, + family.Id, + family.Name, + family.Version, + planHash, + data.Fingerprint(), + data.Currency, + data.PeriodsPerYear, + data.Sessions.Count, + [.. data.Instruments.Select(i => new ReportInstrument(i.Symbol, i.Spec.Venue, i.Bars.Count > 0 ? i.Bars[0].Day : null, + i.Bars.Count > 0 ? i.Bars[^1].Day : null, i.Bars.Count, i.Actions.Count, i.Source))], + chosen.ToString(), + metrics, + benchmark, + costs, + [.. variants.Select((x, i) => new ReportVariant(x.ToString(), sharpes[i], scores[i], runs[i].NetResult, + Metrics(runs[i], data).MaxDrawdown, runs[i].Trades.Count(tr => tr.IsClosed), i == chosenIndex))], + walkForward, + combinatorial, + overfitting, + dsr, + trials, + lookAhead, + checks, + passed, + Limitations(plan, family, data), + curve, + [.. run.Trades.TakeLast(MaxTrades)], + run.Notes.GroupBy(n => n.Reason).ToDictionary(g => g.Key, g => g.Count(), StringComparer.Ordinal), + now, + watch.Elapsed.TotalSeconds); + + var summary = new TrialSummary(data.Currency, chosen.ToString(), run.NetResult, metrics.TotalReturn, metrics.Sharpe, metrics.MaxDrawdown, metrics.Trades, + dsr.Value, pbo?.Probability, sharpes); + return (report, summary); + } + + private static BacktestMetrics Metrics(BacktestRun run, BacktestDataset data) => BacktestMetrics.Of(run, data.PeriodsPerYear); + + /// What this backtest does not model, in plain Italian (SPEC 12.1: the report declares it). + public static IReadOnlyList Limitations(TrialPlan plan, IStrategyFamily family, BacktestDataset data) + { + ArgumentNullException.ThrowIfNull(plan); + ArgumentNullException.ThrowIfNull(family); + ArgumentNullException.ThrowIfNull(data); + var result = new List + { + "I correttivi senza una storia ricostruibile (notizie, calendario economico, trimestrali, modello) non ricevono dati e non intervengono.", + "Il regime di mercato e i limiti di rischio arrivano con la Fase 5: in questo backtest ogni intenzione della strategia passa così com'è.", + "Le decisioni si prendono sulla chiusura e si eseguono all'apertura della seduta dopo, con lo scostamento stimato: nessun inseguimento con ordini limite.", + "Sharpe e volatilità sono calcolati senza tasso privo di rischio.", + }; + if (plan.Data.Source == TrialSources.Research) + { + result.Add("Prezzi dello storico Binance in USDT: passo di prezzo e quantità minima sono stime, i costi sono quelli di OKX."); + } + + if (data.Instruments.Any(i => i.Spec.AssetClass == AssetClasses.Equity && i.Actions.Count == 0)) + { + result.Add("Per alcuni titoli non risultano frazionamenti né dividendi nel periodo: se ce ne sono stati, mancano dal database."); + } + + if (family.Id == "sma_cross") + { + result.Add("L'incrocio di medie è una strategia d'esempio per provare il laboratorio, non una delle strategie di base."); + } + + return result; + } +} diff --git a/src/Anubis/Backtest/Statistics.cs b/src/Anubis/Backtest/Statistics.cs new file mode 100644 index 0000000..2ac0e3d --- /dev/null +++ b/src/Anubis/Backtest/Statistics.cs @@ -0,0 +1,184 @@ +namespace Anubis.Backtest; + +/// The standard normal distribution, written in house (ADR 0003: no statistics package). +public static class Normal +{ + /// Φ(x) with Hart's algorithm 5666 as given by West (2004), accurate to double precision. + public static double Cdf(double x) + { + var z = Math.Abs(x); + double tail; + if (z > 37) + { + tail = 0; + } + else if (z < 7.07106781186547) + { + double[] p = [3.52624965998911E-02, 0.700383064443688, 6.37396220353165, 33.912866078383, 112.079291497871, 221.213596169931, 220.206867912376]; + double[] q = [8.83883476483184E-02, 1.75566716318264, 16.064177579207, 86.7807322029461, 296.564248779674, 637.333633378831, 793.826512519948, 440.413735824752]; + tail = Math.Exp(-z * z / 2) * Horner(p, z) / Horner(q, z); + } + else + { + var b = z + 0.65; + b = z + (4 / b); + b = z + (3 / b); + b = z + (2 / b); + b = z + (1 / b); + tail = Math.Exp(-z * z / 2) / b / 2.506628274631; + } + + return x > 0 ? 1 - tail : tail; + } + + /// Φ⁻¹(p) with Acklam's rational approximation, refined with one Halley step on . + public static double Quantile(double p) + { + if (p <= 0.0) + { + return double.NegativeInfinity; + } + + if (p >= 1.0) + { + return double.PositiveInfinity; + } + + double[] a = [-3.969683028665376e+01, 2.209460984245205e+02, -2.759285104469687e+02, 1.383577518672690e+02, -3.066479806614716e+01, 2.506628277459239e+00]; + double[] b = [-5.447609879822406e+01, 1.615858368580409e+02, -1.556989798598866e+02, 6.680131188771972e+01, -1.328068155288572e+01, 1.0]; + double[] c = [-7.784894002430293e-03, -3.223964580411365e-01, -2.400758277161838e+00, -2.549732539343734e+00, 4.374664141464968e+00, 2.938163982698783e+00]; + double[] d = [7.784695709041462e-03, 3.224671290700398e-01, 2.445134137142996e+00, 3.754408661907416e+00, 1.0]; + const double low = 0.02425; + double x; + if (p < low) + { + var q = Math.Sqrt(-2 * Math.Log(p)); + x = Horner(c, q) / Horner(d, q); + } + else if (p <= 1 - low) + { + var q = p - 0.5; + var r = q * q; + x = Horner(a, r) * q / Horner(b, r); + } + else + { + var q = Math.Sqrt(-2 * Math.Log(1 - p)); + x = -Horner(c, q) / Horner(d, q); + } + + var e = Cdf(x) - p; + var u = e * Math.Sqrt(2 * Math.PI) * Math.Exp(x * x / 2); + return x - (u / (1 + (x * u / 2))); + } + + /// A polynomial with coefficients from the highest power down, evaluated at . + private static double Horner(double[] coefficients, double x) + { + var result = 0.0; + foreach (var k in coefficients) + { + result = (result * x) + k; + } + + return result; + } +} + +/// Moments of a series of returns, as the formulas of Bailey and López de Prado use them. +public readonly record struct Moments(int Count, double Mean, double StandardDeviation, double Skewness, double Kurtosis) +{ + /// Sharpe ratio per period, without a risk-free rate: mean over standard deviation. + public double Sharpe => StandardDeviation > 0 ? Mean / StandardDeviation : 0; + + /// Mean, sample standard deviation, population skewness and (non-excess) kurtosis: 3 for a normal series. + public static Moments Of(ReadOnlySpan values) + { + var n = values.Length; + if (n == 0) + { + return new Moments(0, 0, 0, 0, 3); + } + + var mean = 0.0; + foreach (var v in values) + { + mean += v; + } + + mean /= n; + double m2 = 0, m3 = 0, m4 = 0; + foreach (var v in values) + { + var d = v - mean; + var d2 = d * d; + m2 += d2; + m3 += d2 * d; + m4 += d2 * d2; + } + + var sample = n > 1 ? Math.Sqrt(m2 / (n - 1)) : 0; + var population = m2 / n; + var skew = population > 0 ? m3 / n / Math.Pow(population, 1.5) : 0; + var kurt = population > 0 ? m4 / n / (population * population) : 3; + return new Moments(n, mean, sample, skew, kurt); + } +} + +/// The outcome of the Deflated Sharpe Ratio: every number that went into it, for the report. +public sealed record DeflatedSharpe(double Sharpe, double Benchmark, int Observations, double Skewness, double Kurtosis, int Trials, double SharpeVariance, double Value); + +/// +/// Probabilistic and Deflated Sharpe Ratio (Bailey and López de Prado, "The Sharpe Ratio Efficient Frontier", +/// 2012; "The Deflated Sharpe Ratio", 2014), written in house as SPEC 12.2 asks. Sharpe ratios are per period +/// (daily), not annualised. +/// +public static class SharpeTests +{ + private const double EulerGamma = 0.5772156649015329; + + /// Probability that the true Sharpe ratio exceeds , given the sample's length and shape. + public static double Probabilistic(double sharpe, double benchmark, int observations, double skewness, double kurtosis) + { + if (observations < 2) + { + return 0.5; + } + + var denominator = 1 - (skewness * sharpe) + ((kurtosis - 1) / 4 * sharpe * sharpe); + if (denominator <= 0) + { + return sharpe > benchmark ? 1 : 0; + } + + return Normal.Cdf((sharpe - benchmark) * Math.Sqrt(observations - 1) / Math.Sqrt(denominator)); + } + + /// + /// The Sharpe ratio the best of independent tries would show by chance alone, when + /// their Sharpe ratios vary by around zero. One trial expects zero. + /// + public static double ExpectedMaximum(int trials, double variance) + { + if (trials <= 1 || variance <= 0) + { + return 0; + } + + return Math.Sqrt(variance) * (((1 - EulerGamma) * Normal.Quantile(1 - (1.0 / trials))) + (EulerGamma * Normal.Quantile(1 - (1.0 / (trials * Math.E))))); + } + + /// The Deflated Sharpe Ratio of a series, after tries whose Sharpe ratios had variance . + public static DeflatedSharpe Deflated(ReadOnlySpan returns, int trials, double variance) + { + var m = Moments.Of(returns); + return Deflated(m.Sharpe, m.Count, m.Skewness, m.Kurtosis, trials, variance); + } + + public static DeflatedSharpe Deflated(double sharpe, int observations, double skewness, double kurtosis, int trials, double variance) + { + var benchmark = ExpectedMaximum(trials, variance); + return new DeflatedSharpe(sharpe, benchmark, observations, skewness, kurtosis, trials, variance, + Probabilistic(sharpe, benchmark, observations, skewness, kurtosis)); + } +} diff --git a/src/Anubis/Backtest/Trials.cs b/src/Anubis/Backtest/Trials.cs new file mode 100644 index 0000000..35260c8 --- /dev/null +++ b/src/Anubis/Backtest/Trials.cs @@ -0,0 +1,221 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using Anubis.Core; +using Anubis.Db; + +namespace Anubis.Backtest; + +/// Where the bars of a trial come from. +public static class TrialSources +{ + /// Daily bars and corporate actions collected in the database (SPEC 6.2). + public const string Database = "database"; + + /// The research history on disk (SPEC 6.6): Binance daily archives from 2017, in USDT. + public const string Research = "research"; +} + +/// The data a trial reads: one venue, a few instruments, a period (both days included). +public sealed record TrialData(string Source, string Venue, IReadOnlyList Symbols, DateOnly From, DateOnly To); + +/// +/// The thresholds of SPEC 12.2, fixed in the plan before the trial runs: changing them after seeing the results needs +/// an ADR. +/// +public sealed record TrialThresholds(decimal MinNetAtDoubleCosts, double MinDeflatedSharpe, double MaxOverfitting, double MaxDrawdown) +{ + public static TrialThresholds Default { get; } = new(0m, 0.90, 0.40, 0.25); +} + +/// How the trial validates: walk-forward windows, purged cross-validation, PBO blocks, look-ahead cuts. +public sealed record TrialValidation(int WalkForwardWindows, int CombinatorialGroups, int CombinatorialTestGroups, int OverfittingGroups, int LookAheadCuts) +{ + public static TrialValidation Default { get; } = new(4, 6, 2, 16, 6); +} + +/// +/// The plan of a trial (SPEC 12.2 point 1): rules, grid, data, costs, validations and thresholds, written before any +/// result and never changed. Its hash goes with the report. +/// +public sealed record TrialPlan( + string Strategy, + string StrategyVersion, + IReadOnlyDictionary> Grid, + TrialData Data, + decimal InitialCapital, + IReadOnlyList CostMultipliers, + TrialValidation Validation, + TrialThresholds Thresholds, + string Engine, + DateTimeOffset WrittenAt) +{ + public static readonly JsonSerializerOptions Json = new() + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, + WriteIndented = false, + }; + + public string ToJson() => JsonSerializer.Serialize(this, Json); + + public static TrialPlan FromJson(string text) => JsonSerializer.Deserialize(text, Json) ?? throw new JsonException("Empty plan"); + + /// SHA-256 of the plan's text as stored. + public static string Hash(string json) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(json))); +} + +public static class TrialStatuses +{ + public const string Planned = "planned"; + public const string Running = "running"; + public const string Done = "done"; + public const string Failed = "failed"; +} + +public static class TrialVerdicts +{ + public const string Passed = "passed"; + public const string Rejected = "rejected"; +} + +/// The few numbers of a finished trial the list shows, and the Sharpe ratios the next trials' DSR counts. +public sealed record TrialSummary( + string Currency, + string Chosen, + decimal NetResult, + double TotalReturn, + double Sharpe, + double MaxDrawdown, + int Trades, + double? DeflatedSharpe, + double? Overfitting, + IReadOnlyList VariantSharpes); + +/// A row of the trial register. +public sealed record Trial( + Guid Id, + string Strategy, + string StrategyVersion, + TrialPlan Plan, + string PlanHash, + int Variants, + string Status, + string? Verdict, + string? DataFingerprint, + TrialSummary? Summary, + string? Error, + DateTimeOffset CreatedAt, + DateTimeOffset? StartedAt, + DateTimeOffset? FinishedAt) +{ + public bool IsFinished => Status is TrialStatuses.Done or TrialStatuses.Failed; + + /// The stored plan text still gives the stored hash: nobody changed it after it was written. + public bool PlanIntact { get; init; } = true; +} + +/// The trials before this one of the same strategy: how many variants they tried and their Sharpe ratios. +public sealed record PriorTrials(int Trials, int Variants, IReadOnlyList Sharpes) +{ + public static PriorTrials None { get; } = new(0, 0, []); +} + +/// +/// The trial register (SPEC 12.2 point 1). A plan is inserted once and never updated: the store has no method that +/// changes it, and every later write touches only status, results and times. +/// +public sealed class TrialStore(Database db, IClock clock) +{ + private const string Columns = "id, strategy, strategy_version, plan, plan_hash, variants, status, verdict, data_fingerprint, summary, error, created_at, started_at, finished_at"; + + public async Task PlanAsync(TrialPlan plan, int variants, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(plan); + var id = Ids.NewId(clock); + var json = plan.ToJson(); + var now = clock.UtcNow; + await db.WriteAsync(s => s.ExecuteAsync( + "INSERT INTO backtest_trials (id, strategy, strategy_version, plan, plan_hash, variants, status, created_at) " + + "VALUES (@id, @s, @v, @p, @h, @n, @st, @now)", + cancellationToken, ("id", id), ("s", plan.Strategy), ("v", plan.StrategyVersion), ("p", new DbJson(json)), ("h", TrialPlan.Hash(json)), + ("n", variants), ("st", TrialStatuses.Planned), ("now", now)), cancellationToken).ConfigureAwait(false); + return (await GetAsync(id, cancellationToken).ConfigureAwait(false))!; + } + + public Task GetAsync(Guid id, CancellationToken cancellationToken) => + db.ReadAsync(s => s.FirstOrDefaultAsync($"SELECT {Columns} FROM backtest_trials WHERE id = @id", Read, ("id", id)), cancellationToken); + + public async Task> ListAsync(int limit, CancellationToken cancellationToken) => + await db.ReadAsync(s => s.QueryAsync($"SELECT {Columns} FROM backtest_trials ORDER BY created_at DESC, id DESC LIMIT @n", Read, ("n", limit)), + cancellationToken).ConfigureAwait(false); + + public Task ReportJsonAsync(Guid id, CancellationToken cancellationToken) => + db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT report FROM backtest_trials WHERE id = @id AND report IS NOT NULL", r => r.Json("report").Text, ("id", id)), + cancellationToken); + + /// Marks a planned trial as running. False when it was not planned (another worker took it, or it is finished). + public async Task StartAsync(Guid id, CancellationToken cancellationToken) => + await db.WriteAsync(s => s.ExecuteAsync("UPDATE backtest_trials SET status = @r, started_at = @now WHERE id = @id AND status = @p", + cancellationToken, ("r", TrialStatuses.Running), ("now", clock.UtcNow), ("id", id), ("p", TrialStatuses.Planned)), cancellationToken).ConfigureAwait(false) == 1; + + public Task FinishAsync(Guid id, string fingerprint, TrialSummary summary, string reportJson, bool passed, CancellationToken cancellationToken) => + db.WriteAsync(s => s.ExecuteAsync( + "UPDATE backtest_trials SET status = @d, verdict = @v, data_fingerprint = @f, summary = @s, report = @rep, finished_at = @now WHERE id = @id AND status = @r", + cancellationToken, ("d", TrialStatuses.Done), ("v", passed ? TrialVerdicts.Passed : TrialVerdicts.Rejected), ("f", fingerprint), + ("s", new DbJson(JsonSerializer.Serialize(summary, TrialPlan.Json))), ("rep", new DbJson(reportJson)), ("now", clock.UtcNow), ("id", id), + ("r", TrialStatuses.Running)), cancellationToken); + + public Task FailAsync(Guid id, string error, CancellationToken cancellationToken) => + db.WriteAsync(s => s.ExecuteAsync("UPDATE backtest_trials SET status = @f, error = @e, finished_at = @now WHERE id = @id AND status IN (@p, @r)", + cancellationToken, ("f", TrialStatuses.Failed), ("e", error), ("now", clock.UtcNow), ("id", id), ("p", TrialStatuses.Planned), + ("r", TrialStatuses.Running)), cancellationToken); + + /// + /// After a restart: trials left running go back to planned (their work was lost with the process), and every planned + /// trial is returned oldest first, to run again from the start with the same plan. + /// + public async Task> ResumeAsync(CancellationToken cancellationToken) => + await db.WriteAsync(async s => + { + await s.ExecuteAsync("UPDATE backtest_trials SET status = @p, started_at = NULL WHERE status = @r", cancellationToken, + ("p", TrialStatuses.Planned), ("r", TrialStatuses.Running)).ConfigureAwait(false); + return await s.QueryAsync("SELECT id FROM backtest_trials WHERE status = @p ORDER BY created_at, id", r => r.Id("id"), ("p", TrialStatuses.Planned)) + .ConfigureAwait(false); + }, cancellationToken).ConfigureAwait(false); + + /// The finished trials of other than : what the DSR deflates for. + public async Task PriorAsync(string strategy, Guid except, CancellationToken cancellationToken) + { + var rows = await db.ReadAsync(s => s.QueryAsync( + "SELECT variants, summary FROM backtest_trials WHERE strategy = @s AND id <> @id AND status = @d", + r => (Variants: (int)r.Number("variants"), Summary: r.IsNull("summary") ? null : JsonSerializer.Deserialize(r.Json("summary").Text, TrialPlan.Json)), + ("s", strategy), ("id", except), ("d", TrialStatuses.Done)), cancellationToken).ConfigureAwait(false); + return new PriorTrials(rows.Count, rows.Sum(r => r.Variants), [.. rows.SelectMany(r => r.Summary?.VariantSharpes ?? [])]); + } + + private static Trial Read(DbRow r) + { + var plan = r.Json("plan").Text; + return new Trial( + r.Id("id"), + r.Text("strategy"), + r.Text("strategy_version"), + TrialPlan.FromJson(plan), + r.Text("plan_hash"), + (int)r.Number("variants"), + r.Text("status"), + r.TextOrNull("verdict"), + r.TextOrNull("data_fingerprint"), + r.IsNull("summary") ? null : JsonSerializer.Deserialize(r.Json("summary").Text, TrialPlan.Json), + r.TextOrNull("error"), + r.Moment("created_at"), + r.MomentOrNull("started_at"), + r.MomentOrNull("finished_at")) + { + PlanIntact = TrialPlan.Hash(plan) == r.Text("plan_hash"), + }; + } +} diff --git a/src/Anubis/Backtest/Validation.cs b/src/Anubis/Backtest/Validation.cs new file mode 100644 index 0000000..840bbaf --- /dev/null +++ b/src/Anubis/Backtest/Validation.cs @@ -0,0 +1,469 @@ +using Anubis.Strategies; + +namespace Anubis.Backtest; + +/// One point of a parameter grid: its parameters and its place on each parameter's grid (for neighbours). +public sealed record Variant(int Index, StrategyParameters Parameters, IReadOnlyList Position) +{ + public override string ToString() => Parameters.ToString(); +} + +/// The variants of a strategy family: every point of the grid the family accepts, in a fixed order. +public static class ParameterGrid +{ + /// + /// The Cartesian product of the grids (the family's own, or where given), without the + /// points the family rejects. A family without parameters has one variant. + /// + public static IReadOnlyList Of(IStrategyFamily family, IReadOnlyDictionary>? grids = null) + { + ArgumentNullException.ThrowIfNull(family); + var axes = family.Parameters + .Select(p => (p.Name, Values: (IReadOnlyList)[.. (grids?.GetValueOrDefault(p.Name) ?? p.Grid).Distinct().Order()])) + .ToList(); + if (axes.Any(a => a.Values.Count == 0)) + { + throw new ArgumentException("Every parameter needs at least one value on its grid", nameof(grids)); + } + + var result = new List(); + var position = new int[axes.Count]; + while (true) + { + var parameters = new StrategyParameters(axes.Select((a, i) => KeyValuePair.Create(a.Name, a.Values[position[i]]))); + if (family.Accepts(parameters)) + { + result.Add(new Variant(result.Count, parameters, [.. position])); + } + + // Next point: the last parameter moves fastest. + var k = axes.Count - 1; + while (k >= 0 && ++position[k] == axes[k].Values.Count) + { + position[k] = 0; + k--; + } + + if (k < 0) + { + return result; + } + } + } +} + +/// Count, sum and sum of squares of some daily returns: enough for a mean and a Sharpe ratio. +internal readonly record struct ReturnSums(int Count, double Sum, double Squares) +{ + public static ReturnSums operator +(ReturnSums a, ReturnSums b) => new(a.Count + b.Count, a.Sum + b.Sum, a.Squares + b.Squares); + + public static ReturnSums Of(double[] returns, int from, int to) + { + double sum = 0, squares = 0; + for (var t = from; t < to; t++) + { + sum += returns[t]; + squares += returns[t] * returns[t]; + } + + return new ReturnSums(Math.Max(to - from, 0), sum, squares); + } + + /// Daily Sharpe ratio: mean over sample standard deviation, 0 when it cannot be computed. + public double Sharpe + { + get + { + if (Count < 2) + { + return 0; + } + + var mean = Sum / Count; + var variance = (Squares - (Count * mean * mean)) / (Count - 1); + return variance > 1e-30 ? mean / Math.Sqrt(variance) : 0; + } + } +} + +/// A choice of variant on some data and how it then did on other data. +public sealed record WalkForwardWindow(int Window, int TrainFrom, int TrainTo, int TestFrom, int TestTo, int Chosen, double TestReturn, double TestSharpe); + +/// Walk-forward validation: each window chooses on the past and is judged on what follows. +public sealed record WalkForwardResult(IReadOnlyList Windows, int Purge, double Return, double Sharpe); + +/// Combinatorial purged cross-validation: every path of out-of-sample choices, with its Sharpe ratio. +public sealed record CombinatorialResult(int Groups, int TestGroups, int Splits, int Purge, int Embargo, IReadOnlyList Chosen, IReadOnlyList PathSharpes) +{ + public int Paths => PathSharpes.Count; + + public double MeanSharpe => PathSharpes.Count > 0 ? PathSharpes.Average() : 0; + + public double WorstSharpe => PathSharpes.Count > 0 ? PathSharpes.Min() : 0; + + public double PositiveShare => PathSharpes.Count > 0 ? (double)PathSharpes.Count(s => s > 0) / PathSharpes.Count : 0; +} + +/// Probability of backtest overfitting by CSCV, with the logits it counts. +public sealed record OverfittingResult(int Groups, int Combinations, int Variants, double Probability, IReadOnlyList Logits); + +/// +/// The validations of SPEC 12.2 points 4 and 5 on the daily returns of every variant (one array per variant, all the +/// same length and on the same sessions). Choices follow the plateau rule of point 5: a variant scores its own Sharpe +/// ratio, but no more than the mean of its zone (itself and its neighbours on the grid), so a lonely peak loses to a +/// broad hill and a poor variant does not win by standing next to a good one. +/// +public static class Validation +{ + /// + /// Each variant's value, capped at the mean of its zone: itself and the variants one step away on every axis. + /// + public static double[] PlateauScores(IReadOnlyList variants, IReadOnlyList values) + { + ArgumentNullException.ThrowIfNull(variants); + ArgumentNullException.ThrowIfNull(values); + var scores = new double[variants.Count]; + for (var i = 0; i < variants.Count; i++) + { + double sum = 0; + var count = 0; + for (var j = 0; j < variants.Count; j++) + { + if (Neighbours(variants[i].Position, variants[j].Position)) + { + sum += values[j]; + count++; + } + } + + scores[i] = Math.Min(values[i], sum / count); + } + + return scores; + } + + /// The variant with the best plateau score; ties go to the first in grid order. + public static int ChoosePlateau(IReadOnlyList variants, IReadOnlyList values) + { + var scores = PlateauScores(variants, values); + var best = 0; + for (var i = 1; i < scores.Length; i++) + { + if (scores[i] > scores[best]) + { + best = i; + } + } + + return best; + } + + /// Sessions to drop around a test block: 1% of the series, at least one (López de Prado, AFML ch. 7). + public static int DefaultGap(int observations) => Math.Max(1, (int)Math.Ceiling(observations * 0.01)); + + /// + /// Walk-forward with a growing training window: the series is cut into + 1 equal blocks; + /// window k chooses on blocks 0..k (less the last sessions, which touch the test) and is + /// judged on block k + 1. Null when the series is too short for the blocks. + /// + public static WalkForwardResult? WalkForward(IReadOnlyList variants, IReadOnlyList returns, int windows = 4, int? purge = null) + { + var length = CheckShape(variants, returns); + var gap = purge ?? DefaultGap(length); + var block = length / (windows + 1); + if (windows < 1 || block < 2 + gap) + { + return null; + } + + var result = new List(); + var outOfSample = new List(); + for (var k = 0; k < windows; k++) + { + var testFrom = (k + 1) * block; + var testTo = k == windows - 1 ? length : testFrom + block; + var trainTo = testFrom - gap; + var sharpes = returns.Select(r => ReturnSums.Of(r, 0, trainTo).Sharpe).ToArray(); + var chosen = ChoosePlateau(variants, sharpes); + var test = returns[chosen][testFrom..testTo]; + outOfSample.AddRange(test); + result.Add(new WalkForwardWindow(k + 1, 0, trainTo, testFrom, testTo, chosen, Compound(test), ReturnSums.Of(test, 0, test.Length).Sharpe)); + } + + var all = outOfSample.ToArray(); + return new WalkForwardResult(result, gap, Compound(all), ReturnSums.Of(all, 0, all.Length).Sharpe); + } + + /// + /// Combinatorial purged cross-validation (López de Prado, AFML ch. 12): the series is cut into + /// blocks; every choice of of them is a test set, the rest + /// trains, less sessions before each test block and after it. + /// Each block is tested in several splits; taking them in order builds the paths, each a full out-of-sample series. + /// + public static CombinatorialResult? Combinatorial(IReadOnlyList variants, IReadOnlyList returns, int groups = 6, int testGroups = 2, int? purge = null, int? embargo = null) + { + var length = CheckShape(variants, returns); + ArgumentOutOfRangeException.ThrowIfLessThan(groups, 2); + ArgumentOutOfRangeException.ThrowIfLessThan(testGroups, 1); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(testGroups, groups); + var before = purge ?? DefaultGap(length); + var after = embargo ?? DefaultGap(length); + var bounds = Bounds(length, groups); + if (bounds.Any(b => b.To - b.From < 2 + before + after)) + { + return null; + } + + var splits = Combinations(groups, testGroups); + var chosen = new int[splits.Count]; + // For each group, the out-of-sample returns of every split that tests it, in split order. + var tested = Enumerable.Range(0, groups).Select(_ => new List()).ToArray(); + for (var s = 0; s < splits.Count; s++) + { + var test = splits[s]; + var train = new bool[length]; + for (var g = 0; g < groups; g++) + { + if (!test.Contains(g)) + { + for (var t = bounds[g].From; t < bounds[g].To; t++) + { + train[t] = true; + } + } + } + + foreach (var g in test) + { + for (var t = Math.Max(0, bounds[g].From - before); t < bounds[g].From; t++) + { + train[t] = false; + } + + for (var t = bounds[g].To; t < Math.Min(length, bounds[g].To + after); t++) + { + train[t] = false; + } + } + + var sharpes = returns.Select(r => Masked(r, train).Sharpe).ToArray(); + chosen[s] = ChoosePlateau(variants, sharpes); + foreach (var g in test) + { + tested[g].Add(returns[chosen[s]][bounds[g].From..bounds[g].To]); + } + } + + var paths = tested[0].Count; + var pathSharpes = new double[paths]; + for (var p = 0; p < paths; p++) + { + var path = tested.SelectMany(g => g[p]).ToArray(); + pathSharpes[p] = ReturnSums.Of(path, 0, path.Length).Sharpe; + } + + return new CombinatorialResult(groups, testGroups, splits.Count, before, after, chosen, pathSharpes); + } + + /// + /// Probability of backtest overfitting by combinatorially symmetric cross-validation (Bailey, Borwein, López de + /// Prado and Zhu, 2015). The series is cut into blocks; for every half chosen as + /// in-sample, the variant with the best in-sample Sharpe is ranked among all variants on the other half. The logit + /// of its relative rank is negative when it lands in the worse half; PBO is the share of those cases. Null with + /// fewer than two variants or too short a series. + /// + public static OverfittingResult? Overfitting(IReadOnlyList returns, int groups = 16) + { + ArgumentNullException.ThrowIfNull(returns); + if (returns.Count < 2) + { + return null; + } + + var length = returns[0].Length; + if (returns.Any(r => r.Length != length)) + { + throw new ArgumentException("Every variant needs returns on the same sessions", nameof(returns)); + } + + // Even, and at least 4 sessions per block. + groups = Math.Min(groups, length / 4) / 2 * 2; + if (groups < 4) + { + return null; + } + + var bounds = Bounds(length, groups); + var n = returns.Count; + var sums = new ReturnSums[n, groups]; + for (var v = 0; v < n; v++) + { + for (var g = 0; g < groups; g++) + { + sums[v, g] = ReturnSums.Of(returns[v], bounds[g].From, bounds[g].To); + } + } + + var logits = new List(); + var outOfSample = new double[n]; + foreach (var inSample in Combinations(groups, groups / 2)) + { + var isIn = new bool[groups]; + foreach (var g in inSample) + { + isIn[g] = true; + } + + var best = -1; + var bestSharpe = double.NegativeInfinity; + for (var v = 0; v < n; v++) + { + ReturnSums a = default, b = default; + for (var g = 0; g < groups; g++) + { + if (isIn[g]) + { + a += sums[v, g]; + } + else + { + b += sums[v, g]; + } + } + + if (a.Sharpe > bestSharpe) + { + bestSharpe = a.Sharpe; + best = v; + } + + outOfSample[v] = b.Sharpe; + } + + // Rank 1 is the worst out of sample, n the best; ties share the average rank. + double below = 0, equal = 0; + for (var v = 0; v < n; v++) + { + if (v == best) + { + continue; + } + + if (outOfSample[v] < outOfSample[best]) + { + below++; + } + else if (outOfSample[v] == outOfSample[best]) + { + equal++; + } + } + + var omega = (1 + below + (equal / 2)) / (n + 1); + logits.Add(Math.Log(omega / (1 - omega))); + } + + return new OverfittingResult(groups, logits.Count, n, (double)logits.Count(l => l <= 0) / logits.Count, logits); + } + + /// Growth of 1 over the returns, less 1. + public static double Compound(IEnumerable returns) + { + ArgumentNullException.ThrowIfNull(returns); + var value = 1d; + foreach (var r in returns) + { + value *= 1 + r; + } + + return value - 1; + } + + /// Every way of choosing of items, in lexicographic order. + internal static List Combinations(int n, int k) + { + var result = new List(); + var current = new int[k]; + for (var i = 0; i < k; i++) + { + current[i] = i; + } + + while (true) + { + result.Add([.. current]); + var j = k - 1; + while (j >= 0 && current[j] == n - k + j) + { + j--; + } + + if (j < 0) + { + return result; + } + + current[j]++; + for (var i = j + 1; i < k; i++) + { + current[i] = current[i - 1] + 1; + } + } + } + + private static (int From, int To)[] Bounds(int length, int groups) + { + var result = new (int, int)[groups]; + for (var g = 0; g < groups; g++) + { + result[g] = ((int)((long)length * g / groups), (int)((long)length * (g + 1) / groups)); + } + + return result; + } + + private static ReturnSums Masked(double[] returns, bool[] mask) + { + ReturnSums sums = default; + for (var t = 0; t < returns.Length; t++) + { + if (mask[t]) + { + sums += new ReturnSums(1, returns[t], returns[t] * returns[t]); + } + } + + return sums; + } + + private static bool Neighbours(IReadOnlyList a, IReadOnlyList b) + { + for (var k = 0; k < a.Count; k++) + { + if (Math.Abs(a[k] - b[k]) > 1) + { + return false; + } + } + + return true; + } + + private static int CheckShape(IReadOnlyList variants, IReadOnlyList returns) + { + ArgumentNullException.ThrowIfNull(variants); + ArgumentNullException.ThrowIfNull(returns); + if (variants.Count == 0 || variants.Count != returns.Count) + { + throw new ArgumentException("One return series per variant", nameof(returns)); + } + + var length = returns[0].Length; + if (returns.Any(r => r.Length != length)) + { + throw new ArgumentException("Every variant needs returns on the same sessions", nameof(returns)); + } + + return length; + } +} diff --git a/src/Anubis/Db/Migrations/0004_lab.sql b/src/Anubis/Db/Migrations/0004_lab.sql new file mode 100644 index 0000000..95b3780 --- /dev/null +++ b/src/Anubis/Db/Migrations/0004_lab.sql @@ -0,0 +1,25 @@ +-- Phase 4 schema (SPEC 12.2, ADR 0010): the register of backtest trials of the laboratory. + +-- One row per trial. The plan (strategy, grid, data, costs, validations, thresholds) is written before any result +-- and never changed afterwards: plan_hash is the SHA-256 of its text, so a report always shows the plan it answers. +-- Every trial stays, failed and passed alike: the Deflated Sharpe Ratio counts them all (SPEC 12.2 point 1). +CREATE TABLE backtest_trials ( + id {uuid} PRIMARY KEY, + strategy TEXT NOT NULL, + strategy_version TEXT NOT NULL, + plan {json} NOT NULL, + plan_hash TEXT NOT NULL, + variants INTEGER NOT NULL, + status TEXT NOT NULL, + verdict TEXT, + data_fingerprint TEXT, + summary {json}, + report {json}, + error TEXT, + created_at {timestamp} NOT NULL, + started_at {timestamp}, + finished_at {timestamp} +){strict}; + +CREATE INDEX backtest_trials_strategy ON backtest_trials (strategy, created_at); +CREATE INDEX backtest_trials_status ON backtest_trials (status); diff --git a/src/Anubis/Strategies/Research.cs b/src/Anubis/Strategies/Research.cs new file mode 100644 index 0000000..04dabc0 --- /dev/null +++ b/src/Anubis/Strategies/Research.cs @@ -0,0 +1,152 @@ +namespace Anubis.Strategies; + +/// +/// Buy in equal parts, then hold to the end: the "comprare e tenere" comparison of SPEC 12.2, step 6. Each +/// instrument is bought at its first close, so one whose history starts later is bought then, with the share kept for +/// it. It decides only from its own book, so it can restart from any recorded state. +/// +/// How many instruments share the capital; null means those available at each decision. +public sealed class BuyAndHold(int? instruments = null) : IStrategy +{ + /// A little cash stays aside for the fees, so the last purchase is not cut short. + public const decimal Invested = 0.995m; + + public string Id => BuyAndHoldFamily.FamilyId; + + public string Version => "2"; + + public IReadOnlyList Schedule() => [DecisionTime.AfterClose]; + + public IReadOnlyList Universe(UniverseContext context) + { + ArgumentNullException.ThrowIfNull(context); + return context.Available; + } + + public IReadOnlyList Decide(StrategyContext context) + { + ArgumentNullException.ThrowIfNull(context); + var parts = Math.Max(instruments ?? context.Universe.Count, context.Universe.Count); + if (parts == 0) + { + return []; + } + + var weight = Invested / parts; + return [.. context.Universe.Where(s => context.Portfolio.Of(s) is null).Select(s => new Intent(s, weight, "buy_and_hold"))]; + } +} + +public sealed class BuyAndHoldFamily : IStrategyFamily +{ + public const string FamilyId = "buy_and_hold"; + + public static BuyAndHoldFamily Instance { get; } = new(); + + public string Id => FamilyId; + + public string Version => "2"; + + public string Name => "Comprare e tenere"; + + public string Description => "Compra in parti uguali alla prima chiusura di ogni strumento e tiene fino alla fine. È il confronto di ogni rapporto: dice quanto avrebbe reso non fare nulla."; + + public IReadOnlyList Parameters => []; + + public IStrategy Create(StrategyParameters parameters) => new BuyAndHold(); +} + +/// +/// An example strategy to try the laboratory, not one of the bot's (those arrive with Phase 5): it holds an +/// instrument while the short average of its closes is above the long one. It has no strategy code, so it can never +/// send an order (SPEC 7.2). +/// +public sealed class MovingAverageCross(int fast, int slow) : IStrategy +{ + public string Id => MovingAverageCrossFamily.FamilyId; + + public string Version => "1"; + + public int Fast { get; } = fast; + + public int Slow { get; } = slow; + + public IReadOnlyList Schedule() => [DecisionTime.AfterClose]; + + public IReadOnlyList Universe(UniverseContext context) + { + ArgumentNullException.ThrowIfNull(context); + return context.Available; + } + + public IReadOnlyList Decide(StrategyContext context) + { + ArgumentNullException.ThrowIfNull(context); + var intents = new List(); + var weight = 1m / Math.Max(context.Universe.Count, 1); + foreach (var symbol in context.Universe) + { + var bars = context.Bars(symbol); + if (bars.Count < Slow) + { + continue; + } + + var closes = bars.Closes(Slow); + var slowAverage = closes.Average(); + var fastAverage = closes[^Fast..].Average(); + var held = context.Portfolio.Holds(symbol); + if (!held && fastAverage > slowAverage) + { + intents.Add(new Intent(symbol, weight, "average_cross_up")); + } + else if (held && fastAverage < slowAverage) + { + intents.Add(new Intent(symbol, 0m, "average_cross_down")); + } + } + + return intents; + } +} + +public sealed class MovingAverageCrossFamily : IStrategyFamily +{ + public const string FamilyId = "sma_cross"; + + public static MovingAverageCrossFamily Instance { get; } = new(); + + public string Id => FamilyId; + + public string Version => "1"; + + public string Name => "Incrocio di medie (esempio)"; + + public string Description => "Strategia d'esempio per provare il laboratorio, non una di quelle del bot: tiene uno strumento finché la media breve dei prezzi di chiusura sta sopra la media lunga, in parti uguali tra gli strumenti scelti. Non manda mai ordini."; + + public IReadOnlyList Parameters => + [ + new("fast", "Giorni della media breve", 20m, [10m, 20m, 30m], 2m, 200m, Whole: true), + new("slow", "Giorni della media lunga", 100m, [50m, 100m, 150m], 5m, 400m, Whole: true), + ]; + + public bool Accepts(StrategyParameters parameters) + { + ArgumentNullException.ThrowIfNull(parameters); + return parameters.Whole("fast") < parameters.Whole("slow"); + } + + public IStrategy Create(StrategyParameters parameters) + { + ArgumentNullException.ThrowIfNull(parameters); + return new MovingAverageCross(parameters.Whole("fast"), parameters.Whole("slow")); + } +} + +/// The strategies the laboratory can test. The bot's own strategies join with Phase 5. +public static class StrategyCatalog +{ + public static IReadOnlyList All { get; } = [MovingAverageCrossFamily.Instance, BuyAndHoldFamily.Instance]; + + public static IStrategyFamily? Find(string id) => All.FirstOrDefault(f => f.Id == id); +} diff --git a/src/Anubis/Strategies/Strategy.cs b/src/Anubis/Strategies/Strategy.cs new file mode 100644 index 0000000..24bd73d --- /dev/null +++ b/src/Anubis/Strategies/Strategy.cs @@ -0,0 +1,235 @@ +using System.Globalization; +using Anubis.Data; + +namespace Anubis.Strategies; + +/// Where in the market's day a decision is taken (SPEC 5.3: decision times follow the market calendar). +public enum DecisionAnchor +{ + /// After the close of the session, on its finished bar. Orders go at the next open. + Close, + + /// A given time after the open (live engine, Phase 6). The backtest of daily bars does not support it. + Open, +} + +/// When a strategy decides, expressed against the market calendar, never as a fixed clock time. +public sealed record DecisionTime(DecisionAnchor Anchor, TimeSpan Offset) +{ + public static DecisionTime AfterClose { get; } = new(DecisionAnchor.Close, TimeSpan.Zero); +} + +/// +/// What a strategy wants for one instrument (SPEC 5.2). is the share of the strategy's +/// equity to hold in the instrument after the next execution: 0 means exit. Null keeps the size and only updates the +/// stop. is a protective stop level in today's prices; null keeps the current one. +/// +public sealed record Intent(string Symbol, decimal? TargetWeight, string Reason) +{ + public decimal? Stop { get; init; } + + public override string ToString() => string.Create(CultureInfo.InvariantCulture, $"{Symbol} {(TargetWeight is { } w ? w.ToString(CultureInfo.InvariantCulture) : "keep")} stop {(Stop is { } s ? s.ToString(CultureInfo.InvariantCulture) : "-")} ({Reason})"); +} + +/// Kinds of intent, as the one-step comparison of SPEC 12.3 reads them. +public enum IntentAction +{ + Enter, + Exit, + Resize, + Keep, +} + +/// A position as the strategy sees it: quantity, average entry price and stop in today's prices. +public sealed record Holding(decimal Quantity, decimal AveragePrice, decimal? Stop, DateOnly EnteredOn); + +/// The strategy's own book at the moment of the decision. +public sealed class PortfolioView(decimal cash, decimal equity, IReadOnlyDictionary holdings) +{ + public decimal Cash { get; } = cash; + + /// Cash plus positions at the last close. + public decimal Equity { get; } = equity; + + public IReadOnlyDictionary Holdings { get; } = holdings; + + public Holding? Of(string symbol) => Holdings.GetValueOrDefault(symbol); + + public bool Holds(string symbol) => Holdings.TryGetValue(symbol, out var h) && h.Quantity > 0m; +} + +/// +/// Daily bars of one instrument up to the moment of the decision and not beyond (SPEC 5.3). Prices are adjusted +/// for splits and dividends and anchored to the decision day, so the last bar shows the prices of today: signals +/// read adjusted prices, orders use real ones (trap 9). Index 0 is the oldest bar, − 1 today's. +/// Reading past today throws: there is no way to look ahead through this object. +/// +public sealed class BarSeries +{ + private readonly IReadOnlyList _bars; + private readonly double[] _factors; + private readonly double _anchor; + + internal BarSeries(IReadOnlyList bars, double[] factors, int count) + { + _bars = bars; + _factors = factors; + Count = count; + _anchor = count > 0 ? factors[count - 1] : 1d; + } + + public static BarSeries Empty { get; } = new([], [], 0); + + public int Count { get; } + + public DateOnly Day(int index) => Bar(index).Day; + + public Ohlc Prices(int index) => Bar(index).Prices.Scale(_factors[index] / _anchor); + + public double Close(int index) => Bar(index).Close * (_factors[index] / _anchor); + + public double High(int index) => Bar(index).High * (_factors[index] / _anchor); + + public double Low(int index) => Bar(index).Low * (_factors[index] / _anchor); + + public double Volume(int index) => Bar(index).Volume; + + /// The last closes, oldest first (fewer when the history is shorter). + public double[] Closes(int length) + { + var n = Math.Min(length, Count); + var result = new double[n]; + for (var i = 0; i < n; i++) + { + result[i] = Close(Count - n + i); + } + + return result; + } + + private DailyBar Bar(int index) + { + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); + return _bars[index]; + } +} + +/// What a strategy may read to choose its instruments for a decision. +public sealed record UniverseContext(DateOnly Day, IReadOnlyList Available); + +/// Named numeric parameters of a strategy variant. Values are decimal so a grid is written exactly. +public sealed class StrategyParameters +{ + private readonly SortedDictionary _values; + + public StrategyParameters(IEnumerable> values) + { + _values = new SortedDictionary(StringComparer.Ordinal); + foreach (var (key, value) in values) + { + _values[key] = value; + } + } + + public static StrategyParameters None { get; } = new([]); + + public IReadOnlyDictionary Values => _values; + + public decimal this[string name] => _values.TryGetValue(name, out var v) ? v : throw new KeyNotFoundException("Missing parameter " + name); + + public int Whole(string name) => (int)this[name]; + + /// A stable text such as fast=20;slow=100: equal parameters give equal text. + public override string ToString() => string.Join(';', _values.Select(p => p.Key + "=" + p.Value.ToString(CultureInfo.InvariantCulture))); +} + +/// +/// Everything a strategy may know when it decides (SPEC 5.3): data up to the decision and not beyond, its own book, +/// the instruments' rules. The same object in the backtest, in simulation and with real money: a strategy cannot +/// tell where it runs. +/// +public sealed class StrategyContext( + DateOnly day, + DateTimeOffset now, + IReadOnlyList universe, + PortfolioView portfolio, + Func bars, + Func spec) +{ + public DateOnly Day { get; } = day; + + public DateTimeOffset Now { get; } = now; + + /// Instruments the strategy chose for this decision. + public IReadOnlyList Universe { get; } = universe; + + public PortfolioView Portfolio { get; } = portfolio; + + public BarSeries Bars(string symbol) => bars(symbol); + + public InstrumentSpec? Spec(string symbol) => spec(symbol); +} + +/// A trading strategy (SPEC 5.3): a function of the data up to "now" and of its own book, nothing else. +public interface IStrategy +{ + string Id { get; } + + string Version { get; } + + IReadOnlyList Schedule(); + + IReadOnlyList Universe(UniverseContext context); + + IReadOnlyList Decide(StrategyContext context); +} + +/// +/// A parameter of a strategy family, with the grid tried by default (SPEC 12.2: written before the results). +/// parameters take only whole numbers, such as a number of days. +/// +public sealed record ParameterSpec(string Name, string Label, decimal Default, IReadOnlyList Grid, decimal Min, decimal Max, bool Whole = false); + +/// A strategy and the parameters it can take: the laboratory builds one variant for each point of the grid. +public interface IStrategyFamily +{ + string Id { get; } + + string Version { get; } + + /// The name shown to the user, in Italian. + string Name { get; } + + /// What the strategy does, in plain Italian (SPEC 13.3). + string Description { get; } + + IReadOnlyList Parameters { get; } + + /// False for a point of the grid that makes no sense (a short average longer than the long one). + bool Accepts(StrategyParameters parameters) => true; + + IStrategy Create(StrategyParameters parameters); +} + +/// +/// A step between the strategy and the orders: the correctives of SPEC 8.5 and the risk engine of SPEC 9 plug in +/// here. Until Phase 5 both let everything through (SPEC 17, Phase 4). +/// +public interface IIntentStage +{ + string Name { get; } + + IReadOnlyList Apply(StrategyContext context, IReadOnlyList intents); +} + +public sealed class PassThroughStage(string name) : IIntentStage +{ + public static PassThroughStage Correctives { get; } = new("correctives"); + + public static PassThroughStage Risk { get; } = new("risk"); + + public string Name { get; } = name; + + public IReadOnlyList Apply(StrategyContext context, IReadOnlyList intents) => intents; +} diff --git a/src/Anubis/Trading/Capital.cs b/src/Anubis/Trading/Capital.cs index 66ba715..1aa5df5 100644 --- a/src/Anubis/Trading/Capital.cs +++ b/src/Anubis/Trading/Capital.cs @@ -5,7 +5,7 @@ using Anubis.Db; namespace Anubis.Trading; /// The three base strategies, their broker and their default share of its capital (SPEC 8.6). -public static class Strategies +public static class StrategyNames { public const string CryptoTrend = "crypto_trend"; public const string EquityMomentum = "equity_momentum"; @@ -82,7 +82,7 @@ public sealed class Capital(Database db, IClock clock, Ledger ledger, FxStore fx r => (Account: r.Id("account_id"), Strategy: r.Text("strategy"), Amount: r.Money("amount")), ("k", LedgerKinds.Capital)), cancellationToken).ConfigureAwait(false); var equities = await ledger.EquitiesAsync(cancellationToken).ConfigureAwait(false); return rows.Select(r => new StrategyCapital(r.Strategy, r.Account, r.Currency, - r.Share ?? Strategies.Base.FirstOrDefault(b => b.Name == r.Strategy).Share, + r.Share ?? StrategyNames.Base.FirstOrDefault(b => b.Name == r.Strategy).Share, given.Where(g => g.Account == r.Account && g.Strategy == r.Strategy).Sum(g => g.Amount), equities.Where(e => e.AccountId == r.Account && e.Strategy == r.Strategy).Sum(e => e.Cash))).ToList(); } @@ -95,7 +95,7 @@ public sealed class Capital(Database db, IClock clock, Ledger ledger, FxStore fx throw new CapitalException("Il capitale affidato non può essere negativo."); } - var currency = Strategies.CurrencyOf(broker); + var currency = StrategyNames.CurrencyOf(broker); var previous = await EntrustedAsync(broker, cancellationToken).ConfigureAwait(false); if (previous is not null && previous.AmountEur == amountEur) { diff --git a/src/Anubis/Trading/TradingService.cs b/src/Anubis/Trading/TradingService.cs index db316aa..6db06f5 100644 --- a/src/Anubis/Trading/TradingService.cs +++ b/src/Anubis/Trading/TradingService.cs @@ -96,7 +96,7 @@ public static class AccountSetup if (await s.CountAsync("SELECT COUNT(*) FROM accounts WHERE broker = @b AND environment = @e", ("b", broker), ("e", environment)).ConfigureAwait(false) == 0) { await s.ExecuteAsync("INSERT INTO accounts (id, name, broker, environment, base_currency, created_at) VALUES (@id, @n, @b, @e, @c, @now)", cancellationToken, - ("id", Ids.NewId(clock)), ("n", NameOf(broker, environment)), ("b", broker), ("e", environment), ("c", Strategies.CurrencyOf(broker)), ("now", now)) + ("id", Ids.NewId(clock)), ("n", NameOf(broker, environment)), ("b", broker), ("e", environment), ("c", StrategyNames.CurrencyOf(broker)), ("now", now)) .ConfigureAwait(false); } } @@ -106,7 +106,7 @@ public static class AccountSetup // By default crypto runs in the internal simulator and stocks in the demo account (SPEC 7.1). A strategy // whose ledger is still empty follows the default when a demo account appears; one with a history stays. - foreach (var (strategy, broker, share) in Strategies.Base) + foreach (var (strategy, broker, share) in StrategyNames.Base) { var environment = broker == Venues.Alpaca && alpacaDemo ? TradingEnvironments.Demo : TradingEnvironments.Simulation; var target = accounts.First(a => a.Broker == broker && a.Environment == environment).AccountId; @@ -115,7 +115,7 @@ public static class AccountSetup if (current is null && await s.CountAsync("SELECT COUNT(*) FROM strategy_state WHERE strategy = @s", ("s", strategy)).ConfigureAwait(false) == 0) { await s.ExecuteAsync("INSERT INTO strategy_state (id, strategy, account_id, stage, capital, state, created_at, updated_at, share) VALUES (@id, @s, @a, @st, 0, @j, @now, @now, @sh)", - cancellationToken, ("id", Ids.NewId(clock)), ("s", strategy), ("a", target), ("st", Strategies.Observation), ("j", new DbJson("{}")), ("now", now), ("sh", share)) + cancellationToken, ("id", Ids.NewId(clock)), ("s", strategy), ("a", target), ("st", StrategyNames.Observation), ("j", new DbJson("{}")), ("now", now), ("sh", share)) .ConfigureAwait(false); } else if (current != target diff --git a/tests/Anubis.Tests/BacktestTests.cs b/tests/Anubis.Tests/BacktestTests.cs new file mode 100644 index 0000000..32b9605 --- /dev/null +++ b/tests/Anubis.Tests/BacktestTests.cs @@ -0,0 +1,307 @@ +using Anubis.Backtest; +using Anubis.Data; +using Anubis.Strategies; +using Anubis.Trading; + +namespace Anubis.Tests; + +/// A strategy that says what it is told on given days: its result can be computed by hand. +internal sealed class ScriptedStrategy(IReadOnlyDictionary> script) : IStrategy +{ + public string Id => "scripted"; + + public string Version => "1"; + + public IReadOnlyList Schedule() => [DecisionTime.AfterClose]; + + public IReadOnlyList Universe(UniverseContext context) => context.Available; + + public IReadOnlyList Decide(StrategyContext context) => script.GetValueOrDefault(context.Day) ?? []; +} + +internal static class BacktestData +{ + public static readonly DateOnly D1 = new(2026, 1, 5); + + public static InstrumentSpec Btc() => new(Venues.Okx, "BTC-EUR", "Bitcoin", AssetClasses.Crypto, "EUR") + { + BaseCurrency = "BTC", + TickSize = 0.1m, + LotSize = 0.00000001m, + MinSize = 0.00001m, + Status = "live", + }; + + public static InstrumentSpec Stock(string symbol = "AAPL") => new(Venues.Alpaca, symbol, symbol, AssetClasses.Equity, "USD") + { + Fractionable = true, + Status = "active", + Exchange = "NASDAQ", + }; + + /// Five sessions (open, high, low, close) on consecutive days from . + public static IReadOnlyList FiveDays() => + [ + new(D1, 100, 101, 99, 100, 1000), + new(D1.AddDays(1), 100, 102, 99, 101, 1000), + new(D1.AddDays(2), 102, 104, 101, 103, 1000), + new(D1.AddDays(3), 103, 106, 102, 105, 1000), + new(D1.AddDays(4), 106, 107, 104, 106, 1000), + ]; + + public static BacktestDataset One(InstrumentSpec spec, IReadOnlyList bars, IReadOnlyList? actions = null) => + new([new BacktestInstrument(spec, bars, actions)], spec.QuoteCurrency); + + /// A random walk with a fixed seed: the same series at every run. + public static IReadOnlyList Walk(int days, ulong seed, double start = 100, double drift = 0.0003, double volatility = 0.02, DateOnly? from = null) + { + var gen = new Gen((int)(seed % int.MaxValue)); + var bars = new List(days); + var close = start; + var day = from ?? new DateOnly(2018, 1, 1); + for (var i = 0; i < days; i++) + { + var open = close; + var change = drift + (volatility * gen.Normal()); + close = Math.Round(open * Math.Exp(change), 2); + var high = Math.Round(Math.Max(open, close) * (1 + (0.005 * gen.Double())), 2); + var low = Math.Round(Math.Min(open, close) * (1 - (0.005 * gen.Double())), 2); + bars.Add(new DailyBar(day.AddDays(i), open, high, low, close, 1000)); + } + + return bars; + } +} + +/// The event engine, its costs and its records (issue #34, SPEC 12.1). +[TestClass] +public sealed class BacktestTests +{ + private static readonly DateOnly D1 = BacktestData.D1; + + private static ScriptedStrategy Script(params (DateOnly Day, Intent[] Intents)[] steps) => + new(steps.ToDictionary(s => s.Day, s => (IReadOnlyList)s.Intents)); + + [TestMethod] + [TestCategory(Category.Unit)] + public void AToyStrategyGivesExactlyTheResultComputedByHand() + { + // Half the capital in bitcoin at the first close, everything out at the third. By hand, with OKX's + // price list (0,20% taker kept in the coin bought, 5 bp of slippage, 0,1 € tick): + // buy at the open of day 2: 100 × 1,0005 = 100,05 → tick up → 100,1; 50 BTC = 5.005,00 €; fee 0,1 BTC + // received 49,9 BTC; cash 10.000 − 5.005 = 4.995,00 € + // sell at the open of day 4: 103 × 0,9995 = 102,9485 → tick down → 102,9; 49,9 × 102,9 = 5.134,71 €; + // fee 0,2% = 10,26942 €; cash 4.995 + 5.124,44058 = 10.119,44058 € + var strategy = Script((D1, [new Intent("BTC-EUR", 0.5m, "enter")]), (D1.AddDays(2), [new Intent("BTC-EUR", 0m, "exit")])); + var run = BacktestEngine.Run(strategy, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()), new BacktestOptions { InitialCapital = 10_000m }); + + Assert.AreEqual(10_119.44058m, run.FinalEquity); + Assert.AreEqual(119.44058m, run.NetResult); + Assert.HasCount(2, run.Fills); + Assert.AreEqual(new BacktestFill(D1.AddDays(1), "BTC-EUR", OrderSide.Buy, 50m, 100.1m, 5_005m, 10.01m, "enter"), run.Fills[0]); + Assert.AreEqual(new BacktestFill(D1.AddDays(3), "BTC-EUR", OrderSide.Sell, 49.9m, 102.9m, 5_134.71m, 10.26942m, "exit"), run.Fills[1]); + Assert.AreEqual(20.27942m, run.Fees); + + var trade = run.Trades.Single(); + Assert.AreEqual(new BacktestTrade("BTC-EUR", D1.AddDays(1), D1.AddDays(3), 5_005m, 119.44058m, 20.27942m), trade); + + // Valued at every close: cash plus 49,9 BTC at 101 and 103 while held. + CollectionAssert.AreEqual( + new[] { 10_000m, 10_034.9m, 10_134.7m, 10_119.44058m, 10_119.44058m }, + run.Equity.Select(e => e.Equity).ToArray()); + Assert.IsEmpty(run.Notes); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TheSameToyOnAStockPaysTheRegulatoryFeesRoundedUp() + { + // Alpaca: no commission, CAT 0,000003 $ a share on both sides and SEC 0,0000206 on sales, each rounded + // up to the cent; price step 0,01 $ above 1 $. By hand: + // buy 50 at 100 × 1,0005 = 100,05: 5.002,50 $ + CAT 0,01 → cash 4.997,49 $ + // sell 50 at 103 × 0,9995 = 102,9485 → 102,94: 5.147,00 $ − SEC 0,11 − CAT 0,01 → cash 10.144,37 $ + var strategy = Script((D1, [new Intent("AAPL", 0.5m, "enter")]), (D1.AddDays(2), [new Intent("AAPL", 0m, "exit")])); + var run = BacktestEngine.Run(strategy, BacktestData.One(BacktestData.Stock(), BacktestData.FiveDays())); + + Assert.AreEqual(10_144.37m, run.FinalEquity); + Assert.AreEqual(0.13m, run.Fees); + Assert.AreEqual(new BacktestTrade("AAPL", D1.AddDays(1), D1.AddDays(3), 5_002.5m, 144.37m, 0.13m), run.Trades.Single()); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void StopsFillAtTheirLevelOrAtTheOpenWhenThePriceGapsBelow() + { + // Touched during the session: the low of day 2 (99) reaches the stop at 99,5, sold at 99,5 less slippage. + var touched = Script((D1, [new Intent("BTC-EUR", 0.5m, "enter") { Stop = 99.5m }])); + var run = BacktestEngine.Run(touched, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays())); + var sale = run.Fills.Single(f => f.Side == OrderSide.Sell); + Assert.AreEqual((D1.AddDays(1), 99.4m, BacktestReasons.Stop), (sale.Day, sale.Price, sale.Reason)); + Assert.AreEqual(4_995m + 4_960.06m - 9.92012m, run.FinalEquity); + + // Opened below: the stop raised to 102,5 at the close of day 2 meets an open of 102 and sells there (trap 24). + var gapped = Script((D1, [new Intent("BTC-EUR", 0.5m, "enter")]), (D1.AddDays(1), [new Intent("BTC-EUR", null, "raise_stop") { Stop = 102.5m }])); + run = BacktestEngine.Run(gapped, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays())); + sale = run.Fills.Single(f => f.Side == OrderSide.Sell); + Assert.AreEqual((D1.AddDays(2), 101.9m, BacktestReasons.StopGap), (sale.Day, sale.Price, sale.Reason)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void ASplitChangesSharesAndStopsButNotTheValueAndSignalsSeeOneSeries() + { + // A 2-for-1 split with ex-date on day 4: real prices halve, the adjusted series does not jump. + var bars = BacktestData.FiveDays().Select((b, i) => i >= 3 ? b with { Open = b.Open / 2, High = b.High / 2, Low = b.Low / 2, Close = b.Close / 2 } : b).ToList(); + var split = new CorporateAction("AAPL", CorporateActionKinds.Split, D1.AddDays(3), "test", "s1") { Ratio = 2m }; + var seen = new List(); + var strategy = new Recording(seen, Script((D1, [new Intent("AAPL", 0.5m, "enter") { Stop = 90m }]))); + var run = BacktestEngine.Run(strategy, BacktestData.One(BacktestData.Stock(), bars, [split])); + + // 50 shares became 100, the stop 90 became 45: nothing was sold and the value went on with the price. + Assert.HasCount(1, run.Fills); + Assert.AreEqual(4_997.49m + (100m * 53m), run.FinalEquity); + + // What the strategy saw on the last day: one continuous series in today's prices, no halving. + CollectionAssert.AreEqual(new[] { 50d, 50.5, 51.5, 52.5, 53 }, seen.ToArray()); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void PurchasesNeverBorrowAndOrdersBelowTheMinimumAreNotSent() + { + // Two instruments at weight 0,8 each: the second purchase is cut to the cash left, never past it. + var data = new BacktestDataset( + [new BacktestInstrument(BacktestData.Btc(), BacktestData.FiveDays()), new BacktestInstrument(BacktestData.Btc() with { Symbol = "ETH-EUR", BaseCurrency = "ETH" }, BacktestData.FiveDays())], + "EUR"); + var run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", 0.8m, "a"), new Intent("ETH-EUR", 0.8m, "b")])), data); + Assert.IsTrue(run.Equity.All(e => e.Cash >= 0m), "Cash never goes below zero."); + Assert.IsTrue(run.Notes.Any(n => n.Symbol == "ETH-EUR" && n.Reason == BacktestReasons.NoCash)); + + // A purchase worth less than the minimum size is not sent and the note says why. + run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", 0.00000001m, "tiny")])), BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays())); + Assert.IsEmpty(run.Fills); + Assert.AreEqual(OrderReasons.BelowMinimumSize, run.Notes.Single().Reason); + + // Short selling is refused (SPEC 9.1). + run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", -0.5m, "short")])), BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays())); + Assert.IsEmpty(run.Fills); + Assert.AreEqual(BacktestReasons.NoShortSelling, run.Notes.Single().Reason); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void DoubleCostsCostMoreAndTheSameInputsGiveTheSameRun() + { + var data = BacktestData.One(BacktestData.Btc(), BacktestData.Walk(400, 7)); + var strategy = new MovingAverageCross(10, 40); + var once = BacktestEngine.Run(strategy, data); + var again = BacktestEngine.Run(new MovingAverageCross(10, 40), data); + CollectionAssert.AreEqual(once.Equity.ToArray(), again.Equity.ToArray()); + CollectionAssert.AreEqual(once.Fills.ToArray(), again.Fills.ToArray()); + Assert.IsGreaterThan(4, once.Trades.Count); + + var doubled = BacktestEngine.Run(strategy, data, new BacktestOptions { Costs = new CostModel(2m) }); + Assert.IsGreaterThan(once.Fees, doubled.Fees); + Assert.IsLessThan(once.FinalEquity, doubled.FinalEquity); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TheCorrectivesAndRiskHooksSeeEveryIntentAndCanChangeIt() + { + var seen = new List(); + var block = new Hook("risk", (ctx, intents) => + { + seen.AddRange(intents.Select(i => i.Symbol)); + return []; + }); + var run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", 0.5m, "enter")])), BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()), new BacktestOptions { Risk = block }); + CollectionAssert.AreEqual(new[] { "BTC-EUR" }, seen); + Assert.IsEmpty(run.Fills); + Assert.HasCount(1, run.Decisions[0].Proposed); + Assert.IsEmpty(run.Decisions[0].Final); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void BuyAndHoldKeepsTheShareOfAnInstrumentWhoseHistoryStartsLater() + { + // ETH has bars only from day 30: buy and hold buys it then, with half the equity, not never. + var data = new BacktestDataset( + [ + new BacktestInstrument(BacktestData.Btc(), BacktestData.Walk(60, 3, start: 40_000, from: D1)), + new BacktestInstrument(BacktestData.Btc() with { Symbol = "ETH-EUR", BaseCurrency = "ETH" }, BacktestData.Walk(30, 5, start: 2_000, from: D1.AddDays(30))), + ], "EUR"); + var run = BacktestEngine.Run(new BuyAndHold(2), data); + Assert.HasCount(2, run.Fills); + var btc = run.Fills.Single(f => f.Symbol == "BTC-EUR"); + var eth = run.Fills.Single(f => f.Symbol == "ETH-EUR"); + Assert.AreEqual(D1.AddDays(1), btc.Day); + Assert.AreEqual(D1.AddDays(31), eth.Day); + Assert.AreEqual(0.4975, (double)(btc.Gross / run.InitialCapital), 0.005); + var before = run.Equity.Single(p => p.Day == D1.AddDays(30)).Equity; + Assert.AreEqual(0.4975, (double)(eth.Gross / before), 0.01); + Assert.IsTrue(run.Equity.All(e => e.Cash >= 0m)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void OneCycleRestartsFromARecordedBook() + { + // The live book holds 49,9 BTC since day 2: the strategy decides on day 3 as if the backtest had run till then. + var book = new PortfolioState(4_995m, new Dictionary { ["BTC-EUR"] = new(49.9m, 100.3m, null, D1.AddDays(1)) }); + var exitWhenHeld = new ExitIfHeld(); + var record = BacktestEngine.DecideOnce(exitWhenHeld, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()), book, D1.AddDays(2)); + Assert.AreEqual(D1.AddDays(2), record.Day); + Assert.AreEqual(new Intent("BTC-EUR", 0m, "held"), record.Proposed.Single()); + Assert.AreEqual(4_995m + (49.9m * 103m), exitWhenHeld.Equity, "The book is valued at that day's close."); + } + + private sealed class Recording(List seen, IStrategy inner) : IStrategy + { + public string Id => inner.Id; + + public string Version => inner.Version; + + public IReadOnlyList Schedule() => inner.Schedule(); + + public IReadOnlyList Universe(UniverseContext context) => inner.Universe(context); + + public IReadOnlyList Decide(StrategyContext context) + { + var bars = context.Bars("AAPL"); + if (bars.Count == 5) + { + seen.AddRange(bars.Closes(5)); + } + + return inner.Decide(context); + } + } + + private sealed class Hook(string name, Func, IReadOnlyList> apply) : IIntentStage + { + public string Name => name; + + public IReadOnlyList Apply(StrategyContext context, IReadOnlyList intents) => apply(context, intents); + } + + private sealed class ExitIfHeld : IStrategy + { + public decimal Equity { get; private set; } + + public string Id => "exit_if_held"; + + public string Version => "1"; + + public IReadOnlyList Schedule() => [DecisionTime.AfterClose]; + + public IReadOnlyList Universe(UniverseContext context) => context.Available; + + public IReadOnlyList Decide(StrategyContext context) + { + Equity = context.Portfolio.Equity; + return [.. context.Universe.Where(context.Portfolio.Holds).Select(s => new Intent(s, 0m, "held"))]; + } + } +} diff --git a/tests/Anubis.Tests/BrokerConformanceTests.cs b/tests/Anubis.Tests/BrokerConformanceTests.cs index f3f7cc0..6ed6653 100644 --- a/tests/Anubis.Tests/BrokerConformanceTests.cs +++ b/tests/Anubis.Tests/BrokerConformanceTests.cs @@ -29,7 +29,7 @@ public abstract class BrokerConformance protected abstract string Environment { get; } private string ClientId() => - ClientOrderIds.Build(TradingRig.Installation, Environment, Strategies.CryptoTrend, OrderPurpose.Entry, ClientOrderIds.Key(new Guid("0193a7c0-0000-7000-8000-000000000031")), 1); + ClientOrderIds.Build(TradingRig.Installation, Environment, StrategyNames.CryptoTrend, OrderPurpose.Entry, ClientOrderIds.Key(new Guid("0193a7c0-0000-7000-8000-000000000031")), 1); [TestMethod] public async Task TheVenueAnswersWithItsTimeAndItsCash() diff --git a/tests/Anubis.Tests/LaboratoryScreenTests.cs b/tests/Anubis.Tests/LaboratoryScreenTests.cs new file mode 100644 index 0000000..b096892 --- /dev/null +++ b/tests/Anubis.Tests/LaboratoryScreenTests.cs @@ -0,0 +1,139 @@ +using System.Net; +using Anubis.Backtest; +using Anubis.Core; +using Anubis.Data; +using Anubis.Server; + +namespace Anubis.Tests; + +/// The Laboratorio screen (issue #36): a new test from the form, its report, the comparison. +[TestClass] +public sealed class LaboratoryScreenTests +{ + [TestMethod] + [TestCategory(Category.Integration)] + public async Task TheReportOpensFromTheLaboratory() + { + await using var server = await ServerHarness.StartAsync("docker", ServerHarness.DockerSqlite()); + await server.SignInFirstTimeAsync(); + var db = server.Host.Services!.Db; + var btc = await new InstrumentCatalog(db, SystemClock.Instance).EnsureAsync(MarketDataStoreTests.Okx("BTC-EUR"), CancellationToken.None); + await new BarStore(db, SystemClock.Instance).UpsertDailyAsync(btc.Id, + BacktestData.Walk(400, 3, start: 40_000, drift: 0.001, volatility: 0.03, from: new DateOnly(2024, 1, 1)), "test", CancellationToken.None); + + var empty = await server.TextAsync("/laboratorio"); + StringAssert.Contains(empty, "Nessun test eseguito."); + StringAssert.Contains(empty, "Scrivi il piano e avvia il test"); + StringAssert.Contains(empty, "Migliorie in osservazione"); + StringAssert.Contains(empty, "Stime di costo da confermare"); + + // A grid value out of range goes back to the form with the reason, and nothing is planned. + using (var wrong = await server.PostFormAsync("/laboratorio", "/azioni/laboratorio", Fields(("griglia_fast", "1, 5")))) + { + Assert.AreEqual("/laboratorio?errore=griglia&strategia=sma_cross#nuovo", ServerHarness.Location(wrong)); + } + + StringAssert.Contains(await server.TextAsync("/laboratorio?errore=griglia&strategia=sma_cross"), "fuori dall'intervallo del parametro"); + Assert.HasCount(0, await new TrialStore(db, SystemClock.Instance).ListAsync(10, CancellationToken.None)); + + var first = await PlanAsync(server, Fields()); + var second = await PlanAsync(server, Fields(("griglia_fast", "10"), ("griglia_slow", "30"))); + await WaitAsync(db, first); + await WaitAsync(db, second); + + var list = await server.TextAsync("/laboratorio"); + StringAssert.Contains(list, $"href=\"/laboratorio/{first}\""); + StringAssert.Contains(list, "Completato"); + StringAssert.Contains(list, "Confronta"); + + var report = await server.TextAsync($"/laboratorio/{first}"); + foreach (var part in new[] + { + "Esito del protocollo", "Risultato netto con costi doppi", "Deflated Sharpe Ratio", "Probabilità di sovra-adattamento (PBO)", + "Risultato della variante scelta", "Comprare e tenere", "Risultato per anno", "Sensibilità ai costi", "Finestre che avanzano", + "K-fold purgato combinatorio", "Nessuno sguardo in avanti", "nessuna è cambiata", "Tutte le varianti", "Cosa questo test non considera", + "Fase 5", "Impronta dei dati", "Vedi i numeri in una tabella", + }) + { + StringAssert.Contains(report, part, $"Missing \"{part}\" in the report."); + } + + var compare = await server.TextAsync($"/laboratorio/confronto?a={first}&b={second}"); + StringAssert.Contains(compare, "Stessi dati per entrambi"); + StringAssert.Contains(compare, "non si applica", "The second test has one variant: no PBO."); + + StringAssert.Contains(await server.TextAsync("/laboratorio/confronto?a=nonunid&b=" + first), "Servono due test completati."); + + using var missing = await server.GetAsync($"/laboratorio/{Guid.NewGuid()}"); + StringAssert.Contains(await missing.Content.ReadAsStringAsync(), "Test non trovato."); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task TheDemoRunsNoTests() + { + string demoRoot; + { + await using var server = await ServerHarness.StartAsync("docker", ServerHarness.DockerSqlite(), extraArgs: ["--demo"]); + demoRoot = server.Host.Config.Data.Root; + var page = await server.TextAsync("/laboratorio"); + StringAssert.Contains(page, "Il laboratorio non esegue test in questa modalità."); + Assert.IsFalse(page.Contains("/azioni/laboratorio", StringComparison.Ordinal), "No form in the demo."); + using var post = await server.PostFormAsync("/laboratorio", "/azioni/laboratorio", Fields()); + Assert.AreEqual("/laboratorio?errore=motore&strategia=sma_cross#nuovo", ServerHarness.Location(post)); + } + + // After the server has stopped, so the demo database is closed. + + TempFolder.Delete(demoRoot); + } + + private static (string, string)[] Fields(params (string Name, string Value)[] overrides) + { + var fields = new Dictionary(StringComparer.Ordinal) + { + ["strategia"] = "sma_cross", + ["origine"] = TrialSources.Database, + ["mercato"] = Venues.Okx, + ["strumenti"] = "btc-eur", + ["dal"] = "2024-01-01", + ["al"] = "2025-02-03", + ["capitale"] = "10.000", + ["griglia_fast"] = "5, 10", + ["griglia_slow"] = "20, 40", + }; + foreach (var (name, value) in overrides) + { + fields[name] = value; + } + + return [.. fields.Select(f => (f.Key, f.Value))]; + } + + private static async Task PlanAsync(ServerHarness server, (string, string)[] fields) + { + using var response = await server.PostFormAsync("/laboratorio", "/azioni/laboratorio", fields); + Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode); + var location = ServerHarness.Location(response); + StringAssert.StartsWith(location, "/laboratorio/"); + return Guid.Parse(location["/laboratorio/".Length..]); + } + + private static async Task WaitAsync(Anubis.Db.Database db, Guid id) + { + var store = new TrialStore(db, SystemClock.Instance); + for (var i = 0; i < 600; i++) + { + var trial = await store.GetAsync(id, CancellationToken.None); + if (trial!.IsFinished) + { + Assert.AreEqual(TrialStatuses.Done, trial.Status, trial.Error); + return; + } + + await Task.Delay(50); + } + + Assert.Fail("The trial did not finish in 30 seconds."); + } +} diff --git a/tests/Anubis.Tests/LaboratoryTests.cs b/tests/Anubis.Tests/LaboratoryTests.cs new file mode 100644 index 0000000..a39937f --- /dev/null +++ b/tests/Anubis.Tests/LaboratoryTests.cs @@ -0,0 +1,224 @@ +using Anubis.Backtest; +using Anubis.Core; +using Anubis.Data; + +namespace Anubis.Tests; + +/// The trial register and the laboratory (issue #35): plan first, one trial at a time, report back. +[TestClass] +public sealed class LaboratoryTests +{ + private static readonly DateOnly From = new(2024, 1, 1); + + private static readonly Dictionary> SmallGrid = new() + { + ["fast"] = [5m, 10m], + ["slow"] = [20m, 40m], + }; + + internal static async Task SeedBitcoinAsync(TestDb test, IClock clock, int days = 400) + { + var btc = await new InstrumentCatalog(test.Db, clock).EnsureAsync(MarketDataStoreTests.Okx("BTC-EUR"), CancellationToken.None).ConfigureAwait(false); + await new BarStore(test.Db, clock).UpsertDailyAsync(btc.Id, BacktestData.Walk(days, 3, start: 40_000, drift: 0.001, volatility: 0.03, from: From), "test", + CancellationToken.None).ConfigureAwait(false); + return btc; + } + + internal static TrialRequest Request(params string[] symbols) => + new("sma_cross", TrialSources.Database, Venues.Okx, symbols.Length > 0 ? symbols : ["BTC-EUR"], From, From.AddDays(399), 10_000m, SmallGrid); + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task APlanIsWrittenBeforeTheResultsAndNeverChanges() + { + var clock = new ManualClock(new DateTimeOffset(2026, 11, 11, 9, 0, 0, TimeSpan.Zero)); + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + await SeedBitcoinAsync(test, clock).ConfigureAwait(false); + using var lab = new Laboratory(test.Db, clock, Path.GetTempPath()); + + var trial = await lab.PlanAsync(Request(), CancellationToken.None).ConfigureAwait(false); + Assert.AreEqual(TrialStatuses.Planned, trial.Status); + Assert.AreEqual(4, trial.Variants); + var before = await RawPlanAsync(test, trial.Id).ConfigureAwait(false); + Assert.AreEqual(TrialPlan.Hash(before.Plan), before.Hash); + Assert.AreEqual(TrialThresholds.Default, trial.Plan.Thresholds, "Thresholds are fixed in the plan before the run."); + + clock.Advance(TimeSpan.FromMinutes(1)); + await lab.ProcessAsync(trial.Id, CancellationToken.None).ConfigureAwait(false); + + var done = (await lab.Trials.GetAsync(trial.Id, CancellationToken.None).ConfigureAwait(false))!; + Assert.AreEqual(TrialStatuses.Done, done.Status, done.Error); + Assert.IsNotNull(done.Verdict); + Assert.IsNotNull(done.Summary); + Assert.HasCount(4, done.Summary.VariantSharpes); + Assert.AreEqual(before, await RawPlanAsync(test, trial.Id).ConfigureAwait(false), "The plan text and its hash are never updated."); + + var report = BacktestReport.FromJson((await lab.Trials.ReportJsonAsync(trial.Id, CancellationToken.None).ConfigureAwait(false))!); + Assert.AreEqual(before.Hash, report.PlanHash); + Assert.AreEqual(done.DataFingerprint, report.DataFingerprint); + Assert.HasCount(4, report.Variants); + Assert.AreEqual(1, report.Variants.Count(v => v.Chosen)); + CollectionAssert.AreEqual(new[] { 1m, 2m, 3m }, report.Costs.Select(c => c.Multiplier).ToArray()); + Assert.IsTrue(report.Costs[0].Fees < report.Costs[1].Fees && report.Costs[1].Fees < report.Costs[2].Fees, "Costs grow with the multiplier."); + Assert.IsTrue(report.LookAhead.Passed); + Assert.HasCount(5, report.Checks); + Assert.AreEqual(report.Passed, done.Verdict == TrialVerdicts.Passed); + Assert.AreEqual(4, report.TrialsCounted); + Assert.IsNotNull(report.WalkForward); + Assert.IsNotNull(report.Combinatorial); + Assert.IsNotNull(report.Overfitting); + Assert.IsTrue(report.Curve.Count is > 100 and <= TrialRunner.MaxCurvePoints + 1); + Assert.IsTrue(report.Limitations.Any(l => l.Contains("Fase 5", StringComparison.Ordinal))); + + // A second trial of the same strategy deflates for the variants of both. + var second = await lab.PlanAsync(Request() with { Grid = new Dictionary> { ["fast"] = [10m], ["slow"] = [30m] } }, + CancellationToken.None).ConfigureAwait(false); + await lab.ProcessAsync(second.Id, CancellationToken.None).ConfigureAwait(false); + var again = BacktestReport.FromJson((await lab.Trials.ReportJsonAsync(second.Id, CancellationToken.None).ConfigureAwait(false))!); + Assert.AreEqual(5, again.TrialsCounted); + Assert.IsNull(again.Overfitting, "One variant: no PBO."); + Assert.IsNull(again.Checks.Single(c => c.Key == ReportCheckKeys.Overfitting).Passed); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task TrialsLeftHalfwayStartAgainAndFailuresKeepTheirReason() + { + var clock = new ManualClock(new DateTimeOffset(2026, 11, 11, 9, 0, 0, TimeSpan.Zero)); + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + await SeedBitcoinAsync(test, clock, 60).ConfigureAwait(false); + using var lab = new Laboratory(test.Db, clock, Path.GetTempPath()); + + var halfway = await lab.PlanAsync(Request(), CancellationToken.None).ConfigureAwait(false); + Assert.IsTrue(await lab.Trials.StartAsync(halfway.Id, CancellationToken.None).ConfigureAwait(false)); + Assert.IsFalse(await lab.Trials.StartAsync(halfway.Id, CancellationToken.None).ConfigureAwait(false), "A running trial is not taken twice."); + var unknown = await lab.PlanAsync(Request("XYZ-EUR"), CancellationToken.None).ConfigureAwait(false); + + // The process stopped: on the next start both are planned again, oldest first. + var resumed = await lab.Trials.ResumeAsync(CancellationToken.None).ConfigureAwait(false); + CollectionAssert.AreEqual(new[] { halfway.Id, unknown.Id }, resumed.ToArray()); + Assert.AreEqual(TrialStatuses.Planned, (await lab.Trials.GetAsync(halfway.Id, CancellationToken.None).ConfigureAwait(false))!.Status); + + await lab.ProcessAsync(unknown.Id, CancellationToken.None).ConfigureAwait(false); + var failed = (await lab.Trials.GetAsync(unknown.Id, CancellationToken.None).ConfigureAwait(false))!; + Assert.AreEqual(TrialStatuses.Failed, failed.Status); + StringAssert.Contains(failed.Error, "Strumento sconosciuto"); + Assert.IsNull(await lab.Trials.ReportJsonAsync(unknown.Id, CancellationToken.None).ConfigureAwait(false)); + + // The queue runs in the background and drains the resumed trial too. + using var stop = new CancellationTokenSource(); + var running = lab.RunAsync(stop.Token); + for (var i = 0; i < 600 && !(await lab.Trials.GetAsync(halfway.Id, CancellationToken.None).ConfigureAwait(false))!.IsFinished; i++) + { + await Task.Delay(50).ConfigureAwait(false); + } + + await stop.CancelAsync().ConfigureAwait(false); + await Assert.ThrowsAsync(() => running).ConfigureAwait(false); + Assert.AreEqual(TrialStatuses.Done, (await lab.Trials.GetAsync(halfway.Id, CancellationToken.None).ConfigureAwait(false))!.Status); + + // A plan changed after it was written no longer gives its hash: the trial does not run. + var tampered = await lab.PlanAsync(Request(), CancellationToken.None).ConfigureAwait(false); + await test.Db.WriteAsync(s => s.ExecuteAsync("UPDATE backtest_trials SET plan_hash = @h WHERE id = @id", CancellationToken.None, ("h", "0"), ("id", tampered.Id)), + CancellationToken.None).ConfigureAwait(false); + await lab.ProcessAsync(tampered.Id, CancellationToken.None).ConfigureAwait(false); + var refused = (await lab.Trials.GetAsync(tampered.Id, CancellationToken.None).ConfigureAwait(false))!; + Assert.IsFalse(refused.PlanIntact); + Assert.AreEqual(TrialStatuses.Failed, refused.Status); + StringAssert.Contains(refused.Error, "impronta"); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task TheQueueOutlivesTheDatabaseGoingAway() + { + var clock = new ManualClock(new DateTimeOffset(2026, 11, 11, 9, 0, 0, TimeSpan.Zero)); + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + await SeedBitcoinAsync(test, clock, 60).ConfigureAwait(false); + using var lab = new Laboratory(test.Db, clock, Path.GetTempPath()); + var trial = await lab.PlanAsync(Request(), CancellationToken.None).ConfigureAwait(false); + + // The table is out of reach: every read and write of the queue fails, as when the database restarts. + await RenameAsync(test, "backtest_trials", "backtest_trials_away").ConfigureAwait(false); + using var stop = new CancellationTokenSource(); + var running = lab.RunAsync(stop.Token); + await Task.Delay(300).ConfigureAwait(false); + Assert.IsFalse(running.IsCompleted, "An error of the database does not stop the laboratory."); + + await RenameAsync(test, "backtest_trials_away", "backtest_trials").ConfigureAwait(false); + for (var i = 0; i < 600 && !(await lab.Trials.GetAsync(trial.Id, CancellationToken.None).ConfigureAwait(false))!.IsFinished; i++) + { + clock.Advance(Laboratory.RetryDelay); + await Task.Delay(50).ConfigureAwait(false); + } + + await stop.CancelAsync().ConfigureAwait(false); + await Assert.ThrowsAsync(() => running).ConfigureAwait(false); + var done = (await lab.Trials.GetAsync(trial.Id, CancellationToken.None).ConfigureAwait(false))!; + Assert.AreEqual(TrialStatuses.Done, done.Status, done.Error); + } + + private static Task RenameAsync(TestDb test, string from, string to) => + test.Db.WriteAsync(s => s.ExecuteAsync($"ALTER TABLE {from} RENAME TO {to}", CancellationToken.None), CancellationToken.None); + + [TestMethod] + [TestCategory(Category.Unit)] + public void RequestsOutsideTheRulesAreRefusedBeforeAnythingIsWritten() + { + var now = DateTimeOffset.UnixEpoch; + Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with { Strategy = "nope" }, now)); + Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with { To = From }, now)); + Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with { Symbols = [" "] }, now)); + Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with { InitialCapital = 0m }, now)); + Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with { InitialCapital = 0.5m }, now)); + var half = Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with + { + Grid = new Dictionary> { ["fast"] = [10m, 10.5m] }, + }, now)); + StringAssert.Contains(half.Message, "numeri interi", "10,5 days would run as 10 and count twice in the DSR."); + var outOfRange = Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with + { + Grid = new Dictionary> { ["fast"] = [1m] }, + }, now)); + StringAssert.Contains(outOfRange.Message, "vanno da 2 a 200"); + var none = Assert.ThrowsExactly(() => Laboratory.BuildPlan(Request() with + { + Grid = new Dictionary> { ["fast"] = [60m], ["slow"] = [50m] }, + }, now)); + StringAssert.Contains(none.Message, "Nessuna combinazione"); + + // Without a grid, the family's own; the plan reads back exactly as written. + var (plan, variants) = Laboratory.BuildPlan(Request() with { Grid = null }, now); + Assert.AreEqual(9, variants); + var json = plan.ToJson(); + Assert.AreEqual(json, TrialPlan.FromJson(json).ToJson()); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public async Task TheResearchHistoryLoadsWithEstimatedOrderRules() + { + var root = Directory.CreateTempSubdirectory("anubis-lab-").FullName; + try + { + var store = new ResearchStore(root); + var bars = BacktestData.Walk(50, 9, from: From).Select(b => new ResearchBar(new DateTimeOffset(b.Day.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero), + (decimal)b.Open, (decimal)b.High, (decimal)b.Low, (decimal)b.Close, (decimal)b.Volume)); + await store.WriteAsync(Venues.Binance, "ETHUSDT", Timeframes.Day, bars, CancellationToken.None).ConfigureAwait(false); + + var data = await Laboratory.LoadAsync(null!, SystemClock.Instance, root, + new TrialData(TrialSources.Research, Venues.Binance, ["ETHUSDT"], From.AddDays(10), From.AddDays(29)), CancellationToken.None).ConfigureAwait(false); + Assert.AreEqual("USDT", data.Currency); + Assert.HasCount(20, data.Sessions); + Assert.AreEqual("ETH", data["ETHUSDT"]!.Spec.BaseCurrency); + Assert.IsTrue(data.IsCrypto); + } + finally + { + Directory.Delete(root, recursive: true); + } + } + + private static Task<(string Plan, string Hash)> RawPlanAsync(TestDb test, Guid id) => + test.Db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT plan, plan_hash FROM backtest_trials WHERE id = @id", r => (r.Json("plan").Text, r.Text("plan_hash")), ("id", id))); +} diff --git a/tests/Anubis.Tests/LedgerTests.cs b/tests/Anubis.Tests/LedgerTests.cs index 34ed16d..57e8ab3 100644 --- a/tests/Anubis.Tests/LedgerTests.cs +++ b/tests/Anubis.Tests/LedgerTests.cs @@ -17,7 +17,7 @@ public sealed class LedgerTests await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); var account = await rig.AccountAsync(Venues.Alpaca, TradingEnvironments.Simulation).ConfigureAwait(false); var msft = await rig.InstrumentAsync(TradingRig.Stock("MSFT")).ConfigureAwait(false); - const string strategy = Strategies.EquityMomentum; + const string strategy = StrategyNames.EquityMomentum; await rig.Ledger.RecordCapitalAsync(account.AccountId, strategy, 10000m, "USD", "test", _none).ConfigureAwait(false); var first = await rig.Ledger.RecordFillAsync(rig.Fill(account, msft, strategy, OrderSide.Buy, 10m, 400m, 0.03m) with { ReferencePrice = 399.8m }, _none).ConfigureAwait(false); @@ -65,7 +65,7 @@ public sealed class LedgerTests await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); var account = await rig.AccountAsync(Venues.Okx, TradingEnvironments.Simulation).ConfigureAwait(false); var btc = await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); - const string strategy = Strategies.CryptoTrend; + const string strategy = StrategyNames.CryptoTrend; await rig.Ledger.RecordCapitalAsync(account.AccountId, strategy, 5000m, "EUR", "test", _none).ConfigureAwait(false); // OKX keeps the fee of a purchase in the bought coin (SPEC 4.2): 0.01 bought, 0.00001 kept. @@ -99,14 +99,14 @@ public sealed class LedgerTests var alpaca = await rig.AccountAsync(Venues.Alpaca, TradingEnvironments.Simulation).ConfigureAwait(false); var pepe = await rig.InstrumentAsync(TradingRig.Pepe).ConfigureAwait(false); var aapl = await rig.InstrumentAsync(TradingRig.Stock("AAPL")).ConfigureAwait(false); - await rig.Ledger.RecordCapitalAsync(okx.AccountId, Strategies.CryptoTrend, 1000m, "EUR", "test", _none).ConfigureAwait(false); - await rig.Ledger.RecordCapitalAsync(alpaca.AccountId, Strategies.EquityMomentum, 3000m, "USD", "test", _none).ConfigureAwait(false); + await rig.Ledger.RecordCapitalAsync(okx.AccountId, StrategyNames.CryptoTrend, 1000m, "EUR", "test", _none).ConfigureAwait(false); + await rig.Ledger.RecordCapitalAsync(alpaca.AccountId, StrategyNames.EquityMomentum, 3000m, "USD", "test", _none).ConfigureAwait(false); // A residue below the minimum is written off at market value as a cost: the trade closes (SPEC 7.2). - await rig.Ledger.RecordFillAsync(rig.Fill(okx, pepe, Strategies.CryptoTrend, OrderSide.Buy, 10_000_000m, 0.00001m), _none).ConfigureAwait(false); - await rig.Ledger.RecordFillAsync(rig.Fill(okx, pepe, Strategies.CryptoTrend, OrderSide.Sell, 9_950_000m, 0.000011m), _none).ConfigureAwait(false); - await rig.Ledger.WriteOffResidueAsync(okx.AccountId, pepe.Id, Strategies.CryptoTrend, 0.000011m, "below_minimum", _none).ConfigureAwait(false); - Assert.IsNull(await rig.Ledger.PositionAsync(okx.AccountId, pepe.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + await rig.Ledger.RecordFillAsync(rig.Fill(okx, pepe, StrategyNames.CryptoTrend, OrderSide.Buy, 10_000_000m, 0.00001m), _none).ConfigureAwait(false); + await rig.Ledger.RecordFillAsync(rig.Fill(okx, pepe, StrategyNames.CryptoTrend, OrderSide.Sell, 9_950_000m, 0.000011m), _none).ConfigureAwait(false); + await rig.Ledger.WriteOffResidueAsync(okx.AccountId, pepe.Id, StrategyNames.CryptoTrend, 0.000011m, "below_minimum", _none).ConfigureAwait(false); + Assert.IsNull(await rig.Ledger.PositionAsync(okx.AccountId, pepe.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false)); var residue = (await rig.QueryAsync("SELECT quantity, value FROM residues", r => (Q: r.Money("quantity"), V: r.Money("value"))).ConfigureAwait(false)).Single(); Assert.AreEqual((50_000m, 0.55m), (residue.Q, residue.V)); var trade = (await rig.QueryAsync("SELECT status, realized FROM trades WHERE instrument_id = @i", r => (S: r.Text("status"), R: r.Money("realized")), ("i", pepe.Id)) @@ -116,23 +116,23 @@ public sealed class LedgerTests Assert.AreEqual(okxEquity.Result, trade.R, "The written-off value is a loss of the trade, nothing else."); // A dividend goes to whoever held the stock before the ex-date. - await rig.Ledger.RecordFillAsync(rig.Fill(alpaca, aapl, Strategies.EquityMomentum, OrderSide.Buy, 10m, 200m), _none).ConfigureAwait(false); + await rig.Ledger.RecordFillAsync(rig.Fill(alpaca, aapl, StrategyNames.EquityMomentum, OrderSide.Buy, 10m, 200m), _none).ConfigureAwait(false); rig.Clock.Advance(TimeSpan.FromDays(3)); - Assert.AreEqual(10m, await rig.Ledger.QuantityAtAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, rig.Clock.UtcNow, _none).ConfigureAwait(false)); - Assert.AreEqual(0m, await rig.Ledger.QuantityAtAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, TradingRig.Start.AddMinutes(-1), _none).ConfigureAwait(false)); - await rig.Ledger.RecordDividendAsync(alpaca.AccountId, Strategies.EquityMomentum, 2.5m, "USD", rig.Clock.UtcNow, "Dividendo AAPL", _none).ConfigureAwait(false); + Assert.AreEqual(10m, await rig.Ledger.QuantityAtAsync(alpaca.AccountId, aapl.Id, StrategyNames.EquityMomentum, rig.Clock.UtcNow, _none).ConfigureAwait(false)); + Assert.AreEqual(0m, await rig.Ledger.QuantityAtAsync(alpaca.AccountId, aapl.Id, StrategyNames.EquityMomentum, TradingRig.Start.AddMinutes(-1), _none).ConfigureAwait(false)); + await rig.Ledger.RecordDividendAsync(alpaca.AccountId, StrategyNames.EquityMomentum, 2.5m, "USD", rig.Clock.UtcNow, "Dividendo AAPL", _none).ConfigureAwait(false); // A 4-for-1 split: four times the shares at a quarter of the price, the value unchanged; applied once. - await rig.Ledger.SetStopAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, 180m, _none).ConfigureAwait(false); + await rig.Ledger.SetStopAsync(alpaca.AccountId, aapl.Id, StrategyNames.EquityMomentum, 180m, _none).ConfigureAwait(false); var before = (await rig.Ledger.EquitiesAsync(_none).ConfigureAwait(false)).Single(e => e.AccountId == alpaca.AccountId); // A split whose ex-date came before the purchase changes nothing: those shares were new shares already. Assert.AreEqual(0m, await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-old", 2m, TradingRig.Start.AddDays(-30), _none).ConfigureAwait(false)); - Assert.AreEqual(10m, (await rig.Ledger.PositionAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.Quantity); + Assert.AreEqual(10m, (await rig.Ledger.PositionAsync(alpaca.AccountId, aapl.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!.Quantity); Assert.AreEqual(10m, await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-1", 4m, rig.Clock.UtcNow, _none).ConfigureAwait(false)); Assert.IsNull(await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-1", 4m, rig.Clock.UtcNow, _none).ConfigureAwait(false), "Never twice."); - var split = (await rig.Ledger.PositionAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!; + var split = (await rig.Ledger.PositionAsync(alpaca.AccountId, aapl.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!; Assert.AreEqual((40m, 50m, 45m), (split.Quantity, split.AveragePrice, split.StopPrice)); - Assert.AreEqual(40m, (await rig.Ledger.OpenLotsAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false)).Sum(l => l.Remaining)); + Assert.AreEqual(40m, (await rig.Ledger.OpenLotsAsync(alpaca.AccountId, aapl.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false)).Sum(l => l.Remaining)); var after = (await rig.Ledger.EquitiesAsync(_none).ConfigureAwait(false)).Single(e => e.AccountId == alpaca.AccountId); Assert.AreEqual(before.Equity, after.Equity); Assert.AreEqual(3002.5m, after.Equity, "Capital, plus the dividend; the position at its own price."); @@ -152,7 +152,7 @@ public sealed class LedgerTests var account = await rig.AccountAsync(Venues.Okx, TradingEnvironments.Simulation).ConfigureAwait(false); var btc = await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); var eth = await rig.InstrumentAsync(MarketDataStoreTests.Okx("ETH-EUR")).ConfigureAwait(false); - const string strategy = Strategies.CryptoTrend; + const string strategy = StrategyNames.CryptoTrend; await rig.Ledger.RecordCapitalAsync(account.AccountId, strategy, 100_000m, "EUR", "test", _none).ConfigureAwait(false); var gen = new Gen(Property.DefaultSeed); @@ -213,7 +213,7 @@ public sealed class LedgerTests var alpaca = await rig.AccountAsync(Venues.Alpaca, TradingEnvironments.Demo).ConfigureAwait(false); await rig.Db.WriteAsync(async s => { - foreach (var (strategy, account, share) in new[] { (Strategies.CryptoTrend, okx, 1m), (Strategies.EquityMomentum, alpaca, 0.75m), (Strategies.EquityMeanrev, alpaca, 0.25m) }) + foreach (var (strategy, account, share) in new[] { (StrategyNames.CryptoTrend, okx, 1m), (StrategyNames.EquityMomentum, alpaca, 0.75m), (StrategyNames.EquityMeanrev, alpaca, 0.25m) }) { await s.ExecuteAsync("INSERT INTO strategy_state (id, strategy, account_id, stage, capital, state, created_at, updated_at, share) VALUES (@id, @s, @a, 'observation', 0, @j, @now, @now, @sh)", _none, ("id", Ids.NewId(rig.Clock)), ("s", strategy), ("a", account.AccountId), ("j", new Anubis.Db.DbJson("{}")), ("now", rig.Clock.UtcNow), ("sh", share)) @@ -227,15 +227,15 @@ public sealed class LedgerTests var alpacaCapital = await capital.SetAsync(Venues.Alpaca, 6000m, "test", _none).ConfigureAwait(false); Assert.AreEqual(6600m, alpacaCapital.Amount); var strategies = await capital.StrategiesAsync(Venues.Alpaca, _none).ConfigureAwait(false); - Assert.AreEqual(4950m, strategies.Single(s => s.Strategy == Strategies.EquityMomentum).Capital); - Assert.AreEqual(1650m, strategies.Single(s => s.Strategy == Strategies.EquityMeanrev).Capital); + Assert.AreEqual(4950m, strategies.Single(s => s.Strategy == StrategyNames.EquityMomentum).Capital); + Assert.AreEqual(1650m, strategies.Single(s => s.Strategy == StrategyNames.EquityMeanrev).Capital); var book = new EquityBook(rig.Db, rig.Clock, rig.Ledger, fx); await book.TakeAsync(_none).ConfigureAwait(false); // A gain of 400 € on OKX, then the capital doubles: the return stays 10%, the deposit is no gain. var btc = await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); - await rig.Ledger.RecordFillAsync(rig.Fill(okx, btc, Strategies.CryptoTrend, OrderSide.Buy, 0.05m, 60000m), _none).ConfigureAwait(false); + await rig.Ledger.RecordFillAsync(rig.Fill(okx, btc, StrategyNames.CryptoTrend, OrderSide.Buy, 0.05m, 60000m), _none).ConfigureAwait(false); await rig.Ledger.MarkAsync(btc.Id, 68000m, _none).ConfigureAwait(false); rig.Clock.Advance(TimeSpan.FromMinutes(5)); await book.TakeAsync(_none).ConfigureAwait(false); @@ -244,7 +244,7 @@ public sealed class LedgerTests await book.TakeAsync(_none).ConfigureAwait(false); var series = await rig.QueryAsync("SELECT equity, flows, twr_index, drawdown_pct FROM equity_snapshots WHERE scope = 'strategy' AND strategy = @s ORDER BY taken_at", - r => (E: r.Money("equity"), F: r.Money("flows"), I: r.Money("twr_index"), D: r.Money("drawdown_pct")), ("s", Strategies.CryptoTrend)).ConfigureAwait(false); + r => (E: r.Money("equity"), F: r.Money("flows"), I: r.Money("twr_index"), D: r.Money("drawdown_pct")), ("s", StrategyNames.CryptoTrend)).ConfigureAwait(false); CollectionAssert.AreEqual(new[] { 4000m, 4400m, 8400m }, series.Select(p => p.E).ToArray()); Assert.AreEqual(1.1m, series[^1].I); Assert.AreEqual(0.1m, Returns.TimeWeighted(series.Select(p => (p.E, p.F)).ToList())); @@ -260,6 +260,6 @@ public sealed class LedgerTests var error = await Assert.ThrowsExactlyAsync(() => capital.SetAsync(Venues.Okx, 0m, "test", _none)).ConfigureAwait(false); Assert.Contains("crypto_trend", error.Message); await Assert.ThrowsExactlyAsync(() => - capital.SetSharesAsync(Venues.Alpaca, new Dictionary { [Strategies.EquityMomentum] = 0.8m, [Strategies.EquityMeanrev] = 0.3m }, "test", _none)).ConfigureAwait(false); + capital.SetSharesAsync(Venues.Alpaca, new Dictionary { [StrategyNames.EquityMomentum] = 0.8m, [StrategyNames.EquityMeanrev] = 0.3m }, "test", _none)).ConfigureAwait(false); } } diff --git a/tests/Anubis.Tests/OrderManagerTests.cs b/tests/Anubis.Tests/OrderManagerTests.cs index 4844f2a..3aede63 100644 --- a/tests/Anubis.Tests/OrderManagerTests.cs +++ b/tests/Anubis.Tests/OrderManagerTests.cs @@ -38,10 +38,10 @@ public sealed class OrderManagerTests return new Setup(rig, account, instrument, paper, rig.Manager(paper, account, pendingSplit: pendingSplit)); } - private static TradeIntent Buy(Setup s, decimal quantity, decimal? stop = null, bool aggressive = false, string strategy = Strategies.CryptoTrend) => + private static TradeIntent Buy(Setup s, decimal quantity, decimal? stop = null, bool aggressive = false, string strategy = StrategyNames.CryptoTrend) => new(s.Instrument.Id, strategy, OrderSide.Buy, quantity, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)) { StopLevel = stop, StartAggressive = aggressive }; - private static TradeIntent Sell(Setup s, decimal quantity, string strategy = Strategies.CryptoTrend) => + private static TradeIntent Sell(Setup s, decimal quantity, string strategy = StrategyNames.CryptoTrend) => new(s.Instrument.Id, strategy, OrderSide.Sell, quantity, OrderPurpose.Exit, Ids.NewId(s.Rig.Clock)); private static Task> OrdersAsync(TradingRig rig) => @@ -75,7 +75,7 @@ public sealed class OrderManagerTests CollectionAssert.AreEqual(new[] { ">created", "created>submitted", "submitted>canceled" }, events.ToArray(), "Every transition is recorded."); // Held: 0.01 less the taker fee kept in coin. The slippage against the mid of the first order is measured. - var position = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!; + var position = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!; Assert.AreEqual((0.00998m, 57000m), (position.Quantity, position.StopPrice)); var slippage = (await s.Rig.QueryAsync("SELECT slippage_bp FROM fills", r => r.Money("slippage_bp")).ConfigureAwait(false)).Single(); Assert.IsTrue(slippage is > 0m and < 10m, slippage.ToString(System.Globalization.CultureInfo.InvariantCulture)); @@ -98,7 +98,7 @@ public sealed class OrderManagerTests var orders = await OrdersAsync(s.Rig).ConfigureAwait(false); Assert.HasCount(4, orders); Assert.IsTrue(orders.All(o => o.Status == "canceled")); - Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false)); Assert.IsEmpty(await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false), "Nothing left working."); } @@ -109,7 +109,7 @@ public sealed class OrderManagerTests await using var s = await OkxAsync().ConfigureAwait(false); await s.Manager.SubmitAsync(Buy(s, 0.01m, stop: 40000m, aggressive: true), _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); - var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; + var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; // No price at all: the sale waits, and the stops stay on the broker until it can really leave. s.Rig.QuotesDown = true; @@ -151,12 +151,12 @@ public sealed class OrderManagerTests } TradeIntent BuyOf(Instrument coin, bool aggressive = false) => - new(coin.Id, Strategies.CryptoTrend, OrderSide.Buy, 1m, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)) { StartAggressive = aggressive, StopLevel = 90m }; + new(coin.Id, StrategyNames.CryptoTrend, OrderSide.Buy, 1m, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)) { StartAggressive = aggressive, StopLevel = 90m }; // A position to sell later. await s.Manager.SubmitAsync(BuyOf(coins[8], aggressive: true), _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 2, TimeSpan.FromMinutes(2)).ConfigureAwait(false); - var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, coins[8].Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; + var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, coins[8].Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; var buys = new List(); foreach (var coin in coins.Take(8)) @@ -164,7 +164,7 @@ public sealed class OrderManagerTests buys.Add((await s.Manager.SubmitAsync(BuyOf(coin), _none).ConfigureAwait(false)).Execution!.Value); } - var sale = (await s.Manager.SubmitAsync(new TradeIntent(coins[8].Id, Strategies.CryptoTrend, OrderSide.Sell, held, OrderPurpose.Exit, Ids.NewId(s.Rig.Clock)), _none) + var sale = (await s.Manager.SubmitAsync(new TradeIntent(coins[8].Id, StrategyNames.CryptoTrend, OrderSide.Sell, held, OrderPurpose.Exit, Ids.NewId(s.Rig.Clock)), _none) .ConfigureAwait(false)).Execution!.Value; await s.Rig.TurnsAsync(s.Manager, 1, TimeSpan.FromSeconds(30)).ConfigureAwait(false); @@ -193,7 +193,7 @@ public sealed class OrderManagerTests public async Task AlpacaStopsAreGtcForWholeSharesAndDayForTheFractionAndFollowEveryChange() { await using var s = await AlpacaAsync().ConfigureAwait(false); - await s.Manager.SubmitAsync(Buy(s, 10.5m, stop: 380m, aggressive: true, strategy: Strategies.EquityMomentum), _none).ConfigureAwait(false); + await s.Manager.SubmitAsync(Buy(s, 10.5m, stop: 380m, aggressive: true, strategy: StrategyNames.EquityMomentum), _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); var stops = (await OrdersAsync(s.Rig).ConfigureAwait(false)).Where(o => o.Purpose == "stop" && o.Status == "submitted").OrderBy(o => o.Quantity).ToList(); Assert.AreEqual(2, stops.Count); @@ -202,7 +202,7 @@ public sealed class OrderManagerTests Assert.IsTrue(stops.All(o => o.Stop == 380m && o.Kind == "stop")); // Adding to the position: Alpaca refuses a buy with a sell stop open, so the stops leave, the buy goes, they come back. - await s.Manager.SubmitAsync(Buy(s, 5m, aggressive: true, strategy: Strategies.EquityMomentum), _none).ConfigureAwait(false); + await s.Manager.SubmitAsync(Buy(s, 5m, aggressive: true, strategy: StrategyNames.EquityMomentum), _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); var all = await OrdersAsync(s.Rig).ConfigureAwait(false); Assert.IsTrue(stops.All(old => all.Single(o => o.ClientId == old.ClientId).Status == "canceled")); @@ -215,7 +215,7 @@ public sealed class OrderManagerTests s.Rig.Quote("MSFT", 379m, 379.20m); await s.Paper.PumpAsync(_none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); - Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false)); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false)); var protective = (await OrdersAsync(s.Rig).ConfigureAwait(false)).Single(o => o.Purpose == "protective"); Assert.AreEqual(("market", 0.5m, "filled"), (protective.Kind, protective.Quantity, protective.Status)); Assert.AreEqual("stop", (await s.Rig.QueryAsync("SELECT exit_reason FROM trades", r => r.Text("exit_reason")).ConfigureAwait(false)).Single()); @@ -231,9 +231,9 @@ public sealed class OrderManagerTests await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); // The level to place is already above the price: the stop counts as fired (SPEC 7.2). - await s.Rig.Ledger.SetStopAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, 61000m, _none).ConfigureAwait(false); + await s.Rig.Ledger.SetStopAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, 61000m, _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); - Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false)); Assert.AreEqual("market", (await OrdersAsync(s.Rig).ConfigureAwait(false)).Single(o => o.Purpose == "protective").Kind); Assert.AreEqual("stop", (await s.Rig.QueryAsync("SELECT exit_reason FROM trades", r => r.Text("exit_reason")).ConfigureAwait(false)).Single()); Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM notifications WHERE kind = 'stop_already_passed'", r => r.Id("id")).ConfigureAwait(false)); @@ -248,15 +248,15 @@ public sealed class OrderManagerTests applied = true; return Task.FromResult(first ? new CorporateAction(instrument.Symbol, CorporateActionKinds.Split, new DateOnly(2026, 11, 11), "test", "split-msft") { Ratio = 4m } : null); }).ConfigureAwait(false); - await a.Manager.SubmitAsync(Buy(a, 10m, aggressive: true, strategy: Strategies.EquityMomentum), _none).ConfigureAwait(false); + await a.Manager.SubmitAsync(Buy(a, 10m, aggressive: true, strategy: StrategyNames.EquityMomentum), _none).ConfigureAwait(false); await a.Rig.TurnsAsync(a.Manager, 2, _minute).ConfigureAwait(false); a.Rig.Clock.Advance(TimeSpan.FromDays(1)); a.Rig.Quote("MSFT", 100m, 100.05m); - await a.Rig.Ledger.SetStopAsync(a.Account.AccountId, a.Instrument.Id, Strategies.EquityMomentum, 380m, _none).ConfigureAwait(false); + await a.Rig.Ledger.SetStopAsync(a.Account.AccountId, a.Instrument.Id, StrategyNames.EquityMomentum, 380m, _none).ConfigureAwait(false); await a.Rig.TurnsAsync(a.Manager, 2, _minute).ConfigureAwait(false); Assert.IsTrue(applied); - var position = (await a.Rig.Ledger.PositionAsync(a.Account.AccountId, a.Instrument.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!; + var position = (await a.Rig.Ledger.PositionAsync(a.Account.AccountId, a.Instrument.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!; Assert.AreEqual((40m, 95m), (position.Quantity, position.StopPrice)); Assert.IsFalse((await OrdersAsync(a.Rig).ConfigureAwait(false)).Any(o => o.Purpose == "protective")); var stop = (await OrdersAsync(a.Rig).ConfigureAwait(false)).Single(o => o.Purpose == "stop" && o.Status == "submitted"); @@ -282,7 +282,7 @@ public sealed class OrderManagerTests Assert.HasCount(1, faulty.Sent.Where(r => r.ClientOrderId == lost.ClientId).ToList(), "Sent once."); Assert.AreEqual("done/filled", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM fills", r => r.Id("id")).ConfigureAwait(false), "Every fill was reported twice, recorded once."); - Assert.AreEqual(0.00998m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity); + Assert.AreEqual(0.00998m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!.Quantity); // A refused buy ends, with its reason in the diary. faulty.RejectNext = "insufficient"; @@ -305,14 +305,14 @@ public sealed class OrderManagerTests await using var s = await OkxAsync(TradingRig.Pepe, bid: 0.00001m, ask: 0.00001001m).ConfigureAwait(false); await s.Manager.SubmitAsync(Buy(s, 10_000_000m, aggressive: true), _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); - var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; + var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; Assert.AreEqual(9_980_000m, held); s.Rig.Quote("PEPE-EUR", 0.0000099m, 0.00000991m); var (execution, _) = await s.Manager.SubmitAsync(Sell(s, 9_900_000m) with { StartAggressive = true }, _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); Assert.AreEqual("done/filled", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); - Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false)); var residue = (await s.Rig.QueryAsync("SELECT quantity, reason FROM residues", r => (Q: r.Money("quantity"), R: r.Text("reason"))).ConfigureAwait(false)).Single(); Assert.AreEqual((80_000m, "below_minimum"), (residue.Q, residue.R)); Assert.AreEqual(("closed", "residue"), (await s.Rig.QueryAsync("SELECT status, exit_reason FROM trades", r => (r.Text("status"), r.Text("exit_reason"))).ConfigureAwait(false)).Single()); diff --git a/tests/Anubis.Tests/Property.cs b/tests/Anubis.Tests/Property.cs index 9e32c7f..51bbca6 100644 --- a/tests/Anubis.Tests/Property.cs +++ b/tests/Anubis.Tests/Property.cs @@ -69,6 +69,14 @@ internal sealed class Gen(int seed) /// Uniform value in [0, 1). public double Double() => (NextUInt64() >> 11) * (1.0 / (1UL << 53)); + /// A standard normal value (Box–Muller), for synthetic price series and returns. + public double Normal() + { + var u = 1.0 - Double(); + var v = Double(); + return Math.Sqrt(-2.0 * Math.Log(u)) * Math.Cos(2.0 * Math.PI * v); + } + /// A decimal in [, ] with at most decimals. public decimal Decimal(decimal min, decimal max, int scale) { diff --git a/tests/Anubis.Tests/ReconcilerTests.cs b/tests/Anubis.Tests/ReconcilerTests.cs index baeac8b..181934f 100644 --- a/tests/Anubis.Tests/ReconcilerTests.cs +++ b/tests/Anubis.Tests/ReconcilerTests.cs @@ -25,11 +25,11 @@ public sealed class ReconcilerTests var instrument = await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); var paper = rig.Paper(PaperOptions.Okx(), account.AccountId); await paper.DepositAsync(100_000m, "EUR", _none).ConfigureAwait(false); - await rig.Ledger.RecordCapitalAsync(account.AccountId, Strategies.CryptoTrend, 10_000m, "EUR", "test", _none).ConfigureAwait(false); + await rig.Ledger.RecordCapitalAsync(account.AccountId, StrategyNames.CryptoTrend, 10_000m, "EUR", "test", _none).ConfigureAwait(false); rig.Quote(instrument.Symbol, 60000m, 60010m); var broker = new FaultyBroker(paper, environment); var manager = rig.Manager(broker, account); - await manager.SubmitAsync(new TradeIntent(instrument.Id, Strategies.CryptoTrend, OrderSide.Buy, 0.01m, OrderPurpose.Entry, Ids.NewId(rig.Clock)) + await manager.SubmitAsync(new TradeIntent(instrument.Id, StrategyNames.CryptoTrend, OrderSide.Buy, 0.01m, OrderPurpose.Entry, Ids.NewId(rig.Clock)) { StopLevel = 57000m, StartAggressive = true, @@ -52,7 +52,7 @@ public sealed class ReconcilerTests private static Task> CodesAsync(TradingRig rig, string code) => rig.QueryAsync("SELECT code FROM journal WHERE code = @c", r => r.Text("code"), ("c", code)); - private static TradeIntent Entry(Setup s) => new(s.Instrument.Id, Strategies.CryptoTrend, OrderSide.Buy, 0.001m, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)); + private static TradeIntent Entry(Setup s) => new(s.Instrument.Id, StrategyNames.CryptoTrend, OrderSide.Buy, 0.001m, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)); [TestMethod] [TestCategory(Category.Simulation)] @@ -77,11 +77,11 @@ public sealed class ReconcilerTests s.Rig.Quote("BTC-EUR", 56000m, 56010m); await s.Paper.PumpAsync(_none).ConfigureAwait(false); Assert.IsEmpty(await s.Paper.GetPositionsAsync(_none).ConfigureAwait(false)); - Assert.IsNotNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.IsNotNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false)); var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); Assert.AreEqual((ReconcileOutcomes.Healed, AccountModes.Normal), (report.Outcome, report.Mode), string.Join("; ", report.Differences.Select(d => d.Message))); - Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false)); Assert.AreEqual("stop", (await s.Rig.QueryAsync("SELECT exit_reason FROM trades", r => r.Text("exit_reason")).ConfigureAwait(false)).Single()); Assert.HasCount(1, await CodesAsync(s.Rig, "reconcile_healed").ConfigureAwait(false)); Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM reconciliations WHERE outcome = 'healed'", r => r.Id("id")).ConfigureAwait(false)); @@ -132,7 +132,7 @@ public sealed class ReconcilerTests // Align: the ledger takes the account's number with an adjustment, and the account is normal again. await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false); - Assert.AreEqual(0.00598m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity); + Assert.AreEqual(0.00598m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, StrategyNames.CryptoTrend, _none).ConfigureAwait(false))!.Quantity); state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false); Assert.AreEqual((AccountModes.Normal, ReconcileOutcomes.UserAligned), (state.Mode, state.LastOutcome)); Assert.HasCount(1, await CodesAsync(s.Rig, "account_resolved").ConfigureAwait(false)); @@ -148,7 +148,7 @@ public sealed class ReconcilerTests public async Task AnUnknownOrderOfThisInstallationIsReportedAndCanBeLeftOut() { await using var s = await HoldingAsync().ConfigureAwait(false); - var stray = ClientOrderIds.Build(TradingRig.Installation, TradingEnvironments.Demo, Strategies.CryptoTrend, OrderPurpose.Entry, ClientOrderIds.Key(Guid.NewGuid()), 1); + var stray = ClientOrderIds.Build(TradingRig.Installation, TradingEnvironments.Demo, StrategyNames.CryptoTrend, OrderPurpose.Entry, ClientOrderIds.Key(Guid.NewGuid()), 1); await s.Paper.SubmitOrderAsync(new OrderRequest(stray, "BTC-EUR", OrderSide.Buy, OrderKind.Limit, 0.001m, TimeInForce.Gtc) { LimitPrice = 50000m }, _none).ConfigureAwait(false); // Someone else's order on the same account is none of the bot's business. diff --git a/tests/Anubis.Tests/TradingRulesTests.cs b/tests/Anubis.Tests/TradingRulesTests.cs index 1af0f4d..c2ad216 100644 --- a/tests/Anubis.Tests/TradingRulesTests.cs +++ b/tests/Anubis.Tests/TradingRulesTests.cs @@ -22,7 +22,7 @@ public sealed class TradingRulesTests Assert.AreEqual(installation, ClientOrderIds.InstallationCode(Guid.Parse("0193a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5b"))); Assert.AreEqual(key, ClientOrderIds.Key(Guid.Parse("0193a1b2-0000-7000-8000-000000000001"))); - var id = ClientOrderIds.Build(installation, TradingEnvironments.Demo, Strategies.EquityMomentum, OrderPurpose.Stop, key, 1297); + var id = ClientOrderIds.Build(installation, TradingEnvironments.Demo, StrategyNames.EquityMomentum, OrderPurpose.Stop, key, 1297); Assert.AreEqual(ClientOrderIds.Length, id.Length); Assert.IsLessThanOrEqualTo(32, id.Length); Assert.IsTrue(id.StartsWith("ANU", StringComparison.Ordinal)); @@ -31,16 +31,16 @@ public sealed class TradingRulesTests // From the id alone: strategy, environment, purpose and progressive (SPEC 7.2, 15.1). var parts = ClientOrderIds.Parse(id); Assert.IsNotNull(parts); - Assert.AreEqual(new ClientOrderIdParts(installation, TradingEnvironments.Demo, Strategies.EquityMomentum, OrderPurpose.Stop, key, 1297), parts); + Assert.AreEqual(new ClientOrderIdParts(installation, TradingEnvironments.Demo, StrategyNames.EquityMomentum, OrderPurpose.Stop, key, 1297), parts); Assert.IsTrue(ClientOrderIds.IsOurs(id, installation)); Assert.IsFalse(ClientOrderIds.IsOurs(id, "ZZZZ"), "Another installation's order is not ours."); Assert.IsFalse(ClientOrderIds.IsOurs("my-own-order-123", installation), "The user's own orders are never touched."); Assert.IsNull(ClientOrderIds.Parse(null)); // Every progressive gives a different id; the next one is a different order. - Assert.AreNotEqual(id, ClientOrderIds.Build(installation, TradingEnvironments.Demo, Strategies.EquityMomentum, OrderPurpose.Stop, key, 1298)); + Assert.AreNotEqual(id, ClientOrderIds.Build(installation, TradingEnvironments.Demo, StrategyNames.EquityMomentum, OrderPurpose.Stop, key, 1298)); Assert.ThrowsExactly(() => - ClientOrderIds.Build(installation, TradingEnvironments.Demo, Strategies.CryptoTrend, OrderPurpose.Entry, key, ClientOrderIds.MaxSequence + 1)); + ClientOrderIds.Build(installation, TradingEnvironments.Demo, StrategyNames.CryptoTrend, OrderPurpose.Entry, key, ClientOrderIds.MaxSequence + 1)); Assert.ThrowsExactly(() => ClientOrderIds.Build(installation, TradingEnvironments.Demo, "unknown", OrderPurpose.Entry, key, 1)); } diff --git a/tests/Anubis.Tests/TradingServiceTests.cs b/tests/Anubis.Tests/TradingServiceTests.cs index bd943d8..95b2db1 100644 --- a/tests/Anubis.Tests/TradingServiceTests.cs +++ b/tests/Anubis.Tests/TradingServiceTests.cs @@ -36,9 +36,9 @@ public sealed class TradingServiceTests // Every strategy starts in observation, crypto on OKX and stocks on Alpaca. CollectionAssert.AreEqual(new[] { - (Strategies.CryptoTrend, "OKX simulatore", Strategies.Observation), - (Strategies.EquityMeanrev, "Alpaca simulatore", Strategies.Observation), - (Strategies.EquityMomentum, "Alpaca simulatore", Strategies.Observation), + (StrategyNames.CryptoTrend, "OKX simulatore", StrategyNames.Observation), + (StrategyNames.EquityMeanrev, "Alpaca simulatore", StrategyNames.Observation), + (StrategyNames.EquityMomentum, "Alpaca simulatore", StrategyNames.Observation), }, (await StrategiesAsync(rig).ConfigureAwait(false)).ToArray()); // A second start finds what is there. @@ -55,16 +55,16 @@ public sealed class TradingServiceTests await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); var accounts = await AccountSetup.EnsureAsync(rig.Db, rig.Clock, alpacaDemo: false, okxDemo: false, _none).ConfigureAwait(false); var simulator = accounts.Single(a => a.Broker == Venues.Alpaca); - await rig.Ledger.RecordCapitalAsync(simulator.AccountId, Strategies.EquityMeanrev, 500m, "USD", "test", _none).ConfigureAwait(false); + await rig.Ledger.RecordCapitalAsync(simulator.AccountId, StrategyNames.EquityMeanrev, 500m, "USD", "test", _none).ConfigureAwait(false); accounts = await AccountSetup.EnsureAsync(rig.Db, rig.Clock, alpacaDemo: true, okxDemo: false, _none).ConfigureAwait(false); Assert.HasCount(3, accounts); Assert.IsFalse(accounts.Any(a => a.Environment == TradingEnvironments.Live), "Never a real account in this phase (SPEC 2.2)."); CollectionAssert.AreEqual(new[] { - (Strategies.CryptoTrend, "OKX simulatore", Strategies.Observation), - (Strategies.EquityMeanrev, "Alpaca simulatore", Strategies.Observation), - (Strategies.EquityMomentum, "Alpaca paper", Strategies.Observation), + (StrategyNames.CryptoTrend, "OKX simulatore", StrategyNames.Observation), + (StrategyNames.EquityMeanrev, "Alpaca simulatore", StrategyNames.Observation), + (StrategyNames.EquityMomentum, "Alpaca paper", StrategyNames.Observation), }, (await StrategiesAsync(rig).ConfigureAwait(false)).ToArray(), "A strategy with a history stays where it is."); } @@ -91,7 +91,7 @@ public sealed class TradingServiceTests Assert.AreEqual(1000m, await BookAsync(Venues.Okx, "EUR").ConfigureAwait(false)); Assert.AreEqual(2200m, await BookAsync(Venues.Alpaca, "USD").ConfigureAwait(false), "Funded once, not again at every call."); var shares = await rig.QueryAsync("SELECT strategy, capital FROM strategy_state ORDER BY strategy", r => (r.Text("strategy"), r.Money("capital"))).ConfigureAwait(false); - CollectionAssert.AreEqual(new[] { (Strategies.CryptoTrend, 1000m), (Strategies.EquityMeanrev, 550m), (Strategies.EquityMomentum, 1650m) }, shares.ToArray()); + CollectionAssert.AreEqual(new[] { (StrategyNames.CryptoTrend, 1000m), (StrategyNames.EquityMeanrev, 550m), (StrategyNames.EquityMomentum, 1650m) }, shares.ToArray()); // The dollar falls: saving the same euro again changes nothing, and only a new euro amount is converted at today's rate. await new FxStore(rig.Db, rig.Clock).UpsertAsync([new FxRate("EUR", "USD", new DateOnly(2026, 11, 10), 1.05m, "ecb")], _none).ConfigureAwait(false); @@ -110,8 +110,8 @@ public sealed class TradingServiceTests var msft = await rig.InstrumentAsync(TradingRig.Stock("MSFT")).ConfigureAwait(false); await service.StartAsync(_none).ConfigureAwait(false); var account = service.Managers.Single(m => m.Account.Broker == Venues.Alpaca).Account; - await service.Ledger.RecordCapitalAsync(account.AccountId, Strategies.EquityMomentum, 5000m, "USD", "test", _none).ConfigureAwait(false); - await service.Ledger.RecordFillAsync(rig.Fill(account, msft, Strategies.EquityMomentum, OrderSide.Buy, 10m, 400m, feeCurrency: "USD"), _none).ConfigureAwait(false); + await service.Ledger.RecordCapitalAsync(account.AccountId, StrategyNames.EquityMomentum, 5000m, "USD", "test", _none).ConfigureAwait(false); + await service.Ledger.RecordFillAsync(rig.Fill(account, msft, StrategyNames.EquityMomentum, OrderSide.Buy, 10m, 400m, feeCurrency: "USD"), _none).ConfigureAwait(false); // Bought on Tuesday; Wednesday is the ex-date of 0,75 USD a share; Thursday a 2-for-1 split. A split of last // month is still in the table: it came before the purchase and must change nothing. @@ -129,18 +129,18 @@ public sealed class TradingServiceTests await service.CorporateActionsAsync(_none).ConfigureAwait(false); Assert.IsEmpty(await DividendsAsync().ConfigureAwait(false), "Nothing before the ex-date."); - Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.Quantity, "An old split is not applied to new shares."); + Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!.Quantity, "An old split is not applied to new shares."); rig.Clock.Advance(TimeSpan.FromDays(1)); await service.CorporateActionsAsync(_none).ConfigureAwait(false); await service.CorporateActionsAsync(_none).ConfigureAwait(false); CollectionAssert.AreEqual(new[] { 7.5m }, (await DividendsAsync().ConfigureAwait(false)).ToArray()); - Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.Quantity); + Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!.Quantity); rig.Clock.Advance(TimeSpan.FromDays(1)); await service.CorporateActionsAsync(_none).ConfigureAwait(false); await service.CorporateActionsAsync(_none).ConfigureAwait(false); - var position = (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!; + var position = (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!; Assert.AreEqual((20m, 200m), (position.Quantity, position.AveragePrice)); Assert.HasCount(1, await DividendsAsync().ConfigureAwait(false)); Assert.HasCount(1, await rig.QueryAsync("SELECT code FROM journal WHERE code = 'split_applied'", r => r.Text("code")).ConfigureAwait(false)); @@ -149,6 +149,6 @@ public sealed class TradingServiceTests // Every photograph values the positions at the market, not at their last fill. service.Quotes.Put(new Quote("MSFT", 230m, 230.10m, rig.Clock.UtcNow)); await service.PhotographAsync(_none).ConfigureAwait(false); - Assert.AreEqual(230.05m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.LastPrice); + Assert.AreEqual(230.05m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!.LastPrice); } } diff --git a/tests/Anubis.Tests/TradingSupport.cs b/tests/Anubis.Tests/TradingSupport.cs index f214f53..9456b3a 100644 --- a/tests/Anubis.Tests/TradingSupport.cs +++ b/tests/Anubis.Tests/TradingSupport.cs @@ -46,7 +46,7 @@ internal sealed class TradingRig : IAsyncDisposable public async Task AccountAsync(string broker, string environment, string? name = null) { - var account = new AccountContext(Ids.NewId(Clock), name ?? AccountSetup.NameOf(broker, environment), broker, environment, Strategies.CurrencyOf(broker)); + var account = new AccountContext(Ids.NewId(Clock), name ?? AccountSetup.NameOf(broker, environment), broker, environment, StrategyNames.CurrencyOf(broker)); await Db.WriteAsync(s => s.ExecuteAsync("INSERT INTO accounts (id, name, broker, environment, base_currency, created_at) VALUES (@id, @n, @b, @e, @c, @now)", CancellationToken.None, ("id", account.AccountId), ("n", account.Name), ("b", broker), ("e", environment), ("c", account.Currency), ("now", Clock.UtcNow)), CancellationToken.None).ConfigureAwait(false); return account; diff --git a/tests/Anubis.Tests/ValidationTests.cs b/tests/Anubis.Tests/ValidationTests.cs new file mode 100644 index 0000000..2b5116c --- /dev/null +++ b/tests/Anubis.Tests/ValidationTests.cs @@ -0,0 +1,327 @@ +using Anubis.Backtest; +using Anubis.Strategies; + +namespace Anubis.Tests; + +/// Metrics, Deflated Sharpe Ratio, PBO, walk-forward, purged cross-validation, look-ahead (issues #34, #35). +[TestClass] +public sealed class ValidationTests +{ + private static readonly DateOnly D1 = BacktestData.D1; + + [TestMethod] + [TestCategory(Category.Unit)] + public void TheNormalDistributionMatchesItsTables() + { + Assert.AreEqual(0.5, Normal.Cdf(0), 1e-9); + Assert.AreEqual(0.975, Normal.Cdf(1.959963985), 2e-7); + Assert.AreEqual(0.0013499, Normal.Cdf(-3), 1e-7); + Assert.AreEqual(1.959963985, Normal.Quantile(0.975), 1e-6); + Assert.AreEqual(2.326347874, Normal.Quantile(0.99), 1e-6); + Assert.AreEqual(-3.090232306, Normal.Quantile(0.001), 1e-5); + Property.ForAll(g => 0.0005 + (0.999 * g.Double()), p => Assert.AreEqual(p, Normal.Cdf(Normal.Quantile(p)), 1e-7)); + } + + /// + /// The numerical example of Bailey and López de Prado, "The Deflated Sharpe Ratio" (2014): annualised Sharpe 2,5 + /// on 1250 daily returns with skewness −3 and kurtosis 10, best of 100 trials whose annualised Sharpe ratios have + /// variance 0,5. The article gives an expected maximum of 0,1132 per day and a DSR of 0,9004. + /// + [TestMethod] + [TestCategory(Category.Unit)] + public void TheDeflatedSharpeRatioReproducesTheExampleOfTheArticle() + { + var days = Math.Sqrt(250); + var dsr = SharpeTests.Deflated(2.5 / days, 1250, -3, 10, 100, 0.5 / 250); + Assert.AreEqual(0.1132, dsr.Benchmark, 1e-4); + Assert.AreEqual(0.9004, dsr.Value, 5e-4); + + // One trial: nothing to deflate, the DSR is the probabilistic Sharpe ratio against zero. + Assert.AreEqual(0, SharpeTests.ExpectedMaximum(1, 0.5)); + Assert.AreEqual(SharpeTests.Probabilistic(0.05, 0, 500, 0, 3), SharpeTests.Deflated(0.05, 500, 0, 3, 1, 0.01).Value); + // More trials raise the bar. + Assert.IsTrue(SharpeTests.ExpectedMaximum(1000, 0.002) > SharpeTests.ExpectedMaximum(10, 0.002)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void MomentsOfANormalSampleAreThoseOfTheNormal() + { + var gen = new Gen(11); + var sample = Enumerable.Range(0, 200_000).Select(_ => 0.001 + (0.02 * gen.Normal())).ToArray(); + var m = Moments.Of(sample); + Assert.AreEqual(0.001, m.Mean, 2e-4); + Assert.AreEqual(0.02, m.StandardDeviation, 2e-4); + Assert.AreEqual(0, m.Skewness, 0.03); + Assert.AreEqual(3, m.Kurtosis, 0.05); + Assert.AreEqual(0.05, m.Sharpe, 0.01); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void OnPureNoiseThePbOIsAboutOneHalf() + { + var values = new List(); + for (var seed = 1; seed <= 12; seed++) + { + var pbo = Validation.Overfitting(Noise(10, 800, seed)); + Assert.IsNotNull(pbo); + Assert.AreEqual(12870, pbo.Combinations); + values.Add(pbo.Probability); + } + + Assert.AreEqual(0.5, values.Average(), 0.1); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void WithAVariantBuiltToDominateThePbOFallsToZero() + { + var returns = Noise(10, 800, 5); + var gen = new Gen(99); + returns[3] = [.. Enumerable.Range(0, 800).Select(_ => 0.003 + (0.01 * gen.Normal()))]; + var pbo = Validation.Overfitting(returns); + Assert.IsNotNull(pbo); + Assert.IsTrue(pbo.Probability < 0.02, $"PBO {pbo.Probability}"); + Assert.IsTrue(pbo.Logits.All(l => l > 0)); + + Assert.IsNull(Validation.Overfitting([returns[0]]), "One variant: nothing to compare."); + } + + /// Whatever wins in sample loses out of sample: the PBO goes to 1. + [TestMethod] + [TestCategory(Category.Unit)] + public void WhenTheInSampleWinnerAlwaysLosesOutOfSampleThePbOIsOne() + { + // Two mirror variants whose returns add up to zero: the half where one gains is the half where it loses + // the rest, and where the other gains. + var gen = new Gen(3); + var noise = Enumerable.Range(0, 800).Select(_ => 0.01 * gen.Normal()).ToArray(); + var mean = noise.Average(); + var a = noise.Select(x => x - mean).ToArray(); + var b = a.Select(x => -x).ToArray(); + var pbo = Validation.Overfitting([a, b]); + Assert.IsNotNull(pbo); + Assert.AreEqual(1.0, pbo.Probability, 1e-12); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void PurgedCrossValidationBuildsFifteenSplitsAndFivePaths() + { + var variants = Grid(3, 3); + var returns = Noise(9, 600, 8); + var gen = new Gen(17); + returns[0] = [.. Enumerable.Range(0, 600).Select(_ => 0.01 + (0.01 * gen.Normal()))]; + + var cv = Validation.Combinatorial(variants, returns); + Assert.IsNotNull(cv); + Assert.AreEqual(15, cv.Splits); + Assert.AreEqual(5, cv.Paths); + Assert.AreEqual(6, cv.Purge); + Assert.AreEqual(6, cv.Embargo); + // One variant dominates: every split picks it, and its neighbours do not win by standing next to it. + Assert.IsTrue(cv.Chosen.All(c => c == 0), string.Join(",", cv.Chosen)); + Assert.AreEqual(1.0, cv.PositiveShare); + + Assert.AreEqual(15, Validation.Combinations(6, 2).Count); + Assert.AreEqual(12870, Validation.Combinations(16, 8).Count); + Assert.IsNull(Validation.Combinatorial(variants, [.. returns.Select(r => r[..18])]), "Too short for six blocks."); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void WalkForwardChoosesOnThePastAndIsJudgedOnWhatFollows() + { + var variants = Grid(3, 3); + var returns = Noise(9, 500, 21); + var wf = Validation.WalkForward(variants, returns); + Assert.IsNotNull(wf); + Assert.AreEqual(4, wf.Windows.Count); + Assert.AreEqual(100, wf.Windows[0].TestFrom); + Assert.AreEqual(500, wf.Windows[^1].TestTo); + Assert.AreEqual(5, wf.Purge); + foreach (var w in wf.Windows) + { + Assert.AreEqual(w.TestFrom - wf.Purge, w.TrainTo, "Training stops before the test, less the purge."); + var sharpes = returns.Select(r => Moments.Of(r.AsSpan(0, w.TrainTo)).Sharpe).ToArray(); + Assert.AreEqual(Validation.ChoosePlateau(variants, sharpes), w.Chosen); + } + + for (var k = 1; k < wf.Windows.Count; k++) + { + Assert.AreEqual(wf.Windows[k - 1].TestTo, wf.Windows[k].TestFrom, "Test windows follow one another."); + } + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void ThePlateauRulePrefersABroadHillToALonelyPeak() + { + var variants = Grid(4, 4); + var sharpe = new double[16]; + // A lonely peak in a corner, among poor neighbours... + sharpe[0] = 0.20; + // ...and a hill around (2, 2), lower at its top. + foreach (var v in variants.Where(v => v.Position[0] >= 1 && v.Position[1] >= 1)) + { + sharpe[v.Index] = v.Position[0] == 2 && v.Position[1] == 2 ? 0.10 : 0.08; + } + + var chosen = variants[Validation.ChoosePlateau(variants, sharpe)]; + Assert.AreEqual("a=2;b=2", chosen.ToString()); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TheGridHoldsOnlyThePointsTheFamilyAccepts() + { + var family = new MovingAverageCrossFamily(); + var all = ParameterGrid.Of(family); + Assert.AreEqual(9, all.Count); + Assert.AreEqual("fast=10;slow=50", all[0].ToString()); + Assert.AreEqual("fast=30;slow=150", all[^1].ToString()); + CollectionAssert.AreEqual(new[] { 1, 2 }, all[5].Position.ToArray()); + + var custom = ParameterGrid.Of(family, new Dictionary> { ["fast"] = [20m, 60m], ["slow"] = [50m, 100m] }); + Assert.AreEqual(3, custom.Count, "fast=60;slow=50 is not a valid point."); + Assert.AreEqual(1, ParameterGrid.Of(new BuyAndHoldFamily()).Count); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TheMetricsOfTheToyRunAreThoseComputedByHand() + { + var data = BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()); + var script = new ScriptedStrategy(new Dictionary> + { + [D1] = [new Intent("BTC-EUR", 0.5m, "in")], + [D1.AddDays(2)] = [new Intent("BTC-EUR", 0m, "out")], + }); + var run = BacktestEngine.Run(script, data); + var m = BacktestMetrics.Of(run, data.PeriodsPerYear); + + Assert.AreEqual(10_119.44058m, m.FinalEquity); + Assert.AreEqual(0.011944058, m.TotalReturn, 1e-12); + Assert.AreEqual((10_134.7 - 10_119.44058) / 10_134.7, m.MaxDrawdown, 1e-12); + Assert.AreEqual(D1.AddDays(2), m.DrawdownPeak); + Assert.AreEqual(D1.AddDays(3), m.DrawdownLow); + Assert.AreEqual(1, m.Trades); + Assert.AreEqual(1.0, m.WinRate); + Assert.IsNull(m.ProfitFactor, "No losing trade: the ratio has no value."); + Assert.AreEqual(20.27942m, m.Fees); + Assert.AreEqual(1, m.Years.Count); + Assert.AreEqual(2026, m.Years[0].Year); + Assert.AreEqual(119.44058m, m.Years[0].Result); + var returns = run.DailyReturns(); + Assert.AreEqual(Moments.Of(returns).Sharpe * Math.Sqrt(365), m.Sharpe, 1e-12); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TheLookAheadTestCatchesAStrategyThatReadsTomorrow() + { + var data = BacktestData.One(BacktestData.Btc(), BacktestData.Walk(400, 5)); + var peeking = LookAheadCheck.Run(d => new Peeking(d), data); + Assert.IsFalse(peeking.Passed); + Assert.AreEqual(6, peeking.Cutoffs.Count); + + var honest = LookAheadCheck.Run(_ => new MovingAverageCross(10, 50), data); + Assert.IsTrue(honest.Passed, string.Join("\n", honest.Mismatches)); + Assert.IsTrue(honest.Decisions > 6); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TheOneStepComparisonCountsActionAndSizeDifferences() + { + var held = new HashSet(["A", "B", "C"], StringComparer.Ordinal); + List live = + [ + new("A", 0.10m, "x"), + new("B", 0m, "x"), + new("C", null, "x"), + new("D", 0.05m, "x"), + new("F", 0m, "x"), + ]; + List backtest = + [ + new("A", 0.105m, "x"), + new("B", null, "x"), + new("C", 0.20m, "x"), + new("E", 0.05m, "x"), + ]; + var result = OneStepComparison.Compare(live, backtest, held).ToDictionary(r => r.Symbol); + + Assert.IsFalse(result["A"].Differs, "Sizes 5% apart are the same intent."); + Assert.IsTrue(result["B"].Differs, "Exit against keep."); + Assert.IsTrue(result["C"].Differs, "Keep against resize."); + Assert.IsTrue(result["D"].Differs, "An entry only live."); + Assert.IsTrue(result["E"].Differs, "An entry only in the backtest."); + Assert.IsFalse(result.ContainsKey("F"), "Not held and nobody enters: not compared."); + + var resized = OneStepComparison.Compare([new("A", 0.10m, "x")], [new("A", 0.12m, "x")], held); + Assert.IsTrue(resized[0].Differs, "Sizes 17% apart differ."); + } + + /// + /// A fixed run on fixed data, recorded once: any change to the engine, the costs or the strategy that moves the + /// result shows here. If the change is intended, update the numbers and say why in the commit. + /// + [TestMethod] + [TestCategory(Category.Golden)] + public void TheReferenceRunOfTheAverageCrossDoesNotMove() + { + var data = BacktestData.One(BacktestData.Btc(), BacktestData.Walk(1500, 7, start: 20_000, drift: 0.0008, volatility: 0.03)); + var run = BacktestEngine.Run(new MovingAverageCross(20, 100), data); + var m = BacktestMetrics.Of(run, data.PeriodsPerYear); + + // Recorded on 2026-11-11: a losing run, as an average cross on a random walk should be. + Assert.AreEqual(6_141.9503824973202m, run.FinalEquity); + Assert.AreEqual(232.747558626754m, run.Fees); + Assert.HasCount(15, run.Fills); + Assert.AreEqual(7, m.Trades); + Assert.AreEqual(0.5734871864950309, m.MaxDrawdown, 1e-12); + Assert.AreEqual(-0.20345376375158317, m.Sharpe, 1e-12); + } + + private static double[][] Noise(int variants, int days, int seed) + { + var gen = new Gen(seed); + return [.. Enumerable.Range(0, variants).Select(_ => Enumerable.Range(0, days).Select(_ => 0.01 * gen.Normal()).ToArray())]; + } + + private static List Grid(int a, int b) + { + var result = new List(); + for (var i = 0; i < a; i++) + { + for (var j = 0; j < b; j++) + { + result.Add(new Variant(result.Count, new StrategyParameters([KeyValuePair.Create("a", (decimal)i), KeyValuePair.Create("b", (decimal)j)]), [i, j])); + } + } + + return result; + } + + /// A deliberately wrong strategy: it keeps the whole dataset and buys when tomorrow's close is higher. + private sealed class Peeking(BacktestDataset data) : IStrategy + { + public string Id => "peeking"; + + public string Version => "1"; + + public IReadOnlyList Schedule() => [DecisionTime.AfterClose]; + + public IReadOnlyList Universe(UniverseContext context) => context.Available; + + public IReadOnlyList Decide(StrategyContext context) + { + var bars = data["BTC-EUR"]!.Bars; + var i = data["BTC-EUR"]!.IndexOf(context.Day); + var up = i + 1 < bars.Count && bars[i + 1].Close > bars[i].Close; + return [new Intent("BTC-EUR", up ? 0.9m : 0m, "peek")]; + } + } +}