Esempio n. 1
0
        private static void GetRatesFromAPI(ref List <APICurrencyRates> rates, Sources source)
        {
            if (rates.Any())
            {
                //get rate from api
                if (source == Sources.CurrencyLayer)
                {
                    if (!rates.Any(r => r.CurrencyCdFrom == "USD")) //if usd isn't the list, need it to be, so create them
                    {
                        CurrencySourceLists   sourceList = new CurrencySourceLists(getActiveOnly, SourceCodes[(int)source]);
                        List <CurrencySource> usdOnly    = new List <CurrencySource>();
                        usdOnly.Add(new CurrencySource {
                            CurrencyCd = "USD", SourceFrom = "LAYER", SourceTo = "LAYER", Active = true
                        });
                        rates.AddRange(CreateCurrencyPairsForSourceListAsync(sourceList, usdOnly, source).Result); //build all the pairs for usd that are in the database and add
                    }
                    //Debug.WriteLine("calling currencylayer");
                    Data.APIHandlers.CurrencyLayerAPIHandler clAPI   = new Data.APIHandlers.CurrencyLayerAPIHandler();
                    List <Data.Entities.BasicRateEntity>     clRates = clAPI.GetUSDCurrencyRates();
                    //Debug.WriteLine("have rates?:" + clRates.Any());
                    foreach (Data.Entities.BasicRateEntity clr in clRates)
                    {
                        if (clr.CurrencyFrom != null)
                        {
                            APICurrencyRates ar = rates.SingleOrDefault(rr => rr.CurrencyCdFrom == clr.CurrencyFrom && rr.CurrencyCdTo == clr.CurrencyTo);
                            if (ar != null)
                            {
                                ar.Rate        = clr.Rate;
                                ar.TimeUpdated = clr.TimeUpdated;
                            }
                        }
                    }
                    if (clRates.Any())
                    {
                        CalculateCrossRates(ref rates, clRates.FirstOrDefault().CurrencyFrom);
                        CalculateOppositePairsAndAddToList(ref rates);
                    }

                    // Debug.WriteLine("Done currencylayer");
                }
                else if (source == Sources.Cryptonator)
                {
                    //use btc if in db, else just get the first one and deal
                    string baseRate = (rates.Any(r => r.CurrencyCdFrom == "BTC")) ? "BTC" : rates.FirstOrDefault(r => r.Rate == -1).CurrencyCdFrom;

                    IEnumerable <APICurrencyRates> nextBaseRates = rates.Where(r => r.CurrencyCdFrom == baseRate);
                    //Debug.WriteLine("got base rates to call: " + nextBaseRates.Any());
                    // while (.Any()) //while pairs in the queue
                    for (int i = 0; i < nextBaseRates.Count(); i++)
                    {
                        APICurrencyRates pair = nextBaseRates.ElementAt(i);        //calculateQueue.Dequeue();
                        //call api handler
                        Data.APIHandlers.CryptonatorAPIHandler crAPI = new Data.APIHandlers.CryptonatorAPIHandler();
                        BasicRateEntity clr = crAPI.GetRateForCurrency(pair.CurrencyPairID);
                        Debug.WriteLine("rate retrieved");
                        //we got something back
                        if (clr.CurrencyFrom != null)
                        {
                            //update the rate
                            APICurrencyRates ar = rates.SingleOrDefault(rr => rr.CurrencyCdFrom == clr.CurrencyFrom && rr.CurrencyCdTo == clr.CurrencyTo);
                            if (ar != null)
                            {
                                ar.Rate        = clr.Rate;
                                ar.TimeUpdated = clr.TimeUpdated;
                            }
                        }
                    }

                    //then see if we can calculate any other pairs from this call given a pair set using cross rates. This keeps the api calls down, hopefully.
                    CalculateCrossRates(ref rates, baseRate);
                    CalculateOppositePairsAndAddToList(ref rates);  //this should also keep calls down, doing this here.

                    //Debug.WriteLine("Done crypto");
                }
                else if (source == Sources.Yahoo)
                {
                    //nothing right now
                }
                else
                {
                    throw new NotSupportedException("The source provided is not a supported currency source.");
                }
            }
        }