JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.05k stars 429 forks source link

Working example of GetKlinesAsync in a console app? #17

Closed mynameisibra closed 6 years ago

mynameisibra commented 6 years ago

Been trying to get a working async of the klines, I get it to display the results but not get the new ones in.

JKorf commented 6 years ago

Hi, the GetKlinesAsync method will provide you with a snapshot and history of the requested symbol/interval. It won't automatically update these values for you. In order to receive updates you'll need to use the BinanceSocketClient. I've made a small test program for you which will initially retrieve the klines for a symbol and then keep it updated by subscribing to the socket stream:

private static void TrackKlines()
        {
            List<BinanceKline> lineData;

            using (var client = new BinanceClient())
                // Retrieve initial data
                lineData = client.GetKlines("ETHBTC", KlineInterval.OneMinute).Data.ToList();

            var socketClient = new BinanceSocketClient();
            // Subscribe to socket updates
            socketClient.SubscribeToKlineStream("ETHBTC", KlineInterval.OneMinute, data =>
            {
                var existingKline = lineData.SingleOrDefault(k => k.OpenTime == data.Data.StartTime);
                if (existingKline == null)
                {
                    // Create a new object if we don't have data for this period yet
                    existingKline = new BinanceKline()
                    {
                        CloseTime = data.Data.EndTime,
                        OpenTime = data.Data.StartTime
                    };
                    lineData.Add(existingKline);
                    Console.WriteLine($"Added new kline: {existingKline.OpenTime}-{existingKline.CloseTime}");
                }

                // Update the data of the object
                existingKline.High = data.Data.High;
                existingKline.Low = data.Data.Low;
                existingKline.Close = data.Data.Close;
                existingKline.Open = data.Data.Open;
                existingKline.Trades = data.Data.TradeCount;
                existingKline.Volume = data.Data.Volume;
                Console.WriteLine($"Kline updated. Last price: {lineData.OrderByDescending(l => l.OpenTime).First().Close}");
            });

            Console.ReadLine();
        }
JKorf commented 6 years ago

Did this help you?

mynameisibra commented 6 years ago

Thanks! I haven't gotten a chance to try it yet, I'll let you know


Ibrahim H / Technology Consultant www.fixstop.com

http://www.fixstop.com

Fix Stop at Alafaya 1975 S. Alafaya Trail. Orlando, FL 32828 (407) 456-7551Fix Stop at Chickasaw 599 S. Chickasaw Trail Ste #300. Orlando, FL 32825 (407) 550-0015Fix Stop at Altamonte 1501 E. Altamonte Dr Suite #1015. Casselberry, FL 32730 (407) 588-9711 http://www.fixstop.com

On Fri, Jan 26, 2018 at 4:53 AM, Jan Korf notifications@github.com wrote:

Did this help you?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JKorf/Binance.Net/issues/17#issuecomment-360734646, or mute the thread https://github.com/notifications/unsubscribe-auth/AUF8dbJqBK02_myUaMSFzsVUafyP_Lziks5tOaB-gaJpZM4Rl5bU .

BenVlodgi commented 6 years ago

@mynameisibra I have a forked repo of the sample clients from this repo, with added functionality. https://github.com/BenVlodgi/BinanceClient image

JKorf commented 6 years ago

I'll assume you've managed to get it working. If not please let me know.