Example #1
0
        public void RemoveSecurity(Security security)
        {
            using (var db = new PriceDatabaseContext(ConnectionString))
            {
                try
                {
                    //var sec = GetSecurity(security.Ticker, false, true);
                    //db.Securities.Remove(sec);
                    //db.SaveChanges();
                    db.Securities.Attach(security);
                    db.Entry(security).State = EntityState.Deleted;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log(new LogMessage(ToString() + ".RemoveSecurity()", ex.Message, LogMessageType.SystemError));
                    //throw ex;
                }
            }

        }
Example #2
0
        public bool SetSecurity(Security security, bool Overwrite)
        {

            if (security.DailyPriceBarData.Any(x => x.BarSize != PriceBarSize.Daily))
                throw new UnknownErrorException() { message = "Price Bar data invalid" };

            try
            {
                using (var db = new PriceDatabaseContext(ConnectionString))
                {
                    //// Get the Security entity stored in the database
                    var dbSecurity = (from sec in db.Securities where sec.Ticker == security.Ticker select sec).Include(x => x.DailyPriceBarData).FirstOrDefault();

                    if (dbSecurity == null)
                    {
                        throw new SecurityNotFoundException() { message = "Attempted to reattach non-existent security to database" };
                    }

                    db.Entry(dbSecurity).CurrentValues.SetValues(security);

                    foreach (var newBar in security.DailyPriceBarData)
                    {
                        try
                        {
                            // Skip bars not marked for update
                            if (!newBar.ToUpdate && !Overwrite)
                                continue;

                            // Find existing bar if one exists for this date
                            var currentBar = dbSecurity.DailyPriceBarData.Where(currBar => currBar == newBar);

                            if (currentBar?.Count() > 1)
                            {
                                // More than one bar got saved... delete both and save the new one
                                currentBar.ToList().ForEach(b => db.Entry(b).State = EntityState.Deleted);

                                //// Insert new bar
                                newBar.Security = dbSecurity;
                                dbSecurity.DailyPriceBarData.Add(newBar);
                                newBar.ToUpdate = false;

                                db.SaveChanges();
                            }
                            else if (currentBar.SingleOrDefault() != null)
                            {
                                if (!Overwrite)
                                    continue;
                                // Update bars that are in the new bar collection
                                db.Entry(currentBar).CurrentValues.SetValues(newBar);
                                newBar.ToUpdate = false;
                            }
                            else
                            {
                                //// Insert new bars
                                newBar.Security = dbSecurity;
                                newBar.ToUpdate = false;
                                dbSecurity.DailyPriceBarData.Add(newBar);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                    }

                    db.SaveChanges();

                    return true;
                }
            }
            catch (Exception ex)
            {
                Log(new LogMessage(ToString() + ".SetSecurity()", $"Could not set data for {security.Ticker}; {ex.Message}", LogMessageType.SystemError));
                return false;
            }
        }