Files
AI-Stock-Trader/WebServer/AIPython/currentprice.py
T

33 lines
911 B
Python

from datetime import datetime, timedelta
import sys
import yfinance as yf
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)
# 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
print(f"---RESULT_START---")
print(current_price)
print(f"---RESULT_END---")
if __name__ == "__main__":
getCurrentPrice()