private IReadOnlyCollection <AssetPrice> GetPrices(int step) { var result = new List <AssetPrice>(); var steps = IndicesValuesAndPricesForEachStep.Split("\r\n").ToList(); var assets = steps.First().Split("\t").Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToList(); var prices = steps.TakeLast(steps.Count - 1).ToList()[step].Split("\t").Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToList(); for (var i = 1; i < prices.Count; i++) { var newAssetPrice = new AssetPrice { Asset = assets[i], CrossAsset = "USD", Source = "fakeExchange", Price = decimal.Parse(prices[i]) }; result.Add(newAssetPrice); } return(result); }
private void AddOrUpdateAssetPrice(AssetPrice assetPrice) { var asset = assetPrice.Asset; // asset not existed if (!_assetsPricesCache.ContainsKey(asset)) { var newAssetPrices = new List <AssetPrice> { assetPrice }; _assetsPricesCache[asset] = newAssetPrices; } // asset existed else { List <AssetPrice> assetPrices = _assetsPricesCache[asset]; // Replace by asset, crossAsset and source Predicate <AssetPrice> existedAssetPrices = x => x.Asset == assetPrice.Asset && x.CrossAsset == assetPrice.CrossAsset && x.Source == assetPrice.Source; assetPrices.RemoveAll(existedAssetPrices); assetPrices.Add(assetPrice); } }
public async Task HandleAsync(TickPrice tickPrice) { if (tickPrice.MiddlePrice == null) { return; } // xxx/usd bool shallBeIncluded = tickPrice.AssetPair.EndsWith(Usd); AssetPrice assetPrice = new AssetPrice(); if (shallBeIncluded) { assetPrice.Asset = tickPrice.AssetPair.Replace(Usd, string.Empty); assetPrice.CrossAsset = Usd; assetPrice.Source = tickPrice.Source; assetPrice.Price = tickPrice.MiddlePrice.Value; } // xxx/cross & cross/usd if (!shallBeIncluded) { IReadOnlyList <string> crossAssets = (await _settingsService.GetAsync()).CrossAssets; foreach (string cross in crossAssets) { // if there no cross/usd yet then skip lock (_sync) if (!_assetsTickPricesCache.ContainsKey(cross)) { continue; } // xxx/cross if (!tickPrice.AssetPair.EndsWith(cross)) { continue; } assetPrice.Asset = tickPrice.AssetPair.Replace(cross, string.Empty); assetPrice.CrossAsset = cross; assetPrice.Source = tickPrice.Source; var settings = await _settingsService.GetAsync(); lock (_sync) { var allAssetPrices = GetAssetPrices(settings.Sources); if (!allAssetPrices.ContainsKey(cross)) { continue; } var prices = allAssetPrices[cross]; if (prices == null || !prices.Any()) { continue; } var crossMiddlePrice = Utils.GetMiddlePrice(cross, prices); decimal?crossAsk = tickPrice.Ask * crossMiddlePrice; decimal?crossBid = tickPrice.Bid * crossMiddlePrice; var crossTickPrice = new TickPrice(string.Empty, string.Empty, crossBid, crossAsk, DateTime.UtcNow); decimal?middlePrice = crossTickPrice.MiddlePrice; if (middlePrice == null) { continue; } // calculated cross middle price assetPrice.Price = middlePrice.Value; shallBeIncluded = true; break; } } } if (!shallBeIncluded) { return; } lock (_sync) { AddOrUpdateTickPrice(assetPrice.Asset, tickPrice); AddOrUpdateAssetPrice(assetPrice); } }
public void DoInit(MonoBehaviour pBehaviourOwner, ASSET_TYPE eAssetType, int iAmount) { _pBehaviourOwner = pBehaviourOwner; sAssetPrice = new AssetPrice(eAssetType, iAmount); }