ich arbeite gerade an einem Python-Skript, das mir die prozentuale Veränderung einer Aktie zum Vortag berechnen soll. Dabei nutze ich das yfinance-Modul, um die benötigten Daten zu beziehen. Leider funktioniert mein aktueller Code noch nicht ganz wie gewünscht. Kann mir jemand bei der Behebung des Problems helfen?
Hier ist der Code, den ich bisher habe:
Code: Alles auswählen
import yfinance as yf
from datetime import datetime, timedelta
import pytz
def get_last_change_1d_Vtop(tickersymbol, interval, days):
global change_1d_Vtop, date, current_time
end = datetime.now()
start = end - timedelta(days + 5) # Extend the period by 5 days
data = yf.download(tickersymbol, start=start, end=end, interval=interval)
formatted_dates = [date.strftime("%H:%M:%S" " " "%d/%m/%Y") for date in data.index]
data.index = formatted_dates
# Current date
current_date = datetime.today().date()
# Get current time in Berlin timezone
berlin_timezone = pytz.timezone('Europe/Berlin')
current_time = datetime.now(berlin_timezone).time()
# Check if it's after 22:00
if current_date.weekday() == 0: # Monday
if current_time.hour < 9 or (current_time.hour == 9 and current_time.minute < 30):
date = -4 # Last Friday's close
elif current_time.hour >= 22:
date = -3 # Friday's close
else:
date = -2 # Yesterday's close
elif current_date.weekday() == 6: # Sunday
if current_time.hour < 9 or (current_time.hour == 9 and current_time.minute < 30):
date = -3 # Last Friday's close
elif current_time.hour >= 22:
date = -2 # Friday's close
else:
date = -1 # Yesterday's close
else:
if current_time.hour < 9 or (current_time.hour == 9 and current_time.minute < 30):
date = -3 # Last Friday's close
elif current_time.hour >= 22:
date = -2 # Yesterday's close
else:
date = -1 # Yesterday's close
if len(data) >= abs(date):
last_price = data['Close'].iloc[date]
# Query current price
ticker = yf.Ticker(tickersymbol)
info = ticker.info
currentPrice = info['currentPrice']
price_change = currentPrice - last_price
change_1d_Vtop = (price_change / last_price) * 100
change_1d_Vtop = round(change_1d_Vtop, 2)
return change_1d_Vtop
else:
print("Not enough data available for the specified period.")
return None