Esempio n. 1
0
 public SecurityGraphGenerator(StockQuoteHistory history, Security security)
 {
     this.history                = history;
     this.security               = security;
     nfi.NumberDecimalDigits     = 2;
     nfi.CurrencyNegativePattern = 0;
 }
Esempio n. 2
0
        // Aggressive buy:
        //  1) If three or more DECs in a row, buy 20 % extra
        //  2) If two or more DECs in a row, buy 10 % extra
        // Defensive sell:
        //  3) If four of last changes were INCs, sell 10 % of owned
        //  4) If three of last changes were INCs, sell 5 % of owned
        public void Trade(IStockTrader trader, int index, List <double> quotes)
        {
            double            quote  = quotes[index];
            StockQuoteHistory sqHist = StockQuoteAnalyser.Analyse(quotes, index, HistoryLength);

            if (StratsLib.TryBuyOnDecStreak(trader, sqHist, quote, 3, 5))
            {
                return;
            }
            if (StratsLib.TryBuyOnDecStreak(trader, sqHist, quote, 2, 10))
            {
                return;
            }
            if (StratsLib.TrySellOnMultiInc(trader, sqHist, quote, 4, 10))
            {
                return;
            }
            StratsLib.TrySellOnMultiInc(trader, sqHist, quote, 3, 20);
        }
Esempio n. 3
0
 public bool TrySellOnMultiInc(IStockTrader trader, StockQuoteHistory sqHist, double quote, int noOfIncs, int fraction)
 {
     return(TradeConditional(trader, () => sqHist.Incs >= noOfIncs, trader.TryToSellStock, quote, fraction));
 }
Esempio n. 4
0
 public bool TryBuyOnMultiDec(IStockTrader trader, StockQuoteHistory sqHist, double quote, int noOfDecs, int fraction)
 {
     return(TradeConditional(trader, () => sqHist.Decs >= noOfDecs, trader.TryToBuyStock, quote, fraction));
 }
Esempio n. 5
0
 public bool TrySellOnIncStreak(IStockTrader trader, StockQuoteHistory sqHist, double quote, int streakLength, int fraction)
 {
     return(TradeConditional(trader, () => sqHist.IncStreak >= streakLength, trader.TryToSellStock, quote, fraction));
 }
Esempio n. 6
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();
        }
Esempio n. 7
0
        /// <summary>
        /// This is the specific implementation of the four (currently) existing
        /// trade strategies, found by combining aggressive/defensive strategies
        /// for buying/selling. NB: The "quality" of the strategies as such is not
        /// of importance here :-).
        /// </summary>
        protected override void Trade(int index, List <double> quotes)
        {
            int histLength = 5; // All strategies look five quotes back.

            double            quote  = quotes[index];
            StockQuoteHistory sqHist = StockQuoteAnalyser.Analyse(quotes, index, histLength);

            // Aggressive buy:
            //  1) If three or more DECs in a row, buy 20 % extra
            //  2) If two or more DECs in a row, buy 10 % extra
            // Aggressive sell:
            //  3) If three or more INCs in a row, sell 20 % of owned
            //  4) If two or more INCs in a row, sell 10 % of owned
            if (AggrBuy && AggrSell)
            {
                if (sqHist.DecStreak >= 3)
                {
                    TryToBuyStock(NoOfStocks / 5, quote);
                }
                else if (sqHist.DecStreak >= 2)
                {
                    TryToBuyStock(NoOfStocks / 10, quote);
                }
                else if (sqHist.IncStreak >= 3)
                {
                    TryToSellStock(NoOfStocks / 5, quote);
                }
                else if (sqHist.IncStreak >= 2)
                {
                    TryToSellStock(NoOfStocks / 10, quote);
                }
            }

            // Defensive buy:
            //  1) If four of last changes were DECs, buy 10 % extra
            //  2) If three of last changes were DECs, buy 5 % extra
            // Aggressive sell:
            //  3) If three or more INCs in a row, sell 20 % of owned
            //  4) If two or more INCs in a row, sell 10 % of owned
            if (!AggrBuy && AggrSell)
            {
                if (sqHist.Decs >= 4)
                {
                    TryToBuyStock(NoOfStocks / 10, quote);
                }
                else if (sqHist.Decs >= 3)
                {
                    TryToBuyStock(NoOfStocks / 20, quote);
                }
                else if (sqHist.IncStreak >= 3)
                {
                    TryToSellStock(NoOfStocks / 5, quote);
                }
                else if (sqHist.IncStreak >= 2)
                {
                    TryToSellStock(NoOfStocks / 10, quote);
                }
            }

            // Aggressive buy:
            //  1) If three or more DECs in a row, buy 20 % extra
            //  2) If two or more DECs in a row, buy 10 % extra
            // Defensive sell:
            //  3) If four of last changes were INCs, sell 10 % of owned
            //  4) If three of last changes were INCs, sell 5 % of owned
            if (AggrBuy && !AggrSell)
            {
                if (sqHist.DecStreak >= 3)
                {
                    TryToBuyStock(NoOfStocks / 5, quote);
                }
                else if (sqHist.DecStreak >= 2)
                {
                    TryToBuyStock(NoOfStocks / 10, quote);
                }
                else if (sqHist.Incs >= 4)
                {
                    TryToSellStock(NoOfStocks / 10, quote);
                }
                else if (sqHist.Incs >= 3)
                {
                    TryToSellStock(NoOfStocks / 20, quote);
                }
            }

            // Defensive buy:
            //  1) If four of last changes were DECs, buy 10 % extra
            //  2) If three of last changes were DECs, buy 5 % extra
            // Defensive sell:
            //  3) If four of last changes were INCs, sell 10 % of owned
            //  4) If three of last changes were INCs, sell 5 % of owned
            if (!AggrBuy && !AggrSell)
            {
                if (sqHist.Decs >= 4)
                {
                    TryToBuyStock(NoOfStocks / 10, quote);
                }
                else if (sqHist.Decs >= 3)
                {
                    TryToBuyStock(NoOfStocks / 20, quote);
                }
                else if (sqHist.Incs >= 4)
                {
                    TryToSellStock(NoOfStocks / 10, quote);
                }
                else if (sqHist.Incs >= 3)
                {
                    TryToSellStock(NoOfStocks / 20, quote);
                }
            }
        }