Make current price work with date offsets
This commit is contained in:
@@ -17,8 +17,6 @@ def Predict():
|
|||||||
# get the number of days ago to run the simulation for
|
# get the number of days ago to run the simulation for
|
||||||
DaysBack = int(sys.argv[2])
|
DaysBack = int(sys.argv[2])
|
||||||
|
|
||||||
print(f"Days back: {DaysBack}")
|
|
||||||
|
|
||||||
# calculate the time offsets
|
# calculate the time offsets
|
||||||
end_date = datetime.now() - timedelta(days=DaysBack)
|
end_date = datetime.now() - timedelta(days=DaysBack)
|
||||||
start_date = end_date - timedelta(days=70)
|
start_date = end_date - timedelta(days=70)
|
||||||
@@ -26,9 +24,6 @@ def Predict():
|
|||||||
# convert to string formats
|
# convert to string formats
|
||||||
start_str = start_date.strftime('%Y-%m-%d')
|
start_str = start_date.strftime('%Y-%m-%d')
|
||||||
end_str = end_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)
|
# Define paths (consistent with your previous script)
|
||||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from datetime import datetime, timedelta
|
||||||
import sys
|
import sys
|
||||||
import yfinance as yf
|
import yfinance as yf
|
||||||
|
|
||||||
@@ -6,8 +7,21 @@ def getCurrentPrice():
|
|||||||
# Get the Symbol from ARGV
|
# Get the Symbol from ARGV
|
||||||
symbol = sys.argv[1]
|
symbol = sys.argv[1]
|
||||||
|
|
||||||
|
# get the number of days ago to run the simulation for
|
||||||
|
DaysBack = int(sys.argv[2])
|
||||||
|
|
||||||
ticker = yf.Ticker(symbol)
|
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]
|
current_price = data['Close'].iloc[-1]
|
||||||
|
|
||||||
# Return to C# via stdout
|
# Return to C# via stdout
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ namespace Controllers.ProjectTest {
|
|||||||
foreach(PurchasedStock cur in StockHistory) {
|
foreach(PurchasedStock cur in StockHistory) {
|
||||||
if (cur.Sold == false) {
|
if (cur.Sold == false) {
|
||||||
// Get sell price
|
// 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
|
// Add up the total sale
|
||||||
totalSale += sellPrice;
|
totalSale += sellPrice;
|
||||||
// Mark as sold
|
// Mark as sold
|
||||||
@@ -85,7 +85,7 @@ namespace Controllers.ProjectTest {
|
|||||||
if (i != 0) {
|
if (i != 0) {
|
||||||
|
|
||||||
// Buy predicted stock
|
// 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 ]
|
// Get max stocks user can purchase [ int cast truncates the decimal ]
|
||||||
int MaxQty = (int)( Money / stockPrice );
|
int MaxQty = (int)( Money / stockPrice );
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ namespace Controllers.PythonInterop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetCurrentPrice(string StockSymbol) {
|
public float GetCurrentPrice(string StockSymbol, int DataEndDaysAgo = 0) {
|
||||||
(bool, string) Success = PyProcess.RunPythonProcess(_PyPath, _ExecPath + "/currentprice.py", returns: true, PyArgs: StockSymbol);
|
(bool, string) Success = PyProcess.RunPythonProcess(_PyPath, _ExecPath + "/currentprice.py", returns: true, PyArgs: $"{StockSymbol} {DataEndDaysAgo}");
|
||||||
if (!Success.Item1) {
|
if (!Success.Item1) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user