Пример #1
0
        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();
            }
        }
Пример #2
0
        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 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();
            }
        }