Reactive C# API to Interactive Brokers Trader Workstation (TWS)
- .NET 8.0 library
- supports IB TWS API Stable 10.19 (Mar 25 2024 )
- dependencies: RxSockets, Stringification, NodaTime
- demo applications: Console, WPF
interface IInterReactClient : IAsyncDisposable
{
IPEndPoint RemoteIpEndPoint { get; }
Request Request { get; }
IObservable<object> Response { get; }
Service Service { get; }
}
using System;
using System.Threading.Tasks;
using System.Reactive.Linq;
using InterReact;
// Create the InterReact client by connecting to TWS/Gateway on the local host.
IInterReactClient client = await InterReactClient.ConnectAsync();
// Create a contract object.
Contract contract = new()
{
SecurityType = ContractSecurityType.Stock,
Symbol = "AMZN",
Currency = "USD",
Exchange = "SMART"
};
// Create and then subscribe to the observable which can observe ticks for the contract.
IDisposable subscription = client
.Service
.CreateMarketDataObservable(contract)
.OfTickClass(selector => selector.PriceTick)
.Where(tick => tick.TickType == TickType.LastPrice)
.Subscribe(onNext: priceTick => Console.WriteLine($"Last Price = {priceTick.Price}"));
Console.WriteLine(Environment.NewLine + "press a key to exit...");
Console.ReadKey();
Console.Clear();
// Dispose the subscription to stop receiving ticks.
subscription.Dispose();
// Disconnect from TWS/Gateway.
await client.DisposeAsync();
Interactive Brokers Trader Workstation (TWS) or Gateway must be running with API access enabled. In TWS, navigate to Edit / Global Configuration / API / Settings. Ensure the option "Enable ActiveX and Socket Clients" is selected.