I'm getting a lot of timeouts with the following code and I'm running this method around 10,000 times so I need to figure out how I can optimize this so the timeouts will stop. I am getting timeout errors when I try to call the fill method and I believe it is because the queries aren't done processing yet. It is a local database and it is running SQL Server 2014.
public static StockRating performCalculations(SymbolInfo info)
{
var calc = new Calculations(info.Symbol, info.Market);
StockRating rating = new StockRating();
try
{
using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
using (DailyNyseDataTableAdapter dailyNyseAdapter = new DailyNyseDataTableAdapter())
using (DailyGlobalDataDataTable sandp500Table = new DailyGlobalDataDataTable())
using (DailyNyseDataDataTable dailyNyseTable = new DailyNyseDataDataTable())
{
// fill the s&p500 info first
dailyGlobalAdapter.ClearBeforeFill = true;
dailyGlobalAdapter.FillBySymbol(sandp500Table, Calculations.sp500);
var sp500StockDataQuery = from c in sandp500Table
select new StockData { Close = c.AdjustedClose, High = c.High, Low = c.Low, Volume = c.Volume, Date = c.Date };
calc.sp500Data = sp500StockDataQuery.AsParallel().ToList();
dailyNyseAdapter.FillBySymbol(dailyNyseTable, info.Symbol);
var nyseQuery = from c in sandp500Table
where c.Date >= DateTime.Now.Subtract(TimeSpan.FromDays(60))
join d in dailyNyseTable on c.Date equals d.Date
select new StockMarketCompare { stockClose = d.AdjustedClose, marketClose = c.AdjustedClose };
var nyseStockDataQuery = from c in dailyNyseTable
select new StockData { Close = c.AdjustedClose, High = c.High, Low = c.Low, Volume = c.Volume, Date = c.Date };
calc.stockVsMarketCompareData = nyseQuery.AsParallel().ToList();
calc.stockData = nyseStockDataQuery.AsParallel().ToList();
// get the pct return data that is needed for calculations
calc.fillPctReturns();
if (calc.stockPctReturns.Count > 0 && calc.marketPctReturns.Count > 0)
{
// perform the calculations here
rating.estimatedReturn = calc.calculateExpectedReturn();
rating.longRating = calc.calculateRating(Calculations.ratingType.long_rating);
rating.market = calc.Market;
rating.mediumRating = calc.calculateRating(Calculations.ratingType.medium_rating);
rating.shortRating = calc.calculateRating(Calculations.ratingType.short_rating);
rating.symbol = calc.Symbol;
rating.risk = calc.calculateRisk();
rating.isMarginEnabled = calc.isMarginEnabled();
Console.WriteLine("Ratings calculated for " + info.Symbol + " in the " + info.Market + ". " + stopwatch.ElapsedMilliseconds / 1000 + " seconds elapsed.");
}
else
{
rating = null;
}
}
}
catch (Exception ex)
{
rating = performCalculations(info);
}
return rating;
}
Parallel.For(0, activeSymbolsCount, j =>
{
symbol = symbolTable.AsParallel().ElementAtOrDefault(j).Symbol;
SymbolInfo symbolClass = new SymbolInfo(symbol, market);
StockRating rating = performCalculations(symbolClass);
saveToTable(rating);
});
Fillmethod you mean \$\endgroup\$