Skip to content

A Dotnet Core library to interact with Interactive Broker's Trader Workstation (IB TWS)

Notifications You must be signed in to change notification settings

deltaray-io/AutoFinance.Broker

 
 

Repository files navigation

Interactive Brokers (TWS) API wrapper

This project is a wrapper around the Interactive Brokers C# client API. It aims to make TWS APIs easier to use by making the multithreaded design look single-threaded to the user. It's in dotnet core, so use it in Linux or Windows.

Warning - Important

Running integration tests will place real orders in TWS. Only run integration tests against a paper trading account.

How to use

Use the TwsController object to call into Tws methods.

TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", 7462, 1);
TwsController twsController = twsObjectFactory.TwsController;

await twsController.EnsureConnectedAsync();

Place an order

Contract contract = new Contract
{
    SecType = TwsContractSecType.Stock,
    Symbol = "MSFT",
    Exchange = TwsExchange.Smart,
    PrimaryExch = TwsExchange.Island,
};

Order order = new Order
{
    Action = "BUY",
    OrderType = "MKT",
    TotalQuantity = 1
};

int orderId = await twsController.GetNextValidIdAsync();
bool successfullyPlaced = await twsController.PlaceOrderAsync(orderId, contract, order);

Get a list of the current positions

List<PositionStatusEventArgs> positionStatusEvents = await twsController.RequestPositions();

Get various account information

string accountId = "DU1052488";
ConcurrentDictionary<string, string> accountUpdates = await twsController.GetAccountDetailsAsync(accountId);

Get historical data

Contract contract = new Contract
{
    SecType = TwsContractSecType.Stock,
    Symbol = "MSFT",
    Exchange = TwsExchange.Smart,
    PrimaryExch = TwsExchange.Island,
};

string queryTime = DateTime.Now.AddMonths(-6).ToString("yyyyMMdd HH:mm:ss");

// Call
List<HistoricalDataEventArgs> historicalDataEvents = await twsController.GetHistoricalDataAsync(contract, queryTime, "1 M", "1 day", "MIDPOINT");

Cancel an order

bool cancelationAcknowledged = await twsController.CancelOrderAsync(orderId);

Place a bracket order (Entry + Stop loss + Take profit), where the entry order is pegged to the market.

bool orderAck = await twsController.PlaceBracketOrder(contract, entryAction, quantity, takePrice, stopPrice);

Check out the tests in TwsControllerBaseTests.cs and TwsControllerTests.cs for more examples. A full list of implemented APIs can be found in the TwsController.

Build + Nuget

Build Status

Download the Nuget package of this project here

About

A Dotnet Core library to interact with Interactive Broker's Trader Workstation (IB TWS)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%