I'm not sure if I needed to post all of this but I thought I would include it all just in case. The assignment that I'm working on uses 3 dictionaries and functions to allow the user to add stocks, recommend a sale, and exit I'm currently having a problem with my GetSale or recommend sale function. The GetSale function should find the maximum expected value of selling a stock. The expected sale value of a stock is the current profit minus the future value of the stock:
Expected Sale value = ( ( Current Price - Buy Price ) - Risk * CurrentPrice ) * Shares
The GetSale function should calculate this value for each stock in the portfolio, and return the stock symbol with the highest expected sale value. When I call the GetSale function with option 2 under the Main function I always get "Highest selling stock is with a 0 profit margin." even after entering in different stocks. Is my GetSale function coded so that is what it returns and its not even checking the dictionary I'm new to this so not exactly sure where I went wrong.
Names = {}
Prices = {}
Exposure = {}
def AddName(StockNames, StockPrices, StockExposure):
TheStockName = input("Input stock name. ")
Prompt = "Input the stock symbol for " + TheStockName
Symbol = input(Prompt)
StockNames[Symbol] = TheStockName
StockPrices[Symbol] = None
StockExposure[Symbol] = None
return StockNames
def AddPrices(StockPrices):
while True:
Symbol = input("Input stock symbol for the stock and add price next. ")
if Symbol in Prices:
Prompt = input("Current buying price of " + Symbol + ": ")
BuyPrice = float(Prompt)
Prompt = input("Current selling price of " + Symbol + ": ")
SellPrice = float(Prompt)
StockPrices[Symbol] = [BuyPrice, SellPrice]
return StockPrices
else:
print("This stock does not exist.")
def AddExposure(StockExposure):
while True:
Symbol = input("Input stock symbol for the stock and add exposure next. ")
if Symbol in Exposure:
Prompt = input("Current number of " + Symbol + " stocks that have been purchased? ")
SharesOwned = float(Prompt)
Prompt = input("Current risk factor of " + Symbol + " share ownership? ")
RiskFactor = float(Prompt)
StockExposure[Symbol] = [SharesOwned, RiskFactor]
return StockExposure
else:
print("This stock does not exist.")
def AddStock():
AddName(Names, Prices, Exposure)
AddPrices(Prices)
AddExposure(Exposure)
def GetSale(Names, Prices, Exposure):
HighestStock = 0
HighestStockName = ''
for Stock in Names:
TotalProfit = ((Prices[Stock][1] - Prices[Stock][0]) - Exposure[Stock][1] * Prices[Stock] [0]) * Exposure[Stock][0]
if (TotalProfit > HighestStock):
HighestStock = TotalProfit
HighestStockName = Stock
print("Highest selling stock is", HighestStockName, "with a ", HighestStock, "profit margin.")
def Main():
keepGoing = True
while keepGoing:
print("Menu")
print("1: Add a stock")
print("2: Find the highest sale price")
print("3: Exit")
userResponseString = input("Input selection (1, 2 or 3): ")
try:
userInput = int(userResponseString)
except:
print("Invalid input.")
userChoice = 0
if (userInput == 1):
AddStock()
elif (userInput == 2):
GetSale(Names, Prices, Exposure)
else:
(userInput == 3)
print("Ending stock program.")
keepGoing = False
Main()