From 8df645aac5a7566a61350e13283e35b4537b1b49 Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Wed, 11 Mar 2026 21:49:04 -0700 Subject: [PATCH] Make current price work with date offsets --- WebServer/AIPython/aipredictor.py | 5 ----- WebServer/AIPython/currentprice.py | 16 +++++++++++++++- WebServer/Controllers/ProjectTest.cs | 4 ++-- WebServer/Controllers/PythonInterop.cs | 4 ++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/WebServer/AIPython/aipredictor.py b/WebServer/AIPython/aipredictor.py index 566ced2a..719389a7 100644 --- a/WebServer/AIPython/aipredictor.py +++ b/WebServer/AIPython/aipredictor.py @@ -17,8 +17,6 @@ def Predict(): # get the number of days ago to run the simulation for DaysBack = int(sys.argv[2]) - print(f"Days back: {DaysBack}") - # calculate the time offsets end_date = datetime.now() - timedelta(days=DaysBack) start_date = end_date - timedelta(days=70) @@ -26,9 +24,6 @@ def Predict(): # convert to string formats start_str = start_date.strftime('%Y-%m-%d') end_str = end_date.strftime('%Y-%m-%d') - - print(f"Start Date: {start_str}") - print(f"End Date: {end_str}") # Define paths (consistent with your previous script) SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) diff --git a/WebServer/AIPython/currentprice.py b/WebServer/AIPython/currentprice.py index c2814b17..35bbce00 100644 --- a/WebServer/AIPython/currentprice.py +++ b/WebServer/AIPython/currentprice.py @@ -1,3 +1,4 @@ +from datetime import datetime, timedelta import sys import yfinance as yf @@ -6,8 +7,21 @@ def getCurrentPrice(): # Get the Symbol from ARGV symbol = sys.argv[1] + # get the number of days ago to run the simulation for + DaysBack = int(sys.argv[2]) + ticker = yf.Ticker(symbol) - data = ticker.history(period="1d", interval="1m") + + # Calculate the target date + target_date = datetime.now() - timedelta(days=DaysBack) + start_str = target_date.strftime('%Y-%m-%d') + + # End date must be the day AFTER the target to get that day's data + end_date = target_date + timedelta(days=1) + end_str = end_date.strftime('%Y-%m-%d') + + # Fetch data for that specific 24-hour window + data = ticker.history(start=start_str, end=end_str) current_price = data['Close'].iloc[-1] # Return to C# via stdout diff --git a/WebServer/Controllers/ProjectTest.cs b/WebServer/Controllers/ProjectTest.cs index 2846f002..e3c17ac2 100644 --- a/WebServer/Controllers/ProjectTest.cs +++ b/WebServer/Controllers/ProjectTest.cs @@ -72,7 +72,7 @@ namespace Controllers.ProjectTest { foreach(PurchasedStock cur in StockHistory) { if (cur.Sold == false) { // Get sell price - float sellPrice = cur.Quantity * _aiModule.GetCurrentPrice( cur.Symbol ); + float sellPrice = cur.Quantity * _aiModule.GetCurrentPrice( cur.Symbol, i ); // Add up the total sale totalSale += sellPrice; // Mark as sold @@ -85,7 +85,7 @@ namespace Controllers.ProjectTest { if (i != 0) { // Buy predicted stock - float stockPrice = _aiModule.GetCurrentPrice( HighestRanking.Symbol ); + float stockPrice = _aiModule.GetCurrentPrice( HighestRanking.Symbol, i ); // Get max stocks user can purchase [ int cast truncates the decimal ] int MaxQty = (int)( Money / stockPrice ); diff --git a/WebServer/Controllers/PythonInterop.cs b/WebServer/Controllers/PythonInterop.cs index d2290daf..47d16881 100644 --- a/WebServer/Controllers/PythonInterop.cs +++ b/WebServer/Controllers/PythonInterop.cs @@ -39,8 +39,8 @@ namespace Controllers.PythonInterop { } } - public float GetCurrentPrice(string StockSymbol) { - (bool, string) Success = PyProcess.RunPythonProcess(_PyPath, _ExecPath + "/currentprice.py", returns: true, PyArgs: StockSymbol); + public float GetCurrentPrice(string StockSymbol, int DataEndDaysAgo = 0) { + (bool, string) Success = PyProcess.RunPythonProcess(_PyPath, _ExecPath + "/currentprice.py", returns: true, PyArgs: $"{StockSymbol} {DataEndDaysAgo}"); if (!Success.Item1) { return 0; } else {