0

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()
Shazbot
  • 1
  • 1
  • When you call GetSale(), you have to pass three arguments : Names, Prices, Exposure. From what I'm seeing in Main(), you are passing three dictionaries defined at the very top of your code that could potentially be empty. Have you populated these dictionaries before calling option #2? – Corb3nik Nov 17 '14 at 04:46
  • I think that I filled them, I've gone through the program with option 1 and those values are supposed to be stored in my dictionary. However, even after adding in values I still get Highest selling stock is with a 0 profit margin – Shazbot Nov 17 '14 at 04:50
  • I think what is going on here is that there is a mixup with your global variables (Names, Prices, Exposure)... Since these variables are global, you do not need to pass them through arguments. – Corb3nik Nov 18 '14 at 15:46
  • For example, you can replace def AddName(StockNames, StockPrices, StockExposure) with def AddName()... Then, inside your function, you will assign values by using the 'global' keyword. Here is a SO article on how to use python globals : http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – Corb3nik Nov 18 '14 at 15:47
  • This is a ready for use, free, open-source [Java portfolio-analyzer tool](https://github.com/zappee/portfolio-analyzer). It has the same functionality than you are trying to implement. – zappee Jan 09 '23 at 22:41

0 Answers0