/* *This method consolidates the assets based on their Symbol and type. If there are two assets * with same Symbol and same asset type (i.e two or more Domestic Stocks with the Symbol ABC, they will be * consolidated. * * If there are similar assets but with different currencies, they will be consolidated after converting * their respective prices to Euro. The possibility of having Same symbols, bought in different currency * denominations is an assumption that I made. * */ public static List <IAssets> CompareAssets(List <IAssets> consolidatedList, IAssets asset) { ExchangeRates exchangeRate = new ExchangeRates(); if (consolidatedList.Count == 0) { consolidatedList.Add(asset); return(consolidatedList); } IAssets tempAsset = consolidatedList.Find(x => x.Equals(asset)); if (tempAsset == null) { consolidatedList.Add(asset); return(consolidatedList); } try { if (tempAsset.Currency == asset.Currency) { consolidatedList.Remove(tempAsset); tempAsset.Price = ((tempAsset.Price * tempAsset.Shares) + (asset.Price * asset.Shares)) / (tempAsset.Shares + asset.Shares); tempAsset.Shares += asset.Shares; consolidatedList.Add(tempAsset); return(consolidatedList); } else if (tempAsset.Type == asset.Type) { consolidatedList.Remove(tempAsset); var entryPrice = exchangeRate.GetRate(tempAsset.Price, tempAsset.Currency.ToString(), "EUR"); var assetPrice = exchangeRate.GetRate(asset.Price, asset.Currency.ToString(), "EUR"); tempAsset.Price = ((entryPrice * tempAsset.Shares) + (assetPrice * asset.Shares)) / tempAsset.Shares + asset.Shares; tempAsset.Shares += asset.Shares; tempAsset.Currency = CurrencyTypes.EUR; consolidatedList.Add(tempAsset); return(consolidatedList); } consolidatedList.Add(asset); } catch (Exception e) { Console.WriteLine("\nError:" + e.Message); consolidatedList.Add(tempAsset); } return(consolidatedList); }
public double GetRate(double amount, string fromCurrency, string toCurrency) { double convertedAmount = 0; try { string url = string.Format("https://www.amdoren.com/api/currency.php?api_key=7JCpCqfs54hNuwiYESmpwfErXPEWKs&from={0}&to={1}&amount={2}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount); //If the Number of API hits Quota is over, please comment the above url and uncomment hte below url :) //string url = string.Format("https://www.amdoren.com/api/currency.php?api_key=muPepJzBN7S39r5qhVtpP2HBKspSzn&from={0}&to={1}&amount={2}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount); string result; using (WebClient client = new WebClient()) { result = client.DownloadString(url); ExchangeRates exTemp = Newtonsoft.Json.JsonConvert.DeserializeObject <ExchangeRates>(result); /*Error code 400 indicates that the API hit limit has been exceeded. In that case user can manually enter the * exchange rate*/ if (exTemp.error == 400) { Console.WriteLine("\n\nAPI Hit Limit got over, please enter the exchange rate \n for selected currency in Euro,manually:"); double manualRate = Convert.ToDouble(Console.ReadLine()); return(manualRate * amount); } else if (exTemp.error != 0) { Console.WriteLine("\n\nServer Error!, please enter the exchange rate \n for selected currency in Euro,manually:"); double manualRate = Convert.ToDouble(Console.ReadLine()); return(manualRate * amount); } convertedAmount = Convert.ToDouble(exTemp.amount); } } catch (Exception ex) { Console.WriteLine("\nError!! " + ex.Message); } return(convertedAmount); }
/* * Valuation of the asset is done based on the currency denomination chosen by the user. */ public double Value(string toCurrency) { ExchangeRates exchangeRate = new ExchangeRates(); return(exchangeRate.GetRate((Shares * Price), Currency.ToString(), toCurrency)); }