This class loads financial data from Yahoo.
Inheritance: IMarketLoader
        public static void Generate(FileInfo dataDir)
        {
            IMarketLoader loader = new YahooFinanceLoader();
            var market = new MarketMLDataSet(loader,
                                             Config.INPUT_WINDOW, Config.PREDICT_WINDOW);
            var desc = new MarketDataDescription(
                Config.TICKER, MarketDataType.AdjustedClose, true, true);
            market.AddDescription(desc);

            var end = DateTime.Now; // end today
            var begin = new DateTime(end.Ticks); // begin 30 days ago

            // Gather training data for the last 2 years, stopping 60 days short of today.
            // The 60 days will be used to evaluate prediction.
            begin = begin.AddDays(-60);
            end = end.AddDays(-60);
            begin = begin.AddYears(-2);

            market.Load(begin, end);
            market.Generate();
            EncogUtility.SaveEGB(FileUtil.CombinePath(dataDir, Config.TRAINING_FILE), market);

            // create a network
            BasicNetwork network = EncogUtility.SimpleFeedForward(
                market.InputSize,
                Config.HIDDEN1_COUNT,
                Config.HIDDEN2_COUNT,
                market.IdealSize,
                true);

            // save the network and the training
            EncogDirectoryPersistence.SaveObject(FileUtil.CombinePath(dataDir, Config.NETWORK_FILE), network);
        }
        public void Loader()
        {
            IMarketLoader loader = new YahooFinanceLoader();
            var from = new DateTime(2008, 8, 4);
            var to = new DateTime(2008, 8, 5);
            ICollection<LoadedMarketData> list = loader.Load(new TickerSymbol("aapl"), null, from, to);

            IEnumerator<LoadedMarketData> itr = list.GetEnumerator();
            itr.MoveNext();
            LoadedMarketData data = itr.Current;
            Assert.AreEqual(160, (int) data.GetData(MarketDataType.Close));
            itr.MoveNext();
            data = itr.Current;
            Assert.AreEqual(153, (int) data.GetData(MarketDataType.Close));
        }
        public static MarketMLDataSet GrabData()
        {
            IMarketLoader loader = new YahooFinanceLoader();
            var result = new MarketMLDataSet(loader,
                                             Config.INPUT_WINDOW, Config.PREDICT_WINDOW);
            var desc = new MarketDataDescription(Config.TICKER,
                                                 MarketDataType.AdjustedClose, true, true);
            result.AddDescription(desc);

            var end = DateTime.Now; // end today
            var begin = new DateTime(end.Ticks); // begin 30 days ago
            begin = begin.AddDays(-60);

            result.Load(begin, end);
            result.Generate();

            return result;
        }
        public void MarketData()
        {
            IMarketLoader loader = new YahooFinanceLoader();
            var tickerAAPL = new TickerSymbol("AAPL", null);
            var tickerMSFT = new TickerSymbol("MSFT", null);
            var marketData = new MarketMLDataSet(loader, 5, 1);
            marketData.AddDescription(new MarketDataDescription(tickerAAPL, MarketDataType.Close, true, true));
            marketData.AddDescription(new MarketDataDescription(tickerMSFT, MarketDataType.Close, true, false));
            marketData.AddDescription(new MarketDataDescription(tickerAAPL, MarketDataType.Volume, true, false));
            marketData.AddDescription(new MarketDataDescription(tickerMSFT, MarketDataType.Volume, true, false));
            var begin = new DateTime(2008, 7, 1);
            var end = new DateTime(2008, 7, 31);
            marketData.Load(begin, end);
            marketData.Generate();
            Assert.AreEqual(22, marketData.Points.Count);

            // first test the points
            IEnumerator<TemporalPoint> itr = marketData.Points.GetEnumerator();
            itr.MoveNext();
            TemporalPoint point = itr.Current;

            Assert.AreEqual(0, point.Sequence);
            Assert.AreEqual(4, point.Data.Length);
            Assert.AreEqual(174.68, point[0]);
            Assert.AreEqual(26.87, point[1]);
            Assert.AreEqual(39, (int) (point[2]/1000000));
            Assert.AreEqual(100, (int) (point[3]/1000000));

            itr.MoveNext();
            point = itr.Current;
            Assert.AreEqual(1, point.Sequence);
            Assert.AreEqual(4, point.Data.Length);
            Assert.AreEqual(168.18, point[0]);
            Assert.AreEqual(25.88, point[1]);
            Assert.AreEqual(29, (int) (point[2]/1000000));
            Assert.AreEqual(84, (int) (point[3]/1000000));

            itr.MoveNext();
            point = itr.Current;
            Assert.AreEqual(2, point.Sequence);
            Assert.AreEqual(4, point.Data.Length);
            Assert.AreEqual(170.12, point[0]);
            Assert.AreEqual(25.98, point[1]);
            Assert.AreEqual(18, (int) (point[2]/1000000));
            Assert.AreEqual(37, (int) (point[3]/1000000));

            // now check the actual data
            Assert.AreEqual(16, marketData.Data.Count);
            Assert.AreEqual(20, marketData.InputNeuronCount);
            Assert.AreEqual(1, marketData.OutputNeuronCount);

            IEnumerator<IMLDataPair> itr2 = marketData.Data.GetEnumerator();
            itr2.MoveNext();
            IMLDataPair pair = itr2.Current;
            Assert.AreEqual(20, pair.Input.Count);
            Assert.AreEqual(1, pair.Ideal.Count);

            Assert.AreEqual(-0.037, Math.Round(pair.Input[0]*1000.0)/1000.0);
            Assert.AreEqual(-0.037, Math.Round(pair.Input[1]*1000.0)/1000.0);
            Assert.AreEqual(-0.246, Math.Round(pair.Input[2]*1000.0)/1000.0);
            Assert.AreEqual(-0.156, Math.Round(pair.Input[3]*1000.0)/1000.0);
            Assert.AreEqual(0.012, Math.Round(pair.Input[4]*1000.0)/1000.0);
            Assert.AreEqual(0.0040, Math.Round(pair.Input[5]*1000.0)/1000.0);
            Assert.AreEqual(-0.375, Math.Round(pair.Input[6]*1000.0)/1000.0);
            Assert.AreEqual(-0.562, Math.Round(pair.Input[7]*1000.0)/1000.0);
            Assert.AreEqual(0.03, Math.Round(pair.Input[8]*1000.0)/1000.0);
            Assert.AreEqual(0.0020, Math.Round(pair.Input[9]*1000.0)/1000.0);
            Assert.AreEqual(0.57, Math.Round(pair.Input[10]*100.0)/100.0);
            Assert.AreEqual(0.929, Math.Round(pair.Input[11]*1000.0)/1000.0);
            Assert.AreEqual(0.025, Math.Round(pair.Input[12]*1000.0)/1000.0);
            Assert.AreEqual(-0.0070, Math.Round(pair.Input[13]*1000.0)/1000.0);
            // for some reason, Yahoo likes to vary the volume numbers slightly, sometimes!
            Assert.AreEqual(0.1, Math.Round(pair.Input[14]*10.0)/10.0);
            Assert.AreEqual(-0.084, Math.Round(pair.Input[15]*1000.0)/1000.0);
            Assert.AreEqual(-0.03, Math.Round(pair.Input[16]*1000.0)/1000.0);
            Assert.AreEqual(-0.024, Math.Round(pair.Input[17]*1000.0)/1000.0);
            Assert.AreEqual(0.008, Math.Round(pair.Input[18]*1000.0)/1000.0);
            Assert.AreEqual(-0.172, Math.Round(pair.Input[19]*1000.0)/1000.0);

            itr2.MoveNext();
            pair = itr2.Current;
            Assert.AreEqual(20, pair.Input.Count);
            Assert.AreEqual(1, pair.Ideal.Count);

            Assert.AreEqual(0.012, Math.Round(pair.Input[0]*1000.0)/1000.0);
            Assert.AreEqual(0.0040, Math.Round(pair.Input[1]*1000.0)/1000.0);
            Assert.AreEqual(-0.375, Math.Round(pair.Input[2]*1000.0)/1000.0);
            Assert.AreEqual(-0.562, Math.Round(pair.Input[3]*1000.0)/1000.0);
            Assert.AreEqual(0.03, Math.Round(pair.Input[4]*1000.0)/1000.0);
            Assert.AreEqual(0.0020, Math.Round(pair.Input[5]*1000.0)/1000.0);
            Assert.AreEqual(0.6, Math.Round(pair.Input[6]*10.0)/10.0);
            Assert.AreEqual(0.929, Math.Round(pair.Input[7]*1000.0)/1000.0);
            Assert.AreEqual(0.025, Math.Round(pair.Input[8]*1000.0)/1000.0);
            Assert.AreEqual(-0.0070, Math.Round(pair.Input[9]*1000.0)/1000.0);
            Assert.AreEqual(0.1, Math.Round(pair.Input[10]*10.0)/10.0);
            Assert.AreEqual(-0.084, Math.Round(pair.Input[11]*1000.0)/1000.0);
            Assert.AreEqual(-0.03, Math.Round(pair.Input[12]*1000.0)/1000.0);
            Assert.AreEqual(-0.024, Math.Round(pair.Input[13]*1000.0)/1000.0);
            Assert.AreEqual(0.0080, Math.Round(pair.Input[14]*1000.0)/1000.0);
            Assert.AreEqual(-0.172, Math.Round(pair.Input[15]*1000.0)/1000.0);
            Assert.AreEqual(0.014, Math.Round(pair.Input[16]*1000.0)/1000.0);
            Assert.AreEqual(0.0090, Math.Round(pair.Input[17]*1000.0)/1000.0);
            Assert.AreEqual(-0.1, Math.Round(pair.Input[18]*10.0)/10.0);
            Assert.AreEqual(0.066, Math.Round(pair.Input[19]*1000.0)/1000.0);
        }
        /// <summary>
        /// Called to load training data for a company.  This is how the training data is actually created.
        /// To prepare input data for recognition use the CreateData method.  The training set will be
        /// added to.  This allows the network to learn from multiple companies if this method is called
        /// multiple times.
        /// </summary>
        /// <param name="symbol">The ticker symbol.</param>
        /// <param name="training">The training set to add to.</param>
        /// <param name="from">Beginning date</param>
        /// <param name="to">Ending date</param>
        public void LoadCompany(String symbol, BasicMLDataSet training, DateTime from, DateTime to)
        {
            IMarketLoader loader = new YahooFinanceLoader();
            TickerSymbol ticker = new TickerSymbol(symbol);
            IList<MarketDataType> dataNeeded = new List<MarketDataType>();
            dataNeeded.Add(MarketDataType.AdjustedClose);
            dataNeeded.Add(MarketDataType.Close);
            dataNeeded.Add(MarketDataType.Open);
            dataNeeded.Add(MarketDataType.High);
            dataNeeded.Add(MarketDataType.Low);
            List<LoadedMarketData> results = (List<LoadedMarketData>)loader.Load(ticker, dataNeeded, from, to);
            results.Sort();

            for (int index = PredictWindow; index < results.Count - EvalWindow; index++)
            {
                LoadedMarketData data = results[index];

                // determine bull or bear position, or neither
                bool bullish = false;
                bool bearish = false;

                for (int search = 1; search <= EvalWindow; search++)
                {
                    LoadedMarketData data2 = results[index + search];
                    double priceBase = data.GetData(MarketDataType.AdjustedClose);
                    double priceCompare = data2.GetData(MarketDataType.AdjustedClose);
                    double diff = priceCompare - priceBase;
                    double percent = diff / priceBase;
                    if (percent > BullPercent)
                    {
                        bullish = true;
                    }
                    else if (percent < BearPercent)
                    {
                        bearish = true;
                    }
                }

                IMLDataPair pair = null;

                if (bullish)
                {
                    pair = CreateData(results, index, true);
                }
                else if (bearish)
                {
                    pair = CreateData(results, index, false);
                }

                if (pair != null)
                {
                    training.Add(pair);
                }
            }
        }
        /// <summary>
        /// Load the market data.
        /// </summary>
        /// <returns>True if the data was loaded.</returns>
        private bool LoadMarketData()
        {
            try
            {
                IMarketLoader loader = new YahooFinanceLoader();
                TickerSymbol ticker = new TickerSymbol(this.Company.Text);
                IList<MarketDataType> needed = new List<MarketDataType>();
                needed.Add(MarketDataType.AdjustedClose);
                needed.Add(MarketDataType.Close);
                needed.Add(MarketDataType.Open);
                needed.Add(MarketDataType.High);
                needed.Add(MarketDataType.Low);
                DateTime from = this.starting -TimeSpan.FromDays(365);
                DateTime to = this.starting + TimeSpan.FromDays(365*2);
                this.marketData = (List<LoadedMarketData>)loader.Load(ticker, needed, from, to);
                this.marketData.Sort();

                this.numberOfDays = (int)((ActualWidth - FIRST_DAY_OFFSET) / DAY_WIDTH);
                this.numberOfDays = Math.Min(numberOfDays, this.marketData.Count);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("Ticker symbol likely invalid.\n"+e.Message, "Error Loading Data");
                return false;
            }
        }