Esempio n. 1
0
        private static async Task<ICryptoOrderBook> StartCoinbase(string pair, bool optimized, bool l2Optimized)
        {
            var url = CoinbaseValues.ApiWebsocketUrl;
            var communicator = new CoinbaseWebsocketCommunicator(url) { Name = "Coinbase" };
            var client = new CoinbaseWebsocketClient(communicator);

            var source = new CoinbaseOrderBookSource(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 SubscribeRequest(
                new[] { pair },
                ChannelSubscriptionType.Level2
            ));

            return orderBook;
        }
Esempio n. 2
0
        public async Task Heartbeat()
        {
            var url = CoinbaseValues.ApiWebsocketUrl;

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

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

                    await communicator.Start();

                    client.Send(new SubscribeRequest(new [] { "BTC-EUR" }, ChannelSubscriptionType.Heartbeat));

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

                    Assert.NotNull(received);
                }
            }
        }
Esempio n. 3
0
        public async Task AutoSnapshotReloading_ShouldWorkAfterTimeout()
        {
            var url = CoinbaseValues.ApiWebsocketUrl;

            using (var communicator = new CoinbaseWebsocketCommunicator(url))
            {
                using (var client = new CoinbaseWebsocketClient(communicator))
                {
                    var pair = "BTC-USD";

                    var source = new CoinbaseOrderBookSource(client)
                    {
                        LoadSnapshotEnabled = true
                    };
                    var orderBook = new CryptoOrderBook(pair, source)
                    {
                        SnapshotReloadTimeout = TimeSpan.FromSeconds(2),
                        SnapshotReloadEnabled = true
                    };

                    await Task.Delay(TimeSpan.FromSeconds(20));

                    Assert.True(orderBook.BidPrice > 0);
                    Assert.True(orderBook.AskPrice > 0);

                    Assert.NotEmpty(orderBook.BidLevels);
                    Assert.NotEmpty(orderBook.AskLevels);
                }
            }
        }
Esempio n. 4
0
        public async Task ConnectToSource_ShouldHandleOrderBookCorrectly()
        {
            var url = CoinbaseValues.ApiWebsocketUrl;

            using (var communicator = new CoinbaseWebsocketCommunicator(url))
            {
                using (var client = new CoinbaseWebsocketClient(communicator))
                {
                    var pair = "BTC-USD";

                    var source    = new CoinbaseOrderBookSource(client);
                    var orderBook = new CryptoOrderBook(pair, source);

                    await communicator.Start();

                    client.Send(new SubscribeRequest(
                                    new [] { pair },
                                    ChannelSubscriptionType.Level2
                                    ));

                    await Task.Delay(TimeSpan.FromSeconds(5));

                    Assert.True(orderBook.BidPrice > 0);
                    Assert.True(orderBook.AskPrice > 0);

                    Assert.NotEmpty(orderBook.BidLevels);
                    Assert.NotEmpty(orderBook.AskLevels);
                }
            }
        }
        static void Main(string[] args)
        {
            InitLogging();

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

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

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


            var url = CoinbaseValues.ApiWebsocketUrl;

            using (var communicator = new CoinbaseWebsocketCommunicator(url))
            {
                communicator.Name             = "Coinbase-1";
                communicator.ReconnectTimeout = TimeSpan.FromMinutes(1);

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

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

                        // Authenticated subscription
                        // Fails without valid api key, secret and passphrase
                        //await SendSubscriptionRequestsAuthenticated(client);
                    });

                    communicator.Start().Wait();

                    ExitEvent.WaitOne();
                }
            }

            Log.Debug("====================================");
            Log.Debug("              STOPPING              ");
            Log.Debug("====================================");
            Log.CloseAndFlush();
        }
        private static (ITradeSource, IWebsocketClient) GetCoinbase(string pair)
        {
            var url          = CoinbaseValues.ApiWebsocketUrl;
            var communicator = new CoinbaseWebsocketCommunicator(url)
            {
                Name = "Coinbase"
            };
            var client = new CoinbaseWebsocketClient(communicator);

            var source = new CoinbaseTradeSource(client);

            communicator.ReconnectionHappened.Subscribe(x =>
            {
                client.Send(new SubscribeRequest(
                                new[] { pair },
                                ChannelSubscriptionType.Matches
                                ));
            });

            return(source, communicator);
        }
Esempio n. 7
0
        public async Task OnStarting_ShouldGetInfoResponse()
        {
            var url = CoinbaseValues.ApiWebsocketUrl;

            using (var communicator = new CoinbaseWebsocketCommunicator(url))
            {
                string received      = null;
                var    receivedEvent = new ManualResetEvent(false);

                communicator.MessageReceived.Subscribe(msg =>
                {
                    received = msg.Text;
                    receivedEvent.Set();
                });

                await communicator.Start();

                communicator.Send("invalid test request");

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

                Assert.NotNull(received);
            }
        }