C# Examples
C# examples demonstrate the Virtufin services through the API gateway.
Client package
| Package | What it is |
|---|---|
Virtufin.Api.Client |
The API gateway client: State, pub/sub, and every gateway service (workmanager, ...) through dynamic dispatch |
Virtufin.*.DevKit |
The worker devkits (Worker, Behavior, MarketData, Indicator, Strategy, Execution) the notebooks and dotnet/workers/ build on |
The notebooks get all of them from the one Virtufin.Examples.Notebooks package (below). In a project of your own,
dotnet add package Virtufin.Api.Client from the Virtufin feed (Installation).
Verso notebooks
Most C# examples are Verso notebooks in
dotnet/: the devkits end to end (market data, indicator, strategy, executor), the published workers chained
into a pipeline, the live platform pipelines, and tours of Virtufin.Core/Base. The
README lists them all and covers installing Verso and the registry
credentials. Run one headlessly with verso run <notebook> from dotnet/.
Every notebook loads exactly one package:
#!nuget Virtufin.Examples.Notebooks *
Virtufin.Examples.Notebooks is a dependencies-only package
(dotnet/notebook-packages/).
It lists every package the notebooks use, with its whole dependency closure, at one
consistent set of the latest versions. Verso resolves each #!nuget line on its own and
can load two versions of the same assembly from two lines (CS1503/CS0508/CS1705), so
don't add #!nuget lines next to it. To use another package, add it to the BOM's
Directory.Packages.props. CI publishes a new BOM version when its closure changes.
Central Package Management
The example worker projects in dotnet/workers/ pin their versions in
dotnet/workers/Directory.Packages.props (kept current by Renovate):
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<PackageVersion Include="Virtufin.Indicator.DevKit" Version="..." />
Key Patterns
// The context: a name in ~/.config/virtufin/api/contexts.toml, set at the top of the notebook.
const string ContextName = "demo";
// host, port, tls and apiKey come from the context's entry (platform.cs resolves them).
var api = new ApiClient(context.Host, context.Port, context.Tls, context.ApiKey);
dynamic gateway = api; // gateway services resolve at the call site
// A WebSocket connection is a WebSocketFeed worker run by the WorkManager Runtime. It serves
// topics: platform.cs hands the topics a worker listens to to market-data-devkit's FeedResolver
// (topic -> venue preset, split by the venue's limits, a deterministic feed name) and
// FeedOrchestrator (create the feed, or serve a running one its topics), then waits until its
// `status` command reports connected; the feed subscribes btcusdt@kline_1m on every open.
var platform = await Platform.ConnectAsync(ContextName);
var feeds = await platform.ServeTopicsAsync(new[]
{
// act.exchange.binance.candle.update.BTC_USDT.1m, from the naming library (names.cs in examples/dotnet)
NotebookNames.CandleUpdateTopic("BTC_USDT", "1m"),
});
var status = await platform.FeedStatusAsync(feeds[0].WorkerId); // InvokeWorkerCommand "status"
// State: a service the gateway registers ("statestore" on demo and local)
await api.SaveStateAsync("statestore", "x.examples.my-notebook.my-key", "{\"hello\":\"virtufin\"}");
var (value, etag) = await api.GetStateAsync("statestore", "x.examples.my-notebook.my-key");
dotnet/platform.cs wraps this for the notebooks: Platform.ConnectAsync(ContextName) resolves the context,
deploys workers and serves their topics with feeds (ServeTopicsAsync(topics, ...): a worker names
no feed, its topics are its dependency, and the notebook opens the feeds that serve them; FeedStatusAsync, WaitForFeedAsync,
SendAsync and CommandAsync send the feed commands), and CleanupAsync removes everything the notebook
created, the feed workers first. The feed's config and commands are in the
WebSocketFeed docs.