private static List <APICurrencyRates> CreateCurrencyPairsForAPICall(Sources source) { CurrencySourceLists sourceList = new CurrencySourceLists(getActiveOnly, SourceCodes[(int)source]); Task <List <APICurrencyRates> > t = CreateCurrencyPairsForSourceListAsync(sourceList, sourceList.FullCurrencyList, source); //take is test code t.Wait(); //wait for this to create all the pairs return(t.Result); }
private static void SaveRates(List <APICurrencyRates> rates) { //check to make sure usd is in currency table. If not, take it out (it was added due to base calculations) CurrencySourceLists sourceList = new CurrencySourceLists(false); if (!sourceList.FullCurrencyList.Any(fc => fc.CurrencyCd == "USD")) { List <APICurrencyRates> usdRates = rates.Where(r => r.CurrencyCdFrom == "USD").ToList(); Parallel.ForEach(usdRates, ur => { rates.Remove(ur); }); } //save it IEnumerable <APICurrencyRates> ratesToSave = rates.Where(r => r.Rate > 0); int rCount = ratesToSave.Count();//rates.Count(); if (ratesToSave.Any()) { if (ratesToSave.Count() > 5000) { int position = 0; int take = 5000; while (position < rCount) { if (rCount - (position + take) < 0) { take = (rCount - position); } //Debug.WriteLine(position); CurrencyRatesTableTypeCollection ttParam = new CurrencyRatesTableTypeCollection(); ttParam.AddRange(ratesToSave.Skip(position).Take(take)); position = position + take; if (ttParam.Count > 0) { SaveRates(ttParam); } else { break; //if we got nothing on skip, take, we're at the end... } } } else { CurrencyRatesTableTypeCollection ttParam = new CurrencyRatesTableTypeCollection(); ttParam.AddRange(ratesToSave); SaveRates(ttParam); } } }
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."); } } }
private static async Task <List <APICurrencyRates> > GenerateCurrencyPairsAsync(CurrencySourceLists sourceList, CurrencySource cs, Sources source) { IEnumerable <string> toRates; List <APICurrencyRates> rateList = new List <APICurrencyRates>(); toRates = sourceList.GetListOfToCurrencies(cs.SourceFrom, cs.CurrencyCd).Select(c => c.CurrencyCd); //take is test code //Parallel.ForEach(toRates, cd => foreach (string cd in toRates) { APICurrencyRates acr = new APICurrencyRates(cs.CurrencyCd, cd, SourceDelimiters[(int)source]); // Debug.WriteLineIf((acr.CurrencyCdFrom == "BTC"), acr.CurrencyPairID); rateList.Add(acr); } //); //Debug.WriteLine("got past currency pairs"); return(rateList); }
private static async Task <List <APICurrencyRates> > CreateCurrencyPairsForSourceListAsync(CurrencySourceLists sourceList, IEnumerable <CurrencySource> listOfBaseRates, Sources source) { List <APICurrencyRates> rateList = new List <APICurrencyRates>(); //Parallel.ForEach(listOfBaseRates, cs => List <List <APICurrencyRates> > results = new List <List <APICurrencyRates> >(); foreach (CurrencySource cs in listOfBaseRates) { results.Add(await GenerateCurrencyPairsAsync(sourceList, cs, source)); } foreach (List <APICurrencyRates> r in results) { rateList.AddRange(r); } //Debug.WriteLine("rates done for " + source.ToString()); return(rateList); }