Skip to content

bhamer/tws-api-tap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

tws-api-tap

Interactive Brokers Trader Workstation (TWS) API Client implemented using C# Task-based Asynchronous Pattern. Forked from TWS API Version 9.71.

Summary

The goal of this library is to make the TWS API more accessible to new programmers while also offering component-level accessibility for more advanced usage.

Why Task-based Asynchronous Pattern (TAP)?

The intention of having an explicitly defined, and Microsoft recommended, asynchronous pattern as the interface to TWS is to clarify the API Client's behavior and minimize boilerplate code so you can spend more time focusing on your business logic. You'll get documented TAP consumption patterns defining the execution flows and discussing the synchronization implications of asynchronous programming using TAP so you don't need to spend time trying to understand the layers of indirection underlying EWrapper. In fact, this library removes EWrapper and the obligatory method implementations that come with it so you only need to handle messages you care about. You'll get to use async and await to simplify your calling code. And you'll have straightforward mechanisms for controlling execution flow and handling synchronization.

Examples

Get the current time synchronously by waiting on the Task to return a result:

public static void Main()
{
	using (var ibClient = new IbClient())
	{
		ibClient.Connect("127.0.0.1", 7496, 0);	
		var t = ibClient.Response.CurrentTimeAsync(); // capture Task
		ibClient.Request.ReqCurrentTime(); // request current time from server
		Console.WriteLine("Server time is {0}.", t.Result); // wait for Task to return a Result
	}
}

Process tick prices until enter is pressed:

public static void Main()
{
	using (var ibClient = new IbClient())
	{
		ProcessTickPrices(ibClient); // process tick prices asynchronously
		ibClient.Connect("127.0.0.1", 7496, 0);
		ibClient.Request.ReqMktData(1001, ContractSamples.getEurUsdForex(), "", false, GetFakeParameters(3));
		Console.ReadLine(); // wait here and process tick prices
		ibClient.Request.CancelMktData(1001);
	}
}

private static async void ProcessTickPrices(IbClient ibClient)
{
	while (true)
	{
		var tickPrice = await ibClient.Response.TickPriceAsync();
		Console.WriteLine("Tick Price. Ticker Id: {1}, Field: {2}, Price: {3}, CanAutoExecute: {4}", Thread.CurrentThread.ManagedThreadId, tickPrice.TickerId, tickPrice.Field, tickPrice.Price, tickPrice.CanAutoExecute);
	}
}

Future State

Considering using cancellation tokens to cancel data requests instead of having to call an associated cancel request method with the original ticker id. Cancellation-tokens branch has proof of concept code.

This:

var cts = new CancellationTokenSource();
ibClient.Request.ReqMktData(1001, ContractSamples.getEurUsdForex(), "", false, GetFakeParameters(3), cts.Token);
cts.Cancel(); // cancel market data request

Instead of this:

ibClient.Request.ReqMktData(1001, ContractSamples.getEurUsdForex(), "", false, GetFakeParameters(3), cts.Token);
ibClient.Request.CancelMktData(1001);

Warning

This software is provided AS-IS with NO WARRANTY, express or implied. Your use of this software is at your own risk. It may contain any number of bugs, known or unknown, which might cause you to lose money if you use it.

This code is not sanctioned or supported by Interactive Brokers.

About

Interactive Brokers TWS API Client implemented using C# Task-based Asynchronous Pattern

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages