Ejemplo n.º 1
0
        private void AddPaychecks(string employer, decimal paycheck, double inflation)
        {
            Debug.Assert(checking != null); // the .xml file must have a checking account.
            DateTime today             = DateTime.Today;
            DateTime first             = today.AddYears(-Years);
            double   biMonthlyInfation = (inflation / 24);

            DateTime date = new DateTime(first.Year, 1, 1);

            money.BeginUpdate();
            Payee    payee    = money.Payees.FindPayee(employer, true);
            Category category = money.Categories.GetOrCreateCategory("Wages & Salary:Gross Pay", CategoryType.Income);

            category.Type = CategoryType.Income;
            Transactions transactions = money.Transactions;

            for (int paydays = Years * 12 * 2; paydays > 0; paydays--)
            {
                Transaction t = transactions.NewTransaction(this.checking);
                t.Payee    = payee;
                t.Category = category;
                t.Date     = date;
                t.Amount   = paycheck;
                transactions.AddTransaction(t);

                // make it a bi-monthly paycheck
                if (date.Day == 1)
                {
                    date = date.AddDays(15);
                }
                else
                {
                    date = date.AddMonths(1);
                    date = new DateTime(date.Year, date.Month, 1);
                }
                paycheck += (paycheck * (decimal)biMonthlyInfation) / 100M;
            }
            money.EndUpdate();
        }
Ejemplo n.º 2
0
        private void CreateRandomTransactions(List <SampleTransaction> list, double inflation)
        {
            // Now pick randomly from the list to mix things up nicely and spread across 10 year range.
            Random   rand             = new Random();
            DateTime today            = DateTime.Today;
            DateTime first            = today.AddYears(-Years);
            DateTime start            = new DateTime(first.Year, 1, 1); // start in January
            TimeSpan span             = today - first;
            int      totalDays        = (int)span.TotalDays;
            double   monthlyInflation = (inflation / 12);

            money.BeginUpdate();
            Transactions transactions = money.Transactions;
            Accounts     accounts     = money.Accounts;
            Payees       payees       = money.Payees;
            Categories   categories   = money.Categories;

            while (list.Count > 0)
            {
                int i = rand.Next(0, list.Count);
                SampleTransaction st = list[i];
                list.RemoveAt(i);

                SampleAccount  sa = st.Account;
                Account        a  = accounts.FindAccount(sa.Name);
                Payee          p  = payees.FindPayee(st.Payee.Name, true);
                SampleCategory sc = st.Category;
                Category       c  = money.Categories.GetOrCreateCategory(sc.Name, sc.Type);
                if (c.Type == CategoryType.None)
                {
                    c.Type = sc.Type;
                }
                if (c.Root.Type == CategoryType.None)
                {
                    c.Root.Type = sc.Type;
                }

                int      daysFromStart = rand.Next(0, totalDays);
                DateTime date          = start + TimeSpan.FromDays(daysFromStart);

                // spread evenly around the average
                decimal amount = 0;
                if (rand.Next(2) == 1)
                {
                    // above average
                    amount = (decimal)rand.Next(sc.Average * 100, sc.Max * 100) / 100;
                }
                else
                {
                    // below average
                    amount = (decimal)rand.Next(sc.Min * 100, sc.Average * 100) / 100;
                }

                // add inflation
                amount = Inflate(amount, (int)(daysFromStart / 30), (decimal)monthlyInflation);

                Transaction t = transactions.NewTransaction(a);
                t.Payee    = p;
                t.Category = c;
                t.Date     = date;
                t.Amount   = amount;
                transactions.AddTransaction(t);
            }
            money.EndUpdate();

            // now trigger UI update
            foreach (Account a in money.Accounts.GetAccounts())
            {
                money.Rebalance(a);
            }

            // only have to do this because we hid all update events earlier by doing BeginUpdate/EndUpdate on money object.
            // trigger payee update
            money.Payees.BeginUpdate(false);
            money.Payees.EndUpdate();

            // trigger category update
            money.Categories.BeginUpdate(false);
            money.Categories.EndUpdate();
        }
Ejemplo n.º 3
0
        private void CreateRandomTransactions(List <SampleTransaction> list, double inflation)
        {
            // Now pick randomly from the list to mix things up nicely and spread across 10 year range.
            Random   rand             = new Random();
            DateTime today            = DateTime.Today;
            DateTime first            = today.AddYears(-Years);
            DateTime start            = new DateTime(first.Year, 1, 1); // start in January
            TimeSpan span             = today - first;
            int      totalDays        = (int)span.TotalDays;
            double   monthlyInflation = (inflation / 12);

            money.BeginUpdate(this);
            Transactions transactions = money.Transactions;
            Accounts     accounts     = money.Accounts;
            Payees       payees       = money.Payees;
            Categories   categories   = money.Categories;
            int          nextCheck    = 2800;

            while (list.Count > 0)
            {
                int i = rand.Next(0, list.Count);
                SampleTransaction st = list[i];
                list.RemoveAt(i);

                SampleAccount  sa = st.Account;
                Account        a  = accounts.FindAccount(sa.Name);
                Payee          p  = payees.FindPayee(st.Payee.Name, true);
                SampleCategory sc = st.Category;
                Category       c  = money.Categories.GetOrCreateCategory(sc.Name, sc.Type);
                if (c.Type == CategoryType.None)
                {
                    c.Type = sc.Type;
                }
                if (c.Root.Type == CategoryType.None)
                {
                    c.Root.Type = sc.Type;
                }

                int      daysFromStart = rand.Next(0, totalDays);
                DateTime date          = start + TimeSpan.FromDays(daysFromStart);

                // spread evenly around the average
                decimal amount = 0;
                if (rand.Next(2) == 1)
                {
                    // above average
                    amount = (decimal)rand.Next(sc.Average * 100, sc.Max * 100) / 100;
                }
                else
                {
                    // below average
                    amount = (decimal)rand.Next(sc.Min * 100, sc.Average * 100) / 100;
                }

                // add inflation
                amount = Inflate(amount, (int)(daysFromStart / 30), (decimal)monthlyInflation);

                Transaction t = transactions.NewTransaction(a);
                t.Payee    = p;
                t.Category = c;
                t.Date     = date;
                t.Amount   = RoundCents(amount);
                if (st.Payee.Type == PaymentType.Check)
                {
                    t.Number = nextCheck.ToString();
                    nextCheck++;
                }
                transactions.AddTransaction(t);
            }

            // now pay the credit cards once a month from a checking account.
            Account checking = this.checking;

            if (checking != null)
            {
                foreach (var acct in accounts.GetAccounts())
                {
                    if (acct.Type == AccountType.Credit)
                    {
                        // here we know transactions are sorted by date.
                        DateTime endOfMonth = start.AddMonths(1);
                        decimal  balance    = 0;
                        foreach (var t in money.Transactions.GetTransactionsFrom(acct))
                        {
                            balance += t.Amount;
                            if (t.Date >= endOfMonth)
                            {
                                if (balance != 0)
                                {
                                    Transaction payment = transactions.NewTransaction(checking);
                                    payment.Date   = endOfMonth;
                                    payment.Amount = RoundCents(balance);
                                    transactions.AddTransaction(payment);
                                    money.Transfer(payment, acct);
                                    balance    = 0;
                                    endOfMonth = endOfMonth.AddMonths(1);
                                }
                            }
                        }
                    }
                }
            }

            money.EndUpdate();
        }
Ejemplo n.º 4
0
        private void CreateInvestmentSamples(SampleData data, List <Account> brokerageAccounts)
        {
            if (brokerageAccounts.Count == 0)
            {
                return;
            }

            // now balance the accounts.
            foreach (Account a in money.Accounts.GetAccounts())
            {
                money.Rebalance(a);
            }

            // first figure out how much we can spend each year.
            int      year  = DateTime.Now.Year - 10;
            DateTime start = new DateTime(year, 1, 1); // start in January
            DateTime end   = start.AddYears(1);
            Dictionary <int, decimal> cash = new Dictionary <int, decimal>();
            Ownership ownership            = new Ownership();
            decimal   removed = 0;

            foreach (var t in money.Transactions.GetTransactionsFrom(this.checking))
            {
                if (t.Date > end)
                {
                    cash[year] = GetStockMoney(t.Balance);
                    end        = end.AddYears(1);
                    year++;
                }
            }

            money.BeginUpdate(this);

            Dictionary <Account, decimal> cashBalance = new Dictionary <Account, decimal>();

            foreach (var a in brokerageAccounts)
            {
                cashBalance[a] = 0;
            }

            Transactions transactions = money.Transactions;

            for (year = DateTime.Now.Year - 10; year <= DateTime.Now.Year; year++)
            {
                cash.TryGetValue(year, out decimal balance);
                balance -= removed;
                if (balance < 100)
                {
                    continue; // not enough.
                }
                decimal startBalance = balance;

                int numberOfTransactionForThatYear = rand.Next(5, 100); // up to 100 transactions max per year.

                // keep track of how much we own so we never go negative.
                IList <int> selectedDays = GetRandomDaysInTheYearForTransactions(numberOfTransactionForThatYear);

                foreach (var day in selectedDays)
                {
                    // select random security.
                    SampleSecurity ss = data.Securities[rand.Next(0, data.Securities.Count)];
                    // Get or Create the security
                    Security stock = money.Securities.FindSecurity(ss.Name, true);
                    stock.SecurityType = ss.SecurityType;
                    stock.Symbol       = ss.Symbol;

                    // select a random brokerage.
                    Account a = brokerageAccounts[rand.Next(brokerageAccounts.Count)];

                    var canSpend = balance + cashBalance[a];
                    if (canSpend < 100)
                    {
                        break;
                    }

                    // the date the purchase or sale was done
                    var date = new DateTime(year, 1, 1).AddDays(day);

                    // How many unit bought or sold
                    var quote = GetClosingPrice(ss.Symbol, date);

                    Transaction t     = null;
                    decimal     owned = ownership.GetUnits(a, ss.Symbol);
                    if (owned > 4 && rand.Next(3) == 1)
                    {
                        // make this a sell transaction.
                        // Create a new Transaction
                        t      = money.Transactions.NewTransaction(a);
                        t.Date = date;
                        //-----------------------------------------------------
                        // Create the matching Investment transaction
                        Investment i = t.GetOrCreateInvestment();
                        i.Transaction = t;
                        i.Security    = stock;
                        i.UnitPrice   = quote;
                        i.Price       = quote;
                        i.Type        = InvestmentType.Sell;
                        // Create the a SELL transaction
                        i.Units = rand.Next(1, (int)(owned / 2));
                        ownership.AddUnits(a, ss.Symbol, -i.Units);
                        // Calculate the Payment or Deposit amount
                        t.Amount = RoundCents(i.Units * i.UnitPrice);
                    }
                    else
                    {
                        int max = (int)(canSpend / quote);
                        if (max > 0)
                        {
                            // Create a new Transaction
                            t      = money.Transactions.NewTransaction(a);
                            t.Date = date;

                            //-----------------------------------------------------
                            // Create the matching Investment transaction
                            Investment i = t.GetOrCreateInvestment();
                            i.Transaction = t;
                            i.Security    = stock;
                            i.UnitPrice   = quote;
                            i.Price       = quote;
                            i.Type        = InvestmentType.Buy;
                            i.Units       = rand.Next(1, max);
                            ownership.AddUnits(a, ss.Symbol, i.Units);
                            // Calculate the Payment or Deposit amount
                            t.Amount = RoundCents(i.Units * i.UnitPrice * -1);
                        }
                    }

                    if (t != null)
                    {
                        t.Payee         = money.Payees.FindPayee(ss.Name, true);
                        t.Category      = money.Categories.InvestmentStocks;
                        balance        += t.Amount;
                        cashBalance[a] += t.Amount;
                        // Finally add the new transaction
                        money.Transactions.AddTransaction(t);
                    }
                }

                foreach (var acct in brokerageAccounts)
                {
                    var amount = cashBalance[acct];
                    if (amount < 0)
                    {
                        // then we need to transfer money to cover the cost of the stock purchases.
                        Transaction payment = transactions.NewTransaction(checking);
                        payment.Date   = new DateTime(year, 1, 1);
                        payment.Amount = RoundCents(amount);
                        transactions.AddTransaction(payment);
                        money.Transfer(payment, acct);
                        removed           += -amount;
                        cashBalance[acct] += -amount;
                    }
                }
            }
            money.EndUpdate();

            // now balance the accounts again!
            foreach (Account a in money.Accounts.GetAccounts())
            {
                money.Rebalance(a);
            }
        }