Question

write this program in python plz Many investment management companies are switching from manual stock trading...

write this program in python plz

Many investment management companies are switching from manual stock trading done by humans to automatic stock trading by computers. You've been tasked to write a simple automatic stock trader function that determines whether to buy, sell, or do nothing (hold) for a particular account. It should follow the old saying "Buy low, sell high!"

This function takes 4 input parameters in this order:

  1. current_shares - current number of shares of this stock in the account (int).
  2. purchase_price - price (per share) paid for current stock in the account (float).
  3. market_price - current market price (per share) of stock in the account (float).
  4. available_funds - maximum amount the client is willing to spend on a stock purchase (float).

Any transaction (buy or sell) costs $10. This $10 must be paid out of the available_funds for a purchase, or out of the proceeds of a stock sale. Be sure to account for this fee in your profit calculations.

A purchase would be considered profitable when the current market price is lower than the purchase price, and the available funds will allow us to buy enough shares so that the difference in value will cover the $10 transaction fee. In this case the function should return the string "Buy # shares" where # is an integer representing the number of shares to purchase.

A sale would be considered profitable when the current market price is higher than the purchase price, and the value gained by selling the shares will cover the $10 transaction fee. In this case the function should return the string "Sell # shares" where # is an integer representing the number of shares to sell.

If neither a buy nor a sell would be profitable, then the function should return the string "Hold shares."

Here are some test cases that your function should satisfy:  

Test 1 Test 2 Test 3 Test 4 Test 5 Test 6
current_shares 10 20 15 1 10 1
purchase_price 100 2 12 1 1 1
market_price 1 1 1 11 3 12
available_funds 10 21 12 0 30 0
OUTPUT Hold shares Buy 11 shares Buy 2 shares HoldShares Sell 10 shares Sell 1 shares

Rationale for test cases:

Test 1
Even though the current market price is very low (compared to the purchase price), after paying the $10 transaction fee, we would not have any funds left to buy shares; so we can only hold.

Test 2
After paying the $10 transaction fee, there are enough funds remaining to buy 11 shares. At a purchase_price vs. market_price difference of $1 per share, our 11 shares represent a value gain of $11 dollars, which is $1 more than the $10 transaction fee - so we come out $1 ahead.

Test 3
After paying the $10 transaction fee, there are enough funds remaining to buy 2 shares. At a purchase_price vs. market_price difference of $11 per share, our 2 shares represent a value gain of $22 dollars, which is $12 more than the $10 transaction fee - so we come out $12 ahead.

Test 4
Selling our 1 share for $11 will leave us with just $1 after we pay the $10 transaction fee. That is the same as what we paid for it, and we won't make any profit - so we should hold.

Test 5
With a market_price vs. purchase_price vs. difference of $2 per share, we stand to make $20 from the sale of our 10 shares. This is $10 more than the price of the transaction fee, so we will come out $10 ahead - therefore we should sell all 10 shares.

Test 6
Our 1 share is worth $11 more than we paid for it at the current market price. The $11 dollars obtained by selling that share now will still leave us with a profit of $1 after paying the $10 transaction fee. Profit is profit, so we should sell.

Things to think about when you’re designing and writing this program:

  1. Look for opportunities to use variables as a way to name things, and to break larger more complex expressions down into simpler expressions.
  2. Spend some time choosing your names carefully. Names should be descriptive.
  3. Try to design and write your code a few lines at a time. Design and write a few lines, then run some tests to see if these lines are doing what you want them to do. If they are not, then analyze and correct them before you move on to the next few lines of code.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

================================================================================================

def correct_decision(current_shares,purchase_price,market_price,available_funds):

    if market_price>purchase_price :
        profit = current_shares*(market_price-purchase_price)
        if profit>10:
            print('Sell {} shares'.format(current_shares))
        else:
            print('Hold Shares')
    elif purchase_price>market_price:

        remaining_funds=available_funds-10
        if remaining_funds<=0:
            print('Hold Shares')
        else:
            shares_can_be_bought = remaining_funds//market_price
            print('Buy {} shares'.format(shares_can_be_bought))

correct_decision(10,100,1,10)
correct_decision(20,2,1,21)
correct_decision(15,12,1,12)
correct_decision(1,1,11,0)
correct_decision(10,1,3,30)
correct_decision(1,1,12,0)

========================================================================================

Add a comment
Know the answer?
Add Answer to:
write this program in python plz Many investment management companies are switching from manual stock trading...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem Statement Last month Tian purchased some stock in Acme Software, Inc. Here are the details...

    Problem Statement Last month Tian purchased some stock in Acme Software, Inc. Here are the details of the purchase:  The number of shares that Tian purchased was 1,000  When Tian purchased the stock, he paid $33.92 per share  Tian paid his stockbroker a commission that amounted to 2% of the amount he paid for the stock. One week later, Tian sold the stock. Here are the details of the sale: The number of shares sold was 1,000...

  • (PYTHON) Stock Transaction Program Last month Joe purchased some stock from StockTrade.   1. Write a function(s)...

    (PYTHON) Stock Transaction Program Last month Joe purchased some stock from StockTrade.   1. Write a function(s) to allow the user to input the followings: ·The name of the stock ·Number of shares Joe bought · Stock purchase price · Stock selling price ·Broker commission 2. Write function(s) to calculate: and: · The amount of money Joe paid for the stock (number of shares bought * purchase price) · The amount of commission Joe paid his broker when he bought the...

  • A stock market is a public market for trading a company's The difference between the price...

    A stock market is a public market for trading a company's The difference between the price at which a dealer will sell a certain security and the price at which a dealer will buy a security is called the Cole, a trader, wants to buy 1,000 shares of XYZ stock, while a second trader, Abigail, is willing to sell 1,500 shares of the same stock. Unfortunately, Cole and Abigail don't know one another and must complete their transactions using the...

  • Requires Python to answer A client wishes to keep track of his investment in shares. Write...

    Requires Python to answer A client wishes to keep track of his investment in shares. Write a program to help him manage his stock portfolio. You are to record both the shares that he is holding as well as shares that he has sold. For shares be is curreatly holding, record the following data: .a 3-character share code, share name, last purchase date, volume currently held and average purchase price (refer to description under part cii) option 2) For shares...

  • 5. The stock market A stock market isa market for trading a company's stocks and derivatives....

    5. The stock market A stock market isa market for trading a company's stocks and derivatives. In a dealer market, some dealers hold a oertain inventory of specific securities and create a liquid market by purchasing and selling their inventories. These dealers make a market and are thus called market makers. Agents in the market bring investors to the dealers through a network of terminals and electronic systems. Where do dealer profits come from in a dealer market? O Dividend...

  • Please indicate which type of orders you should place and example how the order will be...

    Please indicate which type of orders you should place and example how the order will be executed in each of the following examples: You want to buy shares of intel to diversify your portfolio. You believe the share price is approximately at the “fair” value, and you want the trade done quickly and cheaply. You want to buy shares of Intel but believe that the current stock price is too high given the firm’s prospects. If the shares could be...

  • CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow...

    CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow a user to buy, sell and view stocks in a stock portfolio. This document will describe the minimum expected functions for a grade of 90. Your mission is to “go and do better.” You’ll find a list of enhancement options at the end of this document. Objectives By the end of this project, the student will be able to • write a GUI program...

  • 1. Jim sold 20 put option contracts on XYZ stock with an exercise price of $35.5 and an option price of $2.30. Toda...

    1. Jim sold 20 put option contracts on XYZ stock with an exercise price of $35.5 and an option price of $2.30. Today, the option expires and the underlying stock is selling for $34.30 a share. What is your total profit or loss on this investment? 2 ABC CO just paid a $1.20 annual dividend. The company has a policy whereby the dividend increases by 2.5 % annually. You would like to purchase 100 shares of stock in this firm...

  • 1. Jim sold 20 put option contracts on XYZ stock with an exercise price of $35.5 and an option price of $2.30. Toda...

    1. Jim sold 20 put option contracts on XYZ stock with an exercise price of $35.5 and an option price of $2.30. Today, the option expires and the underlying stock is selling for $34.30 a share. What is your total profit or loss on this investment? 2 ABC CO just paid a $1.20 annual dividend. The company has a policy whereby the dividend increases by 2.5 % annually. You would like to purchase 100 shares of stock in this firm...

  • 6. Stock repurchases Companies with excess cash often employ share repurchase plans in place of or...

    6. Stock repurchases Companies with excess cash often employ share repurchase plans in place of or along with cash dividends. Share repurchase plans can help investors liquidate their holdings by selling their stock to the issuing company and earning from capital gains. Consider the case of St. Sebastian Company: St. Sebastian Company has forecasted a net income of $5,300,000 for this year. Its common stock currently trades at $21 per share, and the company currently has 830,000 shares of common...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT