C# · REST + WebSocket · NinjaTrader 8

C# NinjaTrader 8 API examples

C# / .NET integration with NinjaTrader 8 over the CrossTrade REST + WebSocket API. HttpClient + ClientWebSocket, no third-party deps.

Why C#

Using C# against the CrossTrade NinjaTrader API

C# is a natural fit for NinjaTrader users (NT8 itself is C#). System.Net.Http and System.Net.WebSockets.ClientWebSocket are built-in. The CancelAndBracket endpoint is a useful safety primitive: it atomically cancels working orders and replaces them with a stop + target bracket.

REST System.Net.Http.HttpClient
WebSocket System.Net.WebSockets.ClientWebSocket
REST · animated

Watch the request get written

A pseudo-developer types out a real POST CancelAndBracket call against your NinjaTrader 8 account. The animation runs on a loop; the static snippet below is yours to copy.

cancelandbracket.cs
Copy this

Complete C# CancelAndBracket example

using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;

var http = new HttpClient();
http.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", "YOUR_TOKEN");

var body = new {
    instrument = "MES 12-25",
    stopPrice  = 5820.0,
    targetPrice = 5840.0,
    quantity   = 1,
};

var res = await http.PostAsJsonAsync(
    "https://app.crosstrade.io/v1/api/accounts/Sim101/orders/cancel-and-bracket",
    body);

Console.WriteLine(await res.Content.ReadAsStringAsync());
WebSocket · animated

Streaming data with System.Net.WebSockets.ClientWebSocket

Same flow on a persistent connection: open the socket, send a subscribe message, receive market data or P&L pushes in real time. CrossTrade's WebSocket is at wss://app.crosstrade.io/ws/stream with the same Bearer token in the request header.

stream.cs
Copy this

Complete C# WebSocket example

using System.Net.WebSockets;
using System.Text;
using System.Text.Json;

var ws = new ClientWebSocket();
ws.Options.SetRequestHeader("Authorization", "Bearer YOUR_TOKEN");
await ws.ConnectAsync(
    new Uri("wss://app.crosstrade.io/ws/stream"),
    CancellationToken.None);

var sub = JsonSerializer.SerializeToUtf8Bytes(new {
    action = "subscribe",
    instruments = new[] { "ES 12-25" }
});
await ws.SendAsync(sub, WebSocketMessageType.Text, true, CancellationToken.None);

var buf = new byte[4096];
while (ws.State == WebSocketState.Open) {
    var r = await ws.ReceiveAsync(buf, CancellationToken.None);
    Console.WriteLine(Encoding.UTF8.GetString(buf, 0, r.Count));
}

The full C#-compatible API surface

Every endpoint documented with request/response schemas, error codes, rate-limit notes, and language-specific code samples.

FAQ

Common questions

Is C# a good language for algorithmic trading on NinjaTrader 8?

Yes — and uniquely so. NinjaScript (NT8's strategy language) IS C#. That means strategies you write inside NT8 use the same language as the external bots you build with CrossTrade. You can share helper code, model classes, and indicator math across both surfaces with minimal friction. No other language gets this advantage with NinjaTrader.

Can I run this inside NinjaTrader itself (NinjaScript)?

Yes. NinjaScript is C#, so you can call CrossTrade from a NinjaScript strategy, indicator, or addon using the same HttpClient pattern. But normally you don't need to — CrossTrade IS the bridge into NT8. The typical flow is: external C# service calls CrossTrade → CrossTrade calls NT8 → NinjaScript runs. Use the external API path unless you have a specific NT8-internal reason.

Does this work with .NET Framework 4.x?

Yes. HttpClient works in 4.5+, ClientWebSocket is 4.5+ as well, and System.Text.Json is available as a NuGet package back to 4.6.1. The example above assumes .NET 6/7/8 syntax (top-level statements, records) for brevity; minor adjustments are needed for older runtimes but the API surface is identical.

ASP.NET Core integration?

Yes, very natural. Register HttpClient via IHttpClientFactory in Program.cs, inject it into a CrossTradeService class. For the WebSocket, run a hosted service (BackgroundService) that maintains the persistent connection. Webhooks from CrossTrade land as standard ASP.NET Core controller actions.

Performance for high-frequency or low-latency trading?

C# JIT-compiled performance is comparable to Java and within ~2x of native C++ for typical trading workloads. For sub-millisecond strategies (Level 2 market making, latency arbitrage), you'd look at native interop or specialized HFT frameworks. For everything else — bracket orders, daily P&L workflows, signal-based entries — C# is more than fast enough.

Async/await for the WebSocket?

Yes, use it. ClientWebSocket.ReceiveAsync returns a Task; await it in a while loop. For backpressure, buffer incoming frames in a Channel<T> and have a separate consumer task drain them. Don't do heavy work inside the receive loop itself.

Visual Studio, Rider, or VS Code?

Any of them. Rider has the strongest NinjaScript C# support if you're also editing NT8 strategies. Visual Studio for the broadest .NET tooling. VS Code with the C# extension for cross-platform development. The CrossTrade API is the same regardless of editor.

F# support?

Yes — F# runs on the same .NET runtime and can call HttpClient + ClientWebSocket directly. F# computation expressions can make the WebSocket message loop especially clean. If your team likes F#, there's no obstacle to using it with CrossTrade.

Mono / Unity for a trading dashboard?

Yes. Mono supports the same HttpClient + WebSocket APIs. Unity (built on Mono) can fetch positions / P&L for an in-game-style trading dashboard if that's your flavor.

Build your first NinjaTrader 8 trading bot in C#

One bearer token, one URL, every NinjaTrader endpoint. Ship to Sim101 first.