protected StockQuote MapJsonToStockitems(string jsonResults) { using(var sr = new StringReader(jsonResults)) { var sq = new StockQuote(); string line; while((line = sr.ReadLine()) != null) { if(line.StartsWith(",\"t\"")) { sq.Ticker = line.Replace(",\"t\" : ", "").Trim().Trim('"'); continue; } if(line.StartsWith(",\"c\"")) { sq.Change = decimal.Parse(line.Replace(",\"c\" : ", "").Trim().Trim('"')); continue; } if(line.StartsWith(",\"l\"")) { sq.LastTradePrice = decimal.Parse(line.Replace(",\"l\" : ", "").Trim().Trim('"')); continue; } if(line.StartsWith(",\"ltt\"")) { sq.LastTradeTime = line.Replace(",\"ltt\":", "").Trim().Trim('"').Replace("EST","").Trim(); continue; } } return sq; } }
private static StockQuote MapYahooData(YahooFinanceStockData data) { if (data == null) { return null; } var stock = new StockQuote(); stock.Name = data.Name; stock.LastTradePrice = data.LastTradePrice; stock.Ticker = data.Ticker; stock.Volume = data.Volume; stock.Change = data.Change; stock.LastTradeTime = data.LastTradeTime; stock.RealTimeLastTradePrice = decimal.Parse(data.RealTimeLastTradeWithTime .Replace("<b>", "") .Replace("</b>", "") .Replace("N/A -", "") .Trim() ); stock.ChangeRealTime = data.ChangeRealTime; return stock; }
private static decimal CalculateGainLossRealTime(StockQuote quote, Position position) { var moneyISpent = position.PricePerShare * position.Shares; var moneyItsWorth = position.Shares * quote.RealTimeLastTradePrice; return moneyItsWorth - moneyISpent; }
private IDictionary<StockDataItem, string> CalculateItems(IEnumerable<StockDataItem> items, Position position, StockQuote quote) { var dict = new Dictionary<StockDataItem, string>(); foreach (var item in items) { switch (item) { case StockDataItem.GainLoss: dict.Add(item, CalculateGainLoss(quote, position).ToString()); break; case StockDataItem.GainLossRealTime: dict.Add(item, CalculateGainLossRealTime(quote, position).ToString()); break; default: throw new ArgumentException("That StockDataItem type cannot be calculated"); } } return dict; }
private IDictionary<StockDataItem, string> GetStockItems(IEnumerable<StockDataItem> items, StockQuote quote) { var dict = new Dictionary<StockDataItem, string>(); foreach (var item in items) { switch (item) { case StockDataItem.Change: dict.Add(item, quote.Change.ToString()); break; case StockDataItem.Ticker: dict.Add(item, quote.Ticker); break; case StockDataItem.Time: dict.Add(item, quote.LastTradeTime); break; case StockDataItem.Volume: dict.Add(item, quote.Volume); break; case StockDataItem.LastTradePrice: dict.Add(item, quote.LastTradePrice.ToString()); break; case StockDataItem.RealTimeLastTradeWithTime: dict.Add(item, quote.RealTimeLastTradePrice.ToString()); break; case StockDataItem.ChangeRealTime: dict.Add(item, quote.ChangeRealTime); break; } } return dict; }