Exemplo n.º 1
0
        private static async Task SendSubscriptionRequests(BitstampWebsocketClient client)
        {
            //client.Send(new SubscribeRequest("btcusd", Channel.OrderBook));
            //client.Send(new SubscribeRequest("btceur", Channel.OrderBook));

            //client.Send(new SubscribeRequest("btcusd", Channel.OrderBookDetail));
            //client.Send(new SubscribeRequest("btceur", Channel.OrderBookDetail));

            //client.Send(new SubscribeRequest("btcusd", Channel.OrderBookDiff));
            //client.Send(new SubscribeRequest("btceur", Channel.OrderBookDiff));

            client.Send(new SubscribeRequest("btcusd", Channel.Ticker));
            client.Send(new SubscribeRequest("btceur", Channel.Ticker));
        }
        public async Task ConnectTest()
        {
            var url = BitstampValues.ApiWebsocketUrl;

            using (var communicator = new BitstampWebsocketCommunicator(url))
            {
                HeartbeatResponse received = null;
                var receivedEvent          = new ManualResetEvent(false);

                using (var client = new BitstampWebsocketClient(communicator))
                {
                    client.Streams.HeartbeatStream.Subscribe(pong =>
                    {
                        received = pong;
                        receivedEvent.Set();
                    });

                    await communicator.Start();

                    client.Send(new SubscribeRequest("btcusd", Channel.Heartbeat));

                    receivedEvent.WaitOne(TimeSpan.FromSeconds(90));

                    Assert.NotNull(received);
                }
            }
        }
Exemplo n.º 3
0
        private static async Task<ICryptoOrderBook> StartBitstamp(string pair, bool optimized, bool l2Optimized)
        {
            var url = BitstampValues.ApiWebsocketUrl;
            var communicator = new BitstampWebsocketCommunicator(url) { Name = "Bitstamp" };
            var client = new BitstampWebsocketClient(communicator);

            var source = new BitstampOrderBookSource(client);
            var orderBook = l2Optimized ? 
                new CryptoOrderBookL2(pair, source) : 
                (ICryptoOrderBook)new CryptoOrderBook(pair, source);

            if (optimized)
            {
                ConfigureOptimized(source, orderBook);
            }

            _ = communicator.Start();

            // Send subscription request to order book data
            client.Send(new Bitstamp.Client.Websocket.Requests.SubscribeRequest(
                pair,
                Channel.OrderBook
            ));

            return orderBook;
        }
Exemplo n.º 4
0
        private static async Task SendSubscriptionRequests(BitstampWebsocketClient client)
        {
            var orderBook = new SubscribeRequest("btcusd", Channel.OrderBook);
            //var orderBook = new SubscribeRequest("btcusd", ChannelType.Book);
            var orderBookDetail = new SubscribeRequest("btcusd", Channel.OrderBookDetail);


            var orderBookFull = new SubscribeRequest("btcusd", Channel.OrderBookDiff);

            var heartbeat = new SubscribeRequest("btcusd", Channel.Heartbeat);

            client.Send(orderBook).Wait();
            client.Send(orderBookDetail).Wait();

            client.Send(orderBookFull).Wait();
        }
        private static (ITradeSource, IWebsocketClient) GetBitstamp(string pair)
        {
            var url          = BitstampValues.ApiWebsocketUrl;
            var communicator = new BitstampWebsocketCommunicator(url)
            {
                Name = "Bitstamp"
            };
            var client = new BitstampWebsocketClient(communicator);

            var source = new BitstampTradeSource(client);

            communicator.ReconnectionHappened.Subscribe(x =>
            {
                client.Send(new Bitstamp.Client.Websocket.Requests.SubscribeRequest(
                                pair,
                                Channel.Ticker
                                ));
            });

            return(source, communicator);
        }
Exemplo n.º 6
0
        private static void SubscribeToStreams(BitstampWebsocketClient client)
        {
            client.Streams.ErrorStream.Subscribe(x =>
                                                 Log.Warning($"Error received, message: {x?.Message}"));

            client.Streams.SubscriptionSucceededStream.Subscribe(x =>
            {
                Log.Information($"SUCESS!");
                Log.Information($"{x?.Symbol} {x?.Channel} {x}");
            });

            client.Streams.UnsubscriptionSucceededStream.Subscribe(x =>
            {
                Log.Information($"Unsubscribed!");
                Log.Information($"{x?.Symbol} {x?.Channel} {x}");
            });

            /*
             * client.Streams.OrderBookStream.Subscribe(x =>
             * {
             *  Log.Information($"Order book");
             *  Log.Information($"{x.Symbol} {x.Channel} {x.Data?.Asks.FirstOrDefault()?.Price} {x.Data?.Asks.FirstOrDefault()?.Amount??0} {x.Data?.Asks.FirstOrDefault()?.Side}");
             *  Log.Information($"{x.Symbol} {x.Channel} {x.Data?.Bids.FirstOrDefault()?.Price} {x.Data?.Bids.FirstOrDefault()?.Amount ?? 0} {x.Data?.Bids.FirstOrDefault()?.Side}");
             * });*/

            client.Streams.OrdersStream.Subscribe(x =>
            {
                //Log.Information($"{x.Data} {x.Data.Asks[0]} {x.Data.EventBids[0]}");
                //Log.Information($"{x.Symbol} {x.Data.Channel} {x.Data.Amount}");
            });

            client.Streams.OrderBookDetailStream.Subscribe(x =>
            {
                Log.Information($"Order book detail snapshot");
                Log.Information($"{x.Symbol} {x.Channel} {x.Data?.Asks.FirstOrDefault()?.Price} {x.Data?.Asks.FirstOrDefault()?.Amount ?? 0} {x.Data?.Asks.FirstOrDefault()?.Side}");
                Log.Information($"{x.Symbol} {x.Channel} {x.Data?.Bids.FirstOrDefault()?.Price} {x.Data?.Bids.FirstOrDefault()?.Amount ?? 0} {x.Data?.Bids.FirstOrDefault()?.Side}");

                var unSubOrderBookDetail = new UnsubscribeRequest("btcusd", Channel.OrderBookDetail);
                client.Send(unSubOrderBookDetail).Wait();
            });

            client.Streams.OrderBookDiffStream.Subscribe(x =>
            {
                Log.Information($"Order book diff");
                Log.Information($"{x.Symbol} {x.Channel} {x.Data?.Asks.FirstOrDefault()?.Price} {x.Data?.Asks.FirstOrDefault()?.Amount ?? 0} {x.Data?.Asks.FirstOrDefault()?.Side}");
                Log.Information($"{x.Symbol} {x.Channel} {x.Data?.Bids.FirstOrDefault()?.Price} {x.Data?.Bids.FirstOrDefault()?.Amount ?? 0} {x.Data?.Bids.FirstOrDefault()?.Side}");
            });


            client.Streams.HeartbeatStream.Subscribe(x =>
                                                     Log.Information($"Heartbeat received, product: {x?.Channel}, seq: {x?.Event}"));

            /*
             * client.Streams.TickerStream.Subscribe(x =>
             *      Log.Information($"Ticker {x.ProductId}. Bid: {x.BestBid} Ask: {x.BestAsk} Last size: {x.LastSize}, Price: {x.Price}")
             *  );
             *
             * client.Streams.TradesStream.Subscribe(x =>
             * {
             *  Log.Information($"Trade executed [{x.ProductId}] {x.TradeSide} price: {x.Price} size: {x.Size}");
             * });
             *
             * client.Streams.OrderBookSnapshotStream.Subscribe(x =>
             * {
             *  Log.Information($"OB snapshot [{x.ProductId}] bids: {x.Bids.Length}, asks: {x.Asks.Length}");
             * });
             *
             * client.Streams.OrderBookUpdateStream.Subscribe(x =>
             * {
             *  Log.Information($"OB updates [{x.ProductId}] changes: {x.Changes.Length}");
             * });
             */

            // example of unsubscribe requests
            //Task.Run(async () =>
            //{
            //    await Task.Delay(5000);
            //    await client.Send(new BookSubscribeRequest("XBTUSD") {IsUnsubscribe = true});
            //    await Task.Delay(5000);
            //    await client.Send(new TradesSubscribeRequest() {IsUnsubscribe = true});
            //});
        }