Esempio n. 1
0
        public async Task SubscribeOrderBook(string symbol, int limit, Action <OrderBookEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            var kucoinClient = new KucoinSocketClient();

            CallResult <UpdateSubscription> result = null;

            try
            {
                result = await kucoinClient.SubscribeToAggregatedOrderBookUpdatesAsync(symbol, async data =>
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);
                        kucoinClient.Dispose();
                        return;
                    }

                    var orderBook = new OrderBook
                    {
                        Symbol        = data.Symbol,
                        Exchange      = Exchange.Kucoin,
                        FirstUpdateId = data.SequenceStart,
                        LastUpdateId  = data.SequenceEnd
                    };

                    orderBook.Asks = (from ask in data.Changes.Asks select new OrderBookPriceLevel {
                        Id = ask.Sequence, Price = ask.Price, Quantity = ask.Quantity
                    }).ToList();
                    orderBook.Bids = (from bid in data.Changes.Bids select new OrderBookPriceLevel {
                        Id = bid.Sequence, Price = bid.Price, Quantity = bid.Quantity
                    }).ToList();

                    try
                    {
                        callback.Invoke(new OrderBookEventArgs {
                            OrderBook = orderBook
                        });
                    }
                    catch (Exception ex)
                    {
                        await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);
                        exception.Invoke(ex);
                        kucoinClient.Dispose();
                        return;
                    }
                }).ConfigureAwait(false);
            }
            catch (Exception)
            {
                if (result != null)
                {
                    await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);

                    kucoinClient.Dispose();
                }

                throw;
            }
        }
Esempio n. 2
0
        public async Task SubscribeStatistics(IEnumerable <string> symbols, Action <StatisticsEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            var kucoinSocketClient = new KucoinSocketClient();

            CallResult <UpdateSubscription> result = null;

            try
            {
                var kucoinClient = new KucoinClient();

                foreach (var symbol in symbols)
                {
                    result = await kucoinSocketClient.SubscribeToSnapshotUpdatesAsync(symbol, async data =>
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            await kucoinSocketClient.Unsubscribe(result.Data).ConfigureAwait(false);
                            return;
                        }

                        try
                        {
                            var symbolStats = new SymbolStats
                            {
                                Symbol             = data.Symbol,
                                Exchange           = Exchange.Kucoin,
                                CloseTime          = data.Timestamp,
                                Volume             = data.Volume,
                                LowPrice           = data.Low,
                                HighPrice          = data.High,
                                LastPrice          = data.LastPrice,
                                PriceChange        = data.ChangePrice,
                                PriceChangePercent = data.ChangePercentage * 100
                            };

                            callback.Invoke(new StatisticsEventArgs {
                                Statistics = new[] { symbolStats }
                            });
                        }
                        catch (Exception ex)
                        {
                            await kucoinSocketClient.Unsubscribe(result.Data).ConfigureAwait(false);
                            exception.Invoke(ex);
                            return;
                        }
                    });
                }
            }
            catch (Exception)
            {
                if (result != null)
                {
                    await kucoinSocketClient.Unsubscribe(result.Data).ConfigureAwait(false);
                }

                throw;
            }
        }
Esempio n. 3
0
        public async Task SubscribeAccountInfo(User user, Action <AccountInfoEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var localUser = user;

            var kucoinClient = new KucoinSocketClient(new KucoinSocketClientOptions {
                ApiCredentials = new KucoinApiCredentials(user.ApiKey, user.ApiSecret, user.ApiPassPhrase)
            });

            CallResult <UpdateSubscription> result = null;

            try
            {
                result = await kucoinClient.SubscribeToBalanceChangesAsync(async data =>
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);
                        kucoinClient.Dispose();
                        return;
                    }

                    var accountInfo = await GetAccountInfoAsync(localUser, cancellationToken).ConfigureAwait(false);

                    try
                    {
                        callback.Invoke(new AccountInfoEventArgs {
                            AccountInfo = accountInfo
                        });
                    }
                    catch (Exception ex)
                    {
                        await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);
                        exception.Invoke(ex);
                        kucoinClient.Dispose();
                        return;
                    }
                }).ConfigureAwait(false);
            }
            catch (Exception)
            {
                if (result != null)
                {
                    await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);

                    kucoinClient.Dispose();
                }

                throw;
            }
        }
Esempio n. 4
0
        public async Task SubscribeTrades(string symbol, int limit, Action <TradeEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            var kucoinClient = new KucoinSocketClient();

            CallResult <UpdateSubscription> result = null;

            try
            {
                bool initialising = true;

                result = await kucoinClient.SubscribeToTradeUpdatesAsync(symbol, async data =>
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);
                        kucoinClient.Dispose();
                        return;
                    }

                    try
                    {
                        if (initialising)
                        {
                            var initialiTrades = await GetTradesAsync(symbol, limit, cancellationToken).ConfigureAwait(false);
                            callback.Invoke(new TradeEventArgs {
                                Trades = initialiTrades
                            });
                            initialising = false;
                        }
                        else
                        {
                            var trade = new Trade
                            {
                                Id       = data.Sequence,
                                Exchange = Exchange.Kucoin,
                                Symbol   = data.Symbol,
                                Price    = data.Price,
                                Time     = data.Timestamp,
                                Quantity = data.Quantity
                            };

                            callback.Invoke(new TradeEventArgs {
                                Trades = new[] { trade }
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);
                        kucoinClient.Dispose();
                        exception.Invoke(ex);
                        return;
                    }
                }).ConfigureAwait(false);
            }
            catch (Exception)
            {
                if (result != null)
                {
                    await kucoinClient.Unsubscribe(result.Data).ConfigureAwait(false);

                    kucoinClient.Dispose();
                }

                throw;
            }
        }