Ejemplo n.º 1
0
        /// <summary>
        /// Update stocks financials and quotes
        /// </summary>
        /// <param name="stock"></param>
        /// <returns></returns>
        public bool Update(Stock stock)
        {
            try
            {
                UpdateFinancialData(stock);

                AlphaVantageLib.Reader alphaVanReader = new AlphaVantageLib.Reader();

                if (String.IsNullOrEmpty(stock.TickerA))
                {
                    stock.TickerA = stock.TickerM + ".f";
                }
                List <AlphaVantageLib.QuoteAlphaV> quotes = alphaVanReader.GetWeeklyQuotes(stock.TickerA);

                // TODO: implement a faster (with less overhead, and minimum deleting) method
                if (quotes != null && 0 < quotes.Count)
                {
                    if (stock.WeeklyQuotes == null)
                    {
                        stock.WeeklyQuotes = new List <Quote>();
                    }
                    else
                    {
                        stock.WeeklyQuotes.Clear();
                    }
                    foreach (AlphaVantageLib.QuoteAlphaV newQuote in quotes)
                    {
                        Quote addMe = new Quote(newQuote);
                        stock.WeeklyQuotes.Add(addMe);
                    }
                }

                // update share price in financials
                foreach (Financials finance in stock.Financials)
                {
                    Logic.QuoteSearcher quoteSearch = new Logic.QuoteSearcher();
                    Quote quote = quoteSearch.FindNearestQuote(stock.WeeklyQuotes, finance.Date);
                    finance.SharePrice = (decimal)quote.Close;
                }

                stock.LastUpdate = DateTime.Now;
                DataAccess.Db.UpdateStock(stock);
                return(true);
            }
            catch (Exception x)
            {
                Log.Error(x);
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read stock data from database and perform data-augmentations (sets references and share price)
        /// </summary>
        /// <returns></returns>
        public List <Stock> FullRead()
        {
            try
            {
                // get data from liteDb
                using (var db = new LiteDatabase(_dataBaseFile))
                {
                    // Get a collection (or create, if it doesn't exist)
                    LiteCollection <Stock> currentCollection = db.GetCollection <Stock>("Stocks");

                    // link financials (needed for some ratios)
                    foreach (Stock stock in currentCollection.FindAll().ToList())
                    {
                        if (stock.Financials != null)
                        {
                            bool updateDb = false;

                            SetReferences(stock.Financials);

                            // update share price in financials in case not set
                            foreach (Financials finance in stock.Financials)
                            {
                                if (finance.SharePrice == 0 && stock.WeeklyQuotes != null)
                                {
                                    Logic.QuoteSearcher quoteSearch = new Logic.QuoteSearcher();
                                    Quote quote = quoteSearch.FindNearestQuote(stock.WeeklyQuotes, finance.Date);
                                    finance.SharePrice = (decimal)(quote?.Close ?? 0);
                                    updateDb           = true;
                                }
                            }

                            if (updateDb)
                            {
                                currentCollection.Update(stock);
                            }
                        }
                        _currentStockCollection.Add(stock);
                    }
                }
                return(_currentStockCollection);
            }
            catch (Exception x)
            {
                Log.Error(x);
                return(null);
            }
        }