public async Task TestRealtimeStreamFuture()
        {
            await client.SignIn();

            using (var socket = new TDAmeritradeStreamClient(client))
            {
                var symbol = "/NQ";

                socket.OnHeartbeatSignal += o => { };
                socket.OnQuoteSignal     += o => { };
                socket.OnTimeSaleSignal  += o => { };
                socket.OnChartSignal     += o => { };
                socket.OnBookSignal      += o => { };

                await socket.Connect();

                await socket.SubscribeQuote(symbol);

                await socket.SubscribeChart(symbol, TDChartSubs.CHART_FUTURES);

                await socket.SubscribeTimeSale(symbol, TDTimeSaleServices.TIMESALE_FUTURES);

                await Task.Delay(1000);

                Assert.IsTrue(socket.IsConnected);
                Assert.IsTrue(socket.IsConnected);
                await socket.Disconnect();
            }
        }
        public async Task TestRealtimeStream()
        {
            await client.SignIn();

            using (var socket = new TDAmeritradeStreamClient(client))
            {
                var symbol = "SPY";
                socket.OnHeartbeatSignal += o => { };
                socket.OnQuoteSignal     += o => { };
                socket.OnTimeSaleSignal  += o => { };
                socket.OnChartSignal     += o => { };
                socket.OnBookSignal      += o => { };
                await socket.Connect();

                await socket.SubscribeQuote(symbol);

                await socket.SubscribeChart(symbol, TDChartSubs.CHART_EQUITY);

                await socket.SubscribeTimeSale(symbol, TDTimeSaleServices.TIMESALE_EQUITY);

                await socket.SubscribeBook(symbol, TDBookOptions.LISTED_BOOK);

                await socket.SubscribeBook(symbol, TDBookOptions.NASDAQ_BOOK);

                await Task.Delay(1000);

                Assert.IsTrue(socket.IsConnected);
                await socket.Disconnect();
            }
        }
        public async Task TestQOSRequest()
        {
            await client.SignIn();

            using (var socket = new TDAmeritradeStreamClient(client))
            {
                await socket.Connect();

                await socket.RequestQOS(TDQOSLevels.FAST);
            }
        }
        public async Task RecordStream()
        {
            await client.SignIn();

            Console.WriteLine($"IsSignedIn : {client.IsSignedIn}");

            Console.WriteLine("Input Symbol :");
            var symbols = Console.ReadLine();

            Console.WriteLine("Input Save Directory :");
            var path = Console.ReadLine();

            if (string.IsNullOrEmpty(path))
            {
                path = "/Records/";
            }

            Console.WriteLine("Save Format 0) All 1) Json 2) Binary:");
            char format = Console.ReadKey().KeyChar;

            Console.WriteLine();

            Console.WriteLine("Input QOS : 0-5 (500, 750, 1000, 1500, 3000, 5000)ms");
            var qos = Console.ReadKey().KeyChar;

            Console.WriteLine();
            int qosInt = 0;

            int.TryParse(qos.ToString(), out qosInt);

            var txt_path = $"{path}/{DateTime.UtcNow.ToString("yyyy-MM-dd")}.txt";

            if (!File.Exists(txt_path))
            {
                using (var s = File.Create(txt_path)) { }
            }

            using (var socket = new TDAmeritradeStreamClient(client))
            {
                async void Retry()
                {
                    if (!terminated)
                    {
                        Console.WriteLine("Retrying...");
                        await Task.Delay(5000);

                        Connect();
                    }
                }

                async void Connect()
                {
                    Console.WriteLine("Connecting...");
                    try
                    {
                        await socket.Connect();

                        if (socket.IsConnected)
                        {
                            await socket.RequestQOS((TDQOSLevels)qosInt);

                            await Task.Delay(1000);

                            await socket.SubscribeQuote(symbols);

                            await socket.SubscribeChart(symbols, IsFutureSymbol(symbols)?TDChartSubs.CHART_FUTURES : TDChartSubs.CHART_EQUITY);

                            await socket.SubscribeTimeSale(symbols, IsFutureSymbol(symbols)?TDTimeSaleServices.TIMESALE_FUTURES : TDTimeSaleServices.TIMESALE_EQUITY);

                            await socket.SubscribeBook(symbols, TDBookOptions.LISTED_BOOK);

                            await socket.SubscribeBook(symbols, TDBookOptions.NASDAQ_BOOK);

                            await socket.SubscribeBook(symbols, TDBookOptions.FUTURES_BOOK);
                        }
                        else if (!terminated)
                        {
                            Retry();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error " + ex);
                        Retry();
                    }
                }

                socket.OnJsonSignal += (m) =>
                {
                    if (format == '0' || format == '1')
                    {
                        lock (txt_path)
                        {
                            Console.WriteLine(m);
                            using (var s = File.AppendText(txt_path))
                            {
                                s.WriteLine(m);
                            }
                        }
                    }
                };

                socket.OnConnect += (s) =>
                {
                    if (!s)
                    {
                        Connect();
                    }
                };

                Connect();
                Console.WriteLine("Type any key to quit");
                Console.ReadLine();
                terminated = true;
                await socket.Disconnect();
            }
        }
Exemple #5
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Starting Client");
            Console.WriteLine("...");

            //var serviceCollection = new ServiceCollection();
            //serviceCollection.AddDataProtection();
            //var services = serviceCollection.BuildServiceProvider();
            //var protection = services.GetService<IDataProtectionProvider>();
            //var cache = new TDProtectedCache(protection);
            //var client = new TDAmeritradeClient(cache);

            var cache  = new TDUnprotectedCache();
            var client = new TDAmeritradeClient(cache);

            Console.WriteLine("1 to sign in fresh, 2 to refresh signin, 3 streaming");
            var option = Console.ReadKey();

            switch (option.Key)
            {
            case ConsoleKey.D1:
                Console.WriteLine("Paste consumer key :");
                var key = Console.ReadLine();
                Console.WriteLine("Opening Browser. Please sign in.");
                client.RequestAccessToken(key);

                Console.WriteLine("Paste the code. This is in the browser url bar 'code={code}'.");
                var code = Console.ReadLine();
                await client.PostAccessToken(key, code);

                Console.WriteLine($"Authenticated {client.IsSignedIn}.");
                Console.ReadLine();
                break;

            case ConsoleKey.D2:
                await client.PostRefreshToken();

                Console.WriteLine($"Authenticated {client.IsSignedIn}.");
                Console.ReadLine();
                break;

            case ConsoleKey.D3:
                await client.PostRefreshToken();

                Console.WriteLine($"Authenticated {client.IsSignedIn}.");
                var socket = new TDAmeritradeStreamClient(client);
                socket.OnMessage += (m) =>
                {
                    Console.WriteLine(m);
                };
                await socket.Connect();

                await socket.Subscribe_Chart("MSFT");

                Console.ReadLine();
                break;

            default:
                return;
            }
        }