Пример #1
0
        public void Create()
        {
            string temp = Path.Combine(Path.GetTempPath(), "MyMoney");

            Directory.CreateDirectory(temp);

            string path = Path.Combine(temp, "SampleData.xml");

            ProcessHelper.ExtractEmbeddedResourceAsFile("Walkabout.Database.SampleData.xml", path);

            SampleDatabaseOptions options = new SampleDatabaseOptions();

            options.Owner      = Application.Current.MainWindow;
            options.SampleData = path;
            if (options.ShowDialog() == false)
            {
                return;
            }

            string zipPath = Path.Combine(temp, "SampleStockQuotes.zip");

            ProcessHelper.ExtractEmbeddedResourceAsFile("Walkabout.Database.SampleStockQuotes.zip", zipPath);

            string quoteFolder = Path.Combine(temp, "StockQuotes");

            if (Directory.Exists(quoteFolder))
            {
                Directory.Delete(quoteFolder, true);
            }
            System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, temp);
            foreach (var file in Directory.GetFiles(quoteFolder))
            {
                var target = Path.Combine(this.stockQuotePath, Path.GetFileName(file));
                if (!File.Exists(target))
                {
                    File.Copy(file, target, true);
                }
            }

            path = options.SampleData;

            double inflation = options.Inflation;

            SampleData    data = null;
            XmlSerializer s    = new XmlSerializer(typeof(SampleData));

            using (XmlReader reader = XmlReader.Create(path))
            {
                data = (SampleData)s.Deserialize(reader);
            }

            foreach (SampleSecurity ss in data.Securities)
            {
                var history = StockQuoteHistory.Load(quoteFolder, ss.Symbol);
                if (history != null)
                {
                    this.quotes[ss.Symbol] = history;
                    this.manager.DownloadLog.AddHistory(history);
                }
            }
            int totalFrequency = data.GetTotalFrequency();

            List <SampleTransaction> list = new List <SampleTransaction>();
            List <Account>           brokerageAccounts = new List <Account>();

            foreach (SampleAccount sa in data.Accounts)
            {
                // Create all the accounts.
                Accounts accounts = money.Accounts;
                Account  a        = accounts.FindAccount(sa.Name);
                if (a == null)
                {
                    a = accounts.AddAccount(sa.Name);
                }
                a.Type = sa.Type;
                if (a.Type == AccountType.Checking)
                {
                    this.checking = a;
                }
                a.TaxStatus = sa.TaxStatus;

                // Create this many transactions
                int count = sa.Frequency;

                // by scaling the payee frequencies to match the above desired count.
                double ratio = (double)count / (double)totalFrequency;

                if (a.Type == AccountType.Brokerage || a.Type == AccountType.Retirement)
                {
                    brokerageAccounts.Add(a);
                }
                else
                {
                    // create flat list of all payees to choose from so it fits the histogram
                    List <SamplePayee> payees = new List <SamplePayee>();
                    foreach (SamplePayee payee in data.Payees)
                    {
                        switch (payee.Type)
                        {
                        case PaymentType.Debit:
                        case PaymentType.Check:
                            if (sa.Type != AccountType.Checking)
                            {
                                continue;
                            }
                            break;

                        case PaymentType.Credit:
                            if (sa.Type != AccountType.Credit)
                            {
                                continue;
                            }
                            break;
                        }

                        foreach (SampleCategory sc in payee.Categories)
                        {
                            int newFrequency = (int)(sc.Frequency * ratio);
                            for (int i = 0; i < newFrequency; i++)
                            {
                                list.Add(new SampleTransaction()
                                {
                                    Account  = sa,
                                    Payee    = payee,
                                    Category = sc
                                });
                            }
                        }
                    }
                }
            }

            money.BeginUpdate(this);
            // create the securities and stock splits
            foreach (var sec in data.Securities)
            {
                Security stock = money.Securities.FindSecurity(sec.Name, true);
                if (sec.Splits != null)
                {
                    foreach (var split in sec.Splits)
                    {
                        var exists = money.StockSplits.FindStockSplitByDate(stock, split.Date);
                        if (exists == null)
                        {
                            StockSplit stockSplit = money.StockSplits.NewStockSplit();
                            stockSplit.Security    = stock;
                            stockSplit.Date        = split.Date;
                            stockSplit.Numerator   = split.Numerator;
                            stockSplit.Denominator = split.Denominator;
                        }
                    }
                }
            }
            money.EndUpdate();

            CreateRandomTransactions(list, inflation);

            AddPaychecks(options.Employer, options.PayCheck, inflation);

            // now with any spare cash we can buy stocks.
            CreateInvestmentSamples(data, brokerageAccounts);

            // 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();

            money.OnLoaded();
        }
Пример #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(this);
            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();

            money.OnLoaded();
        }