public static StockPerformanceSet Average(StockPerformanceSet[] performances) { StockPerformanceSet ToReturn = new StockPerformanceSet(); List <float> R14 = new List <float>(); List <float> R30 = new List <float>(); List <float> R90 = new List <float>(); List <float> R180 = new List <float>(); List <float> R360 = new List <float>(); foreach (StockPerformanceSet sps in performances) { R14.Add(sps.Return14); R30.Add(sps.Return30); R90.Add(sps.Return90); R180.Add(sps.Return180); R360.Add(sps.Return360); } ToReturn.Return14 = R14.Average(); ToReturn.Return30 = R30.Average(); ToReturn.Return90 = R90.Average(); ToReturn.Return180 = R180.Average(); ToReturn.Return360 = R360.Average(); return(ToReturn); }
public async Task GenerateFromTransactionsFileAsync(string path) { //Get the name of the stock PrintStatus("Getting symbol from path."); Symbol = System.IO.Path.GetFileName(path).Replace(".json", "").ToUpper().Trim(); Console.WriteLine("Symbol: " + Symbol); //Get the transactions ResearchToolkit rt = new ResearchToolkit(); PrintStatus("Loading transactions from file."); NonDerivativeTransaction[] alltransactions = await rt.OpenNonDerivativeTransactionsAsync(path); PrintStatus(alltransactions.Length.ToString() + " transactions found."); PrintStatus("Filtering to focus... "); NonDerivativeTransaction[] focus = rt.FilterToTransactionsOfInterest(alltransactions); PrintStatus(focus.Length.ToString() + " transactions to focus on."); //Get the average performance StockPerformanceSet sps_avg = new StockPerformanceSet(); PrintStatus("Getting average stock performance for this period."); try { await sps_avg.CalculateAverageReturnsAsync(Symbol, new DateTime(2010, 1, 1), new DateTime(2019, 12, 31)); } catch (Exception ex) { throw new Exception("Fatal failure while trying to download average returns for the period: " + ex.Message); } PrintStatus("Average perforamance downloaded!"); AveragePerformance = sps_avg; //Get the performances after each buy List <StockPerformanceSet> PerformancesFollowingBuys = new List <StockPerformanceSet>(); PrintStatus("Starting the performance calculation following each insider buy..."); int PCount = 1; foreach (NonDerivativeTransaction ndt in focus) { StockPerformanceSet sps = new StockPerformanceSet(); PrintStatus("Calculating performance since buy on " + ndt.TransactionDate.Value.ToShortDateString() + " (" + PCount.ToString() + "/" + focus.Length.ToString() + ")..."); try { await sps.CalculateReturnsAsync(Symbol, ndt.TransactionDate.Value); Console.WriteLine("Performance calculated!"); PerformancesFollowingBuys.Add(sps); } catch (Exception ex) { Console.WriteLine("Failure while processing performance since buy on " + ndt.TransactionDate.Value.ToShortDateString() + ": " + ex.Message); } PCount = PCount + 1; } //Add them PerformancesFollowingInsiderBuys = PerformancesFollowingBuys.ToArray(); PrintStatus("Generation complete!"); }