From 45353b8a017d27780081f72cec28bae698dc247d Mon Sep 17 00:00:00 2001 From: "derek.holloway" Date: Thu, 12 Mar 2026 09:35:12 -0700 Subject: [PATCH] Finalize the preditctor algorithm --- WebServer/Components/Pages/Home.razor | 2 +- WebServer/Controllers/Automation.cs | 13 +++++++++++++ WebServer/Controllers/ProjectTest.cs | 24 ++++++++++++++++-------- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/WebServer/Components/Pages/Home.razor b/WebServer/Components/Pages/Home.razor index fb58464b..c98f054b 100644 --- a/WebServer/Components/Pages/Home.razor +++ b/WebServer/Components/Pages/Home.razor @@ -207,7 +207,7 @@ async Task RunTest(){ D683_Project_Test tests = new D683_Project_Test(aiModule); float score = await tests.Simulate(); - TestResults = score.ToString(); + TestResults = "Performance [measured in dollars difference from starting money]: " + score.ToString(); } } \ No newline at end of file diff --git a/WebServer/Controllers/Automation.cs b/WebServer/Controllers/Automation.cs index 97676e34..c4baafe2 100644 --- a/WebServer/Controllers/Automation.cs +++ b/WebServer/Controllers/Automation.cs @@ -22,6 +22,13 @@ namespace Controllers.Automation { // Start this process on a background thread so its non-blocking Task thread = new Task(() => { + // Check if the day is a weekend + DateTime chosenDate = DateTime.Now.AddDays(-DaysBefore); + if (chosenDate.DayOfWeek == DayOfWeek.Saturday || chosenDate.DayOfWeek == DayOfWeek.Sunday) { + // If its a weekend skip + return; + } + // Load the userlist List VerifiedUserList = new List(){ "TESTMODE" }; if (!testmode) { @@ -121,6 +128,9 @@ namespace Controllers.Automation { // Get sell price float sellPrice = cur.Quantity * _aiModule.GetCurrentPrice( cur.Symbol ); + if (sellPrice == 0) { + return -1f; + } // Try create payment session (bool, string) createResult = _paymentProcessor.CreatePayment(username); @@ -157,6 +167,9 @@ namespace Controllers.Automation { // Get Stock Price float stockPrice = _aiModule.GetCurrentPrice( stockSymbol ); + if (stockPrice == 0) { + return -1f; + } // Get max stocks user can purchase [ int cast truncates the decimal ] int MaxQty = (int)( Money / stockPrice ); diff --git a/WebServer/Controllers/ProjectTest.cs b/WebServer/Controllers/ProjectTest.cs index e3c17ac2..a541b70f 100644 --- a/WebServer/Controllers/ProjectTest.cs +++ b/WebServer/Controllers/ProjectTest.cs @@ -1,5 +1,6 @@ using Controllers.PythonInterop; using Entities; +using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Razor.TagHelpers; namespace Controllers.ProjectTest { @@ -36,26 +37,29 @@ namespace Controllers.ProjectTest { // Run once for each day 30 days straight for (int i=30; i>=0; i--) { + // Check if the day is a weekend + DateTime chosenDate = DateTime.Now.AddDays(-i); + if (chosenDate.DayOfWeek == DayOfWeek.Saturday || chosenDate.DayOfWeek == DayOfWeek.Sunday) { + // If its a weekend skip + continue; + } + // Go through each watched stock and find the highest prediction List threadpool = new List(); foreach(Stock cur in TrackedStocks) { - // Predict the trend on a new thread - Task thread = new Task(async () => { + Task thread = new Task(() => { (string, float)Result = _aiModule.PredictAI(cur.Symbol, i); - // If error log it if (!string.IsNullOrEmpty(Result.Item1)){ Console.WriteLine(Result.Item1); } - // Write the score to the users tracked stocks cur.Score = Result.Item2; }); thread.Start(); threadpool.Add(thread); } - // Wait for all processes to finish await Task.WhenAll( threadpool ); @@ -68,18 +72,20 @@ namespace Controllers.ProjectTest { } // Sell all stocks - float totalSale = 0; foreach(PurchasedStock cur in StockHistory) { if (cur.Sold == false) { // Get sell price float sellPrice = cur.Quantity * _aiModule.GetCurrentPrice( cur.Symbol, i ); + if (sellPrice == 0) { + // Account for holidays + goto next; + } // Add up the total sale - totalSale += sellPrice; + Money = Money + sellPrice; // Mark as sold cur.Sold = true; } } - Money += totalSale; // Dont buy anything on the last run if (i != 0) { @@ -98,6 +104,8 @@ namespace Controllers.ProjectTest { } ); Money = Money - ( stockPrice * MaxQty ); } + + next:; } // Return a score [Bigger than 0 is money earned] or [Less than 0 is money lost]