/// <summary> /// Eventhandler for updating the Sharevalues in the database. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void TimerTick(object sender, System.EventArgs e) { // if its a friday night... if (/*DateTime.Now.DayOfWeek == DayOfWeek.Friday*/ DateTime.Now.DayOfWeek != DayOfWeek.Saturday && DateTime.Now.DayOfWeek != DayOfWeek.Sunday && DateTime.Now.Hour >= 22) { // ... get all shares in the portfolio var shares = DataBaseHelper.GetAllItemsFromDB <Share>(); //remove the shares which where no share is currently purchased for (int i = shares.Count - 1; i >= 0; i--) { var orders = DataBaseHelper.GetItemsFromDB <Order>(shares[i]); double amountRemaining = 0; foreach (var o in orders) { if (o.OrderType == ShareComponentType.Buy) { amountRemaining += o.Amount; } else if (o.OrderType == ShareComponentType.Sell) { amountRemaining -= o.Amount; } } if (amountRemaining < 1) { shares.RemoveAt(i); } } // for each of these shares... foreach (var share in shares) { // ...get the latest value in the database var latestValues = DataBaseHelper.GetItemsFromDB <ShareValue>(share)?.OrderByDescending((v) => v.Date); if (latestValues.Count() > 0) { var latestValue = latestValues.First(); // if it is from today... if (latestValue?.Date.Date == DateTime.Today) { // ... we can ignore the following and continue with the next share continue; } } try { await Task.Run(async() => { var price = await RegexHelper.GetSharePriceAsync(share); if (price == 0.0) { var prices = DataBaseHelper.GetItemsFromDB <ShareValue>(share) ?.Where(sv => sv.Price != 0.0) ?.OrderBy(sv => sv.Date) ?.Select(sv => new { sv.Price }); var lastprice = prices?.LastOrDefault(); price = lastprice.Price; } // create a new sharevalue ShareValue s = new ShareValue() { Date = DateTime.Today, ISIN = share.ISIN, Price = price, }; // and add it to the database DataBaseHelper.AddShareValueToDB(s); }); } catch (NullReferenceException NullRefEx) { Logger.Log($"TimerTick: {NullRefEx.Message}: {NullRefEx.StackTrace}"); } } } }