MarketDataService wires the collector, the reference sources, the universes and the corporate actions into the scheduler. Short jobs (rates, calendar, mood, spread samples, corporate actions) run on their schedule; long downloads (catalogues, the first history, thousands of stocks, the Binance archives) run on a background worker the jobs only wake, so they never delay the backup or each other. The live crypto feed follows the crypto universe, or BTC, ETH and SOL until one exists. Alpaca uses only the paper account keys; without them the stock part is off and the sources list says why. Market data is off in demo mode, with ANUBIS_MARKET_DATA=off, and in tests that bring no fake network. New: GET /api/data/quality (JSON or Markdown) and /api/data/sources, and "anubis data report|binance|csv" on the command line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
49ed73e6f6
commit
1e71038209
10 files changed
+898
-6
No files matched your search
@@ -0,0 +1,130 @@
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using Anubis.Brokers;
|
||||
using Anubis.Core;
|
||||
using Anubis.Data;
|
||||
using Anubis.Db;
|
||||
|
||||
namespace Anubis.Server;
|
||||
|
||||
/// <summary>
|
||||
/// Command line tools for the data (Phase 2): the quality report, and the imports into the research history of the
|
||||
/// data folder (SPEC 6.6). They read the same startup configuration as the server.
|
||||
/// </summary>
|
||||
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" +
|
||||
"Opzioni comuni: [--platform docker|desktop] [--data-dir <cartella>]";
|
||||
|
||||
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"))
|
||||
{
|
||||
await output.WriteLineAsync(Usage).ConfigureAwait(false);
|
||||
return 2;
|
||||
}
|
||||
|
||||
var (positional, rest) = Split(args[1..]);
|
||||
environment ??= Environment.GetEnvironmentVariables().Cast<DictionaryEntry>().ToDictionary(e => (string)e.Key, e => e.Value as string, StringComparer.Ordinal);
|
||||
var options = ServerOptions.Parse([.. rest], environment);
|
||||
var config = options.LoadConfig(environment);
|
||||
var clock = new SystemClock();
|
||||
try
|
||||
{
|
||||
switch (args[0])
|
||||
{
|
||||
case "report":
|
||||
return await ReportAsync(config, clock, rest, output).ConfigureAwait(false);
|
||||
case "binance" when positional.Count == 3:
|
||||
config.Data.Ensure();
|
||||
var month = DateOnly.ParseExact(positional[2] + "-01", "yyyy-MM-dd", CultureInfo.InvariantCulture);
|
||||
using (var http = handler is null ? new HttpClient() : new HttpClient(handler, disposeHandler: false))
|
||||
{
|
||||
http.DefaultRequestHeaders.UserAgent.ParseAdd($"{AnubisInfo.Name}/{AnubisInfo.Version}");
|
||||
var importer = new BinanceImporter(new ApiHttp(http, clock, "binance"), new ResearchStore(config.Data.Research), clock);
|
||||
await WriteAsync(output, await importer.ImportAsync(positional[0].ToUpperInvariant(), positional[1], month.Year, month.Month, CancellationToken.None)
|
||||
.ConfigureAwait(false)).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return 0;
|
||||
case "csv" when positional.Count == 1:
|
||||
config.Data.Ensure();
|
||||
var file = Path.GetFullPath(positional[0]);
|
||||
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;
|
||||
default:
|
||||
await output.WriteLineAsync(Usage).ConfigureAwait(false);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
await output.WriteLineAsync("Non riuscito: " + Redactor.Redact(ex.Message)).ConfigureAwait(false);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<int> ReportAsync(Platform.StartupConfig config, IClock clock, IReadOnlyList<string> rest, TextWriter output)
|
||||
{
|
||||
if (!config.IsComplete)
|
||||
{
|
||||
await output.WriteLineAsync("La configurazione d'avvio non è completa: avvia prima il server una volta.").ConfigureAwait(false);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Redactor.Register(config.Db!.Password);
|
||||
var days = Option(rest, "--days") is { } d ? int.Parse(d, CultureInfo.InvariantCulture) : 7;
|
||||
await using var db = Database.Open(config.Db!, allowCreate: false);
|
||||
var markdown = (await QualityReport.BuildAsync(db, clock, TimeSpan.FromDays(days), CancellationToken.None).ConfigureAwait(false)).ToMarkdown();
|
||||
if (Option(rest, "--out") is { } path)
|
||||
{
|
||||
await File.WriteAllTextAsync(path, markdown).ConfigureAwait(false);
|
||||
await output.WriteLineAsync("Rapporto scritto in " + Path.GetFullPath(path)).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await output.WriteAsync(markdown).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static Task WriteAsync(TextWriter output, ImportResult result)
|
||||
{
|
||||
var lines = new List<string>
|
||||
{
|
||||
string.Create(CultureInfo.InvariantCulture,
|
||||
$"{result.Venue} {result.Symbol} {result.Timeframe}: {result.Bars} barre, dal {result.First:yyyy-MM-dd HH:mm} al {result.Last:yyyy-MM-dd HH:mm} (UTC)."),
|
||||
};
|
||||
lines.AddRange(result.Notes.Select(n => "- " + n));
|
||||
return output.WriteLineAsync(string.Join(Environment.NewLine, lines));
|
||||
}
|
||||
|
||||
private static string? Option(IReadOnlyList<string> args, string name)
|
||||
{
|
||||
for (var i = 0; i < args.Count - 1; i++)
|
||||
{
|
||||
if (args[i] == name)
|
||||
{
|
||||
return args[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Positional arguments come first; from the first "--" option on, everything is options.
|
||||
private static (List<string> Positional, List<string> Options) Split(string[] args)
|
||||
{
|
||||
var index = Array.FindIndex(args, a => a.StartsWith("--", StringComparison.Ordinal));
|
||||
return index < 0 ? (args.ToList(), []) : (args[..index].ToList(), args[index..].ToList());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user