Ejemplo n.º 1
0
        private void CreateFullTableFromData(DataContainer dataContainer)
        {
            string prefix = "Creating full data...";

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 0), ConsoleColor.Gray);

            // Create datatable
            DataTable = new DataTable();

            // First column is date
            DataTable.Columns.Add("Date", typeof(DateTime));

            // Another columns are products
            List <string> products = GetAllProducts(dataContainer);

            foreach (string p in products)
            {
                DataTable.Columns.Add("2_" + p, typeof(double));
            }

            // Weather
            List <string> stations = GetAllStations(dataContainer);

            foreach (string s in stations)
            {
                DataTable.Columns.Add("3_" + s + "-PRCP", typeof(double));
                DataTable.Columns.Add("3_" + s + "-SNOW", typeof(double));
                DataTable.Columns.Add("3_" + s + "-TMAX", typeof(double));
                DataTable.Columns.Add("3_" + s + "-TMIN", typeof(double));
            }

            // Forex
            List <string> pairs = GetAllPairs(dataContainer);

            foreach (string p in pairs)
            {
                DataTable.Columns.Add("4_" + p, typeof(double));
            }

            // Google
            List <string> words = GetAllWords(dataContainer);

            foreach (string w in words)
            {
                DataTable.Columns.Add("5_" + w, typeof(double));
            }

            // WikiTrends
            List <string> wikiWords = GetAllWikiWords(dataContainer);

            foreach (string w in wikiWords)
            {
                DataTable.Columns.Add("6_" + w, typeof(double));
            }

            // Futures
            List <string> futuresNames = GetAllFutures(dataContainer);

            foreach (string w in futuresNames)
            {
                DataTable.Columns.Add("7_" + w, typeof(double));
            }

            // Fundamentals
            List <string> fundamentalsNames = GetAllFundamentals(dataContainer);

            foreach (string w in fundamentalsNames)
            {
                DataTable.Columns.Add("8_" + w, typeof(double));
            }

            DataTable.BeginLoadData();

            // Pocet radku
            int rowLength = DataTable.Columns.Count;

            // Adding rows
            int drawEvery = Utils.PercentIntervalByLength(dataContainer.Events.Count);
            int i         = 0;

            foreach (KeyValuePair <DateTime, Event> kv in dataContainer.Events)
            {
                // Date
                DateTime date = kv.Key;

                // Event
                Event e = kv.Value;

                // Row data
                List <object> row = new List <object>(rowLength);

                // Add date
                row.Add(date);

                // Add product prices
                foreach (string p in products)
                {
                    row.Add(e.ProductsDatas[p].Price);
                }

                // Add weather data
                foreach (string s in stations)
                {
                    _WeatherData weather = null;
                    if (!e.WeatherDatas.TryGetValue(s, out weather))
                    {
                        weather = new _WeatherData();
                    }

                    row.Add(weather.Precipitation);
                    row.Add(weather.Snow);
                    row.Add(weather.TemperatureMax);
                    row.Add(weather.TemperatureMin);
                }

                // Add forex data
                foreach (string s in pairs)
                {
                    _ForexData pair = null;
                    if (!e.ForexDatas.TryGetValue(s, out pair))
                    {
                        pair = new _ForexData();
                    }

                    row.Add(pair.Price);
                }

                // Add google data
                foreach (string w in words)
                {
                    _GoogleData word = null;
                    if (!e.GoogleDatas.TryGetValue(w, out word))
                    {
                        word = new _GoogleData();
                    }

                    row.Add(word.Value);
                }

                // Add WikiTrends data
                foreach (string w in wikiWords)
                {
                    _WikiTrendsData word = null;
                    if (!e.WikiTrendsDatas.TryGetValue(w, out word))
                    {
                        word = new _WikiTrendsData();
                    }

                    row.Add(word.Value);
                }

                // Add Futures data
                foreach (string w in futuresNames)
                {
                    _FuturesData fd = null;
                    if (!e.FuturesDatas.TryGetValue(w, out fd))
                    {
                        fd = new _FuturesData();
                    }

                    row.Add(fd.Price);
                }

                // Add Fundamentals data
                foreach (string w in fundamentalsNames)
                {
                    _FundamentalsData fd = null;
                    if (!e.FundamentalsData.TryGetValue(w, out fd))
                    {
                        fd = new _FundamentalsData();
                    }

                    row.Add(fd.Value);
                }

                // Add row to datatable
                DataTable.Rows.Add(row.ToArray());

                // Update progress bar
                if (i++ % drawEvery == 0)
                {
                    Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, (double)i / dataContainer.Events.Count * 100.0), ConsoleColor.Gray);
                }
            }

            DataTable.EndLoadData();

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 100), ConsoleColor.Green);
            Console.WriteLine();
        }
Ejemplo n.º 2
0
        private void Cache(DataContainer dataContainer, _InternalData internalData)
        {
            string prefix = "5/5 Caching...";

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 0), ConsoleColor.Gray);

            try
            {
                // Serialize or deserialize
                _Cache cache = null;
                if (Reload || !File.Exists(SerializedFile))
                {
                    cache = new _Cache(internalData);

                    Serializer.Serialize(SerializedFile, cache);
                }
                else
                {
                    cache = (_Cache)Serializer.Deserialize(SerializedFile);
                }

                Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 50), ConsoleColor.Gray);

                int drawEvery = Utils.PercentIntervalByLength(cache.Station.Count * 2);

                // Save to datacontainer
                for (int i = 0; i < cache.Station.Count; ++i)
                {
                    // Create weather data
                    _WeatherData wd = new _WeatherData()
                    {
                        Precipitation  = cache.Precipitation[i],
                        Snow           = cache.Snow[i],
                        TemperatureMax = cache.TemperatureMax[i],
                        TemperatureMin = cache.TemperatureMin[i]
                    };

                    // Station
                    string station = cache.Station[i];

                    // Get event
                    Event e = dataContainer.GetEvent(new DateTime(cache.Date[i]));

                    // Add to event
                    e.WeatherDatas.Add(station, wd);


                    if (i % drawEvery == 0)
                    {
                        Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 50 + (double)i / cache.Station.Count * 50), ConsoleColor.Gray);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Utils.DrawMessage("", ex.Message, ConsoleColor.Red);
                Console.WriteLine();

                System.Environment.Exit(1);
            }

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 100), ConsoleColor.Green);
            Console.WriteLine();
        }