Fase 12 — Dati e matematica dei panieri (#187)
Research-only data and maths for basket mean reversion (ADR 0028): a seam between the trial judge and the engines, the basket maths in src/Anubis/Baskets, sub-day research bars, ECB crosses and FX minute import. refs #118, refs #119, refs #120, refs #121 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #187.
This commit is contained in:
1 parent
93cdf7850d
commit
e0a438a0b2
40 files changed
+4464
-110
No files matched your search
@@ -106,17 +106,9 @@ internal static class BacktestCommand
|
||||
}
|
||||
|
||||
/// <summary>The ECB history of EUR/USD, kept next to the research data so an offline run uses the same rates.</summary>
|
||||
private static async Task<IReadOnlyList<FxRate>> RatesAsync(ApiHttp http, string researchRoot, bool offline, TextWriter output)
|
||||
internal static async Task<IReadOnlyList<FxRate>> RatesAsync(ApiHttp http, string researchRoot, bool offline, TextWriter output)
|
||||
{
|
||||
var path = Path.Combine(researchRoot, "ecb-eurofxref-hist.zip");
|
||||
if (!offline || !File.Exists(path))
|
||||
{
|
||||
var zip = await http.GetBytesAsync(Ecb.History, null, null, CancellationToken.None).ConfigureAwait(false)
|
||||
?? throw new MarketDataException("BCE: lo storico dei cambi non è disponibile.", retryable: true);
|
||||
await File.WriteAllBytesAsync(path, zip).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var rates = Ecb.ParseHistoryCsv(Ecb.ExtractHistoryCsv(await File.ReadAllBytesAsync(path).ConfigureAwait(false)), ["USD"]);
|
||||
var rates = await Ecb.LoadHistoryAsync(http, researchRoot, offline, ["USD"], CancellationToken.None).ConfigureAwait(false);
|
||||
await output.WriteLineAsync(string.Create(CultureInfo.InvariantCulture, $"Cambi BCE EUR/USD: {rates.Count} giorni, dal {rates[0].Date:yyyy-MM-dd} al {rates[^1].Date:yyyy-MM-dd}."))
|
||||
.ConfigureAwait(false);
|
||||
return rates;
|
||||
|
||||
@@ -15,15 +15,18 @@ internal static class DataCommand
|
||||
{
|
||||
private const string Usage =
|
||||
"Uso: anubis data report [--days N] [--out file.md]\n" +
|
||||
" anubis data binance <SIMBOLO> <1d|1m> <AAAA-MM> (per esempio BTCUSDT 1d 2017-08)\n" +
|
||||
" anubis data csv <file.csv> (colonne descritte in file.mapping.json o mapping.json)\n" +
|
||||
" anubis data binance <SIMBOLO> <1d|1m|15m|30m|1h|4h|8h> <AAAA-MM> (per esempio BTCUSDT 1d 2017-08)\n" +
|
||||
" anubis data csv <file.csv> (colonne descritte in file.mapping.json o mapping.json)\n" +
|
||||
" anubis data ecb [--offline] (le 28 coppie delle otto valute dai cambi giornalieri della BCE, dal 1999;\n" +
|
||||
" con --offline usa lo storico già scaricato, anche da backtest base)\n" +
|
||||
" anubis data fx <file.csv> [--symbol EURUSD] (file a un minuto di Dukascopy o HistData: scrive le barre da 1 e da 15 minuti)\n" +
|
||||
"Opzioni comuni: [--data-dir <cartella>, obbligatoria fuori dal container]";
|
||||
|
||||
public static async Task<int> RunAsync(string[] args, TextWriter? output = null, IReadOnlyDictionary<string, string?>? environment = null,
|
||||
HttpMessageHandler? handler = null)
|
||||
{
|
||||
output ??= Console.Out;
|
||||
if (args.Length < 1 || args[0] is not ("report" or "binance" or "csv"))
|
||||
if (args.Length < 1 || args[0] is not ("report" or "binance" or "csv" or "ecb" or "fx"))
|
||||
{
|
||||
await output.WriteLineAsync(Usage).ConfigureAwait(false);
|
||||
return 2;
|
||||
@@ -58,6 +61,26 @@ internal static class DataCommand
|
||||
var result = await new CsvImporter(new ResearchStore(config.Data.Research)).ImportAsync(file, CsvImporter.MappingFor(file), CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
await WriteAsync(output, result).ConfigureAwait(false);
|
||||
return 0;
|
||||
case "ecb" when positional.Count == 0:
|
||||
config.Data.Ensure();
|
||||
using (var http = handler is null ? new HttpClient() : new HttpClient(handler, disposeHandler: false))
|
||||
{
|
||||
http.Timeout = TimeSpan.FromMinutes(5);
|
||||
http.DefaultRequestHeaders.UserAgent.ParseAdd($"{AnubisInfo.Name}/{AnubisInfo.Version}");
|
||||
await EcbAsync(new ApiHttp(http, clock, Ecb.Source), new ResearchStore(config.Data.Research), rest.Contains("--offline"), output)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return 0;
|
||||
case "fx" when positional.Count == 1:
|
||||
config.Data.Ensure();
|
||||
foreach (var imported in await new FxMinuteImporter(new ResearchStore(config.Data.Research))
|
||||
.ImportAsync(Path.GetFullPath(positional[0]), Option(rest, "--symbol"), CancellationToken.None).ConfigureAwait(false))
|
||||
{
|
||||
await WriteAsync(output, imported).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return 0;
|
||||
default:
|
||||
await output.WriteLineAsync(Usage).ConfigureAwait(false);
|
||||
@@ -65,13 +88,41 @@ internal static class DataCommand
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or MarketDataException or HttpRequestException or FormatException or System.Text.Json.JsonException
|
||||
or InvalidOperationException or TimeZoneNotFoundException or System.Data.Common.DbException or UnauthorizedAccessException)
|
||||
or InvalidOperationException or TimeZoneNotFoundException or System.Data.Common.DbException or UnauthorizedAccessException
|
||||
or System.IO.InvalidDataException)
|
||||
{
|
||||
await output.WriteLineAsync("Non riuscito: " + Redactor.Redact(ex.Message)).ConfigureAwait(false);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The 28 pairs from the ECB history (the zip shared with backtest base), one series each under research/ecb.</summary>
|
||||
private static async Task EcbAsync(ApiHttp http, ResearchStore store, bool offline, TextWriter output)
|
||||
{
|
||||
var rates = await Ecb.LoadHistoryAsync(http, store.Root, offline, EcbCrosses.Rates, CancellationToken.None).ConfigureAwait(false);
|
||||
var crosses = EcbCrosses.Build(rates);
|
||||
var written = crosses.Where(c => c.Value.Count > 0).ToList();
|
||||
if (written.Count == 0)
|
||||
{
|
||||
throw new MarketDataException("BCE: lo storico non contiene i cambi delle sette valute.", retryable: false);
|
||||
}
|
||||
|
||||
foreach (var (pair, bars) in written)
|
||||
{
|
||||
await store.WriteAsync(EcbCrosses.Venue, pair, Timeframes.Day, bars, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var days = written.SelectMany(c => c.Value.Select(b => b.OpensAt)).Distinct().Order().ToList();
|
||||
var missing = days.Count * crosses.Count - crosses.Values.Sum(b => b.Count);
|
||||
await output.WriteLineAsync(string.Create(CultureInfo.InvariantCulture,
|
||||
$"Coppie di valute dai cambi BCE: {written.Count} coppie, {days.Count} giorni, dal {days[0]:yyyy-MM-dd} al {days[^1]:yyyy-MM-dd}.")).ConfigureAwait(false);
|
||||
if (missing > 0)
|
||||
{
|
||||
await output.WriteLineAsync(string.Create(CultureInfo.InvariantCulture,
|
||||
$"- {missing} barre giornaliere mancano perché quel giorno la BCE non ha pubblicato uno dei due cambi.")).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<int> ReportAsync(Platform.StartupConfig config, IClock clock, IReadOnlyList<string> rest, TextWriter output)
|
||||
{
|
||||
if (!config.IsComplete)
|
||||
|
||||
@@ -26,7 +26,7 @@ static async Task<int> RunAsync(string[] args)
|
||||
return await AccessCommand.RunAsync(args[1..]).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// "anubis data report|binance|csv": the data quality report and the research imports (Phase 2).
|
||||
// "anubis data report|binance|csv|ecb|fx": the data quality report and the research imports (Phases 2 and 12).
|
||||
if (args.Length >= 1 && args[0] == "data")
|
||||
{
|
||||
return await DataCommand.RunAsync(args[1..]).ConfigureAwait(false);
|
||||
|
||||
Reference in new issue
Block a user