Ejemplo n.º 1
0
        private async Task GetInitialTickPrices()
        {
            foreach (var instrument in Instruments)
            {
                try
                {
                    var orderBook =
                        await apiClient.MakeGetRequestAsync <List <OrderBook> >(
                            $"{Config.EndpointUrl}/api/OrderBooks/{instrument.Name}", ctSource.Token);

                    var tickPrices = orderBook.GroupBy(x => x.AssetPair)
                                     .Select(g => new TickPrice(
                                                 new Instrument(Name, g.Key),
                                                 g.FirstOrDefault()?.Timestamp ?? DateTime.UtcNow,
                                                 g.FirstOrDefault(ob => !ob.IsBuy)?.Prices.Select(x => x.Price).DefaultIfEmpty(0).Min() ?? 0,
                                                 g.FirstOrDefault(ob => ob.IsBuy)?.Prices.Select(x => x.Price).DefaultIfEmpty(0).Max() ?? 0)
                                             )
                                     .Where(x => x.Ask > 0 && x.Bid > 0);

                    foreach (var tickPrice in tickPrices)
                    {
                        _lastAsks[instrument.Name] = tickPrice.Ask;
                        _lastBids[instrument.Name] = tickPrice.Bid;

                        await _tickPriceHandler.Handle(tickPrice);
                    }
                }
                catch (Exception e)
                {
                    await LykkeLog.WriteErrorAsync(nameof(LykkeExchange), nameof(GetInitialTickPrices), e);
                }
            }
        }
Ejemplo n.º 2
0
        private async Task LogAsync(Exception ex, [CallerMemberName] string context = null)
        {
            if (LykkeLog == null)
            {
                return;
            }

            await LykkeLog.WriteErrorAsync(_gdaxExchangeTypeName, _gdaxExchangeTypeName, context, ex);
        }
Ejemplo n.º 3
0
        private async Task <TResult> ExecuteApiMethod <TResult>(Func <CancellationToken, Task <TResult> > method, CancellationToken token)
        {
            try
            {
                var response = await method(token);

                return(response);
            }
            catch (ApiException ex)
            {
                await LykkeLog.WriteErrorAsync(nameof(BitfinexExchange), method.Method.Name, ex);

                throw;
            }
            catch (Exception ex)
            {
                await LykkeLog.WriteErrorAsync(nameof(BitfinexExchange), method.Method.Name, ex);

                throw new ApiException(ex.Message, HttpStatusCode.InternalServerError);
            }
        }
Ejemplo n.º 4
0
        protected override void StartImpl()
        {
            ctSource = new CancellationTokenSource();

            CheckServerTime(ctSource.Token)
            .ContinueWith(task =>
            {
                if (!task.IsFaulted && task.Result)
                {
                    OnConnected();
                }
            });

            if (config.PubQuotesToRabbit)
            {
                pricesJob = Task.Run(async() =>
                {
                    var lasts = Instruments.ToDictionary(x => x.Name, x => 0L);

                    while (!ctSource.IsCancellationRequested)
                    {
                        try
                        {
                            foreach (var pair in config.SupportedCurrencySymbols)
                            {
                                SpreadDataResult result;

                                try
                                {
                                    result = await publicData.GetSpread(ctSource.Token, pair.ExchangeSymbol, lasts[pair.LykkeSymbol]);
                                }
                                catch (Exception e)
                                {
                                    await LykkeLog.WriteErrorAsync(
                                        nameof(Kraken),
                                        nameof(KrakenExchange),
                                        nameof(pricesJob),
                                        e);
                                    continue;
                                }

                                lasts[pair.LykkeSymbol] = result.Last;
                                var prices = result.Data.Single().Value.Select(x =>
                                                                               new TickPrice(Instruments.Single(i => i.Name == pair.LykkeSymbol),
                                                                                             x.Time, x.Ask, x.Bid)).ToArray();

                                if (prices.Any())
                                {
                                    if (prices.Length == 1 && prices[0].Time == DateTimeUtils.FromUnix(lasts[pair.LykkeSymbol]))
                                    {
                                        // If there is only one price and it has timestamp of last one, ignore it.
                                    }
                                    else
                                    {
                                        foreach (var tickPrice in prices)
                                        {
                                            await _tickPriceHandler.Handle(tickPrice);
                                        }
                                    }
                                }

                                await Task.Delay(TimeSpan.FromSeconds(10), ctSource.Token);
                            }

                            await CheckExecutedOrders();
                        }
                        catch (Exception e)
                        {
                            await LykkeLog.WriteErrorAsync(
                                nameof(Kraken),
                                nameof(KrakenExchange),
                                nameof(pricesJob),
                                e);
                        }
                    }

                    OnStopped();
                });
            }
        }