Answered You can hire a professional tutor to get the answer.

QUESTION

I am struggling with returns. I am writing a program to do automatic stock trading. Here is what I have written:

I am struggling with returns. I am writing a program to do automatic stock trading. Here is what I have written:

import math

current_shares = int(input("What is the current number of shares in the account? "))

purchase_price = float(input("What was the original price (per share) paid? "))

market_price = float(input("What is the current market price (per share)? " ))

available_funds = float(input("What is the maximum you are willing to spend on a stock purchase? "))

def trade_action(current_stock, purchase_price, current_price, investment):

  profit = (market_price * current_shares) - (current_shares * purchase_price) - 10

  if available_funds > 10 and purchase_price > market_price:

    amount_to_buy = (available_funds - 10) / market_price

    return "Buy " + str(amount_to_buy ) + " " + "shares "

  elif market_price > purchase_price and profit > 0:

    return "Sell " + str(profit ) + " " + "shares "

  elif profit <= 0:

    return "Hold shares "

No matter what metrics I enter, it always returns "hold shares". Why is this?

Metric #1

Inputs:                                                                                                                        

    current_shares  = 1                                                                                                          

    purchase_price  = 100                                                                                                        

    market_price    = 1                                                                                                          

    available_funds = 11  

Expected output: Buy 1 shares                                                                                                  

  Actual output  : Hold shares 

Metric #2

Inputs:                                                                                                                        

    current_shares  = 1                                                                                                          

    purchase_price  = 1                                                                                                          

    market_price    = 12                                                                                                         

    available_funds = 0                                                                                                          

  Expected output: Sell 1 shares                                                                                                 

  Actual output  : Hold shares  

Now I know there is something wrong with my returns.... because when I run the code like this:

profit = (market_price * current_shares) - (current_shares * purchase_price) - 10 

if available_funds > 10 and purchase_price > market_price: 

amount_to_buy = (available_funds - 10) / market_price 

message = "Buy " + str(amount_to_buy ) + " " + "shares " 

elif market_price > purchase_price and profit > 0: 

message = "Sell " + str(profit ) + " " + "shares " 

elif profit <= 0: 

message = "Hold shares "

print(message)

It ALWAYS returns the correct values

Metric #1

Inputs:                                                                                                                        

    current_shares  = 1                                                                                                          

    purchase_price  = 100                                                                                                        

    market_price    = 1                                                                                                          

    available_funds = 11  

Expected output: Buy 1 shares                                                                                                  

  Actual output  : Buy 1 shares 

Metric #2

Inputs:                                                                                                                        

    current_shares  = 1                                                                                                          

    purchase_price  = 1                                                                                                          

    market_price    = 12                                                                                                         

    available_funds = 0                                                                                                          

  Expected output: Sell 1 shares                                                                                                 

  Actual output  : Sell 1 shares

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question