Пример #1
0
        static void Main(string[] args)
        {
            List <string> stocks = new List <string>();

            // instantiate sqlHandle
            SqlHandler sqlHandle = new SqlHandler("radhika Chigurupati", "1212",
                                                  @"DESKTOP-LLFV7OU\SQLEXPRESS", "stockdb");

            if (!sqlHandle.isConnected())
            {
                return;
            }

            // stocks file name
            string       filename  = @"C:\Users\radhi\Desktop\stocks.txt";
            StreamWriter errorfile = new StreamWriter(@"C:\Users\radhi\Desktop\errors.txt");

            if (!File.Exists(filename))
            {
                Console.WriteLine("Stocks file doesn't exist!");
                return;
            }

            // read stocks
            string[] lines = File.ReadAllLines(filename);
            foreach (string line in lines)
            {
                stocks.Add(line);
            }
            Console.WriteLine("Successfully read " + stocks.Count.ToString() + " stocks from file!");

            // start and end dates
            DateTime startDt = new DateTime(2014, 01, 01);
            DateTime endDt   = new DateTime(2015, 06, 01);

            // get stock data into database
            WebStockDataReader wdr = new WebStockDataReader();
            int rc = 0;

            Console.WriteLine("Retrieving stocks data from yahoo and storing into database...");
            int cnt = 0;

            foreach (string stock in stocks)
            {
                rc = wdr.getStockData(sqlHandle, stock, startDt, endDt);
                cnt++;
                if (cnt % 100 == 0)
                {
                    // show progress to user
                    Console.WriteLine("Finished retrieving " + cnt.ToString() + " stocks / "
                                      + stocks.Count.ToString() + " stocks ...(still in progress)");
                }
                if (rc != 0)
                {
                    errorfile.WriteLine(stock);
                }
            }
            errorfile.Close();
            Console.WriteLine("Completed retrieving stock data and storing into database!");

            // get market data -- sp 500 into database
            wdr.getStockData(sqlHandle, "^GSPC", startDt, endDt);
            Console.WriteLine("Completed retrieving market data and storing into database!");

            DateTime betaStDt      = new DateTime(2014, 06, 01);
            DateTime betaEndDt     = new DateTime(2014, 12, 31);
            int      rollingWindow = 30;

            cnt = 0;
            foreach (string stock in stocks)
            {
                List <StockData> stk_data = sqlHandle.getPriceData(stock, betaStDt.Subtract(TimeSpan.FromDays(30)), betaEndDt);
                calculateAndStoreBetas(sqlHandle, stock, stk_data, rollingWindow);
                cnt++;
                if (cnt % 100 == 0)
                {
                    // show progress
                    Console.WriteLine("Finished calculating and storing betas for "
                                      + cnt.ToString() + " stocks ... (still in progress)");
                }
            }

            Console.WriteLine("Program finished calculating betas and storing into database .. Press any key to exit");
            Console.ReadKey();
        }