Exemplo n.º 1
0
        /// <summary>
        /// Gets updates for a list of assets.
        /// Fires the OnSingleAssetUpdated event for each updated asset from the list.
        /// Fires the OnApiError event if something has gone wrong.
        /// </summary>
        /// <param name="assets">The assets<see cref="List{Asset}"/> to update.</param>
        protected override async Task GetAssetUpdatesAsync(List <Asset> assets)
        {
            if (assets.Count < 1)
            {
                return;
            }

            List <string> fromIds = new List <string>();

            assets.ForEach(ass => fromIds.Add(ass.AssetId));

            bool callFailed = false;
            int  timeout    = this.ApiData.UpdateInterval * 1000;

            do
            {
                callFailed = false;

                try
                {
                    Price response = await this.client.SimpleClient.GetSimplePrice(fromIds.ToArray(), this.attachedConvertCurrencies.ToArray(), true, true, true, false);

                    this.ApiData.IncreaseCounter(1);
                    this.FireOnAppDataChanged();



                    assets.ForEach(ass =>
                    {
                        try
                        {
                            var assres  = response[ass.AssetId];
                            var convert = ass.ConvertCurrency.ToLower();

                            if (assres.ContainsKey(ass.ConvertCurrency.ToLower()))
                            {
                                ass.Price            = (double)assres[convert];
                                ass.LastUpdated      = DateTime.Now;
                                ass.PercentChange24h = (double)assres[convert + "_24h_change"];
                                ass.PercentChange7d  = -101;
                                ass.PercentChange1h  = -101;
                                ass.MarketCap        = (double)assres[convert + "_market_cap"];
                                ass.Volume24hConvert = assres[convert + "_24h_vol"].ToString();
                                ass.SupplyAvailable  = -1;
                                ass.SupplyTotal      = -1;
                                this.FireOnAssetUpdateReceived(ass);
                            }
                        }
                        catch (KeyNotFoundException)
                        {
                            // ignore
                        }
                    });
                }
                catch (HttpRequestException)
                {
                    // no internet connection?
                    callFailed = true;
                    Thread.Sleep(retryDelay);
                    timeout -= retryDelay;
                }
                catch (Exception e)
                {
                    OnApiErrorEventArgs args = new OnApiErrorEventArgs
                    {
                        ErrorType    = ErrorType.General,
                        ErrorMessage = e.Message
                    };

                    this.FireOnApiError(args);
                }
            }while (callFailed && timeout >= 0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets updates for a list of assets.
        /// Fires the OnSingleAssetUpdated event for each updated asset from the list.
        /// Fires the OnApiError event if something has gone wrong.
        /// </summary>
        /// <param name="assets">The assets<see cref="List{Asset}"/> to update.</param>
        protected override async Task GetAssetUpdatesAsync(List <Asset> assets)
        {
            if (assets.Count < 1)
            {
                return;
            }

            string[] symbols = new string[assets.Count];
            int      timeout = this.ApiData.UpdateInterval * 1000;

            for (int i = 0; i < assets.Count; i++)
            {
                symbols[i] = assets[i].Symbol;
            }

            bool callFailed;

            do
            {
                callFailed = false;

                try
                {
                    var response = await Yahoo.Symbols(symbols).Fields(Field.Symbol, Field.RegularMarketPrice, Field.Currency).QueryAsync();

                    this.ApiData.IncreaseCounter(1);
                    this.FireOnAppDataChanged();

                    foreach (Asset asset in assets)
                    {
                        if (asset.ConvertCurrency == "EUR" && this.eurExchangeRate == 0)
                        {
                            callFailed = true;
                            break;
                        }

                        try
                        {
                            var assetResponse = response[asset.Symbol];

                            if (asset.Symbol == "EUR=X")
                            {
                                this.eurExchangeRate = assetResponse.RegularMarketPrice;
                                continue;
                            }

                            Asset updatedAsset = null;

                            if (assetResponse.Currency == "USD")
                            {
                                updatedAsset = new Asset()
                                {
                                    AssetId          = assetResponse.Symbol,
                                    Symbol           = assetResponse.Symbol,
                                    Name             = asset.Name,
                                    ConvertCurrency  = asset.ConvertCurrency,
                                    LastUpdated      = DateTime.Now,
                                    Price            = asset.ConvertCurrency == "USD" ? assetResponse.RegularMarketPrice : assetResponse.RegularMarketPrice * this.eurExchangeRate,
                                    PercentChange1h  = -101,
                                    PercentChange24h = -101,
                                    PercentChange7d  = -101,
                                    MarketCap        = -1,
                                    SupplyAvailable  = -1,
                                    SupplyTotal      = -1
                                };
                            }
                            else if (assetResponse.Currency == "EUR")
                            {
                                // response in EUR
                                updatedAsset = new Asset()
                                {
                                    AssetId          = assetResponse.Symbol,
                                    Symbol           = assetResponse.Symbol,
                                    Name             = asset.Name,
                                    ConvertCurrency  = asset.ConvertCurrency,
                                    LastUpdated      = DateTime.Now,
                                    Price            = asset.ConvertCurrency == "EUR" ? assetResponse.RegularMarketPrice : assetResponse.RegularMarketPrice / this.eurExchangeRate,
                                    PercentChange1h  = -101,
                                    PercentChange24h = -101,
                                    PercentChange7d  = -101,
                                    MarketCap        = -1,
                                    SupplyAvailable  = -1,
                                    SupplyTotal      = -1
                                };
                            }
                            else
                            {
                                throw new Exception("Die API antwortete mit einem ungültigen Tradingpaar: " + assetResponse.Currency + "/" + assetResponse.Symbol);
                            }

                            this.FireOnAssetUpdateReceived(updatedAsset);
                        }
                        catch (KeyNotFoundException)
                        {
                            // ignore
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    // no internet connection?
                    callFailed = true;
                    Thread.Sleep(retryDelay);
                    timeout -= retryDelay;
                }
                catch (Exception e)
                {
                    OnApiErrorEventArgs args = new OnApiErrorEventArgs
                    {
                        ErrorType    = ErrorType.General,
                        ErrorMessage = e.Message
                    };

                    this.FireOnApiError(args);
                }
            }while (callFailed && timeout >= 0);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets updates for a list of assets.
        /// Fires the OnSingleAssetUpdated event for each updated asset from the list.
        /// Fires the OnApiError event if something has gone wrong.
        /// </summary>
        /// <param name="assets">The assets<see cref="List{Asset}"/> to update.</param>
        protected override async Task GetAssetUpdatesAsync(List <Asset> assets)
        {
            if (assets.Count < 1)
            {
                return;
            }

            List <string> fromSymbols = new List <string>();

            assets.ForEach(ass => fromSymbols.Add(ass.Symbol));

            bool callFailed = false;
            int  timeout    = this.ApiData.UpdateInterval * 1000;

            do
            {
                callFailed = false;

                try
                {
                    PriceMultiFullResponse response = await this.client.Prices.MultipleSymbolFullDataAsync(fromSymbols, this.attachedConvertCurrencies);

                    this.ApiData.IncreaseCounter(1);
                    this.FireOnAppDataChanged();

                    var res = response.Raw;

                    if (res == null)
                    {
                        return;
                    }

                    assets.ForEach(ass =>
                    {
                        this.attachedConvertCurrencies.ForEach(con =>
                        {
                            try
                            {
                                CoinFullAggregatedData data = res[ass.Symbol][con];

                                if (ass.ConvertCurrency == con)
                                {
                                    ass.Price            = (double)data.Price;
                                    ass.LastUpdated      = DateTime.Now;
                                    ass.PercentChange24h = data.ChangePCT24Hour != null? (double)data.ChangePCT24Hour : -101;
                                    ass.PercentChange24h = -101;
                                    ass.PercentChange1h  = -101;
                                    ass.MarketCap        = data.MarketCap != null? (double)data.MarketCap : -1;
                                    ass.SupplyAvailable  = -1;
                                    ass.SupplyTotal      = -1;
                                    ass.Volume24hConvert = data.TotalVolume24HTo != null? data.TotalVolume24HTo.ToString() : "-";

                                    this.FireOnAssetUpdateReceived(ass);
                                }
                            }
                            catch (KeyNotFoundException)
                            {
                                // ignore
                            }
                        });
                    });
                }
                catch (HttpRequestException)
                {
                    // no internet connection?
                    callFailed = true;
                    Thread.Sleep(retryDelay);
                    timeout -= retryDelay;
                }
                catch (Exception e)
                {
                    OnApiErrorEventArgs args = new OnApiErrorEventArgs
                    {
                        ErrorType    = ErrorType.General,
                        ErrorMessage = e.Message
                    };

                    this.FireOnApiError(args);
                }
            }while (callFailed && timeout >= 0);
        }