Exemple #1
0
        static void Main(string[] args)
        {
            // host and port can be specified in the constructor or Connect()
            using (var client = new TwsClient(clientId: 1, host: "127.0.0.1", port: 7496))
                using (var subscriptions = new CompositeDisposable())
                {
                    // The errors observable is always available
                    // Here we just print them all to the console
                    subscriptions.Add(client.Errors.Subscribe(
                                          et => Console.WriteLine("TWS {2} #{0}: {1}", et.Code, et.Message, et.IsError()? "error" : "message"),
                                          ex => Console.WriteLine("TWS Exception: {0}", ex.Message)
                                          ));

                    Console.WriteLine("Connecting to TWS...");
                    try
                    {
                        var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
                        client.Connect(ct: cts.Token); // disposing will disconnect
                    }
                    catch
                    {
                        Console.WriteLine("Connection failed, exiting...");
                        return;
                    }
                    Console.WriteLine("Connected.");

                    Console.WriteLine("Requesting protfolio snapshot...");
                    var snapshot = client.RequestPortfolioSnapshot();
                    subscriptions.Add(snapshot.SubscribeToConsole());

                    var live = client.RequestPortfolioData();
                    subscriptions.Add(live.SubscribeToConsole());

                    snapshot.Wait(); // let the snapshot be fully printed on console before going further
                    Console.WriteLine("Press ENTER to start live updates, \nENTER again to request stapshot update in the middle of live updates, \nthen ENTER to stop live updates.");
                    Console.ReadLine();
                    var live_connect = live.Connect();
                    Console.ReadLine();
                    subscriptions.Add(client.RequestPortfolioSnapshot().SubscribeToConsole());
                    Console.ReadLine();
                    live_connect.Dispose();

                    Console.WriteLine("Press ENTER to disconnect.");
                    Console.ReadLine();
                }
            Console.WriteLine("Finished.");
        }
        // Application entry point
        static void Main(string[] args)
        {
            client = new TwsClient();

            Console.WriteLine("Connecting...");
            // Connect to the Trader Workstation client
            ConnectToTws();
            WaitForConnection();

            // Now we can do whatever we want
            Console.WriteLine("Please enter a ticker symbol");
            GetCurrentTime();
            // We will implement ticker feed functionality once we
            // have the basic application working.
            //
            // var symbol = Console.ReadLine();
            // SubscribeToTickerFeed(symbol);

            DisconnectFromClient();
            Console.WriteLine("Finished.");
        }
Exemple #3
0
        static void Main(string[] args)
        {
            // host and port can be specified in the constructor or Connect()
            using (var client = new TwsClient(clientId: 1, host: "127.0.0.1", port: 7496))
                using (var subscriptions = new CompositeDisposable())
                {
                    // The errors observable is always available
                    // Here we just print them all to the console
                    subscriptions.Add(client.Errors.Subscribe(
                                          et => Console.WriteLine("TWS {2} #{0}: {1}", et.Code, et.Message, et.IsError()? "error" : "message"),
                                          ex => Console.WriteLine("TWS Exception: {0}", ex.Message)
                                          ));

                    Console.WriteLine("Connecting to TWS...");
                    try
                    {
                        var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
                        client.Connect(ct: cts.Token); // disposing will disconnect
                    }
                    catch
                    {
                        Console.WriteLine("Connection failed, exiting...");
                        return;
                    }
                    Console.WriteLine("Connected.");


                    var contract = new Contract()
                    {
                        Symbol   = "MSFT",
                        Exchange = "SMART",
                        SecType  = "STK",
                        Currency = "USD",
                    };

                    Console.WriteLine("Intraday data:");
                    IObservable <Bar> hdata = client.RequestHistoricalData(
                        contract,
                        DateTime.Now,
                        duration: "1 D", // 1 day window
                        barSizeSetting: "30 mins",
                        whatToShow: "TRADES",
                        useRTH: true
                        );
                    // hdata is a cold observable; the request to TWS is sent on subscription
                    // Since Main() is not async, we cannot use await here
                    try
                    {
                        hdata.Do(Console.WriteLine).Timeout(TimeSpan.FromSeconds(15)).Wait();
                    }
                    catch (TimeoutException ex)
                    {
                        Console.WriteLine(ex.Message);
                        return;
                    }

                    // Example: this is what it would look like with await
                    //await hdata.Do(Console.WriteLine);

                    // It also can be done w/o using Do() but with more ceremony.
                    // Since it is a cold observable, it has to be turned hot with Publish()
                    //var pub = hdata.Publish();
                    //var subj = pub.Subscribe(Console.WriteLine);
                    //var con = pub.Connect();
                    //await pub;
                    //subj.Dispose();
                    //con.Dispose();

                    Console.WriteLine("Daily data:");
                    IObservable <Bar> hdata2 = client.RequestHistoricalData(
                        contract,
                        DateTime.Now,
                        duration: "1 W", // 1 week window
                        barSizeSetting: "1 day",
                        whatToShow: "TRADES",
                        useRTH: true
                        );
                    hdata2.Do(bar => Console.WriteLine(bar.ToString(TimeZoneInfo.Utc))).Wait();

                    Console.WriteLine("Press ENTER to disconnect.");
                    Console.ReadLine();
                }
            Console.WriteLine("Finished.");
        }