예제 #1
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;
        }
        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);
                }
            }
        }
        /// <summary>
        /// Change client and resubscribe to the new streams
        /// </summary>
        public void ChangeClient(BitstampWebsocketClient client)
        {
            CryptoValidations.ValidateInput(client, nameof(client));

            _client = client;
            _subscription?.Dispose();
            Subscribe();
        }
예제 #4
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));
        }
예제 #5
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();
        }
예제 #6
0
        private static async Task Main(string[] args)
        {
            InitLogging();

            AppDomain.CurrentDomain.ProcessExit   += CurrentDomainOnProcessExit;
            AssemblyLoadContext.Default.Unloading += DefaultOnUnloading;
            Console.CancelKeyPress += ConsoleOnCancelKeyPress;

            Console.WriteLine("|=======================|");
            Console.WriteLine("|    COINBASE CLIENT    |");
            Console.WriteLine("|=======================|");
            Console.WriteLine();

            Log.Debug("====================================");
            Log.Debug("              STARTING              ");
            Log.Debug("====================================");


            var url = BitstampValues.ApiWebsocketUrl;

            using (var communicator = new BitstampWebsocketCommunicator(url))
            {
                communicator.Name = "Bitstamp-1";
                //communicator.ReconnectTimeoutMs = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;

                using (var client = new BitstampWebsocketClient(communicator))
                {
                    SubscribeToStreams(client);

                    communicator.ReconnectionHappened.Subscribe(async type =>
                    {
                        Log.Information($"Reconnection happened, type: {type}, resubscribing..");
                        await SendSubscriptionRequests(client);
                    });

                    await communicator.Start();

                    ExitEvent.WaitOne();
                }
            }

            Log.Debug("====================================");
            Log.Debug("              STOPPING              ");
            Log.Debug("====================================");
            Log.CloseAndFlush();
        }
        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);
        }
예제 #8
0
 /// <inheritdoc />
 public BitstampOrderBookSource(BitstampWebsocketClient client)
 {
     ChangeClient(client);
 }
예제 #9
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($"Subscribed to {x?.Symbol} {x?.Channel}");
            });

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


            client.Streams.OrderBookStream.Subscribe(x =>
            {
                Log.Information($"Order book L2 [{x.Symbol}]");
                Log.Information($"    {x.Data?.Asks.FirstOrDefault()?.Price} " +
                                $"{x.Data?.Asks.FirstOrDefault()?.Amount ?? 0} " +
                                $"{x.Data?.Asks.FirstOrDefault()?.Side} " +
                                $"({x.Data?.Asks?.Length})");
                Log.Information($"    {x.Data?.Bids.FirstOrDefault()?.Price} " +
                                $"{x.Data?.Bids.FirstOrDefault()?.Amount ?? 0} " +
                                $"{x.Data?.Bids.FirstOrDefault()?.Side} " +
                                $"({x.Data?.Bids?.Length})");
            });

            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 L3 [{x.Symbol}]");
                Log.Information($"    {x.Data?.Asks.FirstOrDefault()?.Price} " +
                                $"{x.Data?.Asks.FirstOrDefault()?.Amount ?? 0} " +
                                $"{x.Data?.Asks.FirstOrDefault()?.Side} " +
                                $"({x.Data?.Asks?.Length}) " +
                                $"id: {x.Data?.Asks?.FirstOrDefault()?.OrderId}");
                Log.Information($"    {x.Data?.Bids.FirstOrDefault()?.Price} " +
                                $"{x.Data?.Bids.FirstOrDefault()?.Amount ?? 0} " +
                                $"{x.Data?.Bids.FirstOrDefault()?.Side} " +
                                $"({x.Data?.Bids?.Length}) " +
                                $"id: {x.Data?.Bids?.FirstOrDefault()?.OrderId}");
            });

            client.Streams.OrderBookDiffStream.Subscribe(x =>
            {
                Log.Information($"Order book L2 diffs [{x.Symbol}]");
                Log.Information($"    updates {x.Data?.Asks.Count(y => y.Amount > 0)} " +
                                $"deletes {x.Data?.Asks.Count(y => y.Amount <= 0)}  " +
                                $"{x.Data?.Asks.FirstOrDefault()?.Side} " +
                                $"({x.Data?.Asks?.Length}) ");
                Log.Information($"    updates {x.Data?.Bids.Count(y => y.Amount > 0)} " +
                                $"deletes {x.Data?.Bids.Count(y => y.Amount <= 0)}  " +
                                $"{x.Data?.Bids.FirstOrDefault()?.Side} " +
                                $"({x.Data?.Bids?.Length}) ");
            });


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

            client.Streams.TickerStream.Subscribe(x =>
            {
                Log.Information($"Trade executed [{x.Symbol}] {x.Data?.Side} price: {x.Data?.Price} size: {x.Data?.Amount}");
            });
        }
 /// <inheritdoc />
 public BitstampTradeSource(BitstampWebsocketClient client)
 {
     ChangeClient(client);
 }
예제 #11
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});
            //});
        }