Make current price work with date offsets
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user