Beispiel #1
0
        public async Task ConnectAndDisconnect()
        {
            var cancellationToken = new CancellationTokenSource().Token;
            await _api.ConnectAsync(cancellationToken);

            await _api.CloseConnectionAsync(cancellationToken);
        }
Beispiel #2
0
        protected override async Task MessageLoopImpl()
        {
            try
            {
                await _websocketApi.ConnectAsync(CancellationToken);

                // First subscribe with websockets and ignore all the order events with
                // sequential number less than symbol's orderbook orders
                var subscriptionTask = new AsyncEventAwaiter <string>(ref _websocketApi.Subscribed)
                                       .Await(CancellationToken);
                var ordersListenerTask = _websocketApi.SubscribeToOrderBookUpdatesAsync(
                    _configuration.SupportedCurrencySymbols.Select(s => s.ExchangeSymbol).ToList(),
                    CancellationToken);

                await subscriptionTask; // Wait to be subscribed first
                var   retrieveOrderBooksTask = Task.Run(async() =>
                {
                    foreach (var currencySymbol in _configuration.SupportedCurrencySymbols)
                    {
                        var orderBook = await _restApi.GetFullOrderBook(
                            currencySymbol.ExchangeSymbol, CancellationToken);
                        await HandleRetrievedOrderBook(currencySymbol.ExchangeSymbol, orderBook);
                    }
                });

                await Task.WhenAll(ordersListenerTask, retrieveOrderBooksTask);
            }
            finally
            {
                try
                {
                    if (_websocketApi != null)
                    {
                        using (var cts = new CancellationTokenSource(5000))
                        {
                            await _websocketApi.CloseConnectionAsync(cts.Token);
                        }
                    }
                }
                catch (Exception ex)
                {
                    await Log.WriteErrorAsync(nameof(GdaxOrderBooksHarvester),
                                              "Could not close web sockets connection properly", ex);
                }
            }
        }
Beispiel #3
0
        public async Task SubscribeAndHandleAnonymousTickerEvent()
        {
            _api = new GdaxWebSocketApi(_logger, string.Empty, string.Empty,
                                        string.Empty, _configuration.WssEndpointUrl);
            var cancellationToken = new CancellationTokenSource().Token;

            var tcsTicker = new TaskCompletionSource <GdaxWssTicker>();

            _api.Ticker += (sender, ticker) => {
                tcsTicker.SetResult(ticker);
                return(tcsTicker.Task);
            };

            // Connect and subscribe to web socket events
            await _api.ConnectAsync(cancellationToken);

            try
            {
                // Subscribe
                var subscribed = await SubscribeToOrderBookUpdatesAsync(10000, cancellationToken);

                Assert.True(subscribed);

                // Wait maximum n seconds the received and done events to be received
                await WhenAllTaskAreDone(10000, tcsTicker.Task);
            }
            finally
            {
                await _api.CloseConnectionAsync(cancellationToken);
            }

            // Check if events were received successfuly
            // Ticker event
            Assert.NotNull(tcsTicker.Task);
            Assert.True(tcsTicker.Task.IsCompletedSuccessfully);
            var tick = tcsTicker.Task.Result;

            Assert.Equal(_btcUsd, tick.ProductId);
        }
Beispiel #4
0
        protected override async void StartImpl()
        {
            _webSocketCtSource = new CancellationTokenSource();


            try
            {
                _orderBooksHarvester.Start();

                OnConnected();

                await _websocketApi.ConnectAsync(_webSocketCtSource.Token);


                await _websocketApi.SubscribeToPrivateUpdatesAsync(Instruments.Select(i => i.Name).ToList(),
                                                                   _webSocketCtSource.Token);
            }
            catch (Exception ex)
            {
                await LogAsync(ex);
            }
        }