Code Monkey home page Code Monkey logo

autofinance.broker's Introduction

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

Each call to the API requires its corresponding controller object. All controller objects are initialized with the same parameters.

// Initialize a controller object
TwsObjectFactory twsObjectFactory = new TwsObjectFactory();
TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1);
ITwsNextOrderIdController nextOrderIdController = new TwsNextOrderIdController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler);
TwsOrderPlacementController orderPlacementController = new TwsOrderPlacementController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler);
TwsRequestIdGenerator twsRequestIdGenerator = new TwsRequestIdGenerator();
TwsExecutionController executionController = new TwsExecutionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, twsRequestIdGenerator);

// Ensure the connection is made with TWS before calling any controller methods
await connectionController.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 nextOrderIdController.GetNextValidIdAsync();
bool successfullyPlaced = await orderPlacementController.PlaceOrderAsync(orderId, contract, order);

Get a list of the current positions

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

Get various account information

string accountId = "DU1052488";
ConcurrentDictionary<string, string> accountUpdates = await accountUpdatesController.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 historicalDataController.GetHistoricalDataAsync(contract, queryTime, "1 M", "1 day", "MIDPOINT");

Cancel an order

bool cancelationAcknowledged = await orderCancelationController.CancelOrderAsync(orderId);

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

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

Build + Nuget

Build Status

Download the Nuget package of this project here

autofinance.broker's People

Contributors

amittleider avatar dmausttruefit avatar dylinmaust avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.