ich muss eine Uni Aufgabe lösen und habe einen code geschrieben. Komme aber gerade nicht weiter. Ich weiß, dass es hierfür wahrscheinlich eine einfache Lösung gibt aber mein Kopf ist gerade matsch. Vllt könnt ihr mir ausnahmsweise kurs aushelfen.
Ich würde gerne folgendes machen: Ich wähle eine AG aus. Gebe das Datum ein und wie viele Aktien ich gekauft hätte. Leider hab ich es nciht geschafft es mit einem Währungsbetrag zu machen..
Dann soll herauskommen: wie viel gewinn oder verlust ich mit dem Kauf der Aktie zum eingegebenen Datum gemacht hätte.
Mein Code:
Code: Alles auswählen
import sys
import requests
import json
import yfinance as yf
from datetime import date,datetime
def valid_stock(stock_name,api_key):
base_url = "https://yfapi.net/v6/finance/quote"
querystring = {"symbols": stock_name}
headers = {
'x-api-key': api_key
}
response = requests.request("GET", base_url, headers=headers, params=querystring)
json_data = json.loads(response.text)
if len(json_data['quoteResponse']['result']) == 0:
return False
else:
return True
if __name__ == '__main__':
api_key = "83esSjubXI6KUOuwEKncQXs4iR13Vsw8nhMdn0nf"
if len(sys.argv) == 2:
stock_name = sys.argv[1]
if valid_stock(stock_name, api_key):
date_of_buy = input(f"Enter the date (format: yyyy-mm-dd) you bought {stock_name}: ")
quantity = int(input(f"Enter the quantity of {stock_name} you owns: "))
end = str(str(date_of_buy.split("-")[0])+"-12-30")
data_one = yf.download(stock_name, date_of_buy, end)
today = date.today()
start = str(str(today.year)+"-01-01")
end = str(str(today.year)+"-"+str(today.month)+"-"+str(today.day))
data_two = yf.download(stock_name, start, end)
stock_previous_val = sum(data_one['Close'].tolist())/len(data_one['Close'].tolist())
stock_current_val = sum(data_two['Close'].tolist()) / len(data_two['Close'].tolist())
print(f"Stock {stock_name} Value in Year {date_of_buy.split('-')[0]} : ${round(stock_previous_val,2)}")
print(f"Stock {stock_name} Value in Year {today.year} : ${round(stock_current_val,2)}")
print(f"Expected Profit or Loss: ${round((stock_current_val - stock_previous_val)*quantity,2)}")
else:
print("Please Enter the Stock Name as: python single_stock.py stock_name")
Enter the date (format: yyyy-mm-dd) you bought AMZN: 2022-06-30
Enter the quantity of AMZN you owns: 1
[*********************100%***********************] 1 of 1 completed
[*********************100%***********************] 1 of 1 completed
a) Stock AMZN Value in Year 2022 : $107.56
b) Stock AMZN Value in Year 2022 : $140.31
Expected Profit or Loss: $32.75
C:\Users\Maximilian\Desktop\CodPy>
Was ich nicht verstehe. Eig müssten die Werte a & b gleich sein. Ich verstehe nicht wieso diese unterschiedlich sind. Kann mir jemand erklären wieso das der Fall ist?
Vielen Dank!!
LG
Saiba