Coder Social home page Coder Social logo

dshe / interreact Goto Github PK

View Code? Open in Web Editor NEW
46.0 11.0 12.0 17.79 MB

Reactive C# API to Interactive Brokers Trader Workstation.

License: Apache License 2.0

C# 100.00%
ib api reactive interactive-brokers trader-workstation tws reactive-extensions rx ibkr ibapi

interreact's Introduction

InterReact   Version License Ukraine

Reactive C# API to Interactive Brokers Trader Workstation (TWS)

  • .NET 6.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; }
}

Example

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 = SecurityType.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
    .CreateTickObservable(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();

Notes

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.

interreact's People

Contributors

dshe avatar memoryfraction avatar michaelreaton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

interreact's Issues

Placing order error

when creating new order and trying to place it
Order neworder = new Order();
neworder.OrderType = OrderType.Limit;
neworder.TradeAction = TradeAction.Buy;
neworder.LimitPrice = 1;
neworder.TimeInForce = TimeInForce.Day;
neworder.TotalQuantity = 1;

        neworder.AlgoStrategy  = "Adaptive";
        neworder.AlgoParams.Add(new Tag("adaptivePriority", "Patient"));  <-- here 

get error

Alert: {RequestId:1823, OrderId:1823, Code:320, Message:"Error reading request.Message id 1823. Unable to parse data. java.lang.NumberFormatException: For input string: "Patient"", AlertType:HasId}

HistoricalBarSize, OneSecond is incorrectly assigned.

In HistoricalBarSize class, OneSecond is set as "1 sec". However as stated in IBKR error message:
"Historical data bar size setting is invalid. Legal ones are: 1 secs, 5 secs, 10 secs, 15 secs, 30 secs, 1 min, 2 mins, 3 mins, 5 mins, 10 mins, 15 mins, 20 mins, 30 mins, 1 hour, 2 hours, 3 hours, 4 hours, 8 hours, 1 day, 1W, 1M--"
It is supposed to be set to "1 secs"

Errors during the use

Met several errors while using the latest master branch

1 Compiler errors as below
1
commented the compiler errors and continue.

2 unit test failures
image

IBKR Pro Account + IBGW 10.19 Version + Windows 10 Pro 22H2
image

Wonder, is it only me met these errors or errors happens widely? the repo appears to be good I test during Nov 2023.
I don't know what happened or anything wrong I did?
Any suggestion?

Thanks

OrderStatusReports don't appear to be handled

I updated to the latest and tried using the OpenOrdersObservable and it doesn't appear to be handling/receiving the OrderStatusReports. A new order will cause a "Submitted" OrderStatusReport to be received. When it's cancelled a "Cancelled" report should be received.

How to Implement the below function points?

How to implement the below function points?
1 List GetOpeningOrders(string symbol, startDt, endDt = DateTime.Now)
2 Order CancelOrder(int orderId), GetCanceledOrders()
3 List GetCompletedOrders(startDt, endDt = DateTime.Now)
4 List GetSymbols(int number = 500) // get all the valid symbols, e.g. ranking by market cap

thinking, if I can have a sample code, I can implement the rest of the order function , and Pull Request

Thanks

Alert when placing order

Hi,

When I'm trying to place a simple order with OrderType of Market Order and Limit Order, they both returned with alert message: "Order processing failed. Algorithm definition not found. Code=439. Id=1180."
var order = new Order { TradeAction = TradeAction.Buy, TotalQuantity = 100, OrderType = OrderType.Market };
Highly appreciate for any clues.

Return no record after request contract detail

        var contract = new Contract
        {
            SecurityType = InterReact.StringEnums.SecurityType.Stock,
            Symbol = "MSFT",
            Currency = "USD",
            Exchange = "SMART"
        };

        //client.Request.RequestCurrentTime();

        //var test = await client.Services.HistoricalDataObservable(contract);

        var contractDetailsList = await client.Services.ContractDetailsObservable(contract);

        var contractDetails = contractDetailsList.Single();

Var contractDetails returns zero.

When I debug the solution, IB gateway log shows the contract detail is returned, yet the program doesn't seem to hit the contractDetail.

Any idea?

Thanks.

A question about the as-is design

In the below code, it uses Take(timeSpan) to wait the response, which may cause concurrent issue.
Why not use the event , e.g. PlaceOrderCompleteEvent, to let the caller know: "Hi, subscribe this event, when I am ready, I let you know"
e.g. this article: https://zhuanlan.zhihu.com/p/30341365

public async Task<IList<OpenOrder>> RequestOpenOrdersAsync()
{
    Task<IList<OpenOrder>> task =
        Response
        .OfType<OpenOrder>()
        **.Take(TimeSpan.FromMilliseconds(500))**
        .ToList()
        .ToTask();
    Request.RequestOpenOrders();
    var orders = await task;
    return orders;
}

Feel free to correct me if my understanding is incorrect.

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.