public Task <IEnumerable <AssetPair> > GetAllEnabledAsync()
 {
     return(Policy
            .Handle <Exception>()
            .WaitAndRetryForeverAsync(
                retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                (exception, timespan) => _log.Error(exception))
            .ExecuteAsync(async() => (await _apiService.GetAllAssetPairsAsync()).Where(a => !a.IsDisabled)));
 }
Beispiel #2
0
        public async Task <Dictionary <string, List <string> > > GetPopularPairsAsync(List <string> assetIds)
        {
            var result     = new Dictionary <string, List <string> >();
            var assets     = new[] { "USD", "EUR", "CHF", "BTC" };
            var assetPairs = (await _assetsServiceWithCache.GetAllAssetPairsAsync()).Where(x => !x.IsDisabled).ToList();

            foreach (var assetId in assetIds)
            {
                var pairs = new List <string>();

                foreach (var asset in assets)
                {
                    if (asset == assetId)
                    {
                        continue;
                    }

                    var assetPair = assetPairs.FirstOrDefault(x =>
                                                              x.BaseAssetId == assetId && x.QuotingAssetId == asset ||
                                                              x.BaseAssetId == asset && x.QuotingAssetId == assetId);

                    if (assetPair != null)
                    {
                        pairs.Add(assetPair.Id);
                    }
                }

                result.Add(assetId, pairs);
            }

            return(result);
        }
        public async Task InitializeAsync(string walletId, string assetId, double amount)
        {
            if (Math.Abs(amount) <= Double.Epsilon)
            {
                return;
            }

            IReadOnlyCollection <AssetPair> assetPairs = await _assetsServiceWithCache.GetAllAssetPairsAsync();

            AssetPair assetPair = assetPairs
                                  .SingleOrDefault(o => o.BaseAssetId == assetId && o.QuotingAssetId == QuoteAssetId);

            if (assetPair == null)
            {
                assetPair = assetPairs
                            .SingleOrDefault(o => o.BaseAssetId == QuoteAssetId && o.QuotingAssetId == assetId);
            }

            if (assetPair == null)
            {
                throw new InvalidOperationException($"Asset pair found by asset '{assetId}' and '{QuoteAssetId}'");
            }

            Quote quote = await _quoteService.GetAsync(assetId, QuoteAssetId);

            var tradeData = new TradeData
            {
                Id                   = Guid.Empty.ToString("D"),
                Exchange             = ExchangeNames.Lykke,
                AssetPair            = assetPair.Id,
                BaseAsset            = assetId,
                QuoteAsset           = QuoteAssetId,
                Price                = quote.Mid,
                Volume               = (decimal)Math.Abs(amount),
                Type                 = amount < 0 ? TradeType.Sell : TradeType.Buy,
                Time                 = DateTime.UtcNow,
                LimitOrderId         = Guid.Empty.ToString("D"),
                OppositeClientId     = null,
                OppositeLimitOrderId = null
            };

            AssetRealisedPnL assetRealisedPnL = await CalculateAsync(tradeData, walletId, assetId);

            await _assetRealisedPnLRepository.InsertAsync(assetRealisedPnL);

            _log.InfoWithDetails("Realised PnL initialized", new { walletId, assetId, amount });

            _cache.Set(assetRealisedPnL);
        }
Beispiel #4
0
        public async Task <IActionResult> Get(string sessionId, int?cacheTimeout)
        {
            List <AssertPairRate> cacheEntry;

            Guid sId;

            if (!Guid.TryParse(sessionId, out sId))
            {
                sId = Guid.NewGuid();
            }

            if (!_cache.TryGetValue(sId, out cacheEntry))
            {
                var pairsList = await _lykkeMarketProfile.ApiMarketProfileGetAsync();

                var dicPairsList = await _assetsServiceWithCache.GetAllAssetPairsAsync();

                cacheEntry =
                    new List <AssertPairRate>(from rates in (from apr in pairsList

                                                             select new AssertPairRate
                {
                    AssetPair = apr.AssetPair,
                    Bid = (float)apr.BidPrice,
                    Ask = (float)apr.AskPrice
                })
                                              join dic in dicPairsList on rates.AssetPair equals dic.Id
                                              select new AssertPairRate
                {
                    AssetPair = rates.AssetPair,
                    Bid       = rates.Bid,
                    Ask       = rates.Ask,
                    Accuracy  = dic.Accuracy
                }
                                              );



                await StoreAsserts(cacheEntry);


                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetAbsoluteExpiration(TimeSpan.FromMinutes((cacheTimeout ?? 0) == 0 ? _settings.CacheTimeout : cacheTimeout.Value));

                _cache.Set(CacheKeys.Rates, cacheEntry, cacheEntryOptions);
            }

            return(Json(new { SessionId = sId, Asserts = cacheEntry }));
        }
Beispiel #5
0
        public async Task <MemoryStream> MakeAsync(IEnumerable <HistoryModel> operations)
        {
            var assets     = (await _assetsServiceWithCache.GetAllAssetsAsync(true)).ToDictionary(x => x.Id);
            var assetPairs = (await _assetsServiceWithCache.GetAllAssetPairsAsync()).ToDictionary(x => x.Id);

            using (var stream = new MemoryStream())
            {
                using (var streamWriter = new StreamWriter(stream)
                {
                    AutoFlush = true
                })
                {
                    var userCsv = new CsvWriter(streamWriter);
                    userCsv.Configuration.RegisterClassMap <HistoryOperationCsvEntryMap>();

                    userCsv.WriteRecords(operations.Select(x =>
                    {
                        var assetPair      = x.AssetPair != null && assetPairs.ContainsKey(x.AssetPair) ? assetPairs[x.AssetPair] : null;
                        var baseAsset      = x.Asset ?? assetPair?.BaseAssetId;
                        var quoteAsset     = assetPair?.QuotingAssetId;
                        var baseAssetName  = baseAsset != null && assets.ContainsKey(baseAsset) ? (assets[baseAsset].DisplayId ?? baseAsset) : baseAsset;
                        var quoteAssetName = quoteAsset != null && assets.ContainsKey(quoteAsset) ? (assets[quoteAsset].DisplayId ?? quoteAsset) : quoteAsset;

                        return(new HistoryOperationCsvEntry
                        {
                            Date = x.DateTime,
                            Type = x.Type.ToString(),
                            Exchange = "Lykke",
                            BaseAmount = x.Amount,
                            BaseCurrency = baseAssetName,
                            QuoteAmount = x.OppositeAmount,
                            QuoteCurrency = quoteAssetName,
                            FeeCurrency =
                                x.FeeSize != 0
                                    ? (x.FeeAssetId != null && assets.ContainsKey(x.FeeAssetId) ? (assets[x.FeeAssetId].DisplayId ?? x.FeeAssetId) : x.Asset)
                                    : null,
                            Fee = x.FeeSize
                        });
                    }));

                    stream.Seek(0, SeekOrigin.Begin);

                    stream.Position = 0;

                    return(stream);
                }
            }
        }
Beispiel #6
0
        public async Task <Quote> GetAsync(string baseAssetId, string quoteAssetId)
        {
            string directAssetPairId = $"{baseAssetId}{quoteAssetId}";

            if (baseAssetId == quoteAssetId)
            {
                return(new Quote(directAssetPairId, DateTime.UtcNow, 1, 1));
            }

            IReadOnlyCollection <AssetPair> assetPairs = await _assetsServiceWithCache.GetAllAssetPairsAsync();

            bool inverted = false;

            AssetPair assetPair = assetPairs
                                  .SingleOrDefault(o => o.BaseAssetId == baseAssetId && o.QuotingAssetId == quoteAssetId);

            if (assetPair == null)
            {
                inverted = true;

                assetPair = assetPairs
                            .SingleOrDefault(o => o.BaseAssetId == quoteAssetId && o.QuotingAssetId == baseAssetId);
            }

            if (assetPair == null)
            {
                throw new InvalidOperationException($"Asset pair does not exist for '{baseAssetId}'/'{quoteAssetId}'");
            }

            MarketProfile marketProfile = await GetMarketProfileAsync();

            FeedData feedData = marketProfile.Profile.FirstOrDefault(o => o.Asset == assetPair.Id);

            if (feedData == null)
            {
                throw new InvalidOperationException($"No quote for asset pair '{assetPair.Id}'");
            }

            if (inverted)
            {
                return(new Quote(assetPair.Id, feedData.DateTime, 1 / (decimal)feedData.Ask,
                                 1 / (decimal)feedData.Bid));
            }

            return(new Quote(assetPair.Id, feedData.DateTime, (decimal)feedData.Ask,
                             (decimal)feedData.Bid));
        }
Beispiel #7
0
 public async Task <IEnumerable <AssetPair> > GetAllAssetPairs()
 {
     return((await _assetsService.GetAllAssetPairsAsync()).Select(a =>
                                                                  new AssetPair(a.Id, a.Name, a.BaseAssetId, a.QuotingAssetId, a.Accuracy)));
 }
Beispiel #8
0
 public Task <IReadOnlyCollection <AssetPair> > GetAllAssetPairsAsync()
 {
     return(_assetsServiceWithCache.GetAllAssetPairsAsync());
 }